Skip to content

Instantly share code, notes, and snippets.

View willfrew's full-sized avatar

Will Frew willfrew

  • Bucharest, Romania
View GitHub Profile

Keybase proof

I hereby claim:

  • I am willfrew on github.
  • I am willfrew (https://keybase.io/willfrew) on keybase.
  • I have a public key ASA4IO6Vi08zXIQLyvZsh820Df09RbzO7jfpx3-UWMhYego

To claim this, I am signing this object:

@willfrew
willfrew / Getters.md
Created September 16, 2020 08:04
Getters

The Problem with get

Ever since the get and set keywords came to Javascript / Typescript, I've felt uneasy about them. Something about their nature of hiding computation in what appears to be data never really sat well with me. In some projects, I knee-jerk banned them with lint rules but unfortunately I never really thought deeply about why.

Let's start with some assertions:

  1. When you're reading through a piece of code, you are mentally executing the program. Symbolically at first and with specific inputs while debugging.
  2. When you're reading through a piece of code that is new to you, in order to read and mentally execute a piece of code without reading the entire codebase, you need to be able rely on interfaces & type definitions.
  3. When working in a team, it is natural that you will frequently arrive at parts of the codebase that you have not worked on before or have been modified since you last read the code. As the codebase and team
@willfrew
willfrew / basePi.ts
Created June 2, 2020 13:13
Some fun with arbitrary base numbers (with specific tests for base PI)
describe('convert number to arbitrary base-system coefficients', () => {
const truncateToFixed = (num: number, maxFractionalDigits: number): number => {
const multiplier = Math.pow(10, maxFractionalDigits);
return Math.floor(num * multiplier) / multiplier;
}
const toBaseCoefficients = (num: number, base: number): (number | '.')[] => {
const maxFractionalDigits = 10;
let coefficients = [];
@willfrew
willfrew / keybase.md
Created August 25, 2018 14:38
keybase.md

Keybase proof

I hereby claim:

  • I am willfrew on github.
  • I am willfrew (https://keybase.io/willfrew) on keybase.
  • I have a public key ASBJjD93Mb1407x1aVeeirygx_feof8WOAsu8Rmlwanc1go

To claim this, I am signing this object:

@willfrew
willfrew / tuples.ts
Created August 15, 2018 12:53
Fun with tuple types in Typescript 3.0
type Head<T extends unknown[]> = T[0];
type FnWithArgs<T extends unknown[]> = (...args: T) => void;
type TailArgs<T> = T extends (x: unknown, ...args: infer T) => unknown ? T : never;
type Tail<T extends unknown[]> = TailArgs<FnWithArgs<T>>;
// Lol
type Decr<T extends number> =
T extends 10 ? 9 :
T extends 9 ? 8 :
@willfrew
willfrew / host.html
Created June 27, 2018 16:07
Iframe caching test
<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
(function() {
// 1 or 2
let i = Math.round(Math.random()) + 1
@willfrew
willfrew / example.ts
Last active September 6, 2017 15:53
Typescript 2.4.0+ Stack overflow
export interface Foo<T> {
(bar: Bar<T>): void;
}
export interface Bar<T> {
(foo: Foo<T>): Foo<T>;
}
function createBar<T>(): Bar<T> {
return (f) => f;
@willfrew
willfrew / ansible-aur-pkg-installer.md
Last active December 5, 2016 21:56
download, build, and install aur packages with ansible