Skip to content

Instantly share code, notes, and snippets.

View dtipson's full-sized avatar

Drew dtipson

View GitHub Profile
@johanneslumpe
johanneslumpe / gist:3cdb000b5f2816a70041c1306edc97f4
Created May 10, 2016 07:57
Selecting a consecutive entity range in draft-js
const maybeSelectConsecutiveEntity = editorState => {
const selection = editorState.getSelection();
if (!selection.isCollapsed()) {
return editorState;
}
const startKey = selection.getStartKey();
const startOffset = selection.getStartOffset();
const prevOffset = startOffset - 1;
@Avaq
Avaq / combinators.js
Last active May 19, 2024 00:21
Common combinators in JavaScript
const I = x => x
const K = x => y => x
const A = f => x => f (x)
const T = x => f => f (x)
const W = f => x => f (x) (x)
const C = f => y => x => f (x) (y)
const B = f => g => x => f (g (x))
const S = f => g => x => f (x) (g (x))
const S_ = f => g => x => f (g (x)) (x)
const S2 = f => g => h => x => f (g (x)) (h (x))
@DrBoolean
DrBoolean / binary_baby_transformer.js
Last active December 1, 2020 03:12
StateT is useful
import {StateT} from 'fantasy-states'
import Task from 'data.task'
import {prop, compose, map, chain, merge, always} from 'ramda'
// Unfortunately Binary Gendered Baby Page
//==========================================
// data App a b c = App State a (Task b c)
const App = StateT(Task)
const {get, put, modify} = App;
@DrBoolean
DrBoolean / binary_baby.js
Created January 7, 2016 21:04
State Monad is useful example
import State, {get, put, modify} from 'fantasy-states'
import {prop, compose, map, chain, merge, always} from 'ramda'
// Unfortunately Binary Gendered Baby Page
//==========================================
const babies = [{id: 2, name: 'Anjali', sex: 'F'}, {id: 3, name: 'Antonio', sex: 'M'}]
// isFemale :: Baby -> Bool
@DrBoolean
DrBoolean / mcft.js
Created December 30, 2015 04:44
Monoidal Contravariant Functors and Transducers
const daggy = require('daggy');
const {foldMap} = require('pointfree-fantasy')
const {concat, toUpper, prop, identity, range, compose} = require('ramda');
// Contravariant functors usually have this shape F(a -> ConcreteType).
// In other words, some type holding a function which is parametric on its input, but not output.
// They don't always have that shape, but it's a good intuition
// Covariant functors are what we're used to, which are parametric in their output
//================================================================
@DrBoolean
DrBoolean / comonads-are-objects.js
Created December 23, 2015 19:55
Comonads are objects js style.
// http://www.haskellforall.com/2013/02/you-could-have-invented-comonads.html
const daggy = require('daggy');
const {toUpper, prop, identity, range, curry, compose} = require('ramda');
const Config = daggy.tagged("opts")
Config.prototype.inspect = function() {
return `Config(${this.opts})`
}
//https://gist.github.com/quelgar/4661081
const daggy = require('daggy');
const {range} = require('ramda');
// type to hold an index and the array
const Pointer = daggy.tagged('i', 'a')
// Comonad instance
Pointer.prototype.extract = function() { return this.a[this.i] }
@raine
raine / index.js
Last active October 6, 2015 17:36
const lineLens = lens(split('\n'), join('\n'));
const mapLines = curry((fn, str) =>
over(lineLens, map(fn), str));
const adjustLine = curry((fn, n, str) =>
over(lineLens, adjust(fn, n), str))
mapLines(toUpper, 'foo\nbar') // => 'FOO\nBAR'
adjustLine(toUpper, 0, 'foo\nbar') // => 'FOO\nbar"
@tel
tel / Lens.js
Created June 20, 2015 05:06
Really simple van Laarhoven lenses in Javascript
const IdModule = {
fmap: (fn) => (x) => fn(x),
}
const ConstModule = {
fmap: (fn) => (x) => x,
}
/**
@divarvel
divarvel / continuation.js
Last active May 27, 2023 08:12
implementation of the continuation monad in JS
console.log("\033[39mRunning tests…");
function assertEquals(actual, expected, description) {
if(typeof actual === "undefined") {
console.error("\033[31m" + description + " not implemented\033[39m");
} else {
if(actual !== expected) {
console.error("\033[31m" + description + " failed, expected " + expected + ", got " + actual + "\033[39m");
} else {
console.log(description + " \033[32m ok\033[39m");
}