Skip to content

Instantly share code, notes, and snippets.

View raimohanska's full-sized avatar

Juha Paananen raimohanska

View GitHub Profile
@raimohanska
raimohanska / bacon.watchfile.coffee
Created January 27, 2014 14:20
Bacon.js wrapper for fs.watch (node.js)
fs=require "fs"
Bacon=require "baconjs"
Bacon.watchFile = (filename) ->
Bacon.fromBinder (sink) ->·
w = fs.watch filename, sink
-> w.close()
Bacon.watchFile("lol.txt").log()
@raimohanska
raimohanska / princess-and-lion.elm
Created January 26, 2014 20:54
Sketch for a Lion Chases Princess Game
module PrincessVsLion where
import Keyboard
-- character positions
princess = foldp inc 4 (pressesOf Keyboard.space)
lion = foldp inc 0 (fps 2)
-- combined game state
gameState = lift2 makeState princess lion
@raimohanska
raimohanska / gist:7801563
Created December 5, 2013 07:38
Bacon.Observable.prototype.logWithTimestamp
Bacon.Observable.prototype.logWithTimestamp = function() {
var args = Array.prototype.slice.call(arguments, 0)
this.onValue(function(value) {
console.log.apply(console, [new Date().getTime()].concat(args.concat([value])))
})
return this
}
@raimohanska
raimohanska / requireInOrder.js
Created September 24, 2013 11:51
RequireJS require things in given order
function requireInOrder(paths, done) {
if (paths.length == 0) {
done()
} else {
require(paths.slice(0,1), function() {
requireInOrder(paths.slice(1), done)
})
}
}
@raimohanska
raimohanska / Bacon.fromNodeStream.coffee
Last active January 6, 2024 04:19
Bacon.fromNodeStream
fs = require("fs")
Bacon = require("baconjs")
# Bacon.fromNodeStream converts Node.js ReadableStream into an EventStream
Bacon.fromNodeStream = (stream) ->
Bacon.fromBinder (sink) ->
listeners = {}
addListener = (event, listener) ->
listeners[event] = listener
stream.on event, listener
@raimohanska
raimohanska / gist:6158612
Created August 5, 2013 19:16
Snake in Elm
import Keyboard
import Window
startPos = {x = 10, y = 10}
startDir = {x = 1, y = 0}
dir = foldp rotate startDir Keyboard.arrows
rotateLeft p = {x=p.y * (0-1), y=p.x}
rotateRight p = {x=p.y, y=p.x * (0-1)}
rotate dir p = if | dir.x < 0 -> rotateLeft p
| dir.x > 0 -> rotateRight p
@raimohanska
raimohanska / index.js
Created July 18, 2013 11:37
requirebin sketch
'use strict';
var $ = require('jquery2');
var Bacon = require('baconjs');
$.fn.asEventStream = Bacon.$.asEventStream;
var _ = require('lodash');
// Input events
var mouseDownE = $(document).asEventStream('mousedown'),
mouseUpE = $(document).asEventStream('mouseup')
@raimohanska
raimohanska / index.js
Created July 15, 2013 07:11
requirebin sketch
'use strict';
var $ = require('jquery2');
var Bacon = require('baconjs');
$.fn.asEventStream = Bacon.$.asEventStream;
var _ = require('lodash');
// Input events
var mouseDownE = $(document).asEventStream('mousedown'),
mouseUpE = $(document).asEventStream('mouseup')
@raimohanska
raimohanska / bacon-fold.js
Created January 31, 2013 21:13
Fold in Bacon.js
Bacon.Observable.prototype.fold = function(seed, f) {
var scanned = this.scan(seed, f)
return scanned.changes().mapEnd().map(scanned)
}
@raimohanska
raimohanska / arrays.roy
Created November 28, 2012 15:14
Array helpers in Roy, Part II
let jsarray xs =
let plusOne = Array.prototype.concat.apply [1] xs
let jsarr = Array.apply null plusOne
jsarr.slice 1
let always x = (\() -> x)
let length xs = (jsarray xs).length
let empty xs = (length xs) == 0
let concat xs ys = (jsarray xs).concat ys
let cons x xs = concat [x] xs