Skip to content

Instantly share code, notes, and snippets.

View michaelsproul's full-sized avatar

Michael Sproul michaelsproul

View GitHub Profile
@michaelsproul
michaelsproul / find_errors.py
Created April 14, 2015 06:44
Rust Unexplained Error Finder
# Run from within src
import subprocess
for err_no in range(500):
err_string = "E%04d" % err_no
# Determine if the error needs a message.
try:
subprocess.check_output(["git", "grep", err_string + ",", "librustc/diagnostics.rs"])
@michaelsproul
michaelsproul / rename.hs
Created April 26, 2015 13:12
Haskell File Rename
-- Even vanilla Haskell is quite nice for scripting. I prefer it to Bash!
import System.Directory
import System.Posix.Files hiding (rename)
main = do
currDir <- getCurrentDirectory
photos <- getDirectoryContents currDir
mapM rename photos
rename :: String -> IO ()
@michaelsproul
michaelsproul / librustc.json
Last active August 29, 2015 14:20
Metadata dump for librustc errors (2015-04-28).
{
"E0001": {
"description": "\nThis error suggests that the expression arm corresponding to the noted pattern\nwill never be reached as for all possible values of the expression being\nmatched, one of the preceding patterns will match.\n\nThis means that perhaps some of the preceding patterns are too general, this one\nis too specific or the ordering is incorrect.\n",
"use_site": {
"filename": "/home/michael/Programming/rust/src/librustc/middle/check_match.rs",
"line": 334
}
},
"E0002": {
"description": "\nThis error indicates that an empty match expression is illegal because the type\nit is matching on is non-empty (there exist values of this type). In safe code\nit is impossible to create an instance of an empty type, so empty match\nexpressions are almost never desired. This error is typically fixed by adding\none or more cases to the match expression.\n\nAn example of an empty type is `enum Empty { }`.\n",
@michaelsproul
michaelsproul / uber.rs
Created July 9, 2015 06:22
Radix Trie Traversal Function
fn uber<K, V, Key, Value, Output,
RootFn, NoChildFn, FullMatchFn, PartialMatchFn, FirstPrefixFn>
(
trie: &mut Trie<K, V>,
key: Key,
value: Value,
mut key_fragments: NibbleVec,
root_fn: RootFn,
no_child_fn: NoChildFn,
full_match_fn: FullMatchFn,
@michaelsproul
michaelsproul / sample.hs
Created August 9, 2015 13:14
COMP3109 Sample Haskell
import Data.List
data Task = Task {
p :: Integer,
l :: Integer,
d :: Integer
} deriving (Show, Eq)
-- Compute the optimal penalty for a list of tasks.
solve :: [Task] -> Integer
@michaelsproul
michaelsproul / postgres_to_excel_csv.sql
Created January 5, 2016 08:17
Postgres timeseries data to Excel compatible CSV
COPY (SELECT to_char(timestamp, 'DD/MM/YY HH24:MI:SS'), etc FROM table) TO '/some/file.csv' DELIMITER ',' CSV HEADER;
@michaelsproul
michaelsproul / insert_after_main.py
Created February 16, 2016 05:34
Insert code after C main function...
#!/usr/bin/env python3
# You probably shouldn't use this, it is guaranteed awful... Although it did work for me...
import re
import os
import sys
MAIN_REGEX = re.compile(rb".*int.*main.*\(.*int.*argc.*\).*")
BRACE_REGEX = re.compile(rb".*{.*")
@michaelsproul
michaelsproul / family_first.md
Last active November 19, 2016 10:54
What if Family First weren't part of the SA senate election?

Here's the result when Family First ARE included in the count:

  1. Simon BIRMINGHAM (Liberal)
  2. Penny WONG (Australian Labor Party)
  3. Cory BERNARDI (Liberal)
  4. Nick XENOPHON (Nick Xenophon Team)
  5. Don FARRELL (Australian Labor Party)
  6. Anne RUSTON (Liberal)
  7. Stirling GRIFF (Nick Xenophon Team)
  8. Alex GALLACHER (Australian Labor Party)
@michaelsproul
michaelsproul / csv_impl_trait.rs
Created January 8, 2017 01:57
impl Iterator and csv::Reader don't play nice
pub fn parse_preferences_file<T: Read>(reader: &mut csv::Reader<T>, groups: &[Group],
candidates: &[CandidateId], constraints: &Constraints)
-> impl Iterator<Item=IOBallot>> {
reader.decode::<PrefRow>()
.map(|raw| parse_single_ballot(raw, groups, candidates, constraints))
)
}