Skip to content

Instantly share code, notes, and snippets.

Iven Marquardt ivenmarquardt

Block or report user

Report or block ivenmarquardt

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View race-conditions.md

Asynchronous operations entail race conditions provided they are evaluated in parellel. However, a counter should always be evaluated in sequence as per logic. I'd claim this reveals another flaw of Javascript's Promise type: It is actually a blend of the two types (sequential) Task and Parallel. While the former is a monad the latter is only an applicative.

Bergi has a point, of course. Asynchronous operations in parallel should solely rely on immutable data. However, operations that are inherently sequential should be evaluated as such and thus it doesn't matter if the underlying data is immutable or not.

I am not that versed in dealing with promises, so I probably doesn't use the type correctly. However, it should be enough to illustrate my point.

@ivenmarquardt
ivenmarquardt / lazy-evaluation.js
Last active May 18, 2018
Lazy evaluation, lazy getters, eta reduction, function composition, implicit thunks through deferred type
View lazy-evaluation.js
// Lazy Getters
/* What enables Tail Recursion Modulo Cons in Javascript is a thunk in Weak Head Normal Form.
An expression in weak head normal form has been evaluated to the outermost data constructor,
but contains sub-expressions that may not have been fully evaluated. In Javascript only thunks
can prevent sub-expressions from being immediately evaluated. */
const cons = (head, tail) => ({head, tail});
@ivenmarquardt
ivenmarquardt / value-polymorphism.js
Last active May 4, 2018
Value Polymorphism with Clojure Style Overloaded Functions
View value-polymorphism.js
// type constructor
const toTypeTag = x => {
const tag = Object.prototype.toString.call(x);
return tag.slice(tag.lastIndexOf(" ") + 1, -1);
};
const TAG = Symbol("TAG");
@ivenmarquardt
ivenmarquardt / todo-app.js
Last active Apr 2, 2019
Functional Reactive Programming (FRP) implemented with a couple rules, types and combinators
View todo-app.js
// Based on this blog post: https://medium.com/@iquardt/taming-the-dom-50c8f1a6e892
/* RULES
* Only the program may manipulate the output display, never the user
* User input is presented in the form of events
* GUI elements generate events only in response to user input, never in response to program output
*/
@ivenmarquardt
ivenmarquardt / frp.js
Created Apr 3, 2018
Functional reactive programming types: Event(-Stream) / Behavior
View frp.js
// data constructor
const Data = name => Dcons => {
const Data = k => {
const t = new Tcons();
t[`run${name}`] = k;
t.tag = name;
return t;
};
@ivenmarquardt
ivenmarquardt / effect.js
Last active Mar 5, 2018
Effect type applicative computation - deferred runtime error
View effect.js
const Data = Tcons => Dcons => {
const Data = x => {
const t = new Tcons();
t[`run${Tcons.name}`] = x;
t.tag = Tcons.name
return t;
};
return Dcons(Data);
};
@ivenmarquardt
ivenmarquardt / typecheck.js
Last active Mar 8, 2017
Dynamic Javascript type checker for curried functions (functional programming, currying, partial application, debug, debugging)
View typecheck.js
const reflectf = (...preds) => (...arities) => (f, tag = f.name) => (...args) => {
if (args.length !== arities[0]) {
throw new TypeError(`${tag} expects an ${arities[0]}-ary lambda (${args.length}-ary given)`);
}
args.forEach((arg, i) => preds[i](arg));
const r = f(...args);
if (args.length === arities.length) return (preds[1](r), r);
@ivenmarquardt
ivenmarquardt / foldlk.js
Created Oct 17, 2016
Catamorphism, fold, lazy evaluation, continuation, short circuiting, iteration
View foldlk.js
const foldlk = f => acc => xs => {
const next = (acc, key) => xs.length === key
? acc
: f(acc) (xs[key], acc => next(acc, key + 1));
return next(acc, 0);
};
const every = f => foldlk(x => (y, k) => f(y) ? k(true) : false) (true);
const some = f => foldlk(x => (y, k) => f(y) ? true : k(false)) (false);
@ivenmarquardt
ivenmarquardt / paralk.js
Last active Oct 18, 2016
Paramorphism, extended catamorphism, fold, iteration
View paralk.js
const paralk = f => acc => xs => {
const next = (acc, [head, ...tail]) => head === undefined
? acc
: f(acc) (head, tail, acc => next(acc, tail));
return next(acc, xs);
};
const drop = n => paralk(acc => (x, tail, k) => acc.length + 1 === n ? tail : (acc.push(x), k(acc))) ([]);
You can’t perform that action at this time.