Closed paranormal closed 5 years ago
The method actually needs to be named fantasy-land/concat
:
// Sum :: Number -> Sum
const Sum = value => ({
value,
[util.inspect.custom]: () => `Sum (${value})`,
'fantasy-land/concat': sum => Sum (value + sum.value),
});
console.log (Z.concat (Sum (14), Sum (28))); // -> Sum (42)
Thank you very much, I was playing with Pair as an Apply and hit this. Is there any reason why it should be fantasy-land/concat? It's different from how strings or arrays works.
Is there any reason why it should be fantasy-land/concat?
The prefix makes it possible to add a compliant fantasy-land/concat
method to a type that already has a non-compliant concat
method. The prefix also makes it obvious whether a particular method is intended to comply with the Fantasy Land specification.
It's different from how strings or arrays works.
True. It's worth noting that String#concat
and Array#concat
are specialized in ways that their Fantasy Land counterparts are not:
> 'foo'.concat('bar', 'baz')
'foobarbaz'
> ['foo'].concat('bar')
['foo', 'bar']
> ['foo'].concat(['bar'], ['baz'])
['foo', 'bar', 'baz']
I recommend using S.concat
as it provides a consistent interface:
> S.concat ('foo') ('bar')
'foobar'
> S.concat (['foo']) (['bar', 'baz'])
['foo', 'bar', 'baz']
> S.concat (S.Just ('foo')) (S.Just ('bar'))
Just ('foobar')
Thank you for this very descriptive explanation.
From how it works I think it's not very interoperable. Let's say I have hand-made Semogroup
It obeys the laws, nothing is wrong with it, it misses input type checks but this is a different story... https://github.com/sanctuary-js/sanctuary-type-classes/blob/v11.0.0/index.js#L1393
Why this function doesn't check it's input parameters for .concat method and use it instead?