Skip to content

Instantly share code, notes, and snippets.

@cqfd
cqfd / async.js
Created July 7, 2015 23:31
Async/await example.
/*
* To run:
* npm install -g babel
*
* babel-node --stage 0 this-script.js
*/
import fs from 'fs'
const mkDir = makePromisey(::fs.mkdir)
@cqfd
cqfd / asynchrony.md
Last active August 29, 2015 14:24
Summary: asynchrony with promises and generators.

Yield for sequencing

pogo : *r -> promise[r]

pogo(function*() {
  yield sleep(1000)
  const json = yield $.getJSON('/endpoint')
  return json
 
var path = require('path');
var webpack = require('webpack');
var config = module.exports = {
context: __dirname,
entry: './index.js',
output: {
path: path.join(__dirname, 'build'),
filename: 'index.js'
},
@cqfd
cqfd / promises.md
Last active August 29, 2015 14:24
Lifting promises.

Idea: programming with a promise for an a as if it were "more or less" an a. That is, we don't want to have to change our programming style too much when transitioning from dealing with actual as to promises for as.

function lift(f) {
  return function(...promises) {
    return Promise.all(promises).then(values => f.apply(null, values))
  }
}
@cqfd
cqfd / Generators.hs
Created June 23, 2015 10:46
Generators in Haskell.
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
module Generators where
import Prelude hiding (take, filter, foldr)
import Control.Applicative
import Control.Monad
import Control.Monad.Trans
@cqfd
cqfd / generators.md
Last active August 29, 2015 14:23
From ES6 generators to Go-style CSP.

Generators

function* teammate(name) {
  console.log(`Hi! I'm ${name} and I'm your teammate.`)
  const num = yield "I need a number!"
  const num2 = yield "I need another number!"
  console.log("Ok, here's my work.")
  return num * num2
}
@cqfd
cqfd / cogo.js
Last active August 29, 2015 14:23
Co/go (run with iojs --harmony_arrow_functions --harmony_rest_parameters)
'use strict';
/*
* The trampoline. (Lots of inspiration taken from github.com/tj/co.)
*/
function go(star, ...args) {
return new Promise((resolve, reject) => {
const gen = star.apply(null, args);
gen.resolve = resolve;
@cqfd
cqfd / polymorphism.md
Last active August 29, 2015 14:22
Polymorphism sketch.

Let's say we have the following schema:

create_table "users", force: :cascade do |t|
  t.string   "name"
  t.string   "email"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end
@cqfd
cqfd / webpack.config.js
Created June 4, 2015 19:37
Babel + runtime webpack config.
var path = require('path');
var webpack = require('webpack');
var config = module.exports = {
context: __dirname,
entry: './app/frontend/javascripts/entry.js',
output: {
path: path.join(__dirname, 'app', 'assets', 'javascripts'),
filename: 'bundle.js'
},
@cqfd
cqfd / co-react.js
Last active August 29, 2015 14:22
Example of using co and React.
const Promise = require('bluebird');
const co = require('co');
const React = require('react');
function* sleep(ms) {
yield Promise.delay(ms);
}
function usersView(users) {
const lis = users.map(u => <li>{u.name}</li>);