-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
115 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { curryN } from 'ramda'; | ||
|
||
/** | ||
* Weaves a configuration into function returning the runnable monad like `Reader` or `Free`. | ||
* This allows us to pre-bind the configuration in advance and use the weaved function | ||
* without need to explicitly pass the configuration on every call. | ||
* | ||
* @func weaveLazy | ||
* @memberOf RA | ||
* @since {@link https://char0n.github.io/ramda-adjunct/1.10.0|v1.10.0} | ||
* @category Function | ||
* @sig (*... -> *) -> (* -> *) -> (*... -> *) | ||
* @param {Function} fn The function to weave | ||
* @param {Function} configAccessor The function that returns the configuration object | ||
* @return {Function} Auto-curried weaved function | ||
* @example | ||
* | ||
* const { Reader: reader } = require('monet'); | ||
* | ||
* const log = value => reader( | ||
* config => config.log(value) | ||
* ); | ||
* | ||
* const consoleAccessor = R.always(console); | ||
* | ||
* // no weaving | ||
* log('test').run(console); //=> prints 'test' | ||
* | ||
* // weaving | ||
* const wlog = RA.weaveLazy(log, consoleAccessor); | ||
* wlog('test'); //=> prints 'test' | ||
*/ | ||
const weaveLazy = curryN(2, (fn, configAccessor) => | ||
curryN(fn.length, (...args) => fn(...args).run(configAccessor())) | ||
); | ||
|
||
export default weaveLazy; |
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { sum, curry, always } from 'ramda'; | ||
import { Reader as reader } from 'monet'; | ||
|
||
import RA from '../src/index'; | ||
import eq from './shared/eq'; | ||
|
||
|
||
describe('weaveLazy', function() { | ||
const unaryReader = a => reader(config => config + a); | ||
const binaryReader = (a, b) => reader(config => config + a + b); | ||
const variadicReader = (...args) => reader(config => sum(args) + config); | ||
const mixedReader = (a, b, ...args) => reader(config => sum(args.concat(a, b, config))); | ||
|
||
it('tests weaving', function() { | ||
const wunaryReader = RA.weaveLazy(unaryReader, always(1)); | ||
const wbinaryReader = RA.weaveLazy(binaryReader, always(1)); | ||
|
||
eq(wunaryReader(1), 2); | ||
eq(wbinaryReader(2, 3), 6); | ||
}); | ||
|
||
it('tests currying', function() { | ||
const wbinaryReader1 = RA.weaveLazy(binaryReader, always(1)); | ||
const wbinaryReader2 = RA.weaveLazy(binaryReader)(always(1)); | ||
|
||
eq(wbinaryReader1(2, 3), 6); | ||
eq(wbinaryReader2(2, 3), 6); | ||
}); | ||
|
||
it('tests auto-currying on fixed function signature', function() { | ||
const wbinaryReader = RA.weaveLazy(binaryReader, always(1)); | ||
|
||
eq(wbinaryReader(2, 3), 6); | ||
eq(wbinaryReader(2)(3), 6); | ||
}); | ||
|
||
it('tests auto-currying on curried fixed function signature', function() { | ||
const wbinaryReader = RA.weaveLazy(curry(binaryReader), always(1)); | ||
|
||
eq(wbinaryReader(2, 3), 6); | ||
eq(wbinaryReader(2)(3), 6); | ||
}); | ||
|
||
it('tests auto-currying on variadic function signature', function() { | ||
const wvariadicReader = RA.weaveLazy(variadicReader, always(1)); | ||
|
||
eq(wvariadicReader(1, 2, 3), 7); | ||
}); | ||
|
||
it('tests auto-currying on curried variadic function signature', function() { | ||
const wvariadicReader = RA.weaveLazy(curry(variadicReader), always(1)); | ||
|
||
eq(wvariadicReader(1, 2, 3), 7); | ||
}); | ||
|
||
it('tests auto-currying on mixed function signature', function() { | ||
const wmixedReader = RA.weaveLazy(mixedReader, always(1)); | ||
|
||
eq(wmixedReader(2, 3, 4), 10); | ||
eq(wmixedReader(2)(3, 4), 10); | ||
}); | ||
|
||
it('tests auto-currying on curried mixed function signature', function() { | ||
const wmixedReader = RA.weaveLazy(curry(mixedReader), always(1)); | ||
|
||
eq(wmixedReader(2, 3, 4), 10); | ||
eq(wmixedReader(2)(3, 4), 10); | ||
}); | ||
}); |