Skip to content

Instantly share code, notes, and snippets.

View unscriptable's full-sized avatar
🏔️

John Hann unscriptable

🏔️
View GitHub Profile
'use strict'
/*@flow*/
// Creates a function that clones dates and shifts by the
// seconds specified.
// Example:
// >>> import { seconds } from './shift'
// >>> const fiveSecondsLater = seconds(5)
// >>> const d = new Date()
// >>> fiveSecondsLater(d) - d
@unscriptable
unscriptable / try.purescript
Created June 13, 2016 14:35
simple purescript that dont werk
module Parse where
import Prelude
import Data.Traversable (scanr)
import Data.List (List(Cons), (:))
data Loc = Loc { line :: Int }
data LocRange
= LocRange {
@unscriptable
unscriptable / CheckedPayment.js
Last active July 1, 2016 22:49
Compile-time enforcement of validation via type system via a container w/ smart constructors
'use strict';
/*@flow*/
/*
Here's an example of how to enforce at compile-time that an object is
validated before it is used in code where it could potentially fail at
run-time.
This one works by "containing" an object purely in another flow type.
(Sorta like a `newtype` in Haskell.) Actually, this code cheats; the
'use strict';
/*@flow*/
// TODO: parameterize by argument type when flow supports it:
// type Func<A,R> = (...args:A) => R
// type Adviser<F:Func<A,R>> = (x:F) => F
// export type TraceAdvice<A,R> = (args:A, result:R) => mixed
type Func<R> = () => R
type Adviser<F:Function> = (x:F) => F
// Sequence an array of promise-producing functions.
export const sequence
: (ops:Array<() => Promise>) => Promise
= ops =>
ops.reduce((p, op) => p.then(op), Promise.resolve())
@unscriptable
unscriptable / unsafeClass.js
Created December 19, 2015 23:13
I thought classes were safe :(
'use strict';
/*@flow*/
class Square {
/*::
x: number;
y: number;
w: number;
*/
constructor (x/*: number*/, y/*: number*/, w/*: number*/) {
@unscriptable
unscriptable / flowcart.js
Last active January 8, 2016 16:54
A typesafe shopping cart in flowtype (see http://flowtype.org)
'use strict';
/* @flow */
// A typesafe shopping cart in flowtype.
// Flow relies heavily on EcmaScript constructs, primarily `class` to
// define types. This feels quite restrictive if you've been using a strongly
// typed language, such as Haskell. Still, I managed to build this "safe"
// suite of shopping cart functions that prevents the developer from doing
// nonsensical things with the cart. For instance, you can't compile code
'use strict';
/* @flow */
// Cart items.
type SKU = string;
type Item = {
sku: SKU,
quantity: number
@unscriptable
unscriptable / PhantomVisit.hs
Last active September 22, 2015 21:04
Implement Visit using phantom types and smart constructors
module PhantomVisit (
Visit,
CallAhead,
Remote,
WalkIn,
Rsv,
callAhead,
remote,
walkIn,
rsv
@unscriptable
unscriptable / maybe.js
Last active August 29, 2015 14:20
Maybe monad in javascript
export class Maybe {
constructor (f) {
this._run = f;
}
/**
* Transform the result of a maybe operation.
*/
map (f) {