Skip to content

Instantly share code, notes, and snippets.

Avatar

John Myles White johnmyleswhite

View GitHub Profile
View lift.jl
import MacroTools: postwalk
function missing_check(es)
if length(es) == 0
false
elseif length(es) == 1
Expr(:call, :ismissing, es[1])
elseif length(es) == 2
Expr(
:call,
View thunk.jl
struct ExpressionThunk
source::Any
thunk::Any
end
macro thunk(e)
quote
ExpressionThunk(
$(QuoteNode(e)),
() -> $(esc(e))
View df_deep_copy.jl
x = [1, 2]
y = [3, 4]
z = [x, y]
z′ = copy(z)
z′[1] === x
import DataFrames: DataFrame
View example2.jl
import Plots
r = range(0.0, 1.0, length=512)
p = Plots.plot(
r,
x -> x^2 * (1 - x)^2,
)
Plots.png(p, "output2.png")
View example.R
#!/usr/bin/Rscript
png("output.png")
curve(x^2 * (1 - x)^2, from = 0, to = 1)
dev.off()
# time Rscript example.R
# null device
# 1
#
View logistic_comparisons.jl
julia> import BenchmarkTools: @btime
julia> @inline _logistic_bounds(x::Float16) = (Float16(-16.64), Float16(7.625))
_logistic_bounds (generic function with 1 method)
julia> @inline _logistic_bounds(x::Float32) = (-103.27893f0, 16.635532f0)
_logistic_bounds (generic function with 2 methods)
julia> @inline _logistic_bounds(x::Float64) = (-744.4400719213812, 36.7368005696771)
_logistic_bounds (generic function with 3 methods)
View expr_kinds.txt
│ Row │ expr_type │ count │ percentage │
│ │ Any │ Int64 │ Float64 │
├─────┼────────────────────────────────┼────────┼─────────────┤
│ 1 │ ExprKind{:outer} │ 1 │ 0.000226006 │
│ 2 │ ExprKind{:/=} │ 1 │ 0.000226006 │
│ 3 │ ExprKind{:typed_vcat} │ 1 │ 0.000226006 │
│ 4 │ ExprKind{:.+=} │ 1 │ 0.000226006 │
│ 5 │ ExprKind{:%=} │ 3 │ 0.000678017 │
│ 6 │ ExprKind{:⊻=} │ 7 │ 0.00158204 │
│ 7 │ ExprKind{:.=} │ 8 │ 0.00180804 │
View runtimes.jl
import RCall: @R_str
function runtimes(n_reps)
times = Array{Float64}(undef, n_reps)
x = rand(10_000_000)
for i in 1:n_reps
times[i] = @elapsed sum(x)
end
times
end
View graphemes.R
> library("stringr")
> str_sub("", start = -1)
[1] "̃"
> str_sub("ñ", start = -1)
[1] "ñ"
View prob_f64.jl
function prob_f64(n)
s = 0
for _ in 1:n
z_i = rand(UInt)
x_i = reinterpret(Float64, z_i)
s += Int(x_i == x_i + 1.0)
end
s, n
end