Skip to content

Instantly share code, notes, and snippets.

Avatar

James Cook jwscook

View GitHub Profile
@jwscook
jwscook / Functionals.jl
Created Jun 9, 2018
Avoiding functions in types; performance comparison & invitation for code review
View Functionals.jl
module Functionals
abstract type Abstract end
type Functional <: Abstract
lower::Float64
upper::Float64
end
Functional(t::Float64) = Functional(0.0, 12.0 * t)
@jwscook
jwscook / ThreadProblemMWE.jl
Last active Jun 23, 2020
Finding optimal threading of two consecutive double loops where some rows are skipped in the first double loop and the second double loop depends on the output of the first.
View ThreadProblemMWE.jl
using Base.Threads, Random
function run(;waittime=1.0e-3)
function calcsubset(N)
Random.seed!(0)
return sort(shuffle(collect(1:N))[1:Int(ceil(round(N / 3)))])
end
# we know that only a sorted subset of rows need be iterated over for the first loop
@jwscook
jwscook / RandAlwaysHalf.jl
Created Jul 6, 2020
Use Cassette and ReplMaker to create REPL that has deterministic rand()
View RandAlwaysHalf.jl
# Based on https://gist.github.com/mbauman/d0c27f40e25393ef050b87b4bb6a69dc
using Cassette, ReplMaker
Cassette.@context RandAlwaysHalf
randreturnshalf(s, x) = x
function randreturnshalf(s, f, fx)
if f == rand
return randreturnshalf(s, one(typeof(fx)) / 2)
@jwscook
jwscook / ReplaceRand.jl
Last active Jul 7, 2020
@replacerand macro to replace calls to rand() by a given function e.g. x->0.5
View ReplaceRand.jl
"""
A macro `@replacerand` that replaces the value returned by a call to `rand` with something else.
"""
using Cassette
Cassette.@context ReplaceRand
Cassette.overdub(ctx::ReplaceRand, fn::typeof(rand), args...) = ctx.metadata(args...)
@jwscook
jwscook / Cuckoo.jl
Last active Jul 14, 2020
The Cuckoo replaces eggs in a nest without the developer knowing, but not a very good way.
View Cuckoo.jl
module Cuckoo
export @cuckoo
using Cassette
Cassette.@context Ctx
function _cuckoo(chicks::Dict, nest)
for (oldchicks, newchicks) chicks
@eval function Cassette.overdub(ctx::Ctx,
View Reproducible.jl
using Random
const seed = hash(time())
Random.seed!(seed)
searchstring = "const seed = "
open("$(@__FILE__)" * ".reproducible", "w") do io
for line in readlines(open(@__FILE__))
if length(line) > length(searchstring) && line[1:length(searchstring)] == searchstring
You can’t perform that action at this time.