Skip to content

Instantly share code, notes, and snippets.

Avatar

David Philipson dphilipson

View GitHub Profile
@dphilipson
dphilipson / simplest-git.md
Last active Dec 2, 2019
The Simplest Git
View simplest-git.md

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:

View ReactComponentBoilerplate.tsx
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 / react-typescript-setup.md
Last active Apr 19, 2019
React/TypeScript setup notes
View react-typescript-setup.md

React/TypeScript setup steps

Setting up the environment

  • Run
    yarn create react-app my-app --typescript
    cd my-app
    
@dphilipson
dphilipson / functionArityOptimization.ts
Created Jun 15, 2018
Benchmarks demonstrating interesting properties of optimization around function arity in JavaScript
View functionArityOptimization.ts
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 / redoodleSagas.ts
Created Feb 28, 2018
Combine redux-saga with Redoodle for marginally better static typing
View redoodleSagas.ts
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 / dateString.ts
Created Dec 12, 2017
Branded strings that represent dates in TypeScript.
View dateString.ts
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);
View SafeMap.ts
// ----- 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 / PackStatistics.java
Created Apr 26, 2016
Computes expected number of packs required to complete a portion of your collection in Hearthstone
View PackStatistics.java
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 {
You can’t perform that action at this time.