Skip to content

Instantly share code, notes, and snippets.

View getify's full-sized avatar
💭
Just coding

Kyle Simpson getify

💭
Just coding
View GitHub Profile
@getify
getify / 1-setup.js
Last active December 15, 2023 13:25
find size of largest region in matrix... solutions are breadth-first iterative (2) and depth-first recursive (3)
// Adapted from: https://www.geeksforgeeks.org/find-length-largest-region-boolean-matrix/
"use strict";
var M1 = [
[ 0, 0, 1, 1, 0 ],
[ 1, 0, 1, 1, 0 ],
[ 0, 1, 0, 0, 0 ],
[ 0, 0, 0, 1, 1 ]
];
@getify
getify / 1-utils.js
Last active June 3, 2023 23:38
illustrating some "dynamic composition" usage
function flow(...fns) {
return arg => fns.reduce((res,fn) => fn(res),arg);
}
function partial(fn,initialArgs) {
return (nextArgs = []) => fn(...initialArgs,...nextArgs);
}
function pick(prop) {
return obj => obj[prop];
}
function prepend(prefix) {
@getify
getify / 1-CalendarItem.js
Last active March 21, 2024 09:11
an illustration (non-trivial example) of many newer JS class features
// abstract class, not intended to be instantiated directly
class CalendarItem {
static #UNSET = Symbol("unset")
static #isUnset(v) {
return v === this.#UNSET;
}
static {
for (let [idx,msg] of [
"ID is already set.",
"ID is unset.",
@getify
getify / 1-works-fine.js
Last active July 16, 2022 18:22
strange inconsistency (between members and statics) with privates+subclassing
class Point2d {
#ID = null
constructor(id) {
this.#ID = id;
}
getID() {
return this.#ID;
}
}
@getify
getify / 1.md
Last active July 3, 2022 12:29
Part 2 of 2, of "In defense of blocks for local scopes", from https://gist.github.com/getify/712d994419326b53cabe20138161908b

In defense of blocks for local scopes (Part 2)

Some have leveled the criticism that "part 1" of this post is unnecessarily contriving a problem that doesn't really exist in "good code" -- if you really need to narrow a scope of some declarations in a function, then that function is too complex already and that bigger problem is what you need to fix.

Just to be extra clear: if a chunk of code is already reasonable to make a function, that's not the sort of code I'm talking about in either of these blog posts. If it's being called multiple times, or if it's completely independent and makes sense as its own logical chunk, make it a function. I'm talking about a different sort of code, a set of a few statements related to each other, that declare one or more variables, but which logically still belong inside another function. That's the context here.

OK, let's stop talking about this stuff abstractly.

A Real Example

@getify
getify / 1.md
Last active March 2, 2023 21:24
In defense of using blocks to create localized scope for variables... (part 1 of 2)
@getify
getify / why-typl-instead-of-ts.md
Last active July 20, 2022 18:26
describing my motivations for designing TypL instead of using TS/Flow

I am often asked why I don't like a tool like TS (or Flow), and by implication, if I don't like it, the assumption is that I don't want any type aware tooling. That's not true, though.

I have many ideas for what I think type-aware tooling in JS could/should be, and they just happen to diverge significantly enough from TS/Flow that I can't bend into pretzels to fit into that mold.

Instead, I've worked on designing a tool I call TypL that I think addresses my concerns. It's in progress.

Here's my main list of motivations for designing TypL instead of TS/Flow:

  1. I want a system that has both compile-time checking AND run-time checking. TypL's design is to compile away the stuff that it checks at compile time and can verify, and leave in the stuff that it knows needs run-time checking. That way, you don't have to write different sorts of type checking for compile-time and run-time. You get both from one set of typing annotation. It doesn't really seem tha
@getify
getify / simplest-monad.js
Last active July 20, 2022 19:18
what's the simplest monad implementation we could express in JS?
function Identity(v) {
return { val: v };
}
function chain(m,fn) {
return fn(m.val);
}
// ^^^ that's it!
@getify
getify / 1-click-to-load.js
Last active December 22, 2021 06:44 — forked from cowboyd/1-click-to-load.js
Handle a stream of clicks by canceling the latest one
// FORKED FROM: https://gist.github.com/cowboyd/74c0fd9e7aa3cccaeda0b84604c0136a
//
// using CAF instead of Effection.
/**
* Implements an operation to fetch the data as described in https://twitter.com/BenLesh/status/1455597411385098242
* The "winner" is unambiguous. It is the request corresponding to the latest click, guaranteed.
* Other important things to note:
*
* 1. The HTTP request is cancelled every time along with its containing task
@getify
getify / 1.js
Last active July 20, 2022 19:20
transducing even with reducers
var filterReducer = pf => cf => (acc,v) => pf(v) ? cf(acc,v) : acc;
var mapReducer = mf => cf => (acc,v) => cf(acc,mf(v));
var compose = (...fs) => v => fs.reduceRight((r,f) => f(r),v);
var concat = (acc,v) => [ ...acc, v ];
var sum = (x,y) => x + y;
// ************************************
var onlyOdds = v => v % 2 == 1;
var double = v => v * 2;