Skip to content

Instantly share code, notes, and snippets.

(function () {
var g, h;
function x() { f = ""; /* var-scoped f gets value "" */ }
function y() { g = f; /* g gets value of var-scoped f */ }
{
/* var-scoped f is undefined, let-scoped f is a function */
h = f; /* h gets value of let-scoped f, a function */
f = 1; /* let-scoped f gets value 1 */
x();
y();
@michaelficarra
michaelficarra / pseudo-privates.es5.js
Last active October 12, 2015 22:33
pseudo-private methods using a capabilities-based approach
var ClassName = (function() {
var PRIVATE = {};
function ClassName(state) {
this.state = state;
}
ClassName.prototype.publicMethod = function publicMethod() {
this.privateMethod(PRIVATE, "...");
};
@michaelficarra
michaelficarra / create-function-with-given-arity.js
Last active August 28, 2015 16:25
creating a function with a given name and arity in ES5 vs. ES2015
var apply = Function.prototype.call.bind(Function.prototype.apply);
function createFunctionES5(name, arity, behaviour) {
var params = Array.apply(null, Array(arity)).map(function (x, p) { return "p" + p; }).join(",");
var code = "return function " + name + "(" + params + ") { return apply(f, this, arguments); }";
return Function("apply", "f", code)(apply, behaviour);
}
var define = Object.defineProperty.bind(Object);
@michaelficarra
michaelficarra / simple-templating.js
Created August 11, 2015 23:54
simple templating system built with ES2015 tagged templates
function escapeUsing(escaper) {
return function(literalParts, ...interpolatedParts) {
let s = literalParts[0];
for (let [interpolatedPart, literalPart] of zip(interpolatedParts, literalParts.slice(1))) {
s += escaper(interpolatedPart) + literalPart;
}
return s;
}
}
@michaelficarra
michaelficarra / countdown-timer.html
Last active August 29, 2015 14:24 — forked from anonymous/index.html
countdown timer
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Countdown Timer</title>
<style>
body, html {
display: flex;
align-items: center;
justify-content: center;
{-# LANGUAGE PolyKinds, KindSignatures, MultiParamTypeClasses #-}
module Main where
class Category (arr :: k -> k -> *) where
id :: arr t t
(.) :: arr b c -> arr a b -> arr a c
instance Category (->) where
id x = x
  • ArrayBinding

    • restElement must be BindingIdentifier or null except when ArrayBinding is part of an AssignmentPattern
  • BindingIdentifier

    • name must match the ES6 IdentifierName lexical grammar except when name is "*default*" and BindingIdentifier is the name of a ClassDeclaration or FunctionDeclaration which is the body of an ExportDefault
    • name must not be a reserved word
    • name must not be a future reserved word in strict mode
  • (12.1.1 and 12.14.5.1 and 14.1.2 and 14.4.1) name must not be a restricted word in strict mode

@michaelficarra
michaelficarra / phantom-rows.purs
Created February 25, 2015 17:48
phantom types using rows in PureScript; needs DataKinds to achieve full awesome
module Main where
--data Language = English | Spanish
--data Censored = Censored | NotCensored
--data Encoding = Plain | EncodingA | EncodingB
data English
data Spanish
data Censored
@michaelficarra
michaelficarra / set.prototype.map.js
Last active August 29, 2015 14:10
ES6 Set.prototype.map issue
var set = new Set([0, 1]),
f = function(x) { return 1 / x; },
g = function(x) { return x ? -0 : x; };
set.map(g); // Set{0}
set.map(g).map(f); // Set{1/0}
set.map(function(x){ return f(g(x)); }); // Set{1/0, -1/0}
@michaelficarra
michaelficarra / monads.js
Last active August 29, 2015 14:07 — forked from pselle/monads.js
function Container(val) {
this.val = val
}
// Functor's `fmap` in Haskell
Container.prototype.map = function(f) {
return new Container(f(this.val));
};
// Monad's `>>=` (pronounced bind) in Haskell