Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed uncurry and added beginnings of stdlib

  • Loading branch information...
commit eada5fe875f10bca9c75b798f95418ad4cddc656 1 parent ae45518
@CRogers authored
Showing with 143 additions and 6 deletions.
  1. +69 −5 lib/pun.js
  2. +6 −1 src/curry.coffee
  3. +68 −0 src/stdlib.coffee
View
74 lib/pun.js
@@ -87,8 +87,8 @@
return pun.uncurry.apply(this, [pun.curry(args.slice(argPos), struct - argsLeft)].concat(structure.slice(i + 1)));
} else {
res = res.apply(this, args.slice(argPos, argPos + struct));
+ argsLeft -= struct;
}
- argsLeft -= 1;
}
};
},
@@ -232,12 +232,76 @@
pun.$.__rawBindIdent = rawBindIdent;
}).call(this);
(function() {
+ var $, foldl, map, stdfuncs, stdtypes, toList, _;
+ stdtypes = {
+ List: pun.ADT({
+ Cons: ['value', 'next'],
+ Nil: []
+ }),
+ BTree: pun.ADT({
+ Branch: ['left', 'right'],
+ Leaf: ['value']
+ })
+ };
+ addExports(stdtypes);
addExports({
- map: function(f) {
- return pun.match($(Array), function(a) {
- return a.map(f);
- });
+ copyStdtypes: function(obj) {
+ var k, v, _results;
+ _results = [];
+ for (k in stdtypes) {
+ v = stdtypes[k];
+ _results.push(obj[k] = v);
+ }
+ return _results;
+ },
+ copyStdfuncs: function(obj) {
+ var b, k, _results;
+ _results = [];
+ for (k in stdfuncs) {
+ b = stdfuncs[k];
+ _results.push(obj[k] = v);
+ }
+ return _results;
+ },
+ copyStdlib: function(obj) {
+ copyStdtypes(obj);
+ return copyStdfuncs(obj);
}
});
+ $ = pun.$;
+ _ = pun._;
+ pun.copyStdtypes(this);
+ toList = pun.match([], function() {
+ return List.Nil();
+ }, $, function(xs) {
+ return List.Cons(xs[0], toList(xs.slice(1)));
+ });
+ map = function(f) {
+ return pun.match($(Array), function(a) {
+ return a.map(f);
+ }, List.Nil(), function() {
+ return List.Nil();
+ }, List.Cons($, $), function(x, xs) {
+ return List.Cons((f(x))((map(f))(xs)));
+ }, BTree.Branch($, $), function(l, r) {
+ return BTree.Branch(((map(f))(l))((map(f))(r)));
+ }, BTree.Leaf($), function(v) {
+ return f(v);
+ });
+ };
+ foldl = function(f, z) {
+ return pun.match($(Array), function(a) {
+ return ([z].concat(a)).reduce(f);
+ }, List.Nil(), function() {
+ return z;
+ }, List.Cons($, $), function(x, xs) {
+ return foldl(f, f(z(x)), xs);
+ });
+ };
+ stdfuncs = {
+ map: pun.uncurry(map, 1, 1),
+ foldl: pun.uncurry(foldl, 2, 1)
+ };
+ addExports(stdfuncs);
}).call(this);
}).call(this);
View
7 src/curry.coffee
@@ -8,16 +8,21 @@ addExports(
(args...) ->
argsLeft = args.length
res = f
+ # for each group of args in f(a,b)(c,d)(e) we run the as many args as we can,
+ # catching new functions each time they are return and using more args on them
for i in [0..structure.length]
struct = structure[i]
argPos = args.length-argsLeft
+ # if we have used all the args just return the result
if argsLeft == 0
return res
+ # if we have run out of args, partial apply the args we got
else if argsLeft < struct
return pun.uncurry.apply this, [pun.curry args[argPos...], struct-argsLeft].concat(structure[i+1...])
+ # get the next function
else
res = res.apply this, args[argPos...argPos+struct]
- argsLeft -= 1
+ argsLeft -= struct
return
View
68 src/stdlib.coffee
@@ -0,0 +1,68 @@
+stdtypes =
+
+ List: pun.ADT
+ Cons: ['value', 'next']
+ Nil: []
+
+ BTree: pun.ADT
+ Branch: ['left', 'right']
+ Leaf: ['value']
+
+addExports stdtypes
+
+addExports
+ copyStdtypes: (obj) ->
+ for k, v of stdtypes
+ obj[k] = v
+
+ copyStdfuncs: (obj) ->
+ for k, b of stdfuncs
+ obj[k] = v
+
+ copyStdlib: (obj) ->
+ copyStdtypes(obj)
+ copyStdfuncs(obj)
+
+
+$ = pun.$
+_ = pun._
+
+pun.copyStdtypes this
+
+#
+# FUNCTIONS
+#
+
+toList = pun.match(
+ [], -> List.Nil()
+ $, (xs) -> List.Cons(xs[0], toList xs[1..])
+)
+
+map = (f) ->
+ pun.match(
+ $(Array), (a) -> a.map f
+ List.Nil(), -> List.Nil()
+ List.Cons($, $), (x,xs) -> List.Cons (f x) ((map f) xs)
+ BTree.Branch($, $), (l, r) -> BTree.Branch ((map f) l) ((map f) r)
+ BTree.Leaf($), (v) -> f v
+ )
+
+foldl = (f, z) ->
+ pun.match(
+ $(Array), (a) -> ([z].concat a).reduce f
+ List.Nil(), -> z
+ List.Cons($, $), (x, xs) -> foldl f, (f z x), xs
+ )
+
+
+#
+# END FUNCTIONS
+#
+
+stdfuncs =
+
+ map: pun.uncurry map, 1, 1
+
+ foldl: pun.uncurry foldl, 2, 1
+
+addExports stdfuncs
Please sign in to comment.
Something went wrong with that request. Please try again.