2 ms·
A function passed to a functor as you described it seems to take only a single argument, which is the value(s) from inside the container. But then, a function
by recursive 8y ago
A function passed to a functor as you described it seems to take only a single argument, which is the value(s) from inside the container.
But then, a function passed to a monoid seems to take two values, as in your examples. (addition, multiplication, concatenation) How is this reconciled?
- mikekchar 8y agoThat's a great question. It's hard to visualise and the number of arguments becomes super important when you start moving on to applicative functors. I'll write in JS just so it's a bit more accessible for people who aren't already doing FP every day. As you say, the function passed to map, takes only a single parameter, which is the value in the container. So I might have the following code: myArray.map(x => x + 1) This adds 1 to everything in the array. However, instead of just adding 1 (which may or may not be useful), I can close over a different value. Consider the following function. let arrayAdd = (array, y) => array.map(x => x + y); Or to be a bit more idiomatic with most FP languages, let's curry the params to arrayAdd: let arrayAdd = a => y => a.map(x => x + y); So now we have a functor, a, and an operation, arrayAdd, and a is a monoid over the operation arrayAdd. We can use it like: let addToArray = arrayAdd(myArray); let newArray = addToArray(42); And obviously if we pass 0 to addToArray it will return us the same array as myArray, showing that 0 is the identity element. Hope that makes sense!
- recursive 8y agoThanks. I've found a lot of category explanations that use Haskell types in their explanation, and then a lot of Haskell types that use the jargon in their definition. It's hard to find a foothold from which to build understanding. This is perfect.