Skip to content

Commit

Permalink
Merge pull request ramda#1741 from davidchambers/ap
Browse files Browse the repository at this point in the history
fix handling of function applicatives in R.ap
  • Loading branch information
davidchambers committed Apr 19, 2016
2 parents fd5b7c8 + 2ae0413 commit d2ef05f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/ap.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
var _concat = require('./internal/_concat');
var _curry2 = require('./internal/_curry2');
var _reduce = require('./internal/_reduce');
var curryN = require('./curryN');
var map = require('./map');


/**
* ap applies a list of functions to a list of values.
*
* Dispatches to the `ap` method of the second argument, if present. Also
* treats functions as applicatives.
* treats curried functions as applicatives.
*
* @func
* @memberOf R
Expand All @@ -28,9 +27,7 @@ module.exports = _curry2(function ap(applicative, fn) {
typeof applicative.ap === 'function' ?
applicative.ap(fn) :
typeof applicative === 'function' ?
curryN(Math.max(applicative.length, fn.length), function() {
return applicative.apply(this, arguments)(fn.apply(this, arguments));
}) :
function(x) { return applicative(x)(fn(x)); } :
// else
_reduce(function(acc, f) { return _concat(acc, map(f, fn)); }, [], applicative)
);
Expand Down
2 changes: 2 additions & 0 deletions test/ap.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ describe('ap', function() {
// (<*>) :: (r -> a -> b) -> (r -> a) -> (r -> b)
// f <*> g = \x -> f x (g x)
eq(h(10), 10 + (10 * 2));

eq(R.ap(R.add)(g)(10), 10 + (10 * 2));
});

it('dispatches to the passed object\'s ap method when values is a non-Array object', function() {
Expand Down

0 comments on commit d2ef05f

Please sign in to comment.