source: node_modules/ramda-adjunct/es/lensTraverse.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 2.2 KB
Line 
1import { traverse, curry, pipe, prop, curryN } from 'ramda';
2import Identity from './fantasy-land/Identity';
3
4/**
5 * Creates a [Traversable](https://github.com/fantasyland/fantasy-land#traversable) lens
6 * from an [Applicative](https://github.com/fantasyland/fantasy-land#applicative)-returning function.
7 *
8 * When executed, it maps an [Applicative](https://github.com/fantasyland/fantasy-land#applicative)-returning
9 * function over a [Traversable](https://github.com/fantasyland/fantasy-land#traversable),
10 * then uses [`sequence`](https://ramdajs.com/docs/#sequence) to transform the resulting Traversable of Applicative
11 * into an Applicative of Traversable.
12 *
13 * Dispatches to the `traverse` method of the third argument, if present.
14 *
15 * @func lensTraverse
16 * @memberOf RA
17 * @since {@link https://char0n.github.io/ramda-adjunct/2.7.0|2.7.0}
18 * @category Relation
19 * @typedef Lens s a = Functor f => (a -> f a) -> s -> f s
20 * @sig fantasy-land/of :: TypeRep f => f ~> a → f a
21 * @sig Applicative f => (a -> f a) -> Lens s a
22 * @sig Applicative f => TypeRep f -> Lens s a
23 * @param {!Object|!Function} TypeRepresentative with an `of` or `fantasy-land/of` method
24 * @return {!function} The Traversable lens
25 * @see {@link http://ramdajs.com/docs/#lens|R.lens}, {@link http://ramdajs.com/docs/#traverse|R.traverse}
26 *
27 * @example
28 *
29 * const maybeLens = RA.lensTraverse(Maybe.of);
30 * const safeDiv = n => d => d === 0 ? Maybe.Nothing() : Maybe.Just(n / d)
31 *
32 * R.over(maybeLens, safeDiv(10), [2, 4, 5]); // => Just([5, 2.5, 2])
33 * R.over(maybeLens, safeDiv(10), [2, 0, 5]); // => Nothing
34 *
35 * R.view(maybeLens, [Maybe.Just(2), Maybe.Just(3)]); // => Maybe.Just([2, 3])
36 *
37 * R.set(maybeLens, Maybe.Just(1), [Maybe.just(2), Maybe.Just(3)]); // => Maybe.Just([1, 1])
38 */
39/* eslint-disable no-nested-ternary */
40var lensTraverse = curryN(1, function (F) {
41 var of = typeof F['fantasy-land/of'] === 'function' ? F['fantasy-land/of'] : typeof F.of === 'function' ? F.of : F;
42 var TypeRep = {
43 'fantasy-land/of': of
44 };
45 return curry(function (toFunctorFn, target) {
46 return Identity.of(traverse(TypeRep, pipe(toFunctorFn, prop('value')), target));
47 });
48});
49/* eslint-enable */
50
51export default lensTraverse;
Note: See TracBrowser for help on using the repository browser.