Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
slavic code master

Łukasz Lityński hex13

💭
slavic code master
View GitHub Profile
View pseudo-lisp-in-rust.rs
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 Apr 15, 2021
Redux-like library in 13 lines
View mini-redux.js
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 Jul 23, 2018
simple way for making universal action creator for Redux
View action-creator.js
'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 Dec 27, 2017
Redux-like store implemented in observables
View createStore.js
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 Nov 30, 2017
FunQuery - prototype of language (this is proposal - syntax and semantic can change)
View gist:00ce6de4cf2d6cf48f7aefd5242735d9
// 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)
};
View hoc vs render function
// hoc
compose(
withState({a: 'setA', b: 'setB'}),
withProps(({ a, b }) => ({ c: a + b }))
);
// render function
return (
<div>
View a.ts
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 Aug 28, 2017
how to serialize symbols in JavaScript
View serializeSymbols.js
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
View a.js
// object literal. Very flexible and with shorthand notation for methods, it's very nice.
const obj = {
addTodo() {
},
removeTodo() {
},
transactions: { // sub object
fetchTodos() {
}
}
@hex13
hex13 / redux-oop.js
Last active Sep 27, 2017
redux OOP way
View redux-oop.js
const reducers = {
add(state, {payload: [number]}) {
return state + number;
},
};
function reducer(state, action) {
if (reducers.hasOwnProperty(action.type))
return reducers[action.type](state, action);
return state;