Skip to content

Instantly share code, notes, and snippets.

View MikeBild's full-sized avatar
🏠
Working from home

Mike Bild MikeBild

🏠
Working from home
View GitHub Profile
@MikeBild
MikeBild / app.js
Last active October 30, 2016 06:22
Memoize - you never need constructor/property based dependency injection in JavaScript
const require1 = require('./require1');
const require2 = require('./require2');
const require3 = require('./require3');
console.log(require1.getValue());
console.log(require2.getValue());
console.log(require3.getValue());
@MikeBild
MikeBild / order-saga.js
Last active October 29, 2016 17:27
Simple Order-Saga example in RxJS (without Saga-Log)
const Rx = require('rx');
// simulate transactions
const bookCar = id => Rx.Observable.return({sagaId: id, 'Car A': true}).delay(1000);
const bookHotel = id => Rx.Observable.return({sagaId: id, 'Hotel B': true}).delay(1000);
const bookFlight = id => Rx.Observable.return({sagaId: id, 'Flight C': true}).delay(1000);
// simulate error
//const bookFlight = id => Rx.Observable.throw({sagaId: id, 'Flight C': true}).delay(1000);
// transaction compensation (idempotence)
@MikeBild
MikeBild / coin-changer.js
Last active October 29, 2016 09:13
Resource lifetime binding to RxJS stream
const Rx = require('rx');
const coinsStream = Rx.Observable.fromArray([1,2,5,10,20,50,100].reverse());
Rx.Observable
// bind CoinResource lifetime to the stream lifetime
.using(() => new CoinResource(9), resource => coinsStream.map(x => ({ amount: resource.getValue(), coin: x })))
// calculations
.reduce((state, e) => {
const amount = (state.length > 0) ? state[state.length-1].rest : e.amount;
@MikeBild
MikeBild / event-sourced.js
Created October 25, 2016 16:32
Event-Source with Event-Log persistence with RxJS
const fs = require('fs');
const Rx = require('rx');
const RxNode = require('rx-node');
const timerStream = new Rx.Subject();
const initialState = {count: 0};
const updateState = (state, msg) => {
state.count++;
return state;
};
@MikeBild
MikeBild / cpu-bound.js
Last active October 25, 2016 14:12
Node.js - CPU bound operations with Rx
const RxNode = require('rx-node');
const Rx = require('rx');
const spawn = require('child_process').spawn;
RxNode.fromStream(spawn('find', ['/','-type','f','-exec', 'cat', '{}', '\+']).stdout)
.map(x => x.toString())
// using python
.map(x => RxNode.fromStream(spawn('./word-count.py', [x]).stdout))
// or using javascript
//.map(x => RxNode.fromStream(spawn('./word-count.js', [x]).stdout))
@MikeBild
MikeBild / mSpec2JUnit.xslt
Created June 18, 2011 18:22
MSpec to JUnit XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="specs" select="//specification"/>
<xsl:template match="MSpec">
<testsuite
name="Specifications"
tests="{count($specs)}"
errors="0"
@MikeBild
MikeBild / csv.js
Created December 7, 2013 16:35
CSV data access in NodeJS
var fs = require("fs");
var stream = fs.createReadStream('sample.csv')
var faithful_data = {
'eruption_duration': [],
'waiting_time': []
};
stream.on('readable', function(data){
var buf;
while (buf = stream.read()) {
@MikeBild
MikeBild / archive.tpl-ejs.html
Last active December 30, 2015 07:23
mikebild.com
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>Mike Bild - Archive</title>
<link rel="stylesheet" href="mikebild.css" type="text/css" />
<link rel="stylesheet" href="pygment_trac.css" type="text/css" />
@MikeBild
MikeBild / func_rec.ex
Last active December 26, 2015 03:29
f.(f) - timeout actor
defmodule Actors do
def publish do
publish(0)
end
def publish(count) do
receive do
{:in, pid} -> pid <- {:out, {{:msg, "demo"},{:count, count}}}
end
publish(count + 1)
@MikeBild
MikeBild / rx_download.html
Last active December 26, 2015 01:09
download data async
<!DOCTYPE html>
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/rxjs/2.1.11/rx.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/rxjs/2.1.11/rx.aggregates.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/rxjs/2.1.18/rx.time.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/rxjs/2.1.11/rx.binding.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/rxjs-jquery/1.1.3/rx.jquery.js"></script>
</head>