Last active
January 30, 2017 22:38
-
-
Save ktec/51fdcad2d5d002081f551a0c5b4b2b5c to your computer and use it in GitHub Desktop.
Revisions
-
ktec revised this gist
Jan 30, 2017 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -98,8 +98,6 @@ const mempty = Sum.empty // [ Sum(0), Sum(1), Sum(2), Sum(3), Sum(4) ] result = getSum(foldl(cons, mempty(), map(Sum, [1, 2, 3, 4]))) console.log(result) -
ktec revised this gist
Jan 30, 2017 . 1 changed file with 10 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,14 +5,20 @@ // inspect: () => `Box(${x})` // }) const fmap = f => xs => xs.map(x => f(x)) const cons = (a, b) => a.concat(b) // const cons = (x, xs) => [x].concat(xs) const car = xs => xs[0] // head const cdr = xs => xs.slice(1) // tail const foldr = (f, v, xs) => xs.reduceRight(f, v) const foldl = (f, v, xs) => xs.reduce(f, v) const id = x => x // const map = (f, xs) => foldr((acc, curr) => cons(f(curr), acc), [], xs) const map = (f, xs) => xs.map(f) const Sum = x => ({ @@ -93,7 +99,7 @@ const mempty = Sum.empty // this is wrong: // result = map(Sum, [1, 2, 3, 4]) result = getSum(foldl(cons, mempty(), map(Sum, [1, 2, 3, 4]))) console.log(result) -
ktec revised this gist
Jan 30, 2017 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -86,9 +86,14 @@ const mempty = Sum.empty // result = Sum(0).concat(Sum(1).concat(Sum(2).concat(Sum(3).concat(Sum(4))))) // xs = map(Sum, [1, 2, 3, 4]) // [ Sum(1), Sum(2), Sum(3), Sum(4) ] // this works: // result = cons(mempty(), [ Sum(1), Sum(2), Sum(3), Sum(4) ]) // [ Sum(0), Sum(1), Sum(2), Sum(3), Sum(4) ] // this is wrong: result = foldl(cons, mempty(), map(Sum, [1, 2, 3, 4])) // [ [ [ [Object], Sum(2) ], Sum(3) ], Sum(4) ] console.log(result) -
ktec created this gist
Jan 30, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,94 @@ // const Box = x => // ({ // map: f => Box(f(x)), // fold: f => f(x), // inspect: () => `Box(${x})` // }) const cons = (x, xs) => [x].concat(xs) const car = xs => xs[0] // head const cdr = xs => xs.slice(1) // tail const foldr = (f, v, xs) => xs.reduceRight(f, v) const foldl = (f, v, xs) => xs.reduce(f, v) const id = x => x const map = (f, xs) => foldr((acc, curr) => cons(f(curr), acc), [], xs) const Sum = x => ({ x, concat: ({x: y}) => Sum(x + y), inspect: () => `Sum(${x})` }) Sum.empty = () => Sum(0) const getSum = m => m.x // Haskell version: // data Fold i o = forall m . Monoid m => Fold (i -> m) (m -> o) // import Data.Monoid // import Prelude hiding (sum) // // import qualified Data.Foldable // // data Fold i o = forall m . Monoid m => Fold (i -> m) (m -> o) // // fold :: Fold i o :: [i] -> o // fold (Fold tally summarise) is = summarise ( reduce (map tally is) ) // where // reduce = Data.Foldable.foldl' (<>) mempty // // sum :: Num n => Fold n n // sum = Fold Sum getSum // // // >>> fold sum [1..10] // 55 // // -- sum = Fold Sum getSum // = print (fold (Fold Sum getSum) [1, 2, 3, 4]) // // -- fold (Fold tally summarize) is = summarize (reduce (map tally is)) // = print (getSum (reduce (map Sum [1, 2, 3, 4]))) // // -- reduce = foldl' (<>) mempty // = print (getSum (foldl' (<>) mempty (map Sum [1, 2, 3, 4]))) // // -- Definition of `map` (skipping a few steps) // = print (getSum (foldl' (<>) mempty [Sum 1, Sum 2, Sum 3, Sum 4])) // // -- `foldl' (<>) mempty` (skipping a few steps) // = print (getSum (mempty <> Sum 1 <> Sum 2 <> Sum 3 <> Sum 4)) // // -- mempty = Sum 0 // = print (getSum (Sum 0 <> Sum 1 <> Sum 2 <> Sum 3 <> Sum 4)) // // -- Sum x <> Sum y = Sum (x + y) // = print (getSum (Sum 10)) // // -- getSum (Sum x) = x // = print 10 // const result = Sum(1).concat(Sum(2)) const mempty = Sum.empty // = print (getSum (Sum 0 <> Sum 1 <> Sum 2 <> Sum 3 <> Sum 4)) // result = getSum(result) // result = getSum(mempty.concat(Sum(1).concat(Sum(2).concat(Sum(3).concat(Sum(4)))))) // map Sum [1, 2, 3, 4] # => [Sum 1, Sum 2, Sum 3, Sum 4] // result = foldl(cons, Sum.empty, map(Sum, [1, 2, 3, 4])) // result = Sum(0).concat(Sum(1).concat(Sum(2).concat(Sum(3).concat(Sum(4))))) // xs = map(Sum, [1, 2, 3, 4]) // [ Sum(1), Sum(2), Sum(3), Sum(4) ] result = foldl(cons, mempty, map(Sum, [1, 2, 3, 4])) // this is wrong: // [ [ [ [Object], Sum(2) ], Sum(3) ], Sum(4) ] console.log(result)