Skip to content

Instantly share code, notes, and snippets.

View iamed2's full-sized avatar

Eric Davies iamed2

  • Beacon Biosignals
  • Winnipeg, MB, Canada
View GitHub Profile
@iamed2
iamed2 / odds.jl
Created November 1, 2021 16:27
Calculate the experimental odds of snake eyes if you're told "at least one of them is a one"
function tally(n=100)
snake_eyes = 0
any_ones = 0
for i in 1:n
rolls = (dice_roll(), dice_roll())
if 1 in rolls # tests at least one of them is a one
any_ones += 1
if rolls[1] == rolls[2] # tests both are one
@iamed2
iamed2 / utex.sh
Created August 20, 2021 03:31
Using Julia completions for LaTeX-like substitutions
julia --startup-file=no --history-file=no -e 'import REPL; print(
let SYMBOLS = REPL.REPLCompletions.latex_symbols
get(SYMBOLS, "\\\$(ARGS[1])") do
SYMBOLS["\\text$(ARGS[1])"]
end
end
)' Sigma
@iamed2
iamed2 / serialization.jl
Created August 12, 2021 22:12
A custom Julia serialization method that works for any primitive type
# Originally written to work with InlineString in WeakRefStrings, this should work for any primitive type
# In most cases, the built-in defaults provided by Julia work fine, using `read` and `write`.
# However, if your `read` and `write` methods are not round-trippable, or they convert your data into a
# less-efficient representation before writing, then you might want to additionally define a custom
# serialization method, using this code.
# It should only be necessary to substitute your type for `Primitive`.
using Serialization
@iamed2
iamed2 / make-project-toml.jl
Created April 3, 2019 22:47
Generate an almost-right Project.toml file from a REQUIRE file in Julia 1.0 <= VERSION < 1.1-
using Pkg
function imports(pkg_dir::AbstractString)
imported = Set{String}()
for (root, dirs, files) in walkdir(pkg_dir)
for file in files
if endswith(file, ".jl")
union!(imported, open(imports, joinpath(root, file)))
end
end
@iamed2
iamed2 / Benchmarking
Last active May 30, 2018 19:31
How to write a GroupBy iterator: bad and good (requires IterTools at branch ed/iterate)
julia> @benchmark collect(groupby2(first, ["face", "foo", "bar", "book", "baz"]))
BenchmarkTools.Trial:
memory estimate: 1.98 KiB
allocs estimate: 60
--------------
minimum time: 14.258 μs (0.00% GC)
median time: 15.473 μs (0.00% GC)
mean time: 16.915 μs (0.00% GC)
maximum time: 207.366 μs (0.00% GC)
--------------
@iamed2
iamed2 / eltype-overhaul (b4eb5c6)
Created September 23, 2016 22:49
Iterators.jl performance comparison on Julia 0.4
Benchmarking Take1 ...
Take1 with cfg = 4096: nruns = 28955, elapsed = 1.516598519 secs
Take1 with cfg = 16384: nruns = 6217, elapsed = 1.307575089 secs
Take1 with cfg = 65536: nruns = 1252, elapsed = 1.152236431 secs
Take1 with cfg = 262144: nruns = 266, elapsed = 0.905467819 secs
Take1 with cfg = 1048576: nruns = 91, elapsed = 1.172819189 secs
Benchmarking Take2 ...
Take2 with cfg = 4096: nruns = 24779, elapsed = 1.368126555 secs
Take2 with cfg = 16384: nruns = 6403, elapsed = 1.47713414 secs
Take2 with cfg = 65536: nruns = 926, elapsed = 0.909415618 secs
@iamed2
iamed2 / Sudoku.jl
Created May 17, 2015 17:26
Sudoku solver written at !!con!
module Sukano
using JuMP
function sudoku_setup()
m = Model()
@defVar(m, 0 <= board[1:9,1:9,1:9] <=1, Int)
# each square can only have one value
@addConstraint(m, squareconst[i=1:9,j=1:9], sum(board[i,j,:]) == 1)
@iamed2
iamed2 / partition.jl
Created August 18, 2014 18:42
Partition a collection into two collections using a predicate
function partition(f::Function, collection)
u = similar(collection)
v = similar(collection)
for el in collection
if f(el)
push!(u, el)
else
push!(v, el)
end
data {
int<lower=1> N_ages;
vector[N_ages] ages;
int K[N_ages];
int N[N_ages];
}
transformed data {
vector[N_ages] mu;
mu <- rep_vector(0, N_ages);
@iamed2
iamed2 / juliabuild
Created December 18, 2013 01:36
Complete build log from `emerge -av julia`.
 * Package: dev-lang/julia-9999
 * Repository: science
 * Maintainer: sci
 * USE: amd64 doc elibc_glibc kernel_linux userland_GNU
 * FEATURES: preserve-libs sandbox
>>> Unpacking source...
Cloning into '/usr/portage/distfiles/egit-src/julia.git'...
remote: Counting objects: 80880, done.
remote: Compressing objects: 0% (1/26694) 
remote: Compressing objects: 1% (267/26694)