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
@jfet97
jfet97 / ReadableStreamAsyncIteratorPrototype.js
Last active September 13, 2019 12:52
ReadableStreamAsyncIteratorPrototype
const ReadableStreamAsyncIteratorPrototype = Object.create(AsyncIteratorPrototype, {
stream: {
get() {
return this[kStream];
},
enumerable: true,
configurable: true
},
next: {
@jfet97
jfet97 / test.js
Created February 6, 2020 09:11
AsyncIterators RxJS
const ticker = interval(1000).pipe(take(100));
function snooze(ms) {
return new Promise(resolve => setTimeout(snooze, ms));
}
async function* values() {
for await (const number of ticker) {
await snooze(2000);
yield number;
@jfet97
jfet97 / destroyTS#PrivateVars.ts
Created March 7, 2020 11:59
An code example taht allow us to break the #private restrictions thanks to the 'this' magic
class ConstNumber {
#privateNumber: number;
#dfbsdfbv =4235;
showCallback: (value: number) => void;
constructor(privateNumber: number, showCallback: (value: number) => void) {
this.#privateNumber = privateNumber;
this.showCallback = showCallback;
}
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",
@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
@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 / 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 / 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 / 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 / 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))
)
}