Permalink
Browse files

0.5.0 curry a whole object

  • Loading branch information...
1 parent bc3e01b commit 50069196f7c19e694b5affe73903a30d8722a5cb @seanhess seanhess committed Aug 28, 2012
Showing with 34 additions and 307 deletions.
  1. +5 −0 README.md
  2. +14 −0 index.coffee
  3. +0 −305 index.js
  4. +4 −2 package.json
  5. +11 −0 test.coffee
View
@@ -48,6 +48,11 @@ The syntax looks best in coffeescript, because you can just prefix a function de
assert.deepEqual [1,2,3].map(add(2)), [3,4,5]
+You can also use `curry` on a whole module, making it easy to work with functions that haven't been declared as curried. It curry any module functions it finds. Since curry doesn't have any effect when the function is called with all arguments, it doens't hurt anything to do this.
+
+ math = curry require('math')
+ add2 = math.add(2)
+
#### Simple Stuff
We need some simple functions so we can call object actions as functions instead of doing it with a "."
View
@@ -9,6 +9,10 @@ fjs = (_) ->
# it will turn add = (a,b) -> a+b into add = (a) -> (b) -> a+b
# so you can call #2 with add(2)(4)
curry = (f) ->
+
+ if not _.isFunction f
+ return curryModule f
+
call = (args...) ->
# if we have at least as many arguments as our f supports
@@ -31,6 +35,16 @@ fjs = (_) ->
call.curryLength = f.length
return call
+ # curries every function on the module
+ curryModule = (module) ->
+ module = _.clone module
+
+ for name, value of module
+ if _.isFunction value
+ module[name] = curry value
+
+ return module
+
# flips the arguments of a function
flip = (f) -> (args...) -> f(args.reverse()...)
id = (a) -> a
View
305 index.js

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -2,10 +2,12 @@
"author": "Sean Hess <sean@i.tv> (http://seanhess.github.com)",
"name": "fjs",
"description": "Utilities for Functional Javascript",
- "version": "0.4.5",
+ "version": "0.5.0",
"homepage": "http://github.com/idottv/fjs",
+ "license": "BSD",
"scripts": {
- "test":"node_modules/.bin/coffee -c index.coffee && node_modules/.bin/mocha -R spec --compilers coffee:coffee-script test.coffee"
+ "test": "node_modules/.bin/mocha -R spec --compilers coffee:coffee-script test.coffee",
+ "prepublish": "node_modules/.bin/coffee -c index.coffee"
},
"repository": {
"type": "git",
View
@@ -19,6 +19,17 @@ describe 'fjs', ->
assert.equal add(2,3), 5
assert.equal add2(3), 5
+ it 'should curry a module with functions', ->
+ module =
+ add: (a, b) -> a + b
+
+ curried = curry module
+ assert.ok curried.add, "didn't copy functions"
+ add2 = curried.add(2)
+ assert.equal add2(3), curried.add(2,3)
+ assert.equal add2(3), module.add(2,3)
+
+
describe 'method', ->
it 'should call something with this', ->
name = (obj) -> obj.name

0 comments on commit 5006919

Please sign in to comment.