The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Create types with Semigroups

Create types with Semigroups

5:52
An introduction to concatting items via the formal Semi-group interface. Semi-groups are simply a type with a concat method that are associative. We define three semigroup instances and see them in action.
Watch this lesson now
Avatar
egghead.io

An introduction to concatting items via the formal Semi-group interface. We define three semigroup instances and see them in action.

Avatar
Vincent

Why would we ever use something like First("blah") ?

In reply to egghead.io
Avatar
Brian Lonsdorf

Here's a "practical" example. The First here always holds an Either.

const First = x =>
({
  fold: f => x.fold(f, f),
  concat: o => x.isLeft ? o : First(x)
})


const find = (xs, f) =>
  List(xs)
  .foldMap(x => First(f(x) ? Right(x) : Left()), First.empty)
  .fold(x =>x)

In general, the motivation to use semigroups is 4 fold (no pun intended)
1. One has instant intuition that we're doing some kind of combination.
2. There are many properties that hold like identity, associativity, and other group theory stuff.
3. Semigroups beget semigroups... they will combine all the subparts (I show an example in the last real world vid)
4. Since they are associative, they can be parallelized by default.

In reply to Vincent
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?