Skip to content

Instantly share code, notes, and snippets.

syntax foo = function(ctx) {
let result = #`foo(`;
result = result.concat(#`)`);
return result;
};
@gabejohnson
gabejohnson / Node-RED-es2015
Last active April 8, 2016 21:12
This gist demonstrates a pattern for using es2015 in the authoring of https://github.com/node-red/node-red nodes. It also contains a gulpfile to build and deploy nodes to a user folder and restart the node-red server. It currently requires the 0.14.0 branch of node-red for proper inheritance.
title file
@gabejohnson
gabejohnson / test.js
Last active October 24, 2016 20:15
Performance of Function.prototype.call
function test(f) {
var counter = 10000000;
performance.clearMarks();
performance.mark('StartTest');
for(var i = counter; i > 0; --i) {
f();
}
performance.mark('EndTest');
var marks = performance.getEntriesByType('mark');
return marks[1].startTime-marks[0].startTime;
const t = Table.of({a:1, b:2, c:3});
const t2 = Table.of({a:4, d:5});
const t3 = t.concat(t2).concat(Table.empty());
t3 // Table {a: 4, b: 2, c: 3, d: 5}
t3.head() // Object {a: 4, d: 5}
t3.tail() // Table {a: 1, b: 2, c: 3}
t3.append({b:6, e:7}) // Table {a: 4, b: 6, c: 3, d: 5, e: 7}
t3.reduce(x => x * x) // Object {a: 16, b: 4, c: 9, d: 25}
Table.empty().alt(t3) // t3
const alias = prefix => T => {
const getKeys = o => Object.keys(o)
.filter(k => k.startsWith(prefix+'/'))
.map(k => k.split('/')[1]);
const scaryMutateObject = o => getKeys(o).forEach(k => o[k] = o[prefix+'/'+k]);
scaryMutateObject(T);
scaryMutateObject(T.prototype);
// I'M RETURNING UNDEFINED!!!
@gabejohnson
gabejohnson / trie.js
Last active March 8, 2017 19:28
A JavaScript port of Data.Trie
// Likely VERY inefficient port of Data.Trie
function Trie(a) {
// could null be a valid value? if so, we can't use S.toMaybe
this.value = S.toMaybe(a);
this.children = {};
this.ks = Object.freeze(this.value.isNothing ? [] : ['']);
};
/* Setoid */
// Trie a ~> Trie a -> Bool
@gabejohnson
gabejohnson / tuples.js
Last active June 9, 2017 15:58
Some tuple implementations
function Unit() { return Unit; }
function Pair(fst, snd) {
'use strict';
if(this === void 0) return new Pair(fst, snd);
this.fst = fst;
this.snd = snd;
}
Pair.prototype.map = //...
//...
@gabejohnson
gabejohnson / fl-interfaces.js
Last active June 13, 2017 12:48
[WIP] Fantasy Land Interfaces
import { class, interface, implements } from 'sweet-interfaces';
const constant = x => _ => x;
const identity = x => x;
const flip = f => (a, b) -> f(b, c);
interface Setoid {
// eq :: Setoid a => a ~> a -> Boolean
eq(b) { return this === b; }
}
@gabejohnson
gabejohnson / PortSolutions.purs
Last active October 13, 2017 21:20
Potential solutions for multiple backend porting issues
-- Potential solutions for multiple backend porting issues
-- 1. Optional PS implementation for optional foreign imports
-- JS has an implementation, Erlang would export bar instead
module Foo (foo) where
-- import `fooImpl` if it's provided by the host implementation
optional foreign import foo :: Number -> Number -> Number
-- define it otherwise
import { Right, Future, pipe, curry2, ifElse, isNotNil, memoize, chain, map } from "../fp"
// mssql.js
import sql from "mssql"
const createPoolAsync = memoize(connectionString =>
new Future( ( reject, resolve ) => {
const pool = new sql.ConnectionPool(
connectionString,
ifElse( isNotNil, reject, () => resolve( pool ) )
);