Skip to content

Instantly share code, notes, and snippets.

View mofas's full-sized avatar

Chung Yen Li mofas

View GitHub Profile
@mofas
mofas / updateable_component.js
Last active October 20, 2016 16:23
Update immutable nested data like breeze
//root data is stored in reducer
const rootData = fromJS({
company: [
{
id: 'company_1',
name: 'foo',
campaign: [
{
id: 'cam_1',
name: 'bar',
@mofas
mofas / helepr.js
Created December 26, 2016 01:53
DrBoolean FP constructure
// from https://github.com/DrBoolean/Practically-Functional
const Box = x =>
({
chain: f => f(x),
map: f => Box(f(x)),
fold: f => f(x),
inspect: () => `Box(${x})`
})
@mofas
mofas / CLI: search
Last active January 26, 2017 09:38
Use Grep for Fast Search from the Command Line
@mofas
mofas / .eslintrc
Created April 22, 2017 05:11
prettier + eslint
{
"parser": "babel-eslint",
"extends": [
"eslint:recommended",
"plugin:import/errors",
"plugin:import/warnings",
"prettier"
],
"plugins": [
"prettier"
@mofas
mofas / list.js
Last active July 21, 2017 01:25
FP data structure: map
const cons = (a, b) => p => p ? a : b;
const car = pair => pair(1);
const cdr = pair => pair(0);
const shift = xs => x => cons(x, xs);
const length = xs => typeof xs === 'function' ? 1 + length(cdr(xs)) : 0;
const get = xs => index => index ? get(cdr(xs))(index - 1) : xs;
const list1 = cons(1, cons(2, 3));
car(list1);
@mofas
mofas / list.js
Last active May 9, 2018 06:16
Build list structure by using function closure
// First we need to build data structure pair(a, b)
// cons: given a and b, and return a function.
// when you pass cons(a, b) as argument to car, it will return a;
// if you pass it to cdr, it will return b.
const cons = (a, b) => p => p ? a : b;
const car = pair => typeof pair === 'function' ? pair(1) : null;
const cdr = pair => typeof pair === 'function' ? pair(0) : null;
// Now we can build list by chaining pair.
// For example, list [1, 2, 3] will be represented as cons(1, cons(2, 3))
@mofas
mofas / map.js
Last active May 9, 2018 06:16
Build map in function closure
// At first, we need to build empty map.
// Empty map is just a function, it will return undefined when invoked.
// When you set new key-value into map, it will wrap a new function around previous map,
// and when you get something from map with key, it will pass key to every nested function until it match or hit empty map.
const map = () => undefined;
const set = map => (key, value) => q => q === key ? value: map;
const get = map => key => typeof map === 'function' ? get(map(key))(key) : map;
// Demo time!
@mofas
mofas / Short_Cut.md
Last active December 3, 2018 15:57
emacs short cut

Common

Reload file

C+x C+v 

Open file

C+x C+f
@mofas
mofas / updateSchema.js
Created October 21, 2017 15:47
GraphQL Schema Printer
#!/usr/bin/env babel-node --optional es7.asyncFunctions
/**
* This file provided by Facebook is for non-commercial testing and evaluation
* purposes only. Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
@mofas
mofas / pure_set.js
Last active December 10, 2017 18:08
Implement set in pure functional way
const empty = () => y => window.Error("not found error");
const extendSet = (y, val, set) => x => x === y ? val : set(x);
const setA = extendSet('a', 3, empty());
setA('a'); // 3
setA('b'); // not fuond error
const setA2 = extendSet('b', 4, setA);
setA2('a'); // 3
setA2('b'); // 4