Skip to content

Instantly share code, notes, and snippets.

@jhusain
jhusain / nofold.ts
Last active January 4, 2018 18:11
You can't write fold in TypeScript (doesn't compile)
interface Combinable<T> {
plus(T): T,
zero(): T
}
class Max implements Combinable<Max> {
private value: number
constructor(num: number) {
this.value = num;
}
@jhusain
jhusain / failingfunctor.purs
Last active December 20, 2017 06:08
Functor instance won't compile
module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Data.Foldable (fold)
import TryPureScript (DOM, h1, h2, p, text, list, indent, link, render, code)
import Data.Map (Map, lookup, insert, empty)
import Data.List
import Data.Either
class Observable<T, E> {
// cant be typed in flow, but perhaps we can
// somehow declare several overloads?
// pipe(Observable<T0, E0> => Observable<T1, E1>): Observable<T1, E1>
// pipe(Observable<T0, E0> => Observable<T1, E1>, Observable<T1, E1> => Observable<T2, E2>): Observable<T2, E2>
// etc...
pipe(...operators) {
return operators.reduce((acc, curr) => curr(acc), this);
}
@jhusain
jhusain / gist:d6007e9cce59ac6f173449cd6841f9fc
Created May 20, 2017 19:12
"Animations Allowed" problem
someObservable.retry(3)
consts tasks =
{
....{....5......2.......3...}
............{......5............4....3}
............................................{5...2...4}
.....................................................{...5}
}
@jhusain
jhusain / Simple Observable impl
Created May 4, 2017 00:26
Observable implementation which optimizes very well in prepack
function Observable(subscribe) {
this._subscribe = subscribe;
}
const of = (v) => new Observable(observer => {
return observer.next(v);
});
Observable.prototype = {
map(f) {
@jhusain
jhusain / PEG grammer for pattern match syntax
Created October 29, 2016 03:29
PEG grammer for pattern match syntax. Forked from JSON grammer.
JSON_text
= ws value:value ws { return value; }
begin_array = ws "[" ws
begin_object = ws "{" ws
end_array = ws "]" ws
end_object = ws "}" ws
name_separator = ws ":" ws
value_separator = ws "," ws
var spec = { enumerable: false }
var x = {};
Object.defineProperty(x, "test", spec);
x.test = "what"
alert(JSON.stringify(x));
var y = {};
Object.defineProperty(y, "test", spec);
@jhusain
jhusain / Response
Created August 19, 2015 18:19
Games JSON Graph
//You shouldn't find your self asking for ids from a Falcor JSON Graph object.
//it seems like you want to build an array of game ids:
{
games: [
{ $type: "ref", value: ["gamesById", 352] },
{ $type: "ref", value: ["gamesById", 428] }
// ...
],
@jhusain
jhusain / gist:db542bf5e2a0b79098ab
Created August 4, 2015 19:52
Redux style app
// mock falcor Model
function Model(root, path) {
this.root = root;
this.path = path || [];
}
Model.prototype.bind = function(path) {
return new Model(this.root, this.path.concat(path));
};
@jhusain
jhusain / gist:3c510869ec091772c440
Last active May 6, 2024 03:32
Observable map function
class Observable {
map(fn) {
if (typeof fn !== "function")
throw new TypeError(fn + " is not a function");
let C = this.constructor[Symbol.species];
return new C(observer => {