This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// emulation of Go's defer in JS | |
// helper function | |
const makeDefer = () => { | |
const deferred = []; | |
return { | |
defer(f) { | |
deferred.unshift(f); | |
}, | |
done(result) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! foo { | |
($a: literal ) => { | |
$a | |
}; | |
($op:tt $a:literal $b:tt) => { | |
foo!($op ($a) $b) | |
}; | |
($op:tt $a:tt $b:literal) => { | |
foo!($op $a ($b)) | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createStore = (reducer, state) => { | |
let subscribers = []; | |
return { | |
dispatch: (action) => { | |
state = reducer(state, action); | |
subscribers.forEach(f => f(state)); | |
}, | |
getState: () => state, | |
subscribe: (f) => { | |
subscribers.push(f); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const AC = type => { | |
const ac = payload => ({type, payload}); | |
ac.type = ac().type; | |
return { [type]: ac, type } | |
}; | |
const { addTodo, type: ADD_TODO } = AC('addTodo'); | |
const { removeTodo, type: REMOVE_TODO } = AC('removeTodo'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xs from 'xstream'; | |
// If I intended to make a serious state-management system based on observables, I would probably not try to imitate Redux | |
// but this was made only for self-learning and for fun. | |
// Redux-like store factory: | |
const createStore = (reducer, initial) => { | |
const action$ = xs.create({start() {},stop() {}}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// someScript.funq | |
def hello { | @name | | |
if (2 < 3) { | |
alert "two is less than three" | |
} else { | |
alert "two is not less than three" | |
}; | |
alert ("Hello, " + @name) | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hoc | |
compose( | |
withState({a: 'setA', b: 'setB'}), | |
withProps(({ a, b }) => ({ c: a + b })) | |
); | |
// render function | |
return ( | |
<div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = (cond) => { if (!cond) throw new Error('assertion error') }; | |
type Sum = (a: any, b: any) => number; | |
const addWithNumbers: Sum = (a, b) => a + b; | |
const numbers = { | |
one: 1, | |
two: 2, | |
three: 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reserializer = (mapping) => { | |
const map = new Map(); | |
Object.keys(mapping).forEach(k => map.set(mapping[k], k)); | |
return { | |
serialize: value => map.get(value), | |
deserialize: value => mapping[value], | |
} | |
} | |
// application |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// object literal. Very flexible and with shorthand notation for methods, it's very nice. | |
const obj = { | |
addTodo() { | |
}, | |
removeTodo() { | |
}, | |
transactions: { // sub object | |
fetchTodos() { | |
} | |
} |
NewerOlder