Skip to content

Instantly share code, notes, and snippets.

View raimohanska's full-sized avatar

Juha Paananen raimohanska

View GitHub Profile
@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 / fp.go
Created February 8, 2023 08:05
Golang FP basics: filter, map, flatMap
func Map[A any, B any](things []A, mapper func (x A) B) []B {
result := make([] B, len(things))
for i := range(things) {
result[i] = mapper(things[i])
}
return result
}
func Filter[A any](things []A, predicate func (x A) bool) []A {
result := make([] A, len(things))
import nextJs from "next"
async function createNextAppWithCustomLogging() {
const nextApp = nextJs({ dev: false })
const server = (await (nextApp as any).getServer()) as any
if (!(server.logError instanceof Function)) throw Error("Assertion fail")
server.logError = (err: Error) => {
logger.error(err)
}
return nextApp
@raimohanska
raimohanska / enocean.ino
Created February 21, 2015 20:48
Arduino enocean dimmer
int SIGNAL_LED = 13;
int POWER_LED = 11;
byte buffer[100];
byte myAddress[] = {-1, -114, 70, -125};
void setup() {
Serial.begin(9600);
pinMode(SIGNAL_LED, OUTPUT);
pinMode(POWER_LED, OUTPUT);
@raimohanska
raimohanska / glitchprevention.md
Last active November 1, 2018 09:37
Bacon.js glitch prevention

The key to glitch prevention is the UpdateBarrier. This component takes care of starting and finishing event transactions. The inTransaction function is used to wrap all event handling, creating a transaction that lasts until the original event and all derived events (those created when passing the event through all the maps, combines etc) have been processed.

In the end of the transaction, a flush occurs. During flush, all pending actions are performed. And what are these action then? They are actions registered using the whenDoneWith function, and are basically requests like "please run this function when the dependencies certain observable have been fully flushed". To satisfy these request, the Up

#!/usr/bin/env node
if (process.argv.length != 3) {
console.error("Usage: psqlurl <url>")
console.error("Expected one argument, got " + (process.argv.length - 2))
process.exit(1)
}
var input = process.argv[2]
var URL = require("url")
var ps = require("child_process")
var url = URL.parse(input)
@raimohanska
raimohanska / princess-vs-lion.elm
Created January 27, 2014 17:57
Princess vs Lion in Elm
module PrincessVsLion where
import Keyboard
data GameState = Ongoing Int Int | LionWon | PrincessWon
proceedGame keyPresses state = case state of
Ongoing princess lion ->
checkEnd (princess + (keyPresses `div` 2)) (lion + 1)
x -> checkRestart keyPresses x
@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
}