Skip to content

Instantly share code, notes, and snippets.

@sgoguen
sgoguen / dispatcher.ts
Last active December 4, 2020 02:51
Proof of concept for proxying lambdas
/*
Typescript allows you to create types that transform existing types into new types.
Here's a TodoList class with a single readonly array property and two fluent methods.
*/
class TodoList {
constructor(readonly items: ReadonlyArray<TodoItem> = []) {
}
@sgoguen
sgoguen / math.ts
Created December 3, 2020 02:13
Parses a math expression and turns it into a type
//. Define some character sets
type LowerChar = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' |
'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' |
'u' | 'v' | 'w' | 'x' | 'y' | 'z';
type UpperChar = `${Capitalize<LowerChar>}`
type AlphaChar = LowerChar | UpperChar;
type NumChar = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '0';
type IsType<T extends string, U> = T extends U ? T : never;
@sgoguen
sgoguen / Morph.cs
Created December 2, 2020 13:53 — forked from badamczewski/Morph.cs
Text Morphing in WPF
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace WPFAnimations
@sgoguen
sgoguen / extracting-update-types.ts
Last active February 24, 2021 16:19
Using TypeScript's Mapped Types and Index Types to extract message types from an interface
// Given an interface like this, I can have TypeScript automatically
// generate message types from all of the "update" methods below.
export interface Person {
// We'll ignore these properties
name: string;
age: number;
dob: Date;
// We'll ignore any method that returns void (We want methods to return new methods)
setDate(newDate: Date): void;
@sgoguen
sgoguen / jeff-atwoods-can-accept-answer.ts
Created February 26, 2020 18:36
Typescript Translation of Jeff's Snippet
enum TrustLevel {
None,
AcceptAllSolutionsTrustLevel
}
class SomeComponent {
private authenticated = false;
private isStaff = false;
private currentUser = {
id: 123,
@sgoguen
sgoguen / anagram.fs
Last active December 12, 2019 05:15 — forked from pragdave/anagram.fs
/// <summary>
/// Builds a map where the keys are word signatures and
/// each value is the list of words that share that signature.
/// The signature of a word is simply a string containing
/// the word's letters, sorted. Thus "dog" and "god" will
/// both have the signature "dgo", and the entry in the map
/// with that key will be those two words.
///
/// This let's us quickly find anagrams
/// </summary>
@sgoguen
sgoguen / movie-list.txt
Created August 16, 2019 17:50
Movies for Robert
Full Metal Jacket
@sgoguen
sgoguen / movie-list.txt
Created August 16, 2019 17:50
Movies for Robert
Full Metal Jacket
let items = [
"1 1/2 cup onions, diced"
"1/4 tsp coriander"
"1 medium carrot"
"1.5 oz olive oil"
"two lemons"
"mint"
]
let tokenize(s) = [ for t in Regex.Split(s, "\\b") do
@sgoguen
sgoguen / wheel.fsx
Created February 13, 2019 04:04
Wheel of Fortune Solver
// First, clone the word list project here: https://github.com/dwyl/english-words.git
// Then save this file in the same folder and run!
open System.IO
// Ok... Here's how it works...
// First, we load all the words from the Moby text fil
let allTheWords = File.ReadAllLines("words.txt")