Skip to content

Instantly share code, notes, and snippets.

View hex13's full-sized avatar
💭
slavic code master

Łukasz Lityński hex13

💭
slavic code master
  • JavaScript developer
  • Warsaw/Poland
View GitHub Profile
// emulation of Go's defer in JS
// helper function
const makeDefer = () => {
const deferred = [];
return {
defer(f) {
deferred.unshift(f);
},
done(result) {
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))
};
@hex13
hex13 / mini-redux.js
Last active April 15, 2021 09:07
Redux-like library in 13 lines
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);
@hex13
hex13 / action-creator.js
Last active July 23, 2018 00:53
simple way for making universal action creator for Redux
'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');
@hex13
hex13 / createStore.js
Last active December 27, 2017 17:11
Redux-like store implemented in observables
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() {}});
@hex13
hex13 / gist:00ce6de4cf2d6cf48f7aefd5242735d9
Last active November 30, 2017 00:00
FunQuery - prototype of language (this is proposal - syntax and semantic can change)
// 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)
};
// hoc
compose(
withState({a: 'setA', b: 'setB'}),
withProps(({ a, b }) => ({ c: a + b }))
);
// render function
return (
<div>
@hex13
hex13 / a.ts
Created September 20, 2017 09:38
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
@hex13
hex13 / serializeSymbols.js
Created August 28, 2017 20:27
how to serialize symbols in JavaScript
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
@hex13
hex13 / a.js
Last active July 7, 2017 12:25
// object literal. Very flexible and with shorthand notation for methods, it's very nice.
const obj = {
addTodo() {
},
removeTodo() {
},
transactions: { // sub object
fetchTodos() {
}
}