Skip to content

Commit

Permalink
feat: add weaveLazy
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n committed Jun 20, 2017
1 parent 8c19a18 commit 8b665d3
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ declare namespace RamdaAdjunct {
weave(fn: Function, config: any): Function;
weave(fn: Function): (config: any) => Function;

/**
* Weave a configuration into function returning the runnable monad like `Reader` or `Free`.
*/
weaveLazy(fn: Function, configAccessor: Function): Function;
weaveLazy(fn: Function): (configAccessor: Function) => Function;

/**
* Identity type.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import liftFN from './liftFN';
import liftF from './liftF';
import cata from './cata';
import weave from './weave';
import weaveLazy from './weaveLazy';
// List
import pickIndexes from './pickIndexes';
import list from './list';
Expand Down Expand Up @@ -113,6 +114,7 @@ export { default as liftFN } from './liftFN';
export { default as liftF } from './liftF';
export { default as cata } from './cata';
export { default as weave } from './weave';
export { default as weaveLazy } from './weaveLazy';
// List
export { default as pickIndexes } from './pickIndexes';
export { default as list } from './list';
Expand Down Expand Up @@ -185,6 +187,7 @@ const RA = {
liftF,
cata,
weave,
weaveLazy,
// List
pickIndexes,
list,
Expand Down
37 changes: 37 additions & 0 deletions src/weaveLazy.js
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;
69 changes: 69 additions & 0 deletions test/weaveLazy.js
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);
});
});

0 comments on commit 8b665d3

Please sign in to comment.