mostly-adequate-guide icon indicating copy to clipboard operation
mostly-adequate-guide copied to clipboard

Chapter 11 arrayToList

Open xielehe opened this issue 6 years ago • 1 comments
trafficstars

In Chapter 11 // arrayToList :: [a] -> List a const arrayToList = List.of;

I execute in the ternimal console.log(arrayToList([1,2,3,4])), got nested array List([[1, 2, 3, 4]]) Is this expected behavior ?

xielehe avatar Jul 09 '19 07:07 xielehe

Look at implementation of list from https://mostly-adequate.gitbooks.io/mostly-adequate-guide/appendix_b.html#list

class List {
  constructor(xs) {
    this.$value = xs;
  }

  // ----- Pointed List
  static of(x) {
    return new List([x]);
  }

  // ----- Functor List
  map(fn) {
    return new List(this.$value.map(fn));
  }
}

So, yes apparently arrayToList([1,2,3,4]) will result List([[1, 2, 3, 4]]) But that does not make sense, Because that means compose(arrayToList, map(f)) !== compose(map(f), arrayToList)

admhemed avatar May 17 '20 03:05 admhemed