Skip to content
Browse files

added method

  • Loading branch information...
1 parent 93fec47 commit a2fccd5d533018a0c2fd06979f8c7e2cf4c31c87 @seanhess seanhess committed Jun 15, 2012
Showing with 93 additions and 12 deletions.
  1. +31 −5 index.coffee
  2. +44 −7 index.js
  3. +18 −0 test.coffee
View
36 index.coffee
@@ -14,9 +14,36 @@ fjs = (_) ->
f args...
# otherwise, return a function with the arguments partially applied
- else (args2...) ->
- innerArgs = args.concat(args2)
- call innerArgs...
+ else
+ inner = (args2...) ->
+ innerArgs = args.concat(args2)
+ call innerArgs...
+ inner.name = f.name
+ inner.toString = -> f.toString()
+ return inner
+
+ call.name = f.name
+ call.toString = -> f.toString()
+ return call
+
+ # flips the arguments of a function
+ flip = (f) -> (args...) -> f(args.reverse()...)
+ id = (a) -> a
+
+ # turns a function that takes its context as the first parameter, and calls it with "this"
+ # useful for turning funcitonal functions into methods on an object
+ # kind of like the reverse of Function.prototype.call
+ method = (func) ->
+ (args...) ->
+ func(this, args...)
+
+
+ # like compose, but backwards. So the first method on the left is the 1st one called.
+ # easier for most people to understand
+ chain = (funcs...) -> compose funcs.reverse()
+
+ functions = {curry, flip, id, method, chain}
+
## OBJECTS
# functional eqivalents to object-y stuff
@@ -43,7 +70,6 @@ fjs = (_) ->
objects = {get, set, call}
-
## DEBUG
log = (arg) ->
console.log arg
@@ -115,7 +141,7 @@ fjs = (_) ->
arrays = {reverse, take}
# export!
- _.extend {curry}, objects, basics, us, arrays, debug
+ _.extend functions, objects, basics, us, arrays, debug
if define?.amd?
define(['underscore'], (_) -> fjs _)
View
51 index.js
@@ -4,23 +4,62 @@
__slice = [].slice;
fjs = function(_) {
- var add, arrays, basics, call, compose, curry, debug, div, each, eq, filter, find, first, get, groupBy, gt, gte, head, indexOf, invoke, last, log, lt, lte, map, max, memoize, min, mult, negate, objects, reduce, rest, reverse, set, sort, sortBy, sub, tail, take, us;
+ var add, arrays, basics, call, chain, compose, curry, debug, div, each, eq, filter, find, first, flip, functions, get, groupBy, gt, gte, head, id, indexOf, invoke, last, log, lt, lte, map, max, memoize, method, min, mult, negate, objects, reduce, rest, reverse, set, sort, sortBy, sub, tail, take, us;
curry = function(f) {
var call;
- return call = function() {
- var args;
+ call = function() {
+ var args, inner;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
if (args.length >= f.length) {
return f.apply(null, args);
} else {
- return function() {
+ inner = function() {
var args2, innerArgs;
args2 = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
innerArgs = args.concat(args2);
return call.apply(null, innerArgs);
};
+ inner.name = f.name;
+ inner.toString = function() {
+ return f.toString();
+ };
+ return inner;
}
};
+ call.name = f.name;
+ call.toString = function() {
+ return f.toString();
+ };
+ return call;
+ };
+ flip = function(f) {
+ return function() {
+ var args;
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
+ return f.apply(null, args.reverse());
+ };
+ };
+ id = function(a) {
+ return a;
+ };
+ method = function(func) {
+ return function() {
+ var args;
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
+ return func.apply(null, [this].concat(__slice.call(args)));
+ };
+ };
+ chain = function() {
+ var funcs;
+ funcs = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
+ return compose(funcs.reverse());
+ };
+ functions = {
+ curry: curry,
+ flip: flip,
+ id: id,
+ method: method,
+ chain: chain
};
get = curry(function(name, obj) {
return obj[name];
@@ -167,9 +206,7 @@
reverse: reverse,
take: take
};
- return _.extend({
- curry: curry
- }, objects, basics, us, arrays, debug);
+ return _.extend(functions, objects, basics, us, arrays, debug);
};
if ((typeof define !== "undefined" && define !== null ? define.amd : void 0) != null) {
View
18 test.coffee
@@ -18,6 +18,14 @@ describe 'fjs', ->
assert.equal add(2,3), 5
assert.equal add2(3), 5
+ describe 'method', ->
+ it 'should call something with this', ->
+ name = (obj) -> obj.name
+ henry = {name: "henry"}
+ henry.getName = fjs.method name
+ assert.equal henry.getName(), "henry"
+
+
describe 'call', ->
it 'should call functions on the object', ->
obj =
@@ -116,6 +124,16 @@ describe 'fjs', ->
assert.deepEqual fjs.reverse(arr), [3,2,1]
assert.deepEqual arr, [1,2,3]
+ describe 'find', ->
+ it 'should work with map on an empty array', ->
+ seeking = [2,3]
+ values = []
+ isValue = curry (target, value) -> value is target
+ findValue = curry (values, target) -> fjs.find isValue(target), values
+ found = fjs.map findValue(values), seeking
+ assert.deepEqual [undefined, undefined], found
+
+

0 comments on commit a2fccd5

Please sign in to comment.
Something went wrong with that request. Please try again.