The Widget King – A tale written in javascript monadic streams with “most”.

$ npm install –save most
On Github
API Docs

The code looks awful in this blog so go check it out on gist.

$ node theWidgetKing.js

Screen Shot 2015-03-02 at 11.28.47 PM


// What do you guys think about this as a way to possibly pass streams
// around from one module to another? I don't think it comes up in simple
// examples but I can see in complex cases where if you wanted a separation of
// concerns for a stream, that you might want them to be passed around. It
// kind of opens up a protocol for communication between modules.
// Essentially this is a widgetMaker module which is responsible for making
// widgets. And the widgetKing module wants to be notified every time a widget
// comes off the factory floor.

// Lol IIFE's and Module Pattern to get this all in one file.
// think of the returns from each module as "module.exports" if they
// were in separate files.

var most = require('most');
var Q = require('q');
var colors = require('colors/safe');

// Character voices / wrapper for console.log.
var say = function() {
console.log.apply(undefined, arguments);

// Alias some colors.
var g =;
var b =;

* This is the MAGIC ingredient which allows the modules to communicate.
* Creates a new most stream from a promise which is waiting on a stream stream.
* @param {Promise} streamPromise
* @returns {Stream}
var mostFromStreamPromise = function(streamPromise) {
return most.create(function(add, end, error) {
streamPromise.then(function(stream) {
stream.observe(add).then(end, error);

* Module1 The WidgetMaker
* He utilizes the "mostFromStreamPromise" to clone two streams to
* perform tasks when he receives a "widgetRequest" from the king. One stream
* allows the widget maker to make widgets, and the other collects the money
* in the incoming requests and calculates his tax payments to the
* honorable king.
* @exports taxes
* @exports widgets
var widgetMaker = (function() {
var requestsDeferred = Q.defer();
var moneyInTheBank = 0;

* A copy of the widget request stream, which will be created
* when the request stream is registered. The resultant "widgets"
* stream is responsible for making widgets.
* @type {Stream|*}
var widgets = mostFromStreamPromise(requestsDeferred.promise)
.scan(makeWidget, {id: 0})

* Also a copy of the widget request stream. This one collects
* money from the requests, and calculates taxes, which it then
* exports.
* @type {Stream|*}
var taxes = mostFromStreamPromise(requestsDeferred.promise)

* Registers the input streams the WidgetMaker susbscribes to.
* Just the never satisfying periodic dribble of widget requests.
function register(widgetKing) {

* scan - Creates a widget with an incremented widgetId, and owner.
function makeWidget(acc, request) {
say(b('Making a widget.'));;
acc.widgetId = request.purchaserName + '-' +;
acc.owner = request.purchaserName;
return acc;

* tap - logging.
function notifyWidgetComplete(widget) {
say(b('Widget off the line.', JSON.stringify(widget), '\n'));

* tap - collect money from requests.
function putMoneyInTheBank(request) {
say(b('Widget request received, collecting cash'));
moneyInTheBank += request.payment;
say(b('Wowzers I have: $' + moneyInTheBank + '\n'));

* map - calculates taxes, complains about it, and reluctantly returns cut.
function giveTheKingHisCut(request) {
var cut = request.payment * 0.5;
moneyInTheBank -= cut;
say(b('I hate taxes, boo hoo '), '-$' + moneyInTheBank);
return cut;

// Think of this like module.exports
return {
register: register,
taxes: taxes,
widgets: widgets

* Module2 The WidgetKing
* The widget king generates his widget requests - five requests over five
* seconds. He has a "mostFromStreamPromise" copy of widgets so he knows when
* he receives one of his widgets. He then adds it to his collection. Another
* task the king does is sit back and add to his fat stack of cash through
* tax collection.
* @exports widgetRequests
var widgetKing = (function(kingName) {
var moneyInTheBank = 1000000;
var widgetsDeferred = Q.defer();
var taxesDeferred = Q.defer();

* Copy of incoming widgetMaker.widgets
* Let's the king stay up on the widgets he receives.
var widgets = mostFromStreamPromise(widgetsDeferred.promise);

* Copy of the incoming widgetMaker.taxes stream
* Gives the king money, which he stores in his bank.
var taxes = mostFromStreamPromise(taxesDeferred.promise);

* Periodic generation of 5 widget requests. Exported
var widgetRequests = most
.periodic(1000, {purchaserName: kingName, payment: 1})

.reduce(collectWidgets, {collection: []})
.then(proclaimGreatnessThroughMaterialWealth, console.error);

.then(null, console.error);

* This function registers the input streams the king subscribes to.
* Namely -- his mother fucking widgets AND his coin!
function register(widgetMaker) {

* reduce - puts widgets in collection, returns accumulated final result.
function collectWidgets(acc, widget) {
return acc;

* observe - Collects tax money in the bank.
function sitBackAndTakeMoney(taxMoney) {
moneyInTheBank += taxMoney;
say(g('Gettin\' that paper'), moneyInTheBank, '\n');

* Reduce Promise succes handler, does what it says it does.
function proclaimGreatnessThroughMaterialWealth(result) {
say(g('I am KING ' + kingName.toUpperCase() + ' behold my Widgets!'));

// I'm either doing this wrong or this doesn't work right. Opening a github question.
// result ends up with five of the last widget........wat?

return {
register: register,
widgetRequests: widgetRequests

* Module
* Module Coupling...maybe
* Module Attach...definitely not
* Module Adhesion...also no.

Read more "The Widget King – A tale written in javascript monadic streams with “most”."

Seattle’s ODESZA Play Portland’s Holocene April 11

ODESZA may be only a couple years old, but they feel like a collaboration with some season and some grit in their teeth.

2012 found Harrison Mills (aka Catacombkid) and Clayton Knight (BeachesBeaches) collaborating after graduation from Western Washington University.

The perfect example of something that is greater than the sum of its parts, ODESZA has about 5 million streams on Soundcloud. Why people are still streaming on Soundcloud, I don’t know. Both of their albums can be downloaded absolutely free to anyone who wants them. “Sun Models,” a recent track released by the duo featuring Madelyn Grant, is over 3/4 million streams already and it’s barely a month old.

The duo has a flair for glitchy lyrics, recognizable and creative samples, and vibrating baselines. There is a dreamlike quality to some of their tracks; like wandering under a blue sky with no particular place to go. You can’t listen to Summer’s Gone and My Friends Never Die without bouncing along in your stride to sampled tracks remixed into something akin to a watercolor rendering of the world around you, or maybe a picture out of focus and disconcertingly saturated colors.

ANALOG FUTURE TOURODESZA is one of our Northwest highlights in the electronic scene along with Portland’s own Emancipator. The two acts even shared billing with Pretty Lights on his last tour and just blew the roof off of Memorial Coliseum.

ODESZA is set to release a new album this year. The pressure may be mounting for this one though. After an LP from a collaboration no one had heard of, making it a surprising hit of the year, and a smash EP that felt like the foot in the door no one can ignore, ODESZA has some pretty big shoes to fill: their own.

They’ve done their fair share of the festival circuit from Sasquatch to MFNW, but April 11th sees them coming through Portland at Holocene while on their first headlining tour in support of My Friends Never Die (a tour for which half the venues sold out), and if you need a little more convincing to drop what you’re doing and buy some luminescent accessories, check out the tracks below.

Not already going to the sold out show or can’t afford the oft ridiculous resale ticket market? Well, we’ll have a full review and photos from the show forthcoming. In the meantime, get this free music inside you right meow.

Read more "Seattle’s ODESZA Play Portland’s Holocene April 11"

Trainspotting: a suggestion

Talk about a weird ass film. Trainspotting is the story of a drug addict in Edinburgh trying to get out of the scene but constantly being pulled back in through a series of event. Heroin, the drug of choice, rules everyone’s lives. This is both a horrifying, tragic film while it is cartoonishly hilarious and represents that filmmaking style that inspired the likes of Guy Ritchie (Snatch), David Fincher (Fight Club), or even Darren Aronofsky (Requiem for a Dream).

Ewan McGregor plays Renton. Along with Spud, Sick Boy, Tommy, and Begbie, make up the main host of characters for this film. From squatting in a practically condemned apartment to cleaning up only to relapse, to selling drugs to some heavy characters, there is nothing this group doesn’t do. They are manic, crazy, dangerous, twisted individuals who seem to want more than the drug life, but simultaneously celebrate it as they wallow in the world they inhabit.

The cinematography is the thing that gets me. The scene of crawling in to the toilet. I loved the scene where Renton lowers into the floor, pulling the rug with him. The freeze-frame style intros, the narration–though sometimes thought to be a cheap story device–works perfectly with Renton and fits the film style in what we see.

A lot of people may have heard of Trainspotting, especially since Ewan McGregor became a star, but not that many people saw it back in the day; it only made 16 million in theaters.  I never saw it then, I was 12, but as soon as I was old enough, I rented it. I recently watched it probably three times last year and will see it again. I like to think that this is a cult classic. There is a heavy fan base, but it is not all comedy. This is heroin and that isn’t pretty. Things get darker, tougher, stranger, and you really care for many of the people you meet. You hope the best, are shocked, and hurt when disaster befalls characters.

I can’t suggest this film enough if you’ve got the stomach for this type of film. It will change your mood after watching it. So be ready. If you’ve got the taste for this kind of movie, then it’s a can’t miss classic that you can see influenced films to come, like Fight Club, Requiem, Snatch, and many more. This film represented the late 90’s well, and it still holds strong some 16 years later.

Read more "Trainspotting: a suggestion"