> S.reduce(S.flip(S.K),[],S.Left([1,2]))
[]
> S.reduce(S.flip(S.K),[],S.Right([1,2]))
[ 1, 2 ]
I was trying to understand sanctuary and its working can anybody explain the above outcomes in detail In my understanding S.reduce takes a mapping function and uses the input array which should be of type either and reduces it But why its empty array in first case and same array in second case
Let's start with a more familiar example: using
S.reduce
on an array.Now, let's specialize the type of
S.reduce
to explain the behaviour above.Next, let's specialize the type of
S.reduce
to see how it will operate on Either values.What can we do when given
S.Left ('foo')
as theEither x a
? We have anx
('foo'
) but noa
. Because we don't have ana
we cannot make use of theb -> a -> b
function. Thus, the onlyb
we can possibly return is the initial value.What can we do when given
S.Right ('bar')
as theEither x a
? We have ana
, which we could feed to theb -> a -> b
function along with the initial value to produce another value of typeb
.If
S.reduce
were to return'initial:'
or'initial:bar:bar:bar'
in the case above it would still be conforming to the type signature, but thefantasy-land/reduce
implementation for Sanctuary's Either type applies the function exactly once when given a Right.