Closed somebody1234 closed 1 year ago
Function
s in JavaScript have a type Function (a | null | undefined) (b | null | undefined)
, but lets say Function a b
for simplicity's sake. While Function a b
doesn't have a Functor
instance, Function SomeType b
does. It can be defined as follows:
Function.prototype['fantasy-land/map'] = function (f) {
var g = this;
return function (a) {
return f(g(a));
};
};
// usage
// `Function Number Number` composed with `Function Object String` => `Function Number String` or `(Number -> String)`
(x => x + 1)['fantasy-land/map'](x => x.toString())(42); // '43'
As evidenced by the result, you can see that Function.prototype['fantasy-land/map']
is equivalent to Function.prototype['fantasy-land/compose']
.
Ah... makes sense I guess, but surely it'd be clearer to just write it as normal composition rather than [map]
?
but surely it'd be clearer to just write it as normal composition rather than [map]?
Yes if you're composing two functions in application code. However, defining an Traversable
instance for Compose f g
requires Functor instances for both f
and g
. The Traversable
instance is defined in terms of those Functor
instances.
Perhaps it would be less confusing if an implementation of Traversable (Compose f g)
were provided in the Traversable
example.
I'm wondering how the implementation of
Compose
works? Assumingf
isCompose (a -> b)
thenf.c
must bea -> b
. However, that would meanf.c['fantasy-land/map']
is undefined (unlessFunctor
is implemented for functions)?Am I just misunderstanding something here, or...?