Skip to content

Instantly share code, notes, and snippets.

View abiodun0's full-sized avatar

Abiodun abiodun0

View GitHub Profile
@abiodun0
abiodun0 / freemonad.js
Created April 7, 2019 11:10
freeMonad with daggy
const daggy = require('daggy')
const Free = daggy.taggedSum({Impure: ['x', 'f'], Pure: ['x']})
Free.of = Free.Pure
const kleisli_comp = (f, g) => x => f(x).chain(g)
Free.prototype.fold = function() {
return this.x.fold.apply(this.x, arguments)
@abiodun0
abiodun0 / minbinaryHeap.hs
Last active January 2, 2023 01:55
Binary Heap, Haskell and JS
module BinaryHeap(empty, insert, minimum, extractMin) where
import Prelude hiding (minimum)
data BinHeap a = Empty
| Node Bool (BinHeap a) a (BinHeap a)
deriving (Show)
empty :: BinHeap a
empty = Empty
@abiodun0
abiodun0 / rbt.hs
Created March 4, 2019 06:02
Red black tree, Haskell
module RedBlackTree
(
Tree,
empty,
member,
insert)
where
data Color = R | B deriving Show
@abiodun0
abiodun0 / pearno.js
Last active April 18, 2022 11:26
Church Encoding / Peano Numbers
const zero = () => false;
const isZero = (a) => a == zero();
const succ = (a) => () => a;
const pred = (a) => a();
const converToPeano = (n, acc = zero()) => n === 0 ? acc : converToPeano(n - 1, succ(acc))
const _0 = zero(),
_1 = succ(_0),
@abiodun0
abiodun0 / avl.hs
Last active December 16, 2020 14:02
Balancing trees with javascript and haskell
module AVLTree where
data BT = L | N Int BT BT deriving (Show, Eq)
-- utility functions
empty = L
depth L = 0
depth (N _ t u) = (max (depth t) (depth u)) + 1
inorder :: BT -> [Int]
@abiodun0
abiodun0 / ThinkAboutMonads.md
Created February 17, 2019 12:48 — forked from cscalfani/ThinkAboutMonads.md
How to think about monads

How to think about Monads

Initially, Monads are the biggest, scariest thing about Functional Programming and especially Haskell. I've used monads for quite some time now, but I didn't have a very good model for what they really are. I read Philip Wadler's paper Monads for functional programming and I still didnt quite see the pattern.

It wasn't until I read the blog post You Could Have Invented Monads! (And Maybe You Already Have.) that I started to see things more clearly.

This is a distillation of those works and most likely an oversimplification in an attempt to make things easier to understand. Nuance can come later. What we need when first learning something is a simple, if inaccurate, model.

This document assumes a beginner's knowledge of pure functional programming and Haskell with some brief encounters of Monads, e.g. [Functors, Applicatives, And

@abiodun0
abiodun0 / abstractions.js
Created February 2, 2019 05:17
fundamental abstractions, setters, getters, iterators
// Abstraction
// Function
const x = (x) => x * x
// getter
const getter = () => 1
// example
const getObject = () => ({
name: 'abiodun'
})
@abiodun0
abiodun0 / uncurry_sum.js
Created January 19, 2019 05:06
some neat tricks
const uncurry = fn => (...args) => args.reduce((acc, arg) => acc(arg), fn)
const add = x => y => x + y
const sum = xs => xs.reduce((x, y) => uncurry(add)(x, y))
sum([1, 2, 3])
// -> 6
const R = require('ramda')
const initObject = {
"gold_bid_usd_toz": "1231.66",
"gold_ask_usd_toz": "1232.66",
"gold_change_dollar_usd_toz": "9.16",
"gold_change_percent_usd_toz": "0.74%",
"gold_high_usd_toz": "1232.21",
"gold_low_usd_toz": "1221.41",
"zara_high_credit": "374"
}
@abiodun0
abiodun0 / adt.js
Created November 7, 2018 22:19
Some Adts
const uncurry = f => args => args.reduce((p, c) => p(c), f)
const curry = n => f => {
const rec = a => acc => (a === 0 ? f(acc) : x => rec(a - 1)([...acc, x]))
return rec(n)([])
}
const mapWithKey = f => o =>
Object.keys(o).reduce((p, k) => ({ ...p, [k]: f(k)(o[k]) }), {})