MostlyAdequate / mostly-adequate-guide

Mostly adequate guide to FP (in javascript)
Other
23.43k stars 1.87k forks source link

Fix sequence section in chapter 12 #605

Open dotnetCarpenter opened 3 years ago

dotnetCarpenter commented 3 years ago

This PR fixes most of the issues in #581.

From chapter 12, Type Feng Shui section, I have only changed line 4 from sequence(Either.of, [Either.of('wing')]); // Right(['wing']) to sequence(List.of, Either.of(['wing'])), // List(Right('wing')), which unlike the original will not throw a TypeError. But also is an example of inverting a Right (List (String)) to a List (Right (String)). The original, Right (List (String)) to Right (List (String)) made little sense.

Line 5, however is not changed and does not highlight anything particular useful, since sequence(Task.of, left('wing')) = Task.of(left('wing')).

sequence(List.of, Maybe.of(['the facts'])); // [Just('the facts')]
sequence(Task.of, new Map({ a: Task.of(1), b: Task.of(2) })); // Task(Map({ a: 1, b: 2 }))
sequence(IO.of, Either.of(IO.of('buckle my shoe'))); // IO(Right('buckle my shoe'))
sequence(List.of, Either.of(['wing'])), // List(Right('wing'))
sequence(Task.of, left('wing')); // Task(Left('wing'))

Only two changes was required to support/index.js.

  1. Right
    traverse(of, fn) {
    return fn(this.$value).map(Either.of); // added return
    }
  2. Map
    // ----- Pointed Map
    static of(x) {
    return new Map(x);
    }
dotnetCarpenter commented 3 years ago

Blocks #607