Skip to content

Commit

Permalink
Update .js files for Foreign.JavaScript and new Signal operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
process-bot committed Jun 28, 2012
1 parent a49a51a commit 9332a8a
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 22 deletions.
85 changes: 83 additions & 2 deletions core-js/Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var Maybe = function() {
};
}
function mapCons(f) { return function(y) { return function(xs) {
var x = f(x);
var x = f(y);
if (x[0] === "Just") return ["Cons", x[1], xs];
return xs;
};
Expand All @@ -48,7 +48,88 @@ var Maybe = function() {
};
}();

return {String: { toText : Value.toText , properEscape : Value.properEscape } ,
var String = function() {

function append(s1) { return function(s2) {
return s1.concat(s2);
};
}
function map(f) { return function(s) {
for (var i = s.length; i--; ) { s[i] = f(s[i]); }
};
}

function intercalate(sep) { return function(ss) {
return Value.listToArray(ss).join(sep);
};
}
function intersperse(sep) { return function(s) {
return s.split("").join(sep);
};
}

function foldl(f) { return function(b) { return function(s) {
var acc = b;
for (var i = 0, len = s.length; i < len; ++i) { acc = f(s[i])(acc); }
return acc;
};
};
}
function foldr(f) { return function(b) { return function(s) {
var acc = b;
for (var i = s.length; i--; ) { acc = f(s[i])(acc); }
return acc;
};
};
}

function concatMap(f) { return function(s) {
var a = s.split("");
for (var i = a.length; i--; ) { a[i] = f(a[i]); }
return a.join("");
};
}

function forall(pred) { return function(s) {
for (var i = s.length; i--; ) { if (!pred(s[i])) {return false}; }
return true;
};
}
function exists(pred) { return function(s) {
for (var i = s.length; i--; ) { if (pred(s[i])) {return true}; }
return false;
};
}

return {cons : append,
snoc : append,
head : function(s) { return s[0]; },
last : function(s) { return s[s.length-1]; },
tail : function(s) { return s.slice(1); },
length : function(s) { return s.length; },
map : map,
intercalate : intercalate,
intersperse : intersperse,
reverse : function(s) { return s.split("").reverse().join(""); },
toLower : function(s) { return s.toLowerCase(); },
toUpper : function(s) { return s.toUpperCase(); },
foldl : foldl,
foldr : foldr,
concat : function(ss) { return Value.listToArray(ss).join(""); },
concatMap : concatMap,
forall : forall,
exists : exists,
/*
filter : filter,
take:,
drop:,
*/
toText : Value.toText,
properEscape : Value.properEscape
};
}();

return {String: {toText:Value.toText, properEscape:Value.properEscape},
Char:Char,
Maybe:Maybe,
List:List
Expand Down
13 changes: 2 additions & 11 deletions core-js/Everything.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@

var eq = function(x,y) {
if (typeof x === "object") {
if (x === y) return true;
if (x.length !== y.length) return false;
for (var i = x.length; i--; ) {
if (!eq(x[i],y[i])) return false;
}
return true;
}
return x === y;
};
var eq = Value.eq;

var includeGlobal = this;
(function() {
Expand Down Expand Up @@ -60,4 +50,5 @@ var ElmCode = {};
ElmCode.Data = Data;
ElmCode.Signal = Signal;
ElmCode.Data.List = List;
ElmCode.Foreign = Foreign;
ElmCode.Prelude = Prelude;
8 changes: 7 additions & 1 deletion core-js/Prelude.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ var Prelude = function() {
lift3 : Signal.lift3,
lift4 : Signal.lift4,
foldp : Signal.foldp,
constant : Signal.constant
constant : Signal.constant,
count : Signal.count,
keepIf : Signal.keepIf,
dropIf : Signal.dropIf,
keepWhen : Signal.keepWhen,
dropWhen : Signal.dropWhen,
dropRepeats : Signal.dropRepeats
};
}();
35 changes: 34 additions & 1 deletion core-js/Value.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@

var Value = function(){

var eq = function(x,y) {
if (typeof x === "object") {
if (x === y) return true;
if (x.length !== y.length) return false;
for (var i = x.length; i--; ) {
if (!eq(x[i],y[i])) return false;
}
return true;
}
return x === y;
};

var Tuple = function() {
var len = arguments.length;
var arr = new Array(len+1);
Expand All @@ -10,6 +23,15 @@ var Value = function(){
return arr;
};

var listToArray = function(list) {
var arr = [];
while (list[0] === "Cons") {
arr.push(list[1]);
list = list[2];
}
return arr;
};

var properEscape = function(str) {
str.replace('"', "&#34;");
str.replace("&", "&#38;");
Expand Down Expand Up @@ -90,10 +112,21 @@ var Value = function(){
curr[2] = ys;
return root;
};

var str = function(s) {
var out = ["Nil"];
for (var i = s.length; i--; ) {
out = ["Cons", s[i], out];
}
return out;
};

return {show:show,
return {eq:eq,
str:str,
show:show,
Tuple:Tuple,
append:append,
listToArray:listToArray,
toText : toText,
properEscape : properEscape};
}();
2 changes: 1 addition & 1 deletion core-js/cat-js.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cat Guid.js Value.js List.js Data.js collage/Color.js Element.js Text.js collage/Shape.js collage/Line.js runtime/Dispatcher.js runtime/Signal.js Prelude.js Everything.js > ../elm-mini.js ; ls
cat Guid.js foreign/JavaScript.js Value.js List.js Data.js collage/Color.js Element.js Text.js collage/Shape.js collage/Line.js runtime/Dispatcher.js runtime/Signal.js Prelude.js Everything.js > ../elm-mini.js ; ls
15 changes: 13 additions & 2 deletions core-js/foreign/JavaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ var Foreign = function() {
return a;
}

var castJSStringToString = castJSArrayToList
var castJSStringToString = castJSArrayToList;
function castStringToJSString(str) {
if (typeof str === "string") return str;
return castListToJSArray(str).join('');
}

function fromTuple(t) { return t.slice(1); }
function toTuple(a) { return a.unshift("Tuple" + a.length); }

return {castJSBoolToBool:castJSBoolToBool,
castBoolToJSBool:castBoolToJSBool,
castJSNumberToFloat:castJSNumberToFloat,
Expand All @@ -46,7 +49,15 @@ var Foreign = function() {
castJSArrayToList:castJSArrayToList,
castListToJSArray:castListToJSArray,
castJSStringToString:castJSStringToString,
castStringToJSString:castStringToJSString
castStringToJSString:castStringToJSString,
castTupleToJSTuple2:fromTuple,
castTupleToJSTuple3:fromTuple,
castTupleToJSTuple4:fromTuple,
castTupleToJSTuple5:fromTuple,
castJSTupleToTuple2:toTuple,
castJSTupleToTuple3:toTuple,
castJSTupleToTuple4:toTuple,
castJSTupleToTuple5:toTuple
};
}();
return {JavaScript:JavaScript};
Expand Down
42 changes: 39 additions & 3 deletions core-js/runtime/Dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,46 @@ var Elm = function() {
};
input.kids.push(this);
};

var dropIf = function(pred,base,input) {
this.id = Guid.guid();
this.value = pred(input.value) ? base : input.value;
this.kids = [];
this.recv = function(timestep, changed) {
var chng = changed && !pred(input.value);
if (chng) { this.value = input.value; }
send(this.kids, timestep, chng);
};
input.kids.push(this);
};
var dropRepeats = function(input) {
this.id = Guid.guid();
this.value = input.value;
this.kids = [];
this.recv = function(timestep, changed) {
var chng = changed && !eq(this.value,input.value);
if (chng) { this.value = input.value; }
send(this.kids, timestep, chng);
};
input.kids.push(this);
};

var dropWhen = function(s1) { return function(b) { return function(s2) {
var pairs = new lift(function(x){return function(y){return [x,y];};},[s1,s2]);
var dropped = new dropIf(function(p){return p[0];},[true,b],pairs);
return new lift(function(p){return p[1];},[dropped]); }; };
};

return {Input: function(x) {return new input(x);},
Lift: function(f,xs){return new lift(f,xs);},
Fold: function(f,b,x){return new fold(f,b,x);}
Fold: function(f,b,x){return new fold(f,b,x);},
keepIf : function(pred) { return function(base) { return function(sig) {
return new dropIf(function(x) { return !pred(x)},base,sig); }; }; },
dropIf : function(pred) { return function(base) { return function(sig) {
return new dropIf(pred,base,sig); }; }; },
keepWhen : function(s) { return dropWhen(new lift(function(b){return !b;},[s])); },
dropWhen : dropWhen,
dropRepeats : function(s) { return new dropRepeats(s);}
};
}();

Expand Down Expand Up @@ -109,8 +146,7 @@ var Dispatcher = function() {
};

var initialize = function() {
var prog = ElmCode.hasOwnProperty("main") ? ElmCode.main : main;
try { program = prog(); } catch (e) { throw e; }
program = ElmCode.main();
if (!program.hasOwnProperty('recv')) {
program = Elm.Input(program);
}
Expand Down
8 changes: 7 additions & 1 deletion core-js/runtime/Signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ var Signal = function() {
return function(e3) { return function(e4) {
return Elm.Lift(f, [e1,e2,e3,e4]); }; }; }; }; },
foldp : function(f) { return function(b) { return function(e) {
return Elm.Fold(f,b,e); }; }; }
return Elm.Fold(f,b,e); }; }; },
count : function(sig){return Elm.fold(function(c){return c+1},0,sig)},
keepIf : Elm.keepIf,
dropIf : Elm.dropIf,
keepWhen : Elm.keepWhen,
dropWhen : Elm.dropWhen,
dropRepeats : Elm.dropRepeats
};
}();

0 comments on commit 9332a8a

Please sign in to comment.