Skip to content

Instantly share code, notes, and snippets.

View yelouafi's full-sized avatar

Yassine Elouafi yelouafi

View GitHub Profile
"use strict"
const assign = Object.assign
function push(arr, item) {
let newArr = (arr && arr.slice()) || []
newArr.push(item)
return newArr
}
<!DOCTYPE html>
<html>
<head>
<title>Redux rocket launcher example</title>
<script src="https://npmcdn.com/redux@latest/dist/redux.min.js"></script>
<script src="https://npmcdn.com/redux-saga@0.9.1/dist/redux-saga.js"></script>
</head>
<body>
<p>
Example created to compare Redux + redux-saga implementation with
@yelouafi
yelouafi / redux-actions-spec.js
Last active February 3, 2016 15:39
redux actions specs
const empty = {}
const ktrue = () => true
const contract = p => (v, field) => {
if(p(v, field))
return v
throw `Invalid value ${v} for field ${field}`
}
// builtin validators
import { createStore, applyMiddleware } from 'redux'
import sagaMiddleware, { runSaga, storeIO, take, put } from 'redux-saga'
///////////////////////////////////////////////////////////////////
//
// Sagas
//
@yelouafi
yelouafi / FP_Observables.js
Last active March 1, 2021 05:53
Observables with pure FP
// Observable is an Union Type, with the following variants
const Empty = () => ['EMPTY']
const Cons = (head, tail) => ['CONS', head, tail]
const Future = promise => ['FUTURE', promise]
// race between 2 promises; each promise will resolve to a lazy value
const lazyRace = (p1, p2) => Promise.race([p1,p2]).then(lazy => lazy())
// function composition
const compose = (...fns) => (arg) => fns.reduceRight((res, f) => f(res), arg)
/** @jsx html */
import { html } from 'snabbdom-jsx';
import Type from 'union-type';
const Action = Type({
Increment : [],
Decrement : [],
});
function init() {
return { nextID: 1, counters: [] };
}
main(
twoCounters.init(), // the initial state
document.getElementById('placeholder'),
twoCounters
);
function update(model, action) {
return action.type === RESET ?
{
first : counter.init(),
second: counter.init()
}
: action.type === UPDATE_FIRST ?
{...model, first : counter.update(model.first, action.data) }
function init() {
return { first: counter.init(), second: counter.init() };
}