Closed Munkyu-Yang closed 3 years ago
Here are some examples, using S.concat
:
> S.concat ('abc') ('def')
'abcdef'
> S.concat ([1, 2, 3]) ([4, 5, 6])
[1, 2, 3, 4, 5, 6]
> S.concat ({x: 1, y: 2}) ({y: 3, z: 4})
{x: 1, y: 3, z: 4}
> S.concat (S.Just ([1, 2, 3])) (S.Just ([4, 5, 6]))
Just ([1, 2, 3, 4, 5, 6])
> S.concat (Sum (18)) (Sum (24))
Sum (42)
S.concat
is easier to understand than the underlying fantasy-land/concat
methods for several reasons:
~>
);fantasy-land/concat
explicitly; andString
, even though String.prototype['fantasy-land/concat']
is undefined.Here is an example of invoking a fantasy-land/concat
method directly:
> S.Just ('abc') ['fantasy-land/concat'] (S.Just ('def'))
Just ('abcdef')
As well ad David's excellent answer, Tom Harding's series on these FantasyLand types is excellent. Semigroups is the fourth entry.
Just a Monoid without zero value. Operation only. f: (A, A) => A
fantasy-land/concat :: Semigroup a => a ~> a -> a
Since there are so many
a
s, it's quite confusing though I've read the documentation. Can someone provide me an actual example please?
If you want to understand the type signature, reading from left to right:
Semigroup a =>
means: "The a
that you will see from now on has a Semigroup"a ~>
means: The following function is a method of an a
(concat is a method of an instance that has a Semigroup)a -> a
means: it's a function that receives an a
and returns something of the same type a
In David's last example the first S.Just ('abc')
is the a
before the ~>
.
@jujoba: There's a typo in the second bullet.
a ~>
means: The following function is a method of an a (chain is a method of an instance that has a Semigroup)
should read
a ~>
means: The following function is a method of an a (concat is a method of an instance that has a Semigroup)
Since there are so many
a
s, it's quite confusing though I've read the documentation. Can someone provide me an actual example please?