Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Just coding

Kyle Simpson getify

💭
Just coding
View GitHub Profile
@getify
getify / 1-post.md
Last active Nov 29, 2022
Comparing: array method chaining, generator delegations, and transducing
View 1-post.md

Comparing: array method chaining, generator delegations, and transducing

I'm writing this quick post to respond to a recent twitter conversation where claims were made about the merits (or lack thereof) of transducers, as they relate to composing list comprehensions (map, filter).

For comparison sake throughout the rest of my post, below you'll find three (actually four!) implementations of a simple list operation demo:

View bfs.java
// this is an experimental Foi (https://github.com/getify/foi-lang) implementation of
// the BFS solution to https://gist.github.com/getify/59ab7443723564eb40d20ab7c45d5f0a
import size, log from #Std;
def M1: <
< 0, 0, 1, 1, 0 >,
< 1, 0, 1, 1, 0 >,
< 0, 1, 0, 0, 0 >,
< 0, 0, 0, 1, 1 >
@getify
getify / 1-setup.js
Last active Oct 11, 2022
find size of largest region in matrix... solutions are breadth-first iterative (2) and depth-first recursive (3)
View 1-setup.js
// 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 Aug 20, 2022
illustrating some "dynamic composition" usage
View 1-utils.js
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 Aug 4, 2022
an illustration (non-trivial example) of many newer JS class features
View 1-CalendarItem.js
// 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 Jul 16, 2022
strange inconsistency (between members and statics) with privates+subclassing
View 1-works-fine.js
class Point2d {
#ID = null
constructor(id) {
this.#ID = id;
}
getID() {
return this.#ID;
}
}
@getify
getify / 1.md
Last active Jul 3, 2022
Part 2 of 2, of "In defense of blocks for local scopes", from https://gist.github.com/getify/712d994419326b53cabe20138161908b
View 1.md

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 Oct 14, 2022
In defense of using blocks to create localized scope for variables... (part 1 of 2)
View 1.md
@getify
getify / why-typl-instead-of-ts.md
Last active Jul 20, 2022
describing my motivations for designing TypL instead of using TS/Flow
View why-typl-instead-of-ts.md

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 Jul 20, 2022
what's the simplest monad implementation we could express in JS?
View simplest-monad.js
function Identity(v) {
return { val: v };
}
function chain(m,fn) {
return fn(m.val);
}
// ^^^ that's it!