Skip to content

Instantly share code, notes, and snippets.

@souporserious
souporserious / build.mjs
Created February 24, 2022 02:48
Build script using esbuild and ts-morph to bundle library code.
import glob from 'fast-glob'
import { build } from 'esbuild'
import { Project } from 'ts-morph'
const project = new Project({
compilerOptions: {
outDir: 'dist',
emitDeclarationOnly: true,
},
tsConfigFilePath: './tsconfig.json',
@ClickerMonkey
ClickerMonkey / types.ts
Last active February 6, 2024 07:21
Typescript Helper Types
// when T is any|unknown, Y is returned, otherwise N
type IsAnyUnknown<T, Y, N> = unknown extends T ? Y : N;
// when T is never, Y is returned, otherwise N
type IsNever<T, Y = true, N = false> = [T] extends [never] ? Y : N;
// when T is a tuple, Y is returned, otherwise N
// valid tuples = [string], [string, boolean],
// invalid tuples = [], string[], (string | number)[]
/* So how does this work?
I'm using ANSI escape sequences to control the behavior of the terminal while
cat is outputting the text. I deliberately place these control sequences inside
comments so the C++ compiler doesn't try to treat them as code.*/
//
/*The commands in the fake code comment move the cursor to the left edge and
clear out the line, allowing the fake code to take the place of the real code.
And this explanation uses similar commands to wipe itself out too. */
//
#include <cstdio>
// Log everything
function logAllCalls(ClassType) {
for (const k in ClassType.prototype) {
try {
if (typeof ClassType.prototype[k] !== "function") {
continue;
}
}
catch(err) {
@naugtur
naugtur / GetOptimizationStatus.md
Created August 9, 2019 21:08 — forked from justjavac/GetOptimizationStatus.md
V8 %GetOptimizationStatus

%GetOptimizationStatus return a set of bitwise flags instead of a single value, to access the value, you need to take the binary representation of the returned value. Now, for example, if 65 is returned, the binary representation is the following:

(65).toString(2).padStart(12, '0');
// 000001000001

Each binary digit acts as a boolean with the following meaning:

@briancavalier
briancavalier / typescript-strongly-typed-variadic-2.md
Last active October 12, 2023 18:34
A technique for strongly-typed, heterogeneous variadic function types in Typescript

Simpler Variadic Function Types in TypeScript (part 2): Higher-order Variadic Functions

In part 1 we looked at a technique that allows expressing strongly-typed variadic functions in a way that solves problems with the current most common approach. It reduces code size and repetition, and it scales to any arity.

I mentioned that the technique can also be extended to higher-order variadic functions, such as the typical zipWith. Let’s explore how to do that.

ZipWith Example

As we did in part 1, let’s start with an example, a typical zipWith on arrays.

@briancavalier
briancavalier / typescript-strongly-typed-variadic-1.md
Last active October 12, 2023 18:34
A technique for strongly-typed, heterogeneous variadic function types in Typescript

Simpler Variadic Function Types in TypeScript (part 1)

Variadic functions, such as the common zip function on arrays, are convenient and remove the need for lots of specific arity-function variants, e.g., zip2, zip3, zip4, etc. However, they can be difficult and tedious to type correctly in TypeScript when the return type depends on the parameter types, and the parameter types are heterogeneous.

Zip Example

Given a typical zip on arrays:

const a: number[] = [1, 2, 3]
@KSXGitHub
KSXGitHub / tuple-utils.ts
Last active September 12, 2018 11:23
Tuple generic
export namespace TupleUtils {
/**
* Get type of first element
* @example `First<[0, 1, 2]>` → `0`
*/
export type First<Tuple extends [any, ...any[]]> = Tuple[0]
/**
* Get type of last element

Falsehoods programmers believe about prices

  1. You can store a price in a floating point variable.
  2. All currencies are subdivided in 1/100th units (like US dollar/cents, euro/eurocents etc.).
  3. All currencies are subdivided in decimal units (like dinar/fils)
  4. All currencies currently in circulation are subdivided in decimal units. (to exclude shillings, pennies) (counter-example: MGA)
  5. All currencies are subdivided. (counter-examples: KRW, COP, JPY... Or subdivisions can be deprecated.)
  6. Prices can't have more precision than the smaller sub-unit of the currency. (e.g. gas prices)
  7. For any currency you can have a price of 1. (ZWL)
  8. Every country has its own currency. (EUR is the best example, but also Franc CFA, etc.)
@wo0dyn
wo0dyn / gist:4113739
Created November 19, 2012 20:41
GitHub HOW-TO “Resync a fork from original repo”

GitHub HOW-TO “Resync a fork from original repo”

I forked a repo dzen/omgcats a couple weeks ago on duboisnicolas/omgcats. I missed some commits so I need to resynchronize dzen/omgcats on duboisnicolas/omgcats.

$ git clone git://github.com/duboisnicolas/omgcats.git
$ cd omgcats 
# New branch for latest commits:
$ git checkout -b upstream/master
# Add new remote upstream from original repo: