[Functional Programming] Function signature

It is really important to understand function signature in functional programming.

The the code example below:

const map = fn => anyFunctor => anyFunctor.map(fn);

'map' is pointfree version of any founctor's map, for example:

Maybe.of('maybe').map(toUpper)

equals:

compose(map(toUpper), Maybe.of)('maybe') // Just 'MAYBE'

So what is the function signature for 'map'?

fn: is a function, we can use 

  (a -> b) : We read it as "A function which take a to b"

anyFunctor: is a Functor, any Functor, we can use:

  f a:  here 'f' means Functor, 'a' means 'any value': We read it as 'Any functor a'

扫描二维码关注公众号,回复: 5263093 查看本文章

anyFunctor.map(fn): is what the return value, it is also a Functor, value may different from 'a':

  f b: here 'f' means Functor, 'b' means 'any value but not the same as 'a' '.

Here we want to point out 'f' is Functor, we can do: 'Functor f =>'

// map :: Functor f => (a -> b) -> f a -> f b

This is the whole result:

// map :: Functor f => (a -> b) -> f a -> f b
const map = fn => anyFunctor => anyFunctor.map(fn);

猜你喜欢

转载自www.cnblogs.com/Answer1215/p/10416213.html