Skip to content

Instantly share code, notes, and snippets.

View damienstanton's full-sized avatar

Damien Stanton damienstanton

View GitHub Profile
@damienstanton
damienstanton / GAT.md
Last active March 7, 2021 20:53
Generic Associated Types (GAT) in Rust

"Higher-kinded types" is a vague term, conflating multiple language features under a single banner, which can be inaccurate.

As background, this RFC includes a brief overview of the notion of kinds and kindedness. Kinds are often called 'the type of a type,' the exact sort of unhelpful description that only makes sense to someone who already understands what is being explained. Instead, let's try to understand kinds by analogy to types.

In a well-typed language, every expression has a type. Many expressions have what are sometimes called 'base types,' types which are primitive to the language and which cannot be described in terms of other types. In Rust, the types bool, i64, usize, and char are all prominent examples of base types.

In contrast, there are types which are formed by arranging other types - functions are a good example of this. Consider this simple function:

fn not(x: bool) -> bool {
@damienstanton
damienstanton / entropy.swift
Last active June 9, 2020 17:45
Entropy methods for String
import Foundation
extension String {
/// Returns the minimum number of bits required to encode the given string
func bitFloor() -> Int { Int(entropy().rounded(.up)) }
/// Returns the metric entropy of the given string, as a measure of randomness
func randomness() -> Double { entropy() / Double(self.count)}
/// Returns the approximate Shannon entropy of the given string
interface Trait {
// some props
}
export default class Foo implements Trait {
private static foo: Foo
private constructor() { }
static instance(): Foo {
if (this.foo == null) this.foo = new Foo()
@damienstanton
damienstanton / generics.go
Created February 20, 2020 20:32
Generics in Go (proposed)
/*
* This code is taken from Ian Lance Taylor's Gophercon 2019 talk on
* a proposed syntax/semantics for generics in Go. As such, it won't
* compile on any released Go compiler as of this posting (early 2020).
*/
// Tree is a generic binary tree
type Tree (type E) struct {
root *node(E)
compare func(E, E) int
@damienstanton
damienstanton / README.md
Last active January 30, 2020 15:04
Go module-based project generator

This simple bash/zsh function automates a make-based Go project that uses modules (No GOPATH muckery required).

Installation

Copy the goproject file somewhere on your $PATH and chmod +x it, or copy the function definition into your own bash/zsh script.

Usage

This is little more than a wrapper around running go mod init <etc>, writing a few files so you don't have to think about it. Expected outputs are shown in comments following each command.

@damienstanton
damienstanton / rust.yml
Created October 9, 2019 20:38
Rust GH workflow
name: Rust
on: [push]
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
@damienstanton
damienstanton / is_unique.rs
Created October 4, 2019 22:07
string is unique
use std::collections::HashMap;
pub fn str_is_unique(s: &str) -> bool {
let mut map: HashMap<char, i32> = HashMap::new();
for c in s.chars() {
*map.entry(c).or_insert(0) += 1;
}
!map.iter().any(|(_, &v)| v > 1)
}
Attributes: [
Attribute {
pound_token: Pound,
style: Outer,
bracket_token: Bracket,
@damienstanton
damienstanton / build.rs
Created September 8, 2019 13:50
rust/cffi build
extern crate cc;
use std::path::Path;
fn main() {
let host_target = "x86_64-apple-darwin";
cc::Build::new()
.cpp(true)
.file("src/cpp/hello.cpp")
.include("src/cpp")
.out_dir(Path::new("."))
@damienstanton
damienstanton / pixel_2xl_abi.md
Last active September 5, 2019 20:54
Rust + Android notes
  • arm64-v8a
  • armeabi-v7a
  • armeabi