Skip to content

Instantly share code, notes, and snippets.

View leihuang23's full-sized avatar
⛰️

Lei Huang leihuang23

⛰️
View GitHub Profile
@leihuang23
leihuang23 / blockchain.js
Created January 7, 2018 13:46
A simple blockchain implemented with JavaScript
const SHA256 = require("crypto-js/sha256");
class Block {
constructor(index, timestamp, data, previousHash = '') {
this.index = index;
this.previousHash = previousHash;
this.timestamp = timestamp;
this.data = data;
this.hash = this.calculateHash();
this.nonce = 0;
@leihuang23
leihuang23 / IO.js
Created August 20, 2018 07:24
The IO monad implemented with JavaScript factory function
function IO(effectFn) {
const __val = effectFn;
const map = fn => IO(() => fn(__val()));
const performUnsafeIO = () => __val();
const chain = fn => IO(() => fn(__val()).performUnsafeIO());
return Object.freeze({
map,
chain,
performUnsafeIO
});
@leihuang23
leihuang23 / reader_future.js
Created August 20, 2018 10:54 — forked from dypsilon/reader_future.js
Reader + Future Monads Usage
/**
* This short program will encrypt the user password
* and insert a new record into a mock database.
*/
const Reader = require('fantasy-readers');
const Future = require('fluture');
const R = require('ramda');
const crypto = require('crypto');
// our mock database
@leihuang23
leihuang23 / fp-either-monad.js
Created August 22, 2018 01:45 — forked from mrosata/fp-either-monad.js
Functional JavaScript Monad Classes - (Maybe Just Nothing) - (Either Left Right) (IOMonad) and my type checking utils
import is from './is-util';
/**
* Either Monad class (from Functional Programming in JavaScript)
*/
class Either {
constructor(value) {
this._value = value;
}
get value () {
@leihuang23
leihuang23 / State.js
Created January 2, 2019 01:00
State monad in JS
const composeK = (...fns) =>
fns.reduce((f, g) => (...args) => g(...args).chain(f));
const liftA2 = (f, m1, m2) => m1.map(f).ap(m2);
const K = a => b => a;
const State = computation => {
const map = f =>
State(state => {
@leihuang23
leihuang23 / easing.js
Created January 15, 2019 02:48 — forked from gre/easing.js
Simple Easing Functions in Javascript - see https://github.com/gre/bezier-easing
/*
* Easing Functions - inspired from http://gizma.com/easing/
* only considering the t value for the range [0, 1] => [0, 1]
*/
EasingFunctions = {
// no easing, no acceleration
linear: function (t) { return t },
// accelerating from zero velocity
easeInQuad: function (t) { return t*t },
// decelerating to zero velocity
@leihuang23
leihuang23 / lens.js
Created January 18, 2019 00:08
Lens implementation in JavaScript
const curry = fn => (...args) =>
args.length >= fn.length ? fn(...args) : curry(fn.bind(undefined, ...args))
const always = a => b => a
const compose = (...fns) => args => fns.reduceRight((x, f) => f(x), args)
const getFunctor = x =>
Object.freeze({
value: x,
@leihuang23
leihuang23 / sequence.js
Created March 28, 2019 12:32
A sequence library implemented with Crockford functions
function toList(next, reversed) {
const arr = [];
let value = next();
while (value !== undefined) {
if (reversed) {
arr.unshift(value);
value = next();
} else {
arr.push(value);
value = next();
@leihuang23
leihuang23 / go.js
Created April 4, 2019 03:47 — forked from Gozala/go.js
Go routines for JS
// Utility function for detecting generators.
let isGenerator = x => {
return Function.isGenerator &&
Function.isGenerator.call(x)
}
// Data type represents channel into which values
// can be `put`, or `received` from. Channel is
// very much like queue where reads and writes are
// synchronized via continuation passing.
@leihuang23
leihuang23 / trie.js
Created March 29, 2020 23:07
Trie implemented in JavaScript
class TrieNode {
constructor(char) {
this.char = char;
this.validWord = false;
this.parent = null;
this.children = [];
}
}
class Trie {