Skip to content

Instantly share code, notes, and snippets.

View masaeedu's full-sized avatar

Asad Saeeduddin masaeedu

  • Montreal, QC, Canada
View GitHub Profile
@masaeedu
masaeedu / test.cs
Last active June 4, 2017 00:52
"var" for props/fields
static class X
{
public static X<T> Create<T>(T t) { return new X<T>(t); }
}
class X<T>
{
T MyT { get; }
public X(T t)
{
newtype Visitor e r = Visitor { visit :: e -> r }
data Employee = PermanentEmployee | Contractor
xmlvisitor :: Visitor Employee String
xmlvisitor = Visitor { visit = visitEmp }
where visitEmp PermanentEmployee = "<employee />"
visitEmp Contractor = "<contractor />"
class Visitee e where
newtype EmployeeVisitor r = EmployeeVisitor { visit :: Employee -> r }
data Employee = PermanentEmployee | Contractor
xmlvisitor :: EmployeeVisitor String
xmlvisitor = EmployeeVisitor { visit = visitEmp }
where visitEmp PermanentEmployee = "<employee />"
visitEmp Contractor = "<contractor />"
class VisitableEmployee e where
accept :: e -> EmployeeVisitor r -> r
@masaeedu
masaeedu / helloWorldCmp.js
Last active September 28, 2017 06:02
Composable stateful components with snabbdom
import { sc } from './sc'
import { h } from 'snabbdom'
// ...snabbdom imports
export const helloWorldCmp = cmp(({ state, setState }) =>
h('div', [
h('div', ['Hello ', state]),
h('input', { on: { input: e => setState(e.target.value) } })
]))
@masaeedu
masaeedu / index.js
Created October 30, 2017 06:34
requirebin sketch
const _ = require("lodash")
// Utilities
const range = n => [...Array(n).keys()]
const cart = (l, r) => _.flatMap(l, li => r.map(ri => [li, ri]))
const partitions = arr => {
const n = arr.length
const masks = range(Math.pow(2, n) - 1)
return masks.map(m => {
let i = 0
@masaeedu
masaeedu / index.js
Last active November 2, 2017 01:46
requirebin sketch
const Tone = require("tone");
const input = `
h x x x x x x x x
s - - x - - - x -
b x - - - x - - -
`;
const parse = input => {
const lines = input.trim().split("\n");
@masaeedu
masaeedu / state.js
Created March 17, 2018 01:58
State monad in JS
// Functor
export const map = f => s => x0 => {
const [v, x1] = s(x0);
return [f(v), x1];
};
// Applicative
export const pure = a => x => [a, x];
export const ap = stf => stv => x0 => {
const [f, x1] = stf(x0);
@masaeedu
masaeedu / reader.js
Last active March 22, 2018 18:50
The Reader monad
const { match } = require("natch");
const compose = f => g => x => f(g(x));
const kleisli = bind => fk => gk => compose(bind(fk))(gk);
// The reader module
const Reader = (() => {
// Constructors
const Pure = v => ({ t: "pure", v });
const Get = v => ({ t: "get", v });
@masaeedu
masaeedu / index.js
Created April 19, 2018 03:40
Datatype-generic instances in JS
const GenericEither = ({ Left, Right, match }) => {
const map = f => match({ Left, Right: x => Right(f(x)) });
const of = Right;
const chain = f => match({ Left, Right: f });
return { map, of, chain };
};
// A concrete representation of an Either
const MyEither = {
@masaeedu
masaeedu / deriving.js
Created April 30, 2018 21:10
Deriving free instances in JS
// Instances
const Arr = (() => {
// Monoid instance (forall a, [a] is a Monoid)
// :: [a]
const empty = []
// :: [a] -> [a] -> [a]
const concat = a => b => a.concat(b)
// Traversable instance (we have no typeclass system, so we need to accept the applicative instance dictionary explicitly)
// :: { "of" :: a -> f a, "lift2": (a -> b -> c) -> f a -> f b -> fc }