Skip to content

Instantly share code, notes, and snippets.

View abiodun0's full-sized avatar

Abiodun abiodun0

View GitHub Profile
@abiodun0
abiodun0 / maybe.ts
Created May 8, 2017 09:16
Monads in Typescript
interface Functor {
fmap: (any) => any;
}
interface Monad extends Functor {
bind: (any) => Monad;
}
interface Maybe extends Monad {
}
@abiodun0
abiodun0 / test.js
Created May 17, 2017 01:34
Immutability in javascript with numbers
// That’s a slightly different thing. Number in JS are always immutable already
let a = 4;
function getA() {
return a++
}
let v1 = getA() // v1 = 4
let v2 = getA() // v1 = 4, v2 = 5
let v3 = getA() // v1 = 4, v2 = 5, v3 = 6
// Because the value of the number doesn’t change in place.
@abiodun0
abiodun0 / endofunctor.hs
Created May 19, 2017 21:02
Endo Functors and Monads
instance Monoid (Endofunctor a) where
append :: (a -> m a) -> (a -> m a) -> (a -> m a)`
append = (>=>)
empty :: (a -> m a)
empty = return
@abiodun0
abiodun0 / pairs.cls
Last active May 28, 2017 22:39
pairs [1,2,3,4,5] => [[1,2], [2,3], [3,4], [4,5]]
(partition 2 1 [1, 2, 3, 4, 5]) ;;=> ((1 2) (2 3) (3 4) (4 5))
@abiodun0
abiodun0 / unfold.js
Last active June 3, 2017 16:24
unfold
const unfold = (f, seed) => {
const go = (f, seed, acc) => {
const res = f(seed);
return res ? go(f, res[1], acc.concat([res[0]])) : acc
}
return go(f, seed, []);
}
unfold(x => x < 26 ? [String.fromCharCode(x+65), x + 1] : undefined, 0) // Alphabets in capital letter
const range = (begin, end) => {
@abiodun0
abiodun0 / linkedlist.js
Last active June 13, 2017 05:50
linkedList and tress functional
const Nil = {};
function _LinkedList(h, t) {
this.head = h;
this.tail = t;
}
const LinkedList = (h, t) => new _LinkedList(h, t);
const newLinkedList = LinkedList(5, LinkedList(7, LinkedList(10, Nil)))
import React from 'react';
import User from './User';
import { connect } from 'react-redux';
import { fetchUsers } from '../actions/index';
import { lifecycle, compose } from 'recompose';
const enhance = compose(
lifecycle({
componentDidMount() {
this.props.fetchUsers();
@abiodun0
abiodun0 / symbolMap.js
Last active June 22, 2017 04:38
withingFuncton
const run = Symbol('run');
const map = Symbol('map');
const ask = Symbol('ask');
const of = Symbol('of');
const flatmap = Symbol('flatmap');
Function[of] = function(x) { return _ => x; };
Function.prototype[flatmap] = function(f) {
return s => f(this(s))(s);
@abiodun0
abiodun0 / infinitListJavscript.js
Created June 22, 2017 04:57
InfinitList javascript
//repeat :: a -> [a]
var repeat = function(a){
return [a].concat( repeat(a) );
}
// take :: Int -> [a] -> [a]
var take = function(n, a){
if(a.length == 0 || n <= 0 ) return [];
return a[0].concat( take( n - 1, a.slice(1) ) );
}
@abiodun0
abiodun0 / usingIoTS.js
Last active June 26, 2017 07:23
fp-ts-io_fantasy
import { IO } from 'fp-ts/lib/IO'
import * as io from 'fp-ts/lib/IO'
import * as array from 'fp-ts/lib/Array'
import { sequence } from 'fp-ts/lib/Traversable'
const add = (classes: string) => (sel: HTMLElement): IO<HTMLElement> => {
return new IO(() => {
sel.classList.add(classes)
return sel
})