From ddbda5cc9a24680ea8d2bc666dc7f1451655b7bc Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Thu, 14 Jul 2016 19:36:27 -0400 Subject: [PATCH 01/17] add partial hook --- src/stdlib/ns/fn.es6 | 5 +++++ src/stdlib/ns/fn/common.es6 | 1 + src/stdlib/ns/fn/hook.es6 | 26 ++++++++++++++++++++++++++ src/stdlib/stdlib.es6 | 3 ++- 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/stdlib/ns/fn.es6 create mode 100644 src/stdlib/ns/fn/common.es6 create mode 100644 src/stdlib/ns/fn/hook.es6 diff --git a/src/stdlib/ns/fn.es6 b/src/stdlib/ns/fn.es6 new file mode 100644 index 00000000..d85b449b --- /dev/null +++ b/src/stdlib/ns/fn.es6 @@ -0,0 +1,5 @@ +export default function(cheddar){ + return cheddar.namespace([ + ["hook", cheddar.from(require("./fn/hook.es6"))] + ]); +} diff --git a/src/stdlib/ns/fn/common.es6 b/src/stdlib/ns/fn/common.es6 new file mode 100644 index 00000000..922a6bb7 --- /dev/null +++ b/src/stdlib/ns/fn/common.es6 @@ -0,0 +1 @@ +// functions common to each of the items diff --git a/src/stdlib/ns/fn/hook.es6 b/src/stdlib/ns/fn/hook.es6 new file mode 100644 index 00000000..ef8db035 --- /dev/null +++ b/src/stdlib/ns/fn/hook.es6 @@ -0,0 +1,26 @@ +export default function hook(cheddar) { + return new cheddar.func( + [ + ["f", {}], + ["g", {}] + ], + function(scope, input){ + return new cheddar.func( + [["args", {Splat: true}]], + function(s, k){ + let args = k("args").value; + + if(args.length > 2 || !args.length) + return `Expected 1 or 2 args, received ${args.length || "none"}`; + + + let [a, b] = args; + let f = input("f"); + let g = input("g"); + + return f.exec(null, [a, g.exec(null, [args.length === 1 ? a : b])]); + } + ) + } + ); +} diff --git a/src/stdlib/stdlib.es6 b/src/stdlib/stdlib.es6 index 03438587..ae61a1c7 100644 --- a/src/stdlib/stdlib.es6 +++ b/src/stdlib/stdlib.es6 @@ -9,6 +9,7 @@ STDLIB.Item("Math", require('./ns/Math')); //STDLIB.Item("Buffer", require('./ns/Buffer')); STDLIB.Item("IO", require('./ns/IO')); STDLIB.Item("HTTP", require('./ns/HTTP')); +STDLIB.Item("fn", require("./ns/fn")); /** Primitives **/ STDLIB.set("String", API.var(API.string)); @@ -16,4 +17,4 @@ STDLIB.set("Number", API.var(API.number)); STDLIB.set("Array", API.var(API.array)); STDLIB.set("Boolean", API.var(API.bool)); -export default STDLIB; \ No newline at end of file +export default STDLIB; From 4402daef7b87fe553de43e904e53ff62a36932f8 Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Thu, 14 Jul 2016 19:37:12 -0400 Subject: [PATCH 02/17] fix hook --- src/stdlib/ns/fn/hook.es6 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/stdlib/ns/fn/hook.es6 b/src/stdlib/ns/fn/hook.es6 index ef8db035..edb5fb14 100644 --- a/src/stdlib/ns/fn/hook.es6 +++ b/src/stdlib/ns/fn/hook.es6 @@ -13,12 +13,10 @@ export default function hook(cheddar) { if(args.length > 2 || !args.length) return `Expected 1 or 2 args, received ${args.length || "none"}`; - - let [a, b] = args; let f = input("f"); let g = input("g"); - return f.exec(null, [a, g.exec(null, [args.length === 1 ? a : b])]); + return f.exec(s, [args[0], g.exec(s, [args.length === 1 ? args[0] : args[1]])]) } ) } From e3a0c4d12505ccba0711bedb50ed8c9df42352ca Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Thu, 14 Jul 2016 19:54:37 -0400 Subject: [PATCH 03/17] finalize hook --- src/stdlib/ns/fn.es6 | 2 +- src/stdlib/ns/fn/hook.es6 | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stdlib/ns/fn.es6 b/src/stdlib/ns/fn.es6 index d85b449b..2065a849 100644 --- a/src/stdlib/ns/fn.es6 +++ b/src/stdlib/ns/fn.es6 @@ -1,5 +1,5 @@ export default function(cheddar){ return cheddar.namespace([ - ["hook", cheddar.from(require("./fn/hook.es6"))] + ["hook", cheddar.from(require("./fn/hook"))] ]); } diff --git a/src/stdlib/ns/fn/hook.es6 b/src/stdlib/ns/fn/hook.es6 index edb5fb14..29d0ce44 100644 --- a/src/stdlib/ns/fn/hook.es6 +++ b/src/stdlib/ns/fn/hook.es6 @@ -15,8 +15,8 @@ export default function hook(cheddar) { let f = input("f"); let g = input("g"); - - return f.exec(s, [args[0], g.exec(s, [args.length === 1 ? args[0] : args[1]])]) + + return f.exec([args[0], g.exec([args.length === 1 ? args[0] : args[1]], null)], null); } ) } From 8ea7240c2fd7366b3fd8e62022591787678b4d3a Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Thu, 14 Jul 2016 20:03:22 -0400 Subject: [PATCH 04/17] add tentative fork --- src/stdlib/ns/fn.es6 | 3 ++- src/stdlib/ns/fn/fork.es6 | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/stdlib/ns/fn/fork.es6 diff --git a/src/stdlib/ns/fn.es6 b/src/stdlib/ns/fn.es6 index 2065a849..a20d2f72 100644 --- a/src/stdlib/ns/fn.es6 +++ b/src/stdlib/ns/fn.es6 @@ -1,5 +1,6 @@ export default function(cheddar){ return cheddar.namespace([ - ["hook", cheddar.from(require("./fn/hook"))] + ["hook", cheddar.from(require("./fn/hook"))], + ["fork", cheddar.from(require("./fn/fork"))] ]); } diff --git a/src/stdlib/ns/fn/fork.es6 b/src/stdlib/ns/fn/fork.es6 new file mode 100644 index 00000000..e0bb42bb --- /dev/null +++ b/src/stdlib/ns/fn/fork.es6 @@ -0,0 +1,26 @@ +export default function fork(cheddar) { + return new cheddar.func( + [ + ["f", {}], + ["g", {}], + ["h", {}] + ], + function(scope, input){ + return new cheddar.func( + [["args", {Splat: true}]], + function(s, k){ + let args = k("args").value; + + let f = input("f"); + let g = input("g"); + let h = input("h"); + + return g.exec([ + f.exec(args, null), + h.exec(args, null) + ], null); + } + ) + } + ); +} From 0b2e80f8485089fd3097d7694994b8f48d8056af Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Thu, 14 Jul 2016 20:52:14 -0400 Subject: [PATCH 05/17] fix fork; add reflexive --- src/stdlib/ns/fn.es6 | 3 ++- src/stdlib/ns/fn/reflexive.es6 | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/stdlib/ns/fn/reflexive.es6 diff --git a/src/stdlib/ns/fn.es6 b/src/stdlib/ns/fn.es6 index a20d2f72..1d1f4f24 100644 --- a/src/stdlib/ns/fn.es6 +++ b/src/stdlib/ns/fn.es6 @@ -1,6 +1,7 @@ export default function(cheddar){ return cheddar.namespace([ ["hook", cheddar.from(require("./fn/hook"))], - ["fork", cheddar.from(require("./fn/fork"))] + ["fork", cheddar.from(require("./fn/fork"))], + ["reflexive", cheddar.from(require("./fn/reflexive"))] ]); } diff --git a/src/stdlib/ns/fn/reflexive.es6 b/src/stdlib/ns/fn/reflexive.es6 new file mode 100644 index 00000000..17db1f7d --- /dev/null +++ b/src/stdlib/ns/fn/reflexive.es6 @@ -0,0 +1,17 @@ +export default function reflexive(cheddar) { + return new cheddar.func( + [ + ["f", {}], + ], + function(scope, input){ + return new cheddar.func( + [["a", {}]], + function(s, k){ + let f = input("f"); + + return f.exec(Array(f.args.length).fill(k("a")), null); + } + ) + } + ); +} From f6060de266d1746c29d03e5b0a5b6bd2bb5a14aa Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Thu, 14 Jul 2016 21:33:24 -0400 Subject: [PATCH 06/17] add various functions --- src/stdlib/ns/fn.es6 | 5 ++++- src/stdlib/ns/fn/common.es6 | 5 +++++ src/stdlib/ns/fn/insert.es6 | 21 +++++++++++++++++++++ src/stdlib/ns/fn/prefixes.es6 | 13 +++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/stdlib/ns/fn/insert.es6 create mode 100644 src/stdlib/ns/fn/prefixes.es6 diff --git a/src/stdlib/ns/fn.es6 b/src/stdlib/ns/fn.es6 index 1d1f4f24..227ef0dd 100644 --- a/src/stdlib/ns/fn.es6 +++ b/src/stdlib/ns/fn.es6 @@ -2,6 +2,9 @@ export default function(cheddar){ return cheddar.namespace([ ["hook", cheddar.from(require("./fn/hook"))], ["fork", cheddar.from(require("./fn/fork"))], - ["reflexive", cheddar.from(require("./fn/reflexive"))] + ["reflexive", cheddar.from(require("./fn/reflexive"))], + ["rflx", cheddar.from(require("./fn/reflexive"))], + ["insert", cheddar.from(require("./fn/insert"))], + ["prefixes", cheddar.from(require("./fn/prefixes"))] ]); } diff --git a/src/stdlib/ns/fn/common.es6 b/src/stdlib/ns/fn/common.es6 index 922a6bb7..d070b45b 100644 --- a/src/stdlib/ns/fn/common.es6 +++ b/src/stdlib/ns/fn/common.es6 @@ -1 +1,6 @@ // functions common to each of the items + +const prefixes = (array) => + array.map((e, i) => array.slice(0, i + 1)); + +export { prefixes }; diff --git a/src/stdlib/ns/fn/insert.es6 b/src/stdlib/ns/fn/insert.es6 new file mode 100644 index 00000000..f0926036 --- /dev/null +++ b/src/stdlib/ns/fn/insert.es6 @@ -0,0 +1,21 @@ +export default function reflexive(cheddar) { + return new cheddar.func( + [ + ["f", {}], + ], + function(scope, input){ + return new cheddar.func( + [["array", {}]], + function(s, k){ + let arr = k("array"); + let f = input("f"); + + if(arr.length === 1) + return arr; + + return arr.value.reduce((prev, cur) => f.exec([prev, cur], null)); + } + ) + } + ); +} diff --git a/src/stdlib/ns/fn/prefixes.es6 b/src/stdlib/ns/fn/prefixes.es6 new file mode 100644 index 00000000..1f5834d5 --- /dev/null +++ b/src/stdlib/ns/fn/prefixes.es6 @@ -0,0 +1,13 @@ +import { prefixes } from "common"; + +export default function prefixes(cheddar) { + return new cheddar.func( + [ + ["array", {}], + ], + function(scope, input){ + return cheddar.init(cheddar.array(...prefixes(array.value))); + } + ); +} + From fd3ce146a4feff11d8078a9dbdd5724929e053ec Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Thu, 14 Jul 2016 22:15:40 -0400 Subject: [PATCH 07/17] added pref, prefix (bugged), and id functions --- src/stdlib/ns/fn.es6 | 4 +++- src/stdlib/ns/fn/common.es6 | 6 ------ src/stdlib/ns/fn/id.es6 | 8 ++++++++ src/stdlib/ns/fn/pref.es6 | 12 ++++++++++++ src/stdlib/ns/fn/prefix.es6 | 19 +++++++++++++++++++ src/stdlib/ns/fn/prefixes.es6 | 13 ------------- 6 files changed, 42 insertions(+), 20 deletions(-) delete mode 100644 src/stdlib/ns/fn/common.es6 create mode 100644 src/stdlib/ns/fn/id.es6 create mode 100644 src/stdlib/ns/fn/pref.es6 create mode 100644 src/stdlib/ns/fn/prefix.es6 delete mode 100644 src/stdlib/ns/fn/prefixes.es6 diff --git a/src/stdlib/ns/fn.es6 b/src/stdlib/ns/fn.es6 index 227ef0dd..b8a1771d 100644 --- a/src/stdlib/ns/fn.es6 +++ b/src/stdlib/ns/fn.es6 @@ -4,7 +4,9 @@ export default function(cheddar){ ["fork", cheddar.from(require("./fn/fork"))], ["reflexive", cheddar.from(require("./fn/reflexive"))], ["rflx", cheddar.from(require("./fn/reflexive"))], + ["pref", cheddar.from(require("./fn/pref"))], ["insert", cheddar.from(require("./fn/insert"))], - ["prefixes", cheddar.from(require("./fn/prefixes"))] + ["prefix", cheddar.from(require("./fn/prefix"))], + ["id", cheddar.from(require("./fn/id"))] ]); } diff --git a/src/stdlib/ns/fn/common.es6 b/src/stdlib/ns/fn/common.es6 deleted file mode 100644 index d070b45b..00000000 --- a/src/stdlib/ns/fn/common.es6 +++ /dev/null @@ -1,6 +0,0 @@ -// functions common to each of the items - -const prefixes = (array) => - array.map((e, i) => array.slice(0, i + 1)); - -export { prefixes }; diff --git a/src/stdlib/ns/fn/id.es6 b/src/stdlib/ns/fn/id.es6 new file mode 100644 index 00000000..b5359613 --- /dev/null +++ b/src/stdlib/ns/fn/id.es6 @@ -0,0 +1,8 @@ +export default function id(cheddar) { + return new cheddar.func( + [["a", {}]], + function(scope, input){ + return input("a"); + } + ) +} diff --git a/src/stdlib/ns/fn/pref.es6 b/src/stdlib/ns/fn/pref.es6 new file mode 100644 index 00000000..a71de9f2 --- /dev/null +++ b/src/stdlib/ns/fn/pref.es6 @@ -0,0 +1,12 @@ +export default function pref(cheddar) { + return new cheddar.func( + [ + ["array", {}], + ], + function(scope, input){ + return cheddar.init(cheddar.array, ...input("array").value.map( + (e, i, a) => cheddar.init(cheddar.array, ...a.slice(0, i + 1)) + )); + } + ); +} diff --git a/src/stdlib/ns/fn/prefix.es6 b/src/stdlib/ns/fn/prefix.es6 new file mode 100644 index 00000000..dd204ad6 --- /dev/null +++ b/src/stdlib/ns/fn/prefix.es6 @@ -0,0 +1,19 @@ +export default function prefix(cheddar) { + return new cheddar.func( + [["f", {}]], + function(scope, k){ + return new cheddar.func( + [["array", {}]], + function(s, input){ + let f = k("f"); + return cheddar.init(cheddar.array, ...input("array").value.map( + (e, i, a) => f.exec( + cheddar.init(cheddar.array, ...a.slice(0, i + 1)), + null + ) + )); + } + ); + } + ) +} diff --git a/src/stdlib/ns/fn/prefixes.es6 b/src/stdlib/ns/fn/prefixes.es6 deleted file mode 100644 index 1f5834d5..00000000 --- a/src/stdlib/ns/fn/prefixes.es6 +++ /dev/null @@ -1,13 +0,0 @@ -import { prefixes } from "common"; - -export default function prefixes(cheddar) { - return new cheddar.func( - [ - ["array", {}], - ], - function(scope, input){ - return cheddar.init(cheddar.array(...prefixes(array.value))); - } - ); -} - From 1c7bed6450fbaa77e2dca50239c44b494efb977f Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Fri, 15 Jul 2016 16:10:48 -0400 Subject: [PATCH 08/17] commit for debugging purposes --- src/stdlib/ns/fn/insert.es6 | 8 ++++++-- src/stdlib/ns/fn/prefix.es6 | 7 +++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/stdlib/ns/fn/insert.es6 b/src/stdlib/ns/fn/insert.es6 index f0926036..03975a00 100644 --- a/src/stdlib/ns/fn/insert.es6 +++ b/src/stdlib/ns/fn/insert.es6 @@ -1,11 +1,13 @@ -export default function reflexive(cheddar) { +export default function insert(cheddar) { return new cheddar.func( [ ["f", {}], ], function(scope, input){ return new cheddar.func( - [["array", {}]], + [["array", { + Type: cheddar.array + }]], function(s, k){ let arr = k("array"); let f = input("f"); @@ -13,6 +15,8 @@ export default function reflexive(cheddar) { if(arr.length === 1) return arr; + // console.log("DEBUG:",arr); + return arr.value.reduce((prev, cur) => f.exec([prev, cur], null)); } ) diff --git a/src/stdlib/ns/fn/prefix.es6 b/src/stdlib/ns/fn/prefix.es6 index dd204ad6..5eb507b4 100644 --- a/src/stdlib/ns/fn/prefix.es6 +++ b/src/stdlib/ns/fn/prefix.es6 @@ -7,10 +7,9 @@ export default function prefix(cheddar) { function(s, input){ let f = k("f"); return cheddar.init(cheddar.array, ...input("array").value.map( - (e, i, a) => f.exec( - cheddar.init(cheddar.array, ...a.slice(0, i + 1)), - null - ) + (e, i, a) => f.exec([ + cheddar.init(cheddar.array, ...a.slice(0, i + 1)) + ], null) )); } ); From 5b0868df26b1934db3a5f8b2242a68d9a1543112 Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Sun, 17 Jul 2016 19:02:34 -0400 Subject: [PATCH 09/17] test file --- test.cdr | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 test.cdr diff --git a/test.cdr b/test.cdr new file mode 100644 index 00000000..368e3bfc --- /dev/null +++ b/test.cdr @@ -0,0 +1,3 @@ +var fib = (n) -> { + print n +} From dfb55003d8ee9aa9ad2d6028cf8c5031e21199a9 Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Sun, 24 Jul 2016 13:43:46 -0400 Subject: [PATCH 10/17] add fn.rflx --- src/stdlib/ns/fn.es6 | 2 +- src/stdlib/ns/fn/rflx.es6 | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/stdlib/ns/fn/rflx.es6 diff --git a/src/stdlib/ns/fn.es6 b/src/stdlib/ns/fn.es6 index b8a1771d..791e2a91 100644 --- a/src/stdlib/ns/fn.es6 +++ b/src/stdlib/ns/fn.es6 @@ -3,7 +3,7 @@ export default function(cheddar){ ["hook", cheddar.from(require("./fn/hook"))], ["fork", cheddar.from(require("./fn/fork"))], ["reflexive", cheddar.from(require("./fn/reflexive"))], - ["rflx", cheddar.from(require("./fn/reflexive"))], + ["rflx", cheddar.from(require("./fn/rflx"))], ["pref", cheddar.from(require("./fn/pref"))], ["insert", cheddar.from(require("./fn/insert"))], ["prefix", cheddar.from(require("./fn/prefix"))], diff --git a/src/stdlib/ns/fn/rflx.es6 b/src/stdlib/ns/fn/rflx.es6 new file mode 100644 index 00000000..df6fb4a5 --- /dev/null +++ b/src/stdlib/ns/fn/rflx.es6 @@ -0,0 +1,20 @@ +export default function reflexive(cheddar) { + return new cheddar.func( + [ + ["f", {}], + ], + function(scope, input){ + return new cheddar.func( + [["a", { + Splat: true + }]], + function(s, k){ + let f = input("f"); + let a = k("a"); + let args = a.value.concat(a.value); + return f.exec(args, null); + } + ) + } + ); +} From 754ce7145adb3896e4037b2c5d740217f0f1cf43 Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Sun, 24 Jul 2016 14:01:25 -0400 Subject: [PATCH 11/17] add fn.curry --- src/stdlib/ns/fn.es6 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/stdlib/ns/fn.es6 b/src/stdlib/ns/fn.es6 index 791e2a91..de052f02 100644 --- a/src/stdlib/ns/fn.es6 +++ b/src/stdlib/ns/fn.es6 @@ -7,6 +7,7 @@ export default function(cheddar){ ["pref", cheddar.from(require("./fn/pref"))], ["insert", cheddar.from(require("./fn/insert"))], ["prefix", cheddar.from(require("./fn/prefix"))], - ["id", cheddar.from(require("./fn/id"))] + ["id", cheddar.from(require("./fn/id"))], + ["curry", cheddar.from(require("./fn/curry"))] ]); } From dc8ffe98704414d0d1effb15bb162060712dc121 Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Sun, 24 Jul 2016 14:22:46 -0400 Subject: [PATCH 12/17] add fn.bind --- src/stdlib/ns/fn.es6 | 3 ++- src/stdlib/ns/fn/bind.es6 | 29 +++++++++++++++++++++++++++++ src/stdlib/ns/fn/curry.es6 | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/stdlib/ns/fn/bind.es6 create mode 100644 src/stdlib/ns/fn/curry.es6 diff --git a/src/stdlib/ns/fn.es6 b/src/stdlib/ns/fn.es6 index de052f02..7626bfdc 100644 --- a/src/stdlib/ns/fn.es6 +++ b/src/stdlib/ns/fn.es6 @@ -8,6 +8,7 @@ export default function(cheddar){ ["insert", cheddar.from(require("./fn/insert"))], ["prefix", cheddar.from(require("./fn/prefix"))], ["id", cheddar.from(require("./fn/id"))], - ["curry", cheddar.from(require("./fn/curry"))] + ["curry", cheddar.from(require("./fn/curry"))], + ["bind", cheddar.from(require("./fn/bind"))] ]); } diff --git a/src/stdlib/ns/fn/bind.es6 b/src/stdlib/ns/fn/bind.es6 new file mode 100644 index 00000000..aebde74a --- /dev/null +++ b/src/stdlib/ns/fn/bind.es6 @@ -0,0 +1,29 @@ +export default function bind(cheddar) { + return new cheddar.func( + [ + ["args", {Splat: true}] + ], + function(scope, input){ + let args = input("args").value; + if(args[0] instanceof cheddar.func){ + let f = args.shift(); + return new cheddar.func( + [["b", {Splat: true}]], + function(s, k){ + return f.exec(k("b").value.concat(args), null); + } + ); + } else if(args[args.length - 1] instanceof cheddar.func){ + let f = args.pop(); + return new cheddar.func( + [["b", {Splat: true}]], + function(s, k){ + return f.exec(args.concat(k("b").value), null); + } + ); + } else { + return "Expected function as first or last argument."; + } + } + ); +} diff --git a/src/stdlib/ns/fn/curry.es6 b/src/stdlib/ns/fn/curry.es6 new file mode 100644 index 00000000..d522623d --- /dev/null +++ b/src/stdlib/ns/fn/curry.es6 @@ -0,0 +1,19 @@ +export default function curry(cheddar) { + return new cheddar.func( + [ + ["f", {}], + ["a", {}] + ], + function(scope, input){ + return new cheddar.func( + [["b", {}]], + function(s, k){ + let f = input("f"); + let a = input("a"); + let b = input("b"); + return f.exec([a, b], null); + } + ) + } + ); +} From 8482f046ed917e6ee9954f0333e7451ba9706b00 Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Sun, 24 Jul 2016 14:56:08 -0400 Subject: [PATCH 13/17] added fn.rev --- src/stdlib/ns/fn.es6 | 3 ++- src/stdlib/ns/fn/rev.es6 | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/stdlib/ns/fn/rev.es6 diff --git a/src/stdlib/ns/fn.es6 b/src/stdlib/ns/fn.es6 index 7626bfdc..33026aa0 100644 --- a/src/stdlib/ns/fn.es6 +++ b/src/stdlib/ns/fn.es6 @@ -9,6 +9,7 @@ export default function(cheddar){ ["prefix", cheddar.from(require("./fn/prefix"))], ["id", cheddar.from(require("./fn/id"))], ["curry", cheddar.from(require("./fn/curry"))], - ["bind", cheddar.from(require("./fn/bind"))] + ["bind", cheddar.from(require("./fn/bind"))], + ["rev", cheddar.from(require("./fn/rev"))] ]); } diff --git a/src/stdlib/ns/fn/rev.es6 b/src/stdlib/ns/fn/rev.es6 new file mode 100644 index 00000000..500fbd0c --- /dev/null +++ b/src/stdlib/ns/fn/rev.es6 @@ -0,0 +1,16 @@ +export default function rev(cheddar) { + return new cheddar.func( + [ + ["f", {}] + ], + function(scope, input){ + return new cheddar.func( + [["a", { Splat: true }]], + function(s, k){ + let a = k("a"); + return input("f").exec(a.value.reverse(), null); + } + ) + } + ); +} From 73c8a4c6d8f9f2be5e025e6f20a30c8fa07b619b Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Sun, 24 Jul 2016 21:58:17 -0400 Subject: [PATCH 14/17] added repeat function --- src/stdlib/ns/fn.es6 | 3 ++- src/stdlib/ns/fn/curry.es6 | 2 +- src/stdlib/ns/fn/repeat.es6 | 27 +++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/stdlib/ns/fn/repeat.es6 diff --git a/src/stdlib/ns/fn.es6 b/src/stdlib/ns/fn.es6 index 33026aa0..99bc3c14 100644 --- a/src/stdlib/ns/fn.es6 +++ b/src/stdlib/ns/fn.es6 @@ -10,6 +10,7 @@ export default function(cheddar){ ["id", cheddar.from(require("./fn/id"))], ["curry", cheddar.from(require("./fn/curry"))], ["bind", cheddar.from(require("./fn/bind"))], - ["rev", cheddar.from(require("./fn/rev"))] + ["rev", cheddar.from(require("./fn/rev"))], + ["repeat", cheddar.from(require("./fn/repeat"))] ]); } diff --git a/src/stdlib/ns/fn/curry.es6 b/src/stdlib/ns/fn/curry.es6 index d522623d..154c0a31 100644 --- a/src/stdlib/ns/fn/curry.es6 +++ b/src/stdlib/ns/fn/curry.es6 @@ -10,7 +10,7 @@ export default function curry(cheddar) { function(s, k){ let f = input("f"); let a = input("a"); - let b = input("b"); + let b = k("b"); return f.exec([a, b], null); } ) diff --git a/src/stdlib/ns/fn/repeat.es6 b/src/stdlib/ns/fn/repeat.es6 new file mode 100644 index 00000000..6320f579 --- /dev/null +++ b/src/stdlib/ns/fn/repeat.es6 @@ -0,0 +1,27 @@ +export default function repeat(cheddar) { + return new cheddar.func( + [ + ["f", {}], + ["n", {}] + ], + function(scope, input){ + return new cheddar.func( + [["args", { Splat: true }]], + function(s, k){ + let f = input("f"); + let n = input("n").value; + let args = k("args"); + if(n === 0) + return args; + + args = args.value; + let res = f.exec(args, null); + while(--n > 0){ + res = f.exec([res], null); + } + return res; + } + ) + } + ); +} From a413e81b8221a87cd7fb1a8bc1957849adb4e0d8 Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Sat, 13 Aug 2016 21:55:21 -0400 Subject: [PATCH 15/17] initialize fn.of --- src/stdlib/ns/fn.es6 | 3 ++- src/stdlib/ns/fn/of.es6 | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/stdlib/ns/fn/of.es6 diff --git a/src/stdlib/ns/fn.es6 b/src/stdlib/ns/fn.es6 index 99bc3c14..6f2a4ed3 100644 --- a/src/stdlib/ns/fn.es6 +++ b/src/stdlib/ns/fn.es6 @@ -11,6 +11,7 @@ export default function(cheddar){ ["curry", cheddar.from(require("./fn/curry"))], ["bind", cheddar.from(require("./fn/bind"))], ["rev", cheddar.from(require("./fn/rev"))], - ["repeat", cheddar.from(require("./fn/repeat"))] + ["repeat", cheddar.from(require("./fn/repeat"))], + ["of", cheddar.from(require("./fn/of"))] ]); } diff --git a/src/stdlib/ns/fn/of.es6 b/src/stdlib/ns/fn/of.es6 new file mode 100644 index 00000000..93cb87dc --- /dev/null +++ b/src/stdlib/ns/fn/of.es6 @@ -0,0 +1,12 @@ +export default function insert(cheddar) { + return new cheddar.func( + [ + ["str", { + Type: cheddar.string + }], + ], + function(scope, input){ + let str = input("str"); + } + ); +} From 864855b8141c117de6af635025174b2950eff3b9 Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Sat, 13 Aug 2016 22:28:32 -0400 Subject: [PATCH 16/17] suspending dev on fn.of for the time being --- src/stdlib/ns/fn.es6 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stdlib/ns/fn.es6 b/src/stdlib/ns/fn.es6 index 6f2a4ed3..4ea69e72 100644 --- a/src/stdlib/ns/fn.es6 +++ b/src/stdlib/ns/fn.es6 @@ -11,7 +11,7 @@ export default function(cheddar){ ["curry", cheddar.from(require("./fn/curry"))], ["bind", cheddar.from(require("./fn/bind"))], ["rev", cheddar.from(require("./fn/rev"))], - ["repeat", cheddar.from(require("./fn/repeat"))], - ["of", cheddar.from(require("./fn/of"))] + ["repeat", cheddar.from(require("./fn/repeat"))] + // ["of", cheddar.from(require("./fn/of"))] ]); } From b555294981b0c4244a75b98c30adf48c803b1dc6 Mon Sep 17 00:00:00 2001 From: Conor O'Brien Date: Tue, 23 Aug 2016 22:46:52 -0400 Subject: [PATCH 17/17] added stricter typing; began writing tests. --- src/stdlib/ns/fn.es6 | 6 +++--- src/stdlib/ns/fn/curry.es6 | 2 +- src/stdlib/ns/fn/fork.es6 | 6 +++--- src/stdlib/ns/fn/hook.es6 | 11 ++++++---- src/stdlib/ns/fn/id.es6 | 2 +- src/stdlib/ns/fn/insert.es6 | 2 +- src/stdlib/ns/fn/of.es6 | 39 ++++++++++++++++++++++++++++++++++ src/stdlib/ns/fn/prefix.es6 | 2 +- src/stdlib/ns/fn/reflexive.es6 | 2 +- src/stdlib/ns/fn/repeat.es6 | 4 ++-- src/stdlib/ns/fn/rev.es6 | 2 +- src/stdlib/ns/fn/rflx.es6 | 20 ----------------- test/tests/stdlib/fn.js | 22 +++++++++++++++++++ 13 files changed, 82 insertions(+), 38 deletions(-) delete mode 100644 src/stdlib/ns/fn/rflx.es6 create mode 100644 test/tests/stdlib/fn.js diff --git a/src/stdlib/ns/fn.es6 b/src/stdlib/ns/fn.es6 index 4ea69e72..2e7e6ed9 100644 --- a/src/stdlib/ns/fn.es6 +++ b/src/stdlib/ns/fn.es6 @@ -3,7 +3,7 @@ export default function(cheddar){ ["hook", cheddar.from(require("./fn/hook"))], ["fork", cheddar.from(require("./fn/fork"))], ["reflexive", cheddar.from(require("./fn/reflexive"))], - ["rflx", cheddar.from(require("./fn/rflx"))], + ["dbl", cheddar.from(require("./fn/dbl"))], ["pref", cheddar.from(require("./fn/pref"))], ["insert", cheddar.from(require("./fn/insert"))], ["prefix", cheddar.from(require("./fn/prefix"))], @@ -11,7 +11,7 @@ export default function(cheddar){ ["curry", cheddar.from(require("./fn/curry"))], ["bind", cheddar.from(require("./fn/bind"))], ["rev", cheddar.from(require("./fn/rev"))], - ["repeat", cheddar.from(require("./fn/repeat"))] - // ["of", cheddar.from(require("./fn/of"))] + ["repeat", cheddar.from(require("./fn/repeat"))], + ["of", cheddar.from(require("./fn/of"))] ]); } diff --git a/src/stdlib/ns/fn/curry.es6 b/src/stdlib/ns/fn/curry.es6 index 154c0a31..ce0d9ecf 100644 --- a/src/stdlib/ns/fn/curry.es6 +++ b/src/stdlib/ns/fn/curry.es6 @@ -1,7 +1,7 @@ export default function curry(cheddar) { return new cheddar.func( [ - ["f", {}], + ["f", { Type: cheddar.func }], ["a", {}] ], function(scope, input){ diff --git a/src/stdlib/ns/fn/fork.es6 b/src/stdlib/ns/fn/fork.es6 index e0bb42bb..7c618661 100644 --- a/src/stdlib/ns/fn/fork.es6 +++ b/src/stdlib/ns/fn/fork.es6 @@ -1,9 +1,9 @@ export default function fork(cheddar) { return new cheddar.func( [ - ["f", {}], - ["g", {}], - ["h", {}] + ["f", { Type: cheddar.func }], + ["g", { Type: cheddar.func }], + ["h", { Type: cheddar.func }] ], function(scope, input){ return new cheddar.func( diff --git a/src/stdlib/ns/fn/hook.es6 b/src/stdlib/ns/fn/hook.es6 index 29d0ce44..dc7990e0 100644 --- a/src/stdlib/ns/fn/hook.es6 +++ b/src/stdlib/ns/fn/hook.es6 @@ -1,10 +1,13 @@ export default function hook(cheddar) { return new cheddar.func( [ - ["f", {}], - ["g", {}] + ["f", { Type: cheddar.func }], + ["g", { Type: cheddar.func }] ], - function(scope, input){ + function(scope, input, args){ + if(args.length !== 2) + return `Expected 2 arguments, received ${args.length}.`; + return new cheddar.func( [["args", {Splat: true}]], function(s, k){ @@ -15,7 +18,7 @@ export default function hook(cheddar) { let f = input("f"); let g = input("g"); - + return f.exec([args[0], g.exec([args.length === 1 ? args[0] : args[1]], null)], null); } ) diff --git a/src/stdlib/ns/fn/id.es6 b/src/stdlib/ns/fn/id.es6 index b5359613..e77d8120 100644 --- a/src/stdlib/ns/fn/id.es6 +++ b/src/stdlib/ns/fn/id.es6 @@ -1,6 +1,6 @@ export default function id(cheddar) { return new cheddar.func( - [["a", {}]], + [["a", { Type: cheddar.func }]], function(scope, input){ return input("a"); } diff --git a/src/stdlib/ns/fn/insert.es6 b/src/stdlib/ns/fn/insert.es6 index 03975a00..2450dd74 100644 --- a/src/stdlib/ns/fn/insert.es6 +++ b/src/stdlib/ns/fn/insert.es6 @@ -1,7 +1,7 @@ export default function insert(cheddar) { return new cheddar.func( [ - ["f", {}], + ["f", { Type: cheddar.func }], ], function(scope, input){ return new cheddar.func( diff --git a/src/stdlib/ns/fn/of.es6 b/src/stdlib/ns/fn/of.es6 index 93cb87dc..30179a7f 100644 --- a/src/stdlib/ns/fn/of.es6 +++ b/src/stdlib/ns/fn/of.es6 @@ -1,3 +1,26 @@ +import { UOP, OP } from "../../../tokenizer/consts/ops"; +import fork from "./fork"; + +const ops = [ + "+", "*", "**", "-", "^", + "&", "|", "/", "%", "@\"" +]; // TODO: make dynamic + +const parse = (str) => { + let tokens = []; + for(let i = 0; i < str.length; i++){ + if(!ops.some(x => x[0] === str[i])) continue; + + let build = ""; + while(ops.some(x => x.indexOf(build + str[i]) >= 0)){ + build += str[i++]; + } + i--; + tokens.push(build); + } + return tokens; +} + export default function insert(cheddar) { return new cheddar.func( [ @@ -7,6 +30,22 @@ export default function insert(cheddar) { ], function(scope, input){ let str = input("str"); + let toks = parse(str.value); + if(toks.length !== 3){ + return "I haven't implemented this yet."; + } + return fork.exec(...toks.map(e => + new cheddar.func( + ["LHS", { }], + ["RHS", { Optional: true }], + function(_scope, _input){ + let LHS = input("LHS"); + let RHS = input("RHS"); + + return LHS.Operator.get(e)(RHS ? LHS : null, RHS || LHS); + } + ) + ), null); } ); } diff --git a/src/stdlib/ns/fn/prefix.es6 b/src/stdlib/ns/fn/prefix.es6 index 5eb507b4..e7620fb4 100644 --- a/src/stdlib/ns/fn/prefix.es6 +++ b/src/stdlib/ns/fn/prefix.es6 @@ -1,6 +1,6 @@ export default function prefix(cheddar) { return new cheddar.func( - [["f", {}]], + [["f", { Type: cheddar.func }]], function(scope, k){ return new cheddar.func( [["array", {}]], diff --git a/src/stdlib/ns/fn/reflexive.es6 b/src/stdlib/ns/fn/reflexive.es6 index 17db1f7d..a22fefb3 100644 --- a/src/stdlib/ns/fn/reflexive.es6 +++ b/src/stdlib/ns/fn/reflexive.es6 @@ -1,7 +1,7 @@ export default function reflexive(cheddar) { return new cheddar.func( [ - ["f", {}], + ["f", { Type: cheddar.func }], ], function(scope, input){ return new cheddar.func( diff --git a/src/stdlib/ns/fn/repeat.es6 b/src/stdlib/ns/fn/repeat.es6 index 6320f579..1141e651 100644 --- a/src/stdlib/ns/fn/repeat.es6 +++ b/src/stdlib/ns/fn/repeat.es6 @@ -1,7 +1,7 @@ export default function repeat(cheddar) { return new cheddar.func( [ - ["f", {}], + ["f", { Type: cheddar.func }], ["n", {}] ], function(scope, input){ @@ -13,7 +13,7 @@ export default function repeat(cheddar) { let args = k("args"); if(n === 0) return args; - + args = args.value; let res = f.exec(args, null); while(--n > 0){ diff --git a/src/stdlib/ns/fn/rev.es6 b/src/stdlib/ns/fn/rev.es6 index 500fbd0c..6c549769 100644 --- a/src/stdlib/ns/fn/rev.es6 +++ b/src/stdlib/ns/fn/rev.es6 @@ -1,7 +1,7 @@ export default function rev(cheddar) { return new cheddar.func( [ - ["f", {}] + ["f", { Type: cheddar.func }] ], function(scope, input){ return new cheddar.func( diff --git a/src/stdlib/ns/fn/rflx.es6 b/src/stdlib/ns/fn/rflx.es6 deleted file mode 100644 index df6fb4a5..00000000 --- a/src/stdlib/ns/fn/rflx.es6 +++ /dev/null @@ -1,20 +0,0 @@ -export default function reflexive(cheddar) { - return new cheddar.func( - [ - ["f", {}], - ], - function(scope, input){ - return new cheddar.func( - [["a", { - Splat: true - }]], - function(s, k){ - let f = input("f"); - let a = k("a"); - let args = a.value.concat(a.value); - return f.exec(args, null); - } - ) - } - ); -} diff --git a/test/tests/stdlib/fn.js b/test/tests/stdlib/fn.js new file mode 100644 index 00000000..fc8e7916 --- /dev/null +++ b/test/tests/stdlib/fn.js @@ -0,0 +1,22 @@ +var TestCheddarFrom = require('../globals').TestCheddarFrom; +var chai = require('chai'); + +describe("fn", function(){ + describe("reflexive", function(){ + it("should work", TestCheddarFrom.Code( + "fn.reflexive((+))(3)", + "6" + )); + it("should work", TestCheddarFrom.Code( + "fn.reflexive((*))(3)", + "9" + )); + }); + + describe("hook", function(){ + it("should work", TestCheddarFrom.Code( + "fn.hook((+),(sqrt),(-))(4)", + "6" + )); + }); +});