re: Create a Snake clone with Hyperapp, part 1 VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Great work! However, unless I am wrong, your updateSnake action is not pure (since it mutates the snake input and thus the state), which is not rec...
 

Thanks for the observation πŸ˜„

You are right that the function updateSnake is not pure because it mutates the snake, however, if you look at the action updateSnake, you'll see that it returns a shallow copy of the state, so hyperapp won't get confused (as far as I know, when you return the same state object the view is not updated, but there are no problems returning shallow copies).

const actions = {
    updateSnake: () => state => ({
        ...state,
        snake: updateSnake(state.snake, state.direction),
    }),
}

Maybe naming both the action and the function updateSnake was a poor choice, though.

I would definitely choose your update method over the naΓ―ve approach if I hadn't put it as an exercise for the reader at the end of the second part :)

code of conduct - report abuse