Skip to content

Instantly share code, notes, and snippets.

View rtpg's full-sized avatar
:shipit:
Investigating some magic

Raphael Gaschignard rtpg

:shipit:
Investigating some magic
View GitHub Profile
@rtpg
rtpg / explain.py
Created July 5, 2021 09:53
Draw out Black AST nodes
#!/usr/bin/env python3
from black import lib2to3_parse
from blib2to3.pytree import Leaf, type_repr
func_example = """
def f(x, y):
# add two operands
return (x + y) # yes just this
"""
@rtpg
rtpg / anagram_classes.py
Created June 22, 2020 14:58
Find the biggest set of anagrams!
"""
An implementation of the Fermat's library anagram detection
https://twitter.com/fermatslibrary/status/1275066521450975234
Takes a list of line seperated words and uses prime factors to
encode letters and identify the equivalency classes.
Prints out the top 10 anagram classes for the provided word list
"""
from collections import defaultdict, Counter
@rtpg
rtpg / assertShape.ts
Last active January 14, 2021 10:10
Assert the shape of your endpoint responses
/**
* First, we're going to declare two types to use for tagging validation, with a function to "run" during compile-time
*
* validate(a: T,b: U) returns Validated<T> if a and b mutually extend each other, if not it returns NotValidated<U>
*
* (by sending the left value in one case and the right value in another the type system is more likely to give "real"
* error messages and tell you what keys you are missing)
*
* Usage is:
* // this should fail at compile time based on the used values
@rtpg
rtpg / sameShape.ts
Last active March 9, 2019 01:56
How to write an assertion that two objects are the same shape in Typescript
// this worked in Typescript 3.2.4
// It feels like we should be able to say SameShape<Q extends R, R extends Q>, but this triggers circular reference
// detection in Typescript. Somehow going to {[K in keyof Q]: Q[K]} solves this (though maybe this won't catch things
// like symbols?)
type SameShape<Q extends R, R extends { [K in keyof Q]: Q[K] }> = {
[K in keyof Q]: R[K] // re-asserting every key of Q is present in R with the same type
} & { // anding the types together will catch any conflicts
[K in keyof R]: Q[K] // re-asserting every key of R is present in Q with the same type
}

Keybase proof

I hereby claim:

  • I am rtpg on github.
  • I am rtpg (https://keybase.io/rtpg) on keybase.
  • I have a public key ASD-wP2usgoqaEFBlO3ZpUS96VO7chqGCyzeRsU2gEWsego

To claim this, I am signing this object:

@rtpg
rtpg / DrawComponents.purs
Created October 1, 2016 06:43
Draw Components (Purescript)
import Data.Exists (Exists, mkExists, runExists)
type Circle = {x:: Int, y:: Int, r:: Int}
type Rectangle = {x:: Int, y:: Int, w:: Int, h:: Int}
drawCircle :: Circle -> String
drawCircle c = "This is a circle!"
drawRectangle :: Rectangle -> String
drawRectangle r = "This is a rectangle!"
import Data.Exists (Exists, mkExists, runExists)
type Circle = {x:: Int, y:: Int, r:: Int}
type Rectangle = {x:: Int, y:: Int, w:: Int, h:: Int}
drawCircle :: Circle -> String
drawCircle c = "This is a circle!"
drawRectangle :: Rectangle -> String
drawRectangle r = "This is a rectangle!"
@rtpg
rtpg / Carmack Plan File Generator.ipynb
Created September 26, 2016 01:00
Carmack Plan File Generator
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@rtpg
rtpg / e-Book Generation.ipynb
Last active September 17, 2016 10:40
My EPUB generation script
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@rtpg
rtpg / Models.js
Last active June 23, 2016 14:55
Example of useful effects
"use strict"
// module App.Models
exports.createUser = function(u){
// you might want to put some real implementations here
return 3;
};
exports.lookupUser = function(u){