Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d3af3a
commit edb746e
Showing
23 changed files
with
247 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
jasmine-node --autotest --coffee --verbose spec | ||
jasmine-node spec --coffee --autotest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
describe "Function", -> | ||
{composableByPositions} = require "../../src/FunctionComposing/Function" | ||
|
||
describe "composableByPositions", -> | ||
it "works", -> | ||
d = [[matches, isEven], [mapped, [sum, 2]]] | ||
f = composableByPositions [0], result | ||
expect(f d, [0, 1, 2, 3]).toEqual [2, 4] | ||
|
||
|
||
|
||
|
||
|
||
sum = (y, x) -> x + y | ||
difference = (y, x) -> x - y | ||
product = (y, x) -> x * y | ||
quotient = (y, x) -> x / y | ||
isEven = (x) -> x % 2 == 0 | ||
isOdd = (x) -> x % 2 == 1 | ||
times = (y, x) -> y for i in [0...x] | ||
matches = (f, xs) -> x for x in xs when f x | ||
mapped = (f, xs) -> f x for x in xs | ||
result = (f, x) -> f x if x? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
describe "FunctionTemplate", -> | ||
{func} = require "../../src/FunctionComposing/FunctionTemplate" | ||
|
||
describe "func", -> | ||
|
||
it "fails when first element is inappropriate", -> | ||
(expect -> func [2, sum, 3]).toThrow() | ||
|
||
it "returns a fully-applied function when all parameters are provided", -> | ||
(expect (func [difference, 17, 3])()).toEqual -14 | ||
(expect (func [product, 3, 6])()).toEqual 18 | ||
|
||
it "returns a partially applied function on lesser arguments", -> | ||
f = func [difference, 12] | ||
(expect f instanceof Function).toBeTruthy() | ||
expect(f 7).toBe -5 | ||
|
||
it "filter on a function works", -> | ||
f = func [matches, isEven] | ||
expect(f [0, 1, 2, 3, 4]).toEqual([0, 2, 4]) | ||
|
||
it "works on nested declarations", -> | ||
f = func [mapped, [sum, 2]] | ||
expect(f [0, 1, 2]).toEqual([2, 3, 4]) | ||
|
||
it "works on uber-nested declarations", -> | ||
f = func [mapped, [mapped, [sum, 3]]] | ||
expect(f [[2, 3], [4]]).toEqual([[5,6], [7]]) | ||
|
||
it "works on nested functions", -> | ||
f = func [[matches, isEven], [mapped, [sum, 2]]] | ||
expect(f [2, 3, 4]).toEqual [4, 6] | ||
expect(f [0, 1, 2, 3]).toEqual [2, 4] | ||
|
||
# it "passes the function unchanged", -> | ||
# expect((func difference) 7, 4).toEqual -3 | ||
|
||
|
||
|
||
sum = (y, x) -> x + y | ||
difference = (y, x) -> x - y | ||
product = (y, x) -> x * y | ||
quotient = (y, x) -> x / y | ||
isEven = (x) -> x % 2 == 0 | ||
isOdd = (x) -> x % 2 == 1 | ||
times = (y, x) -> y for i in [0...x] | ||
matches = (f, xs) -> x for x in xs when f x | ||
mapped = (f, xs) -> f x for x in xs | ||
result = (f, x) -> f x if x? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exports[name] = require "./Async/#{name}" for name in [ | ||
"Function" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
BasicFunctionComposing = require "../BasicFunctionComposing" | ||
|
||
exports.Function = Function | ||
|
||
exports.async = | ||
async = (f) -> | ||
-> callAsync BasicFunctionComposing.Function.partiallyApplied arguments, f | ||
|
||
exports.callAsync = | ||
callAsync = (f) -> | ||
if process? && process.nextTick then process.nextTick f | ||
else setTimeout f, 0 | ||
return | ||
|
||
exports.action = | ||
action = (f) -> | ||
async (args..., cb) -> | ||
cb f.apply this, args | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exports[name] = require "./BasicFunctionComposing/#{name}" for name in [ | ||
"Function" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
exports.Function = Function | ||
|
||
exports.nested = | ||
nested = (container, f) -> | ||
-> container.call this, f.apply this, arguments | ||
|
||
# exports.partiallyApplied = | ||
# partiallyApplied = (args, f) -> | ||
# args = members args unless args instanceof Array | ||
# -> f.apply this, union (members arguments), args | ||
|
||
# union = (ys, xs) -> if not ys then xs else xs.concat ys | ||
# members = (x) -> v for _, v of x | ||
|
||
exports.partiallyApplied = | ||
partiallyApplied = (args, f) -> | ||
-> | ||
args1 = [] | ||
args1.push(arg) for arg in args | ||
args1.push(arg) for arg in arguments | ||
f.apply this, args1 | ||
|
||
|
||
exports.withLength = | ||
withLength = (length, f) -> | ||
args = ("a" + i for i in [0...length]) | ||
body = """ | ||
return function(#{args.join(", ")}) { | ||
return f.apply(this, arguments); | ||
} | ||
""" | ||
(new Function "f", body) f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
exports.func = | ||
func = (template) -> | ||
-> | ||
ln0 = arguments.length.toString() | ||
return f.apply this, arguments for ln, f of template when ln == ln0 | ||
throw "Function does not support #{arguments.length} arguments" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
exports.func = | ||
func = (template) -> | ||
-> | ||
for [types, f] in template | ||
return f.apply this, arguments if argsMatchTypes types, arguments | ||
throw "Function does not support types of supplied arguments: #{(type arg)?.name for arg in arguments}" | ||
|
||
argsMatchTypes = (types, args) -> | ||
return false if args.length != types.length | ||
return false for t, i in types when t? && !instanceOf t, args[i] | ||
true | ||
|
||
instanceOf = (t, x) -> (x instanceof t) || (t == type x) | ||
type = (x) -> x?.constructor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports[k] = v for k, v of require "../FunctionComposing/FunctionTemplate" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.