Skip to content

Instantly share code, notes, and snippets.

Avatar
❤️
Loving programming

Andrea Simone Costa jfet97

❤️
Loving programming
View GitHub Profile
View f-algebra_of_lists.js
// cata alg = alg . fmap(cata alg) . unfix
function cata(alg, FixF) {
return (i) => alg(FixF.fmap(cata(alg, FixF))(FixF.unfix(i)))
}
// ListF a x = NilF | ConsF a x
// where 'a' is the type of the values inside the list,
// whereas 'x' is the type on which the list will be evaluated (aka the carrier type)
const NilF = () => ({
_tag: "NilF"
@jfet97
jfet97 / DFS.js
Last active Jul 1, 2021
Simple Depth First Search in JavaScript
View DFS.js
function isObject(entity) {
return typeof entity === "object" && entity !== null;
}
function getAdjacentNodes(obj) {
return (
Object.entries(obj)
.filter(([, v]) => isObject(v))
)
}
View asyncArray.js
function createAsyncArray(...values) {
return values.map((v) => ({
resolver: () => {},
promise: Promise.resolve(v),
}));
}
function get(a, i) {
if (a[i] == void 0) {
let resolver = () => {};
View scope.js
const scope = callback => ({
with: Function(`with(arguments[0])return(${callback})()`)
});
scope(() => ['a', test, 'c']).with({test: 'b'});
// ["a", "b", "c"]
@jfet97
jfet97 / parser_combinators.hs
Created Dec 24, 2020
Parser combinators in Haskell
View parser_combinators.hs
import Control.Applicative
import Data.Char
newtype Parser a = P (String -> [(a, String)])
parse :: Parser a -> String -> [(a, String)]
parse (P p) s = p s
instance Functor Parser where
@jfet97
jfet97 / ambient.js
Last active Dec 5, 2020
Functional Ambient in JS
View ambient.js
function bind(ambient, name, value) {
return function lookup(n) {
if(n == name) {
return value
} else {
return ambient(n)
}
}
}
@jfet97
jfet97 / clone.js
Last active May 9, 2021
Using graph theory to clone JavaScript objects with circular references
View clone.js
function isObject(entity) {
return typeof entity === "object" && entity !== null;
}
function cloneNonObjectProperties(obj) {
return Object.fromEntries(
Object.entries(obj).filter(([, v]) => !isObject(v))
)
}
@jfet97
jfet97 / abstract_machine.hs
Created Jul 5, 2020
A simple abstract machine to evaluate simple expression using a control stack
View abstract_machine.hs
data Expr = Val Int | Add Expr Expr | Mul Expr Expr deriving (Show)
data Op = EVALA Expr | ADD Int | EVALM Expr | MUL Int
type Cont = [Op]
eval :: Expr -> Cont -> Int
eval (Val n) c = exec c n
eval (Add e f) c = eval e (EVALA f : c)
eval (Mul e f) c = eval e (EVALM f : c)
@jfet97
jfet97 / IO.js
Last active May 6, 2021
Basic monadic IO implementation
View IO.js
const IO = (function(io) {
io.return = x => () => x;
io.unit = io.return;
io.of = io.return;
io.bind = a => fa => () => fa(a())();
io.flatten = aa => io.bind(aa)(a => io.bind(a)(ar => io.return(ar)));
io.join = io.flatten
io.takeLeft = a => b => io.bind(a)(ar => io.bind(b)(_ => io.return(ar)));
io.takeRight = a => b => io.bind(a)(_ => b);
@jfet97
jfet97 / ProprietàPrivate.js
Last active Apr 13, 2020
ES6, niente this, niente weakmap
View ProprietàPrivate.js
const Point = (x = 0, y = 0) => ({
setCartesianCoordinates(_x, _y) {
x = _x; y = _y;
},
setPolarCoordinates(radius, angle) {
x = radius * Math.cos(angle);
y = radius * Math.sin(angle);
},
getCartesianCoordinates() {
return { x, y }