Frames

Untitled

0
1
1import _curry2 from './internal/_curry2';
2import _dispatchable from './internal/_dispatchable';
3import _map from './internal/_map';
4import _reduce from './internal/_reduce';
5import _xmap from './internal/_xmap';
6import curryN from './curryN';
7import keys from './keys';
8
9
10/**
11 * Takes a function and
12 * a [functor](https://github.com/fantasyland/fantasy-land#functor),
13 * applies the function to each of the functor's values, and returns
14 * a functor of the same shape.
15 *
16 * Ramda provides suitable `map` implementations for `Array` and `Object`,
17 * so this function may be applied to `[1, 2, 3]` or `{x: 1, y: 2, z: 3}`.
18 *
19 * Dispatches to the `map` method of the second argument, if present.
20 *
21 * Acts as a transducer if a transformer is given in list position.
22 *
23 * Also treats functions as functors and will compose them together.
24 *
25 * @func
26 * @memberOf R
27 * @since v0.1.0
28 * @category List
29 * @sig Functor f => (a -> b) -> f a -> f b
30 * @param {Function} fn The function to be called on every element of the input `list`.
31 * @param {Array} list The list to be iterated over.
32 * @return {Array} The new list.
33 * @see R.transduce, R.addIndex
34 * @example
35 *
36 * const double = x => x * 2;
37 *
38 * R.map(double, [1, 2, 3]); //=> [2, 4, 6]
39 *
40 * R.map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6}
41 * @symb R.map(f, [a, b]) = [f(a), f(b)]
42 * @symb R.map(f, { x: a, y: b }) = { x: f(a), y: f(b) }
43 * @symb R.map(f, functor_o) = functor_o.map(f)
44 */
45var map = _curry2(_dispatchable(['fantasy-land/map', 'map'], _xmap, function map(fn, functor) {
46 switch (Object.prototype.toString.call(functor)) {
47 case '[object Function]':
48 return curryN(functor.length, function() {
49 return fn.call(this, functor.apply(this, arguments));
50 });
51 case '[object Object]':
52 return _reduce(function(acc, key) {
53 acc[key] = fn(functor[key]);
54 return acc;
55 }, {}, keys(functor));
56 default:
57 return _map(fn, functor);
58 }
59}));
60export default map;
61