Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Just coding

Kyle Simpson getify

💭
Just coding
View GitHub Profile
@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 Jul 5, 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 Jun 12, 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 Jan 31, 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!
@getify
getify / 1-click-to-load.js
Last active Dec 22, 2021 — forked from cowboyd/1-click-to-load.js
Handle a stream of clicks by canceling the latest one
View 1-click-to-load.js
// 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 Jun 30, 2022
transducing even with reducers
View 1.js
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;
View 1.js
function Article({ id }) {
const [article, setArticle] = useState(null);
useEffect(() => {
let cancelToken = new CAF.cancelToken();
let fetchData = CAF(function *fetchData(signal) {
const article = yield API.fetchArticle(id);
setArticle(article);
});
@getify
getify / 1.html
Last active Jun 30, 2022
Ever noticed how vw/vh units in CSS seem to be a bit unreliable on various devices (especially mobile)? Here's my solution.
View 1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<title>Test Page</title>
<script>
// early compute the vw/vh units more reliably than CSS does itself
computeViewportDimensions();
@getify
getify / 1.js
Last active Jun 30, 2022
Converting English number sentences ("one hundred forty two point three") to numeric digits ("142.3")
View 1.js
convert("one hundred five"); // "105"
convert("six hundred and fifty three"); // "653"
convert("zero zero one two three"); // "123"
convert("twelve o three"); // "1203"
convert("thirteen zero nine"); // "1309"
convert("fifteen sixteen"); // "1516"
convert("fourteen ninety two"); // "1492"
convert("nineteen ten"); // "1910"
convert("twelve hundred"); // "1200"
convert("twenty three hundred"); // "2300"
@getify
getify / mass-linked-in-invitation.js
Last active Jun 30, 2022
mass accept/reject of linked-in invitations
View mass-linked-in-invitation.js
processInvitations().catch(console.log);
function delay(ms) { return new Promise(res => setTimeout(res,ms)); }
async function processInvitations() {
var cards = [...document.querySelectorAll("li.invitation-card")];
var acceptCards = cards.filter(card => {
var title = (card.querySelectorAll(".invitation-card__subtitle")[0] || {}).innerHTML || "";
return accept(title);