Skip to content

Instantly share code, notes, and snippets.

@CrossEye
CrossEye / Real World Specification.md
Created January 4, 2022 22:41 — forked from ForbesLindesay/Real World Specification.md
Functional Programming from the perspective of a JavaScript Programmer.

Real World Specification

(aka "Algebraic JavaScript Specification")

This project specifies the behavior of a number of methods that may optionally be added to any object. The motivation behind this is to encourage greater code reuse. You can create functions that just rely on objects having implementations of the methods below, and in doing so you can make them work with a wide variety of different, but related data structures.

Definitions

For the purposes of this, spec, an "entity" is an object that has an [[equivalent]] operation (see bellow) and may implement some or all of the other methods.

@CrossEye
CrossEye / index.html
Created November 3, 2016 03:14 — forked from RubaXa/index.html
String#includes vs. String#indexOf vs. RegExp (http://jsbench.github.io/#a4612afd0cd26e911ee8) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>String#includes vs. String#indexOf vs. RegExp</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>

This came out of the recent JS Promises debate. @ForbesLindesay was confused by liftA2, so I thought I’d try to explain it.

Let me explain liftA2. I’ll tweak @pufuwozu’s examples so that they return values.

// Promises (called when both succeed)
liftA2(readFIle('hello.txt'), readFile('world.txt'), function(hello, world) {
 return hello + ' ' + world;
});
// Optional values (only inserted into database when both exist)

liftA2(optionalUsername, optionalPassword, function(username, password) {

@CrossEye
CrossEye / lens.js
Created June 5, 2014 15:36 — forked from andyhd/lens.js
function lens(get, set) {
var f = function (a) { return get(a); };
f.set = set;
f.mod = function (f, a) { return set(a, f(get(a))); };
return f;
}
var first = lens(
function (a) { return a[0]; },
function (a, b) { return [b].concat(a.slice(1)); }
@CrossEye
CrossEye / where.js
Last active August 29, 2015 13:56 — forked from buzzdecafe/where.js
var where = curry(function where(matchThis, testObj) {
return ramda.all(function (key) {
var val = matchThis[key];
return typeof val == "function" ? val(testObj[key], testObj) :
(testObj[key] === matchThis[key]);
}, Object.keys(matchThis));
});
x1y2 = where({x: 1, y: 2});
x1y2({x: 1}); //=> false
@CrossEye
CrossEye / Functor.js
Last active December 23, 2015 07:58 — forked from buzzdecafe/Functor.js
First Functor Fantasy Follow-up
(function(global) {
// TODO: Make a tree structure from type objects' optional `parentKey` properties. Do a depth-first search of
// this tree instead of the simple linear search of `Object.keys`.
var types = {};
global.Functor = function(config) {
types[config.key] = config;
};
global.fmap = curry(function(f, obj) {
// clean and pure:
function cons(x, y) {
return function(pick) {
return pick(x, y);
}
}
// does more stuff:
function cons(x, y) {
var fn = function(pick) {