Skip to content

Instantly share code, notes, and snippets.

Avatar

Eric Davies iamed2

  • Invenia Technical Computing
  • Winnipeg, MB, Canada
View GitHub Profile
@iamed2
iamed2 / odds.jl
Created Nov 1, 2021
Calculate the experimental odds of snake eyes if you're told "at least one of them is a one"
View odds.jl
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 Aug 20, 2021
Using Julia completions for LaTeX-like substitutions
View utex.sh
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 Aug 12, 2021
A custom Julia serialization method that works for any primitive type
View serialization.jl
# 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 Apr 3, 2019
Generate an almost-right Project.toml file from a REQUIRE file in Julia 1.0 <= VERSION < 1.1-
View make-project-toml.jl
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
How to write a GroupBy iterator: bad and good (requires IterTools at branch ed/iterate)
View Benchmarking
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 Sep 23, 2016
Iterators.jl performance comparison on Julia 0.4
View eltype-overhaul (b4eb5c6)
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
Sudoku solver written at !!con!
View Sudoku.jl
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 Aug 18, 2014
Partition a collection into two collections using a predicate
View partition.jl
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
View mlss14 stan model
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 Dec 18, 2013
Complete build log from `emerge -av julia`.
View juliabuild
 * 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)