Skip to content

Instantly share code, notes, and snippets.

View spion's full-sized avatar
:shipit:

Gorgi Kosev spion

:shipit:
View GitHub Profile
export class CodedError extends Error {
constructor(code, msg) {
super(msg)
this.code = code;
this.msg = msg;
}
static is(code) {
return (e) => e.code == code;
}
static only(code, handler) {
@spion
spion / 01-future.js
Last active June 26, 2016 20:52 — forked from robotlolita/0-specification.md
Future and ReaderT with auto-lifting and example
class Future {
constructor(computation) {
this.fork = computation
this.__future__ = true;
}
static is(val) {
return (val != null && val.__future__ === true)
}
static of(value) {
if (Future.is(value)) return value;
@spion
spion / table.ts
Created November 6, 2015 18:00 — forked from whoeverest/table.ts
/// <reference path="anydb-sql.d.ts" />
import anydbsql = require('anydb-sql');
var db = anydbsql({
url: 'postgres://user:pass@host:port/database',
connections: { min: 2, max: 20 }
});
// Table Post
@spion
spion / existing.sql
Last active November 9, 2015 04:58 — forked from eduardoleon/existing.sql
create table male
( male_id int not null
, primary key (male_id) );
create table female
( female_id int not null
, primary key (female_id) );
create table name
( id int not null
@spion
spion / functional.js
Last active December 6, 2015 13:36 — forked from Arnavion/async-await.ts
Promise.any - async/await vs then()
let pending = new Promise((r,rj) => {})
let waitForever = () => pending
let reject = Promise.reject.bind(promise)
let identity = x => x
let promiseTransform = fns => p => p.then(fns.fullfilment, fns.rejection)
function any(promises) {
let fulfillments = promises.map(promiseTransform({fullfilment: identity, rejection: waitForever}))
let rejections = promises.map(promiseTransform({fullfilment: waitForever, rejection: identity}))
let firstFulfill = Promise.race(fulfillments);
@spion
spion / flowcart.ts
Last active November 27, 2018 00:16 — forked from unscriptable/flowcart.js
A typesafe shopping cart in typescript
// A typesafe shopping cart in typescript.
// Immutable map :)
declare class Map<T, U> {
set(t:T, u:U):Map<T, U>
has(t:T):boolean;
delete(t:T):Map<T,U>
count:number;
}
// These theoretical API requests are deferred promises;
// Not executed until `.then()` is invoked.
let a = new Request('/foo');
let b = new Request('/bar');
let c = new Request('/baz');
// If invoked directly, then issue 3 direct HTTP requests:
Promise.all([ a, b, b ]).then((results) => {
// GET /foo
// GET /bar