Skip to content

Instantly share code, notes, and snippets.

View jfet97's full-sized avatar
🖤
Loving programming

Andrea Simone Costa jfet97

🖤
Loving programming
View GitHub Profile
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 December 24, 2020 15:23
Parser combinators in Haskell
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 December 5, 2020 23:30
Functional Ambient in 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 19:33
Using graph theory to clone JavaScript objects with circular references
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 July 5, 2020 08:08
A simple abstract machine to evaluate simple expression using a control stack
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 19:08
Basic monadic IO implementation
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 April 13, 2020 17:14
ES6, niente this, niente weakmap
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 }
@jfet97
jfet97 / MappaTipi.ts
Last active July 26, 2020 14:19
MappaTipi.ts
export interface Message {
status: string;
query: string;
step: string;
richResponse: boolean;
payload: Types extends (any) ? Payload<Types> : never;
sessionId?: number;
}
type Types = 'text' | 'date' | 'articles' | 'params';
@jfet97
jfet97 / FuzzBuzz.ts
Created April 2, 2020 15:04
TS is turing complete!
// https://gal.hagever.com/posts/typing-the-technical-interview-in-typescript/
type Eq<A, B extends A> = "passes"; // B <= A
// type test_eq = [
// Eq<"Hello", "Hello">, // passes
// Eq<"Hello", "world"> // fails with "'world' does not satisfy constraint 'Hello'"
// ];
type NAN = "invalid number"; // we don't support negative numbers
type ActionsTypesExample = "INIT" | "SYNC" | "LOG_IN" | "LOG_IN_SUCCESS" | "LOG_OUT"
type SafeKeyValueMapExample<KS extends keyof any> = {
readonly [K in KS]: K;
};
const ACTIONS_TYPES_MAP_EXAMPLE: SafeKeyValueMapExample<ActionsTypesExample> = {
INIT: "INIT",
SYNC: "SYNC",
LOG_IN: "LOG_IN",