Skip to content

Commit

Permalink
feat: add liftF
Browse files Browse the repository at this point in the history
Ref #56
  • Loading branch information
char0n committed Apr 16, 2017
1 parent f65a52e commit b475d5d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ declare var RA: RamdaAdjunct.Static;

declare namespace RamdaAdjunct {

interface Apply {
app: Function;
}

interface Variadic<T1, T2> {
(...args: T1[]): T2;
}

export interface Static {
/**
* Checks if input value is `Array`
Expand Down Expand Up @@ -223,6 +231,18 @@ declare namespace RamdaAdjunct {
*/
paths(ps: Array<Array<string | number>>, obj: Object): Array<any>
paths(ps: Array<Array<string | number>>): (obj: Object) => Array<any>

/**
* "lifts" a function to be the specified arity, so that it may "map over" objects that satisfy
* the Apply spec of algebraic structures.
*/
liftFN<T>(arity: number, fn: Variadic<Apply, T>): Apply

/**
* "lifts" a function of arity > 1 so that it may "map over" objects that satisfy
* the Apply spec of algebraic structures.
*/
liftF<T>(fn: Variadic<Apply, T>): Apply
}

}
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import isNotInteger from './isNotInteger';
import stubUndefined from './stubUndefined';
import noop from './noop';
import liftFN from './liftFN';
import liftF from './liftF';
// List
import pickIndexes from './pickIndexes';
import list from './list';
Expand Down Expand Up @@ -88,6 +89,7 @@ export { default as isNotInteger } from './isNotInteger';
export { default as stubUndefined } from './stubUndefined';
export { default as noop } from './noop';
export { default as liftFN } from './liftFN';
export { default as liftF } from './liftF';
// List
export { default as pickIndexes } from './pickIndexes';
export { default as list } from './list';
Expand Down Expand Up @@ -141,6 +143,7 @@ const RA = {
stubUndefined,
noop,
liftFN,
liftF,
// List
pickIndexes,
list,
Expand Down
34 changes: 34 additions & 0 deletions src/liftF.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import liftFN from './liftFN';

/**
* "lifts" a function of arity > 1 so that it may "map over" objects that satisfy
* the Apply spec of algebraic structures. This function is not compatible
* with {@link https://github.com/fantasyland/fantasy-land#apply|FantasyLand Apply spec}.
*
* Lifting is specific for {@link https://github.com/scalaz/scalaz|scalaz} and {@link http://www.functionaljava.org/|functional java} implementations.
* One of the mainstream libraries that uses this Apply spec is {@link https://cwmyers.github.io/monet.js/|monet.js}.
* This function acts as interop for ramda and monet.js.
*
* More info {@link https://github.com/fantasyland/fantasy-land/issues/50|here}.
*
* @func liftF
* @memberOf RA
* @since {@link https://char0n.github.io/ramda-adjunct/1.2.0|v1.2.0}
* @category Function
* @sig Apply a => (a... -> a) -> (a... -> a)
* @param {Function} fn The function to lift into higher context
* @return {Function} The lifted function
* @see {@link RA.liftFN|liftFN}
* @example
*
* const { Maybe } = require('monet');
*
* const add3 = (a, b, c) => a + b + c;
* const madd3 = RA.liftF(add3);
*
* madd3(Maybe.Some(10), Maybe.Some(15), Maybe.Some(17)); //=> Maybe.Some(42)
* madd3(Maybe.Some(10), Maybe.Nothing(), Maybe.Some(17)); //=> Maybe.Nothing()
*/
const liftF = fn => liftFN(fn.length, fn);

export default liftF;
1 change: 1 addition & 0 deletions src/liftFN.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { curry, last, slice, reverse, reduce, pipe, ap, curryN, map, flip } from
* @since {@link https://char0n.github.io/ramda-adjunct/1.2.0|v1.2.0}
* @category Function
* @sig Apply a => Number -> (a... -> a) -> (a... -> a)
* @param {Number} arity The arity of the lifter function
* @param {Function} fn The function to lift into higher context
* @return {Function} The lifted function
* @see {@link http://ramdajs.com/docs/#lift|lift}, {@link http://ramdajs.com/docs/#ap|ap}
Expand Down
28 changes: 28 additions & 0 deletions test/liftF.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Maybe } from 'monet';
import { curry } from 'ramda';

import RA from '../src/index';
import eq from './shared/eq';

const add3 = curry((a, b, c) => a + b + c);
const add4 = curry((a, b, c, d) => a + b + c + d);
const add5 = curry((a, b, c, d, e) => a + b + c + d + e);

describe('liftF', function() {
const addF3 = RA.liftF(add3);
const addF4 = RA.liftF(add4);
const addF5 = RA.liftF(add5);

it('returns a function', function() {
eq(typeof RA.liftF(add3), 'function');
});

it('can lift functions of any arity', function() {
eq(addF3(Maybe.Some(1), Maybe.Some(1), Maybe.Some(1)), Maybe.Some(3));
eq(addF4(Maybe.Some(1), Maybe.Some(1), Maybe.Some(1), Maybe.Some(1)), Maybe.Some(4));
eq(
addF5(Maybe.Some(1), Maybe.Some(1), Maybe.Some(1), Maybe.Some(1), Maybe.Some(1)),
Maybe.Some(5)
);
});
});
File renamed without changes.

0 comments on commit b475d5d

Please sign in to comment.