Skip to content

Instantly share code, notes, and snippets.

View dypsilon's full-sized avatar

Tim Navrotskyy dypsilon

View GitHub Profile
@dypsilon
dypsilon / continuation.js
Created August 8, 2016 09:27
Eager Continuation in JavaScript
const {tagged} = require('daggy');
let Continuation = tagged('x');
Continuation.prototype.of = Continuation.of = x => {
return new Continuation((resolve) => resolve(x));
}
Continuation.prototype.chain = function(f) {
return this.x(f);
@dypsilon
dypsilon / examples.js
Last active April 28, 2024 08:52
Lazy Continuation Monad in JavaScript
const Cont = require('./lazy-continuation');
// pointed version
const c = Cont.of(5) // initial value
.chain((x) => {
return Cont.of(x + 5); // synchronous computation
})
.chain((x) => { // async computation
return new Cont((resolve) => {
setTimeout(() => resolve(x + 15), 500);
@dypsilon
dypsilon / array_functor_test.js
Created August 7, 2016 14:12
This simple test proves, that standard JavaScript arrays are algebraic functors according to the fantasy land spec (https://github.com/fantasyland/fantasy-land)
const eq = (test, a1, a2) => {
if(a1.length === a2.length && a1.every((v,i) => v === a2[i])) {
return;
}
throw new Error(`Test failed: ${ test }`);
};
{ // Functor
const u = Array.of([1, 2, 3]);
@dypsilon
dypsilon / identity.js
Last active September 28, 2019 16:09
A simple implementation of the identity monad without any dependencies. Including testing the algebraic laws.
/**
* The Identity monad is a monad that does not embody any computational strategy.
* It simply applies the bound function to its input without any modification.
* Computationally, there is no reason to use the Identity monad instead of the much simpler
* act of simply applying functions to their arguments.
*
* The purpose of the Identity monad is its fundamental role in the theory of monad transformers.
* Any monad transformer applied to the Identity monad yields a non-transformer version of that monad.
*
* source: https://hackage.haskell.org/package/mtl-2.2.1/docs/Control-Monad-Identity.html
@dypsilon
dypsilon / functor_exercises.js
Created April 28, 2016 22:06
Exercises from "mostly adequate quide to functional programming"
require('../../support');
var Task = require('data.task');
var _ = require('ramda');
// Exercise 1
// ==========
// Use _.add(x,y) and _.map(f,x) to make a function that increments a value inside a functor
var ex1 = _.map(_.add(1));;
function arrayReduce(xf, init, array){
var value = init;
var idx = 0;
var length = array.length;
for(; idx < length; idx++){
value = xf.step(value, array[idx]);
if(isReduced(value)){
value = deref(value);
break;
}
@dypsilon
dypsilon / highland_readdirp.js
Last active August 29, 2015 14:24
Recursive readdir using highland streams.
/**
* This function takes a directory path and returns a flat stream
* with stat objects + full file path in the .path property.
*/
var path = require('path');
var h = require('highland');
var readdir = h.wrapCallback(require('fs').readdir);
var stat = h.wrapCallback(require('fs').stat);
@dypsilon
dypsilon / gulp-bookmarks.md
Created June 9, 2015 00:42
Collection fo Gulp Bookmarks