Skip to content

Instantly share code, notes, and snippets.

View nagyadam2092's full-sized avatar
🤔

Adam Nagy nagyadam2092

🤔
View GitHub Profile
@nagyadam2092
nagyadam2092 / infinite-streams.js
Last active March 6, 2023 06:54
JavaScript infinite streams
const stream = (value, next = null) => ({
map: function(f) {
return stream(f(value), next ? next.map(f) : null)
},
filter: function(pred) {
if (pred(value)) {
return stream(value, next ? next.filter(pred) : null)
} else {
return next ? next.filter(pred) : emptyStream
export function cancellable<A>(
p: Promise<A>,
ok: (a: A) => void,
err: (e: Error) => void,
fin?: () => void
) {
let cancelled = false;
p.then((v) => cancelled || ok(v))
.catch((e) => cancelled || err(e))
.finally(() => cancelled || fin?.());
@nagyadam2092
nagyadam2092 / deepdiff.ts
Created November 10, 2021 07:49
Object deep diff comparison
var deepDiffMapper = function () {
return {
VALUE_CREATED: 'created',
VALUE_UPDATED: 'updated',
VALUE_DELETED: 'deleted',
VALUE_UNCHANGED: 'unchanged',
map: function(obj1, obj2) {
if (this.isFunction(obj1) || this.isFunction(obj2)) {
throw 'Invalid argument. Function given, object expected.';
}
var Id = v => ({v: v,map: f => Id(f(v)) });
var CoFreeF = (a, next) => ({
a: a,
next: next,
extract: () => a,
map: g => CoFreeF(g(a), () => next().map(x=>x.map(g))),
});
var f = n => CoFreeF(n + 1, () => Id(f(n + 1)));
var Id = v => ({v: v,map: f => Id(f(v)) });
var CoFreeF = (a, next) => ({
a: a,
next: next,
extract: () => a,
map: g => CoFreeF(g(a), () => next().map(x=>x.map(g))),
});
@nagyadam2092
nagyadam2092 / dynamic.promise.ts
Created April 10, 2021 07:02
Conditional/dynamic array promise all
interface DynamicPromiseResult {
data: unknown;
promise: string;
}
const promises: Array<Promise<DynamicPromiseResult>> = []
if (true) { // some logic
const promise1 = fetch('https://jsonplaceholder.typicode.com/posts/1').then(data => ({ data, promise: 'promise1' }));
promises.push(promise1);
}
const movieLists = [
// ...
];
const boxartSizeRestrictions = boxart =>
boxart.width === 150 && boxart.height === 200;
const videoBoxartMapper = (boxart, video) => ({
id: video.id,
title: video.title,
var movieLists = [
{
name: "Instant Queue",
videos: [
{
id: 70111470,
title: "Die Hard",
boxarts: [
{
width: 150,
var movieLists = [{
name: "Instant Queue",
videos: [{
"id": 70111470,
"title": "Die Hard",
"boxarts": [{
width: 150,
height: 200,
url: "http://cdn-0.nflximg.com/images/2891/DieHard150.jpg"
},
@nagyadam2092
nagyadam2092 / flappy.html
Created November 30, 2020 09:15 — forked from gullyn/flappy.html
Flappy bird in 228 bytes (improved!)
<body onload="z=c.getContext`2d`,p=Y=Q=',9|z.fillRect(',setInterval(`c.height=W=300,p?Y<W&&Y>P&Y<P+E|p>9?z.fillText(S++,0${Q}0,Y-=M-=.5,9${Q}p-=8,0${Q}p,P+E,9,W),P))):(p=M=S=0,Y=E=99):(p=W+4,P=S%E)`,24)"onclick=M=9><canvas id=c>