on master help sign in
Add Either library, change Maybe API, and add tap to Touch library.
commitc5da28341c0f2d86bf76e3adff02fcd230c1588f authorevancz <info@elm-lang.org> dateWed 9 Jan 2013, 02:07 (14 years ago) parent87be397
5 files changed+127−21
core-elm/Either.elm+12 core-js/Either.js+31 core-js/Maybe.js+2−9 core-js/Signal/Touch.js+81−11 core-js/cat.bat+1−1
core-elm/Either.elmnew file+12
@@ -0,0 +1,12 @@
1+
2+data Either a b = Left a | Right b
3+
4+either f g e = case e of { Left x -> f x ; Right y -> g y }
5+
6+isLeft e = case e of { Left _ -> True ; _ -> False }
7+isRight e = case e of { Right _ -> True ; _ -> False }
8+
9+lefts = Elm.List.filter isLeft
10+rights = Elm.List.filter isRight
11+
12+partition = Elm.List.partition isLeft
core-js/Either.jsnew file+31
@@ -0,0 +1,31 @@
1+
2+Elm.Either = function() {
3+ function Left(a1) { return ['Left',a1]; }
4+ function Right(a1){ return ['Right',a1]; }
5+ function either(f){ return function(g){ return function(e){
6+ switch(e[0]){
7+ case 'Left': return f(e[1]);
8+ case 'Right': return g(e[1]);
9+ }
10+ };};}
11+ function isLeft(e) { return e[0] == 'Left'; }
12+ function isRight(e) { return e[0] == 'Right'; }
13+
14+ function get(es) { return Elm.List.map(function(x){return x[1];})(es); }
15+ function lefts(es) { return get(Elm.List.filter(isLeft)(es)); }
16+ function rights(es) { return get(Elm.List.filter(isRight)(es)); }
17+ function partition(es) {
18+ var lrs = Elm.List.partition(isLeft)(es);
19+ lrs[1] = get(lrs[1]);
20+ lrs[2] = get(lrs[2]);
21+ return lrs;
22+ }
23+ return {Left:Left,
24+ Right:Right,
25+ either:either,
26+ isLeft:isLeft,
27+ isRight:isRight,
28+ lefts:lefts,
29+ rights:rights,
30+ partition:partition};
31+}();
core-js/Maybe.js+2−9
@@ -5,11 +5,6 @@ Elm.Maybe = function() {
55 return xs;
66 };
77 }
8− function fromMaybe(b) { return function(m) {
9− if (m[0] === "Just") return m[1];
10− return b;
11− };
12− }
138 function mapCons(f) { return function(y) { return function(xs) {
149 var x = f(y);
1510 if (x[0] === "Just") return ["Cons", x[1], xs];
@@ -26,12 +21,10 @@ Elm.Maybe = function() {
2621
2722 return {Just : function(x) { return ["Just",x]; },
2823 Nothing : ["Nothing"],
29− catMaybes : Elm.List.foldr(consMaybe)(["Nil"]),
24+ justs : Elm.List.foldr(consMaybe)(["Nil"]),
3025 isJust : function(m) { return m[0] === "Just"; },
3126 isNothing : function(m) { return m[0] === "Nothing"; },
32− fromMaybe : fromMaybe,
33− consMaybe : consMaybe,
34− mapMaybe : function(f) { return Elm.List.foldr(mapCons(f))(["Nil"]); },
27+ cons : consMaybe,
3528 maybe : maybe
3629 };
3730}();
core-js/Signal/Touch.js+81−11
@@ -1,15 +1,69 @@
11
22Elm.Touch = function() {
3− var touches = Elm.Signal.constant(["Nil"]);
3+
4+ function log(id,msg) {
5+ var e = document.getElementById('logger' + id);
6+ if (!e) {
7+ e = document.createElement('div');
8+ e.id = 'logger' + id;
9+ document.body.appendChild(e);
10+ }
11+ e.innerHTML = msg;
12+ }
13+
14+ function Dict() {
15+ this.keys = [];
16+ this.values = [];
17+
18+ this.insert = function(key,value) {
19+ this.keys.push(key);
20+ this.values.push(value);
21+ }
22+ this.lookup = function(key) {
23+ var i = this.keys.indexOf(key)
24+ return i >= 0 ? this.values[i] : {x:0,y:0,t:0};
25+ }
26+ this.remove = function(key) {
27+ var i = this.keys.indexOf(key);
28+ if (i < 0) return;
29+ var t = this.values[i];
30+ this.keys.splice(i,1);
31+ this.values.splice(i,1);
32+ return t;
33+ }
34+ }
35+
36+ var root = Elm.Signal.constant([]),
37+ tapTime = 500,
38+ hasTap = false,
39+ tap = {_:[true],x:[0],y:[0]},
40+ dict = new Dict();
441
542 function touch(t) {
6− return {_ : [true], x: [t.pageX], y: [t.pageY], id: [t.identifier] };
43+ var r = dict.lookup(t.identifier);
44+ return {_ : [true], id: [t.identifier],
45+ x: [t.pageX], y: [t.pageY],
46+ x0: [r.x], y0: [r.y],
47+ t0: [r.t] };
48+ }
49+
50+ function start(e) {
51+ dict.insert(e.identifier,{x:e.pageX,y:e.pageY,t:Date.now()});
52+ }
53+ function end(e) {
54+ var t = dict.remove(e.identifier);
55+ if (Date.now() - t.t < tapTime) {
56+ hasTap = true;
57+ tap = {_:[true], x:[t.x], y:[t.y]};
58+ }
759 }
860
9− function listen(name) {
61+ function listen(name, f) {
1062 function update(e) {
11− var ts = Elm.JavaScript.castJSArrayToList(e.touches);
12− var hasListener = Dispatcher.notify(touches.id, Elm.List.map(touch)(ts));
63+ for (var i = e.changedTouches.length; i--; ) { f(e.changedTouches[i]); }
64+ var ts = new Array(e.touches.length);
65+ for (var i = e.touches.length; i--; ) { ts[i] = touch(e.touches[i]); }
66+ var hasListener = Dispatcher.notify(root.id, ts);
1367 if (!hasListener)
1468 return this.removeEventListener(name,arguments.callee,false);
1569 e.preventDefault();
@@ -17,11 +71,27 @@ Elm.Touch = function() {
1771 Value.addListener(document, name, update);
1872 }
1973
20− listen("touchstart");
21− listen("touchmove");
22− listen("touchend");
23− listen("touchcancel");
24− listen("touchleave");
74+ listen("touchstart", start);
75+ listen("touchmove", function(_){});
76+ listen("touchend", end);
77+ listen("touchcancel", end);
78+ listen("touchleave", end);
79+
80+ function dependency(f) {
81+ var signal = Elm.Signal.lift(f)(root);
82+ root.defaultNumberOfKids += 1;
83+ signal.defaultNumberOfKids = 0;
84+ return signal;
85+ }
86+
87+ var touches = dependency(function(ts) {
88+ return Elm.JavaScript.castJSArrayToList(ts);
89+ });
90+ var taps = function() {
91+ function pred(_) { var b = hasTap; hasTap = false; return b; }
92+ var sig = dependency(function(_) { return tap; });
93+ return Elm.Signal.keepIf(pred)({_:[true],x:[0],y:[0]})(sig);
94+ }();
2595
26− return { touches: touches };
96+ return { touches: touches, taps: taps };
2797}();
core-js/cat.bat+1−1
@@ -1,4 +1,4 @@
1−copy /B Guid.js+foreign\JavaScript.js+foreign\JSON.js+Value.js+List.js+Maybe.js+Char.js+Graphics\Color.js+Graphics\Collage.js+Graphics\Element.js+Text.js+Graphics\Render.js+runtime\Signal.js+runtime\Dispatcher.js+Signal\HTTP.js+Signal\Input.js+Signal\Keyboard.js+Signal\Mouse.js+Signal\Touch.js+Signal\Random.js+Signal\Time.js+Signal\Window.js+Date.js+Prelude.js+Dict.js+Set.js+Automaton.js ..\elm-mini.js
1+copy /B Guid.js+foreign\JavaScript.js+foreign\JSON.js+Value.js+List.js+Maybe.js+Either.js+Char.js+Graphics\Color.js+Graphics\Collage.js+Graphics\Element.js+Text.js+Graphics\Render.js+runtime\Signal.js+runtime\Dispatcher.js+Signal\HTTP.js+Signal\Input.js+Signal\Keyboard.js+Signal\Mouse.js+Signal\Random.js+Signal\Time.js+Signal\Window.js+Date.js+Prelude.js+Signal\Touch.js+Dict.js+Set.js+Automaton.js ..\elm-mini.js
22
33cd ..\elm
44