Elm: understanding faults and mouse clicks

I am currently studying Knit. relatively new to functional programming. I am trying to understand this example from http://elm-lang.org/learn/Using-Signals.elm when counting mouse clicks. they provide the following code:

clickCount = foldp (\click count -> count + 1) 0 Mouse.clicks 

They explain that foldp accepts three arguments: an increment counter, which we defined as an anonymous function with two inputs, an initial state of 0, and a Mouse.clicks signal.

I donโ€™t understand why a variable click is needed in our anonymous function. Why can't we just \ count โ†’ count + 1? Is additional input tied to one of our contributions to foldp?

thanks!

+5
source share
1 answer

You need this because foldp expects a function with two inputs. In this case, the first input is simply ignored by your lambda, but the foldp implementation still puts something there. Mouse.clicks always puts some do-nothing value called Unit .

Some signals have a meaning associated with them, such as Mouse.position . If you want to do something like how far the mouse has moved, you will need to use this option.

+5
source

All Articles