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 / 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 / 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 / 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 / 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 / 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 / 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
const scope = callback => ({
with: Function(`with(arguments[0])return(${callback})()`)
});
scope(() => ['a', test, 'c']).with({test: 'b'});
// ["a", "b", "c"]