Skip to content

Instantly share code, notes, and snippets.

View mrtnbroder's full-sized avatar
🕶️
λ

Martin Broder mrtnbroder

🕶️
λ
View GitHub Profile
@mrtnbroder
mrtnbroder / chainRec.js
Created November 1, 2018 09:00 — forked from richdouglasevans/chainRec.js
Code from the fantas-eel post on ChainRec.
const daggy = require("daggy");
const { Loop, Done } = daggy.taggedSum("Loop", {
Loop: ["b"],
Done: ["a"]
});
Array.empty = () => [];
const Pair = T => {
@mrtnbroder
mrtnbroder / monad.js
Created November 1, 2018 09:00 — forked from richdouglasevans/monad.js
The Monad example from the Fantasy Land series.
const Promise = require("fantasy-promises");
const daggy = require("daggy");
//- Regular `compose` - old news!
//+ compose :: (b -> c)
//+ -> (a -> b)
//+ -> a -> c
const compose = f => g => x => f(g(x));
//- `chain`-sequencing `compose`, fancily
@mrtnbroder
mrtnbroder / extend.js
Created November 1, 2018 09:00 — forked from richdouglasevans/extend.js
All the example code from the Extend article of Fantas, Eel, and Specification.
const daggy = require("daggy");
const { uncurryN } = require("wi-jit");
Array.prototype.empty = () => [];
const Sum = daggy.tagged("Sum", ["value"]);
Sum.prototype.concat = function(that) {
return Sum(this.value + that.value);
};
@mrtnbroder
mrtnbroder / Comonad.js
Created November 1, 2018 09:00 — forked from richdouglasevans/Comonad.js
Code for Game of Life from the Comonad article.
const { tagged } = require("daggy");
const Pair = tagged("Pair", ["_1", "_2"]);
//+ data Store p s = Store (p -> s) p
const Store = tagged("Store", ["lookup", "pointer"]);
Array.prototype.equals = function(that) {
return (
this.length === that.length &&
@mrtnbroder
mrtnbroder / bifunctor-profunctor.js
Created November 1, 2018 08:59 — forked from richdouglasevans/bifunctor-profunctor.js
The main examples from the "Bifunctor + Profunctor" post.
const { Left, Right } = require("fantasy-eithers");
const daggy = require("daggy");
Function.prototype.map = function(f) {
return x => f(this(x));
};
//- Where everything changes...
const login = user => (user.name == "Tom" ? Right(user) : Left("Boo"));
class DatePicker extends React.PureComponent {
constructor(props) {
super(props)
this.picker = PickerModel.create({
calendar: {
value: props.defaultValue,
min: props.min,
max: props.max,
}
})
@mrtnbroder
mrtnbroder / fetch-helpers.js
Created May 18, 2017 08:48 — forked from megamaddu/fetch-helpers.js
Fetch API Helpers
import fetch from 'isomorphic-fetch'
const setupRequestOptions = (options = {}, overrideMethod) => {
if (overrideMethod) options.method = overrideMethod
if (!options.headers) options.headers = {}
options.credentials = 'same-origin'
return options
}
const setupJsonRequestOptions = (options, overrideMethod) => {
@mrtnbroder
mrtnbroder / deep-merge.js
Last active January 16, 2018 09:04
Ramda Receips
import R from 'ramda'
const mergePlan = (x, y) => {
if(Array.isArray(x) && Array.isArray(y)) {
return R.uniq(R.concat(x, y));
}
if(typeof x === 'object' && typeof y === 'object'){
return R.mergeWith(mergePlan, x, y)
}
@mrtnbroder
mrtnbroder / redux-localstorage-middleware.js
Last active April 26, 2017 13:55
Redux localStorage Middleware
import R from 'ramda'
const defaultConfig = (config = {}) => {
const {
localStorage: storage = { removeItem: R.F },
__APP_VERSION__: version = '',
} = __BROWSER__ ? window : {}
const namespace = 'my-cool-app'
const appName = `${namespace}.v${version}`
@mrtnbroder
mrtnbroder / requestIdleCallback-example.js
Created April 26, 2017 12:21
HowTo requestIdleCallback
import R from 'ramda'
const runTask = R.curry((taskList, deadline) => {
while ((deadline.timeRemaining() > 0 || deadline.didTimeout) && taskList.length) {
const task = taskList.shift()
task()
}
if (taskList.length) {