Skip to content

Instantly share code, notes, and snippets.

View dphilipson's full-sized avatar

David Philipson dphilipson

View GitHub Profile
@dphilipson
dphilipson / PackStatistics.java
Created April 26, 2016 21:20
Computes expected number of packs required to complete a portion of your collection in Hearthstone
package me.dphil.statistics
import java.util.Random;
import java.util.concurrent.TimeUnit;
/**
* Many people have written this before and many will write it again. I
* wrote it because it was faster than looking for someone else's solution.
*/
public class PackStatistics {
// ----- Definition -----
class SafeMap<V> {
private readonly o: { [key: string]: V } = {};
public put(key: string, value: V): void {
this.o[key] = value;
}
public has<K extends string>(key: K): this is SafeMapWithKey<K, V> {
@dphilipson
dphilipson / dateString.ts
Created December 12, 2017 22:46
Branded strings that represent dates in TypeScript.
enum DateStringBrand {}
export type DateString = string & DateStringBrand;
function of(date: string | number | Date): DateString {
// This branching needed to satisfy TypeScript.
const dateObject =
typeof date === "string"
? new Date(date)
: typeof date === "number" ? new Date(date) : new Date(date);
@dphilipson
dphilipson / redoodleSagas.ts
Created February 28, 2018 22:33
Combine redux-saga with Redoodle for marginally better static typing
import { TypedAction, TypedActionString } from "redoodle";
import {
ForkEffect,
takeEvery,
takeLatest,
throttle,
} from "redux-saga/effects";
type HelperFunc0<A> = (action: A) => any;
type HelperFunc1<A, T1> = (arg1: T1, action: A) => any;
@dphilipson
dphilipson / functionArityOptimization.ts
Created June 15, 2018 20:18
Benchmarks demonstrating interesting properties of optimization around function arity in JavaScript
import Benchmark from "benchmark";
const array: number[] = [];
for (let i = 0; i < 100000; i++) {
array.push(i);
}
function map<T, U>(arr: T[], f: (item: T) => U): U[] {
const result: U[] = [];
for (let i = 0, { length } = arr; i < length; i++) {
@dphilipson
dphilipson / react-typescript-setup.md
Last active January 11, 2024 16:12
React/TypeScript setup notes

React/TypeScript setup steps

Setting up the environment

  • Run
    yarn create react-app my-app --typescript
    cd my-app
    
import classNames from "classnames";
import React, { memo, ReactElement } from "react";
import "./__NAME__.scss";
interface Props {
className?: string;
}
const __NAME__ = memo(function __NAME__({ className }: Props): ReactElement {
return (
@dphilipson
dphilipson / simplest-git.md
Last active March 3, 2021 08:47
The Simplest Git

The Simplest Git

A minimal set of commands for using Git to collaborate using pull requests.

Note that this is not the only way to do this, but it is one way that works. If someone says to do something else, they aren't necessarily wrong- it could just be a different means of accomplishing the same thing.

The short version

Check out the latest version of master from GitHub:

@dphilipson
dphilipson / read-rust.md
Last active March 8, 2022 02:26
How to Read Rust

How to Read Rust without Knowing Rust

So you need to read a Rust program and you've never seen rust before?

Rust has a well-earned reputation for being a difficult language because of its strict ownership rules, complex type system, and general assumption that anyone learning it has unlimited time and patience. But there's good news! Just because Rust is hard to write doesn't mean it has to be hard to read.

In fact, once you learn a few basics, you might find that Rust is a fairly easy