Permalink
Browse files

[ Add flatten to functional library ]

  • Loading branch information...
1 parent 2871e4d commit 32e48100829fb77b4a6a23b097469438de81624a @jbr committed May 1, 2011
Showing with 56 additions and 0 deletions.
  1. +8 −0 include/functional.sibilant
  2. +16 −0 lib/browser.js
  3. +16 −0 lib/options.js
  4. +16 −0 lib/sibilant.js
@@ -46,4 +46,12 @@
(defun compact (arr)
(select arr (lambda (item)
(as-boolean item))))
+
+(defun flatten (&rest items)
+ (inject [] items
+ (lambda (collector item)
+ (collector.concat
+ (if (array? item)
+ (apply flatten item)
+ item)))))
View
@@ -108,6 +108,22 @@
}));
});
+ var flatten = (function(items) {
+ // items:rest
+ var items = Array.prototype.slice.call(arguments, 0);
+
+ return inject([ ], items, (function(collector, item) {
+ // collector:required item:required
+ return collector.concat((function() {
+ if ((item) && (item).constructor.name === "Array") {
+ return flatten.apply(undefined, item);
+ } else {
+ return item;
+ };
+ })());
+ }));
+ });
+
;
(sibilant)["tokens"] = { };
(sibilant.tokens)["regex"] = "(\\/(\\\\\\\/|[^\\/\\n])+\\/[glim]*)";
View
@@ -88,6 +88,22 @@ var compact = (function(arr) {
}));
});
+var flatten = (function(items) {
+ // items:rest
+ var items = Array.prototype.slice.call(arguments, 0);
+
+ return inject([ ], items, (function(collector, item) {
+ // collector:required item:required
+ return collector.concat((function() {
+ if ((item) && (item).constructor.name === "Array") {
+ return flatten.apply(undefined, item);
+ } else {
+ return item;
+ };
+ })());
+ }));
+});
+
var extractOptions = (function(config, args) {
// config:required args:optional
View
@@ -97,6 +97,22 @@ var compact = (function(arr) {
}));
});
+var flatten = (function(items) {
+ // items:rest
+ var items = Array.prototype.slice.call(arguments, 0);
+
+ return inject([ ], items, (function(collector, item) {
+ // collector:required item:required
+ return collector.concat((function() {
+ if ((item) && (item).constructor.name === "Array") {
+ return flatten.apply(undefined, item);
+ } else {
+ return item;
+ };
+ })());
+ }));
+});
+
sibilant.packageInfo = (function() {
var fs = require("fs");;

0 comments on commit 32e4810

Please sign in to comment.