Skip to content

Instantly share code, notes, and snippets.

@nickfargo
nickfargo / passthrough-process--manual-iterator.litcoffee
Last active August 29, 2015 14:20
Using prochan to demonstrate a CSP pass-through I/O process, along with generator function–manual iterator equivalence

Goal

Define a pass-through process, which conveys data directly over its I/O channels, such that the process itself is effectively an unbuffered logical channel.


Solution

This can be expressed with a standard ES6 generator function, or equivalently by defining a concrete manual iterator class.

@nickfargo
nickfargo / prochan-async.coffee
Created April 15, 2015 05:06
Convert a CSP generator function (“goroutine”) into a Node-style async function
# ES6
async = (generator) ->
g = (args..., callback) ->
try callback null, yield receive go generator, args
catch error then callback error
return
(_) -> proc g, arguments; return
# ES5
async = do ->

The bits of channel state

Definitions

The most pertinent aspects of a Channel’s state can be described in terms of five bits:

CLOSED = 0x10
EMPTY = 0x08
@nickfargo
nickfargo / 0-privacy.js
Last active August 29, 2015 14:17
Privacy via WeakMap
let _ = new WeakMap;
export default {
init: function (obj, map = {}) {
_.set(obj, map);
},
get: function (obj, key) {
return _.get(obj)[key];
},
set: function (obj, key, value) {
(defn filter
([predicate]
(fn [reducing-function]
(fn
([] (reducing-function))
([accumulation] (reducing-function accumulation))
([accumulation input]
(if (predicate input)
(reducing-function accumulation input)
accumulation)))))
@nickfargo
nickfargo / index.litcoffee
Last active August 29, 2015 14:05
Re: Two-way binding between the URL and a state.js machine.

Channel

class Channel
  constructor: ( @bufferSize = 0 ) ->
    @values = []
    @blockedSenders = null
    @blockedReceivers = null

  state @::, 'abstract',
    noSuchMethod: ( name ) ->

runtime

deprecated

Creates iterators with per-instance next methods that close over the private iterator state variables. Safer, but slower, compared to the current classical model.

{ slice } = Array::
operators = require './operators'



module.exports =

Transformers

mapping

“Remove stickers”

mapping = ( map ) -> ( reductor ) -> ( result, input ) ->
  reductor result, map input