-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from vinniegarcia/develop
compose and sequence as applications of accumulate and wrap
- Loading branch information
Showing
10 changed files
with
26 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
'use strict'; | ||
|
||
const accumulate = (fn, iv) => (...args) => args.reduce(fn, iv); | ||
const accumulate = (reducer, iv) => (...args) => args.reduce(reducer, iv); | ||
export const reverse = (reducer, iv) => (...args) => args.reduceRight(reducer, iv); | ||
|
||
export default accumulate; | ||
export default accumulate; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
// function composition | ||
// compose(f, g)(x) = g(f(x)) | ||
'use strict'; | ||
import wrap from './wrap'; | ||
import unwrap from './unwrap'; | ||
import identity from './identity'; | ||
import {reverse} from './accumulate'; | ||
|
||
const compose = (...funcs) => (...args) => unwrap(funcs.reduceRight((current, next) => [next(...current)], args)); | ||
const compose = (...funcs) => (...args) => unwrap(reverse(wrap, args)(...funcs)); | ||
|
||
export default compose; | ||
export default compose; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
// function sequencing | ||
// compose(f, g)(x) = f(g(x)) | ||
'use strict'; | ||
import wrap from './wrap'; | ||
import unwrap from './unwrap'; | ||
import identity from './identity'; | ||
import accumulate from './accumulate'; | ||
|
||
const sequence = (...funcs) => (...args) => unwrap(funcs.reduce((current, next) => [next(...current)], args)); | ||
const sequence = (...funcs) => (...args) => unwrap(accumulate(wrap, args)(...funcs)); | ||
|
||
export default sequence; | ||
export default sequence; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
'use strict'; | ||
// return first element of an array | ||
// without any array packing | ||
const unwrap = ([first,]) => first; | ||
const unwrap = ([first,...rest]) => first; | ||
|
||
export default unwrap; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
'use strict'; | ||
|
||
const wrap = (fn, n) => fn(n); | ||
const wrap = (n, fn) => [fn(...n)]; | ||
|
||
export default wrap; |
This file contains 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
This file contains 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