Permalink
Browse files

add Haskell signatures etc

  • Loading branch information...
1 parent f036a8b commit 0a3355a25b86503c146bbcb2af7f1f3d0bbf3c2b Juha Paananen committed Sep 1, 2011
Showing with 33 additions and 17 deletions.
  1. +0 −1 0.js
  2. +5 −0 1.js
  3. +1 −2 2.html
  4. +7 −3 2.js
  5. +12 −6 3.js
  6. +3 −3 4.js
  7. +2 −0 6.js
  8. +1 −1 intro.html
  9. +2 −1 presentation.js
View
@@ -1,5 +1,4 @@
$(function() {
- // allKeyUps is an Observable stream of all key-up events
var allKeyUps = $(document).toObservable("keyup")
var spaceBarKeyUps = allKeyUps
View
@@ -1,12 +1,17 @@
$(function() {
+ // allKeyUps :: Observable KeyEvent
var allKeyUps = $(document).toObservable("keyup")
var allKeyDowns = $(document).toObservable("keydown")
+ // always :: a -> (b -> a)
function always(value) { return function(_) { return value } }
+ // keyCodeIs :: Int -> (KeyEvent -> Bool)
function keyCodeIs(keyCode) { return function(event) { return event.keyCode == keyCode} }
+ // keyUps :: Int -> Observable KeyEvent
function keyUps(keyCode) { return allKeyUps.Where(keyCodeIs(keyCode)) }
function keyDowns(keyCode) { return allKeyDowns.Where(keyCodeIs(keyCode)) }
+ // keyState :: Int -> Observable Bool
function keyState(keyCode) {
return Rx.Observable.FromArray([false])
.Concat(keyDowns(keyCode).Select(always(true)))
View
@@ -20,8 +20,7 @@
<div id="content">
<h1>CombineLatest</h1>
<ul>
- <li>Key states of arrow keys are mapped to UP, DOWN, LEFT, RIGHT</li>
- <li>More specifically, either an empty array or a singleton array</li>
+ <li>Key state stream for each arrow key will produce for instance ['LEFT'] when key is down and [] otherwise
<li>CombineLatest is used to combine a key state stream with the next one</li>
<li>The concat function is used to collect the state of all arrow keys into a single array</li>
</ul>
View
@@ -1,17 +1,21 @@
$(function() {
+ // keyState :: Int -> a -> Observable [a]
function keyState(keyCode, value) {
return Rx.Observable.FromArray([[]])
.Concat(keyDowns(keyCode).Select(always([value])))
.Merge(keyUps(keyCode).Select(always([])))
}
+ // concat :: [a] -> [a] -> [a]
function concat(a1, a2) {
return a1.concat(a2)
}
-
- keyState(38, 'UP')
+
+ // arrowKeyState :: Observable [String]
+ var arrowKeyState = keyState(38, 'UP')
.CombineLatest(keyState(40, 'DOWN'), concat)
.CombineLatest(keyState(37, 'LEFT'), concat)
.CombineLatest(keyState(39, 'RIGHT'), concat)
- .Subscribe(function(keysDown) { $('#keyState').text(keysDown.join(',')) })
+
+ arrowKeyState.Subscribe(function(keysDown) { $('#keyState').text(keysDown.join(',')) })
})
View
@@ -1,17 +1,23 @@
$(function() {
- function id(x) { return x }
-
+ function nonEmpty(xs) { return xs.length > 0 }
+ function head(xs) { return xs[0] }
+ function latter(a, b) { return b }
+
+ // direction :: Observable [String]
var direction = keyState(38, 'UP')
.CombineLatest(keyState(40, 'DOWN'), concat)
.CombineLatest(keyState(37, 'LEFT'), concat)
.CombineLatest(keyState(39, 'RIGHT'), concat)
+ // movements :: Observable String
var movements = Rx.Observable.Interval(200)
- .CombineLatest(direction, function(_, dir) { return dir })
- .Where(id)
+ .CombineLatest(direction, latter)
+ .Where(nonEmpty)
+ .Select(head)
- movements
+ // movementsSoFar :: Observable [String]
+ var movementsSoFar = movements
.Scan([], function(acc, move) { return acc.concat([move]) })
- .Subscribe(function(moves) {$('#movements').text(moves.join(' ')) });
+ movementsSoFar.Subscribe(function(moves) {$('#movements').text(moves.join(' ')) });
})
View
@@ -3,22 +3,22 @@ $(function() {
function head(array) { return array[0] }
function id(x) { return x }
+ function latter(a, b) { return b }
+ // direction, movements, position :: Observable Vector2D
var direction = keyState(38, new Vector2D(0, -1))
.CombineLatest(keyState(40, new Vector2D(0, 1)), concat)
.CombineLatest(keyState(37, new Vector2D(-1, 0)), concat)
.CombineLatest(keyState(39, new Vector2D(1, 0)), concat)
.Select(head)
var movements = Rx.Observable.Interval(50)
- .CombineLatest(direction, function(_, dir) { return dir })
+ .CombineLatest(direction, latter)
.Where(id)
var position = movements
.Scan(startPos, function(pos, move) { return pos.add(move) })
.StartWith(startPos)
-
position.Subscribe(function(pos) {$('#position').text(pos.x + "," + pos.y)})
-
})
View
@@ -5,11 +5,13 @@ $(function() {
var player = r.image("images/man-left-1.png", startPos.x, startPos.y, 40, 40)
controller.position.Subscribe(function (pos) { player.attr({x : pos.x, y : pos.y}) })
+ // animation :: Observable Int
var animation = controller.position
.Scan(1, function(prev, _) { return prev % 2 + 1})
animation.Subscribe(function (index) {
player.attr({src : "images/man-left-" + index + ".png"})})
+ // angle :: Observable Double
var angle = controller.direction
.Where(id)
.Select(function(vec) { return vec.getAngleDeg()})
View
@@ -29,7 +29,7 @@
<li class="anim">Bindings to JQuery etc
<li class="anim">Combinators (like FP list operations)
<li class="anim">No mutable state! <em>s/forloop/mapreduce/</em>
- <li class="anim">Think Excel!
+ <li class="anim">Think Excel! Think SQL!
<li><blink>|</blink>
</ul>
</div>
View
@@ -1,4 +1,5 @@
-var slides = ['intro.html', '0.html', '1.html', '2.html', '3.html', '4.html', '5.html', '6.html', 'presentation.html', 'outro.html']
+var slides = ['intro.html', '0.html', '1.html', '2.html', '3.html', '4.html'
+ , '5.html', '6.html', 'presentation.html', 'outro.html']
function initPresentation(id) {
showSrc(id + ".js")

0 comments on commit 0a3355a

Please sign in to comment.