Skip to content

Instantly share code, notes, and snippets.

Avatar
:shipit:
Investigating some magic

Raphael Gaschignard rtpg

:shipit:
Investigating some magic
View GitHub Profile
@rtpg
rtpg / explain.py
Created Jul 5, 2021
Draw out Black AST nodes
View explain.py
#!/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 Jun 22, 2020
Find the biggest set of anagrams!
View anagram_classes.py
"""
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 Jan 14, 2021
Assert the shape of your endpoint responses
View assertShape.ts
/**
* 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 Mar 9, 2019
How to write an assertion that two objects are the same shape in Typescript
View sameShape.ts
// 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
}
View keybase.md

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 Oct 1, 2016
Draw Components (Purescript)
View DrawComponents.purs
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!"
View DrawComponents.ps
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 Sep 26, 2016
Carmack Plan File Generator
View Carmack Plan File Generator.ipynb
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 Sep 17, 2016
My EPUB generation script
View e-Book Generation.ipynb
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 Jun 23, 2016
Example of useful effects
View Models.js
"use strict"
// module App.Models
exports.createUser = function(u){
// you might want to put some real implementations here
return 3;
};
exports.lookupUser = function(u){