Last active
November 1, 2019 04:24
-
-
Save hoodwink73/a21406b5e1c30c11d237cb8e73be777e to your computer and use it in GitHub Desktop.
Understanding transducers
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 characters
function add1(v) { return v + 1}; | |
function isOdd(v) {return v % 2 === 1}; | |
function sum (total, v) {return total + v}; | |
var list = [2, 5, 8, 11, 14, 17, 20]; | |
list | |
.map(add1) | |
.filter(isOdd) | |
.reduce(sum) |
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 characters
// as `map`, `filter` has different shapes, we will express them | |
// such that they have the same shape as `reducer` | |
function mapReducer(mappingFn) { | |
return function reducer(list, v) { | |
list.push(mappingFn(v)); | |
return list; | |
} | |
} | |
function filterReducer(predicateFn) { | |
return function reducer(list, v) { | |
if (predicateFn(v)) list.push(v) | |
return list; | |
} | |
} | |
var list = [2, 5, 8, 11, 14, 17, 20]; | |
list | |
.reduce(mapReducer(add1), []) | |
.reduce(filterReducer(isOdd), []) | |
.reduce(sum) |
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 characters
function listCombination(list, v) { | |
list.push(v); | |
return list; | |
} | |
function mapReducer(mappingFn) { | |
return function reducer(list, v) { | |
return listCombination(list, mappingFn(v)); | |
} | |
} | |
function filterReducer(predicateFn) { | |
return function reducer(list, v) { | |
if (predicateFn(v)) | |
listCombination(list, v) | |
} | |
} | |
var list = [2, 5, 8, 11, 14, 17, 20]; | |
list | |
.reduce(mapReducer(add1), []) | |
.reduce(filterReducer(isOdd), []) | |
.reduce(sum) |
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 characters
function listCombination(list, v) { | |
list.push(v); | |
return list; | |
} | |
var mapReducer = curry(function mapReducer(mappingFn, combineFn) { | |
return function reducer (list, v) { | |
return combineFn(list, mappingFn(v)) | |
} | |
}) | |
var filterReducer = curry(function filterReducer(predicateFn, combineFn) { | |
return function reducer (list, v) { | |
if (predicateFn(v)) { | |
return combineFn(list, v) | |
} | |
return list; | |
} | |
}) | |
var transducer = compose(mapReducer(add1), filterReducer(isOdd)) | |
var list = [2, 5, 8, 11, 14, 17, 20]; | |
list | |
.reduce(transducer(listCombination), []) | |
.reduce(sum) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment