Skip to content

Instantly share code, notes, and snippets.

View damienstanton's full-sized avatar

Damien Stanton damienstanton

View GitHub Profile
@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
@damienstanton
damienstanton / estimator.go
Created September 5, 2019 19:09
cocomo notes
package processor
import (
"math"
)
// EstimateCost calculates the cost in dollars applied using generic COCOMO2 weighted values based
// on the average yearly wage
func EstimateCost(effortApplied float64, averageWage int64) float64 {
return effortApplied * float64(averageWage/12) * float64(1.8)