Skip to content

Instantly share code, notes, and snippets.

View tk3369's full-sized avatar
🐶
Published! Hands on design patterns and best practices with Julia.

Tom Kwong tk3369

🐶
Published! Hands on design patterns and best practices with Julia.
View GitHub Profile
((* extends 'article.tplx' *))
((* block commands *))
((( super() )))
% define custom margins
\geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=\maxwidth]{#1}}
((* endblock commands *))
((* block predoc *))
#!/bin/sh
#
# Generate PDF file from ipynb notebook.
# Several tricks are done to post-process the latex files.
if [ $# -ne 1 ]; then
echo "Usage: `basename $0` filename"
exit 1
fi
@tk3369
tk3369 / promote_rule
Created March 12, 2019 06:58
promote_rule example
julia> import Base: promote_rule, convert
julia> struct USD
value
end
julia> struct Painting
rating
end
@tk3369
tk3369 / test.csv
Created May 22, 2019 01:03
test file for DataFrames.append! issue
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
a
x
x
x
x
x
x
x
x
x
@tk3369
tk3369 / memoize_perf.md
Last active July 10, 2021 12:23
Performance of various memoize implementations

Custom cache

Generic function

julia> const fib_cache = Dict()
Dict{Any,Any} with 0 entries

julia> _fib(n) = n < 3 ? 1 : fib(n-1) + fib(n-2)
_fib (generic function with 1 method)

Some notes from Julia Slack

My initial question was why this expression returns false:

julia> Vector{Any} === Vector
false

The answer is quite simply that Vector{Any} is just like any other Vector{T}, for which is a subtype of Vector. Then, we wonder what is Vector.

@tk3369
tk3369 / functors.md
Created October 27, 2019 00:19
Functors

What are functors?

Functors are types that can be mapped over.

Two laws (https://wiki.haskell.org/Functor#Functor_Laws):

  1. mapping an identity function over a functor would return the same functor
  2. mapping function g then f is equivalent to mapping the composed function (f ∘ g)

fmap is a mapping function that operates on functors. It can be defined for specific objects so that the provided mapping function can be applied in the

@tk3369
tk3369 / name_macro.md
Created October 29, 2019 02:49
metaprogramming for capturing the name of a function
julia> macro name(ex)
           ex.head === :function || 
               error("can be used for function defintions only")
           name = ex.args[1].args[1]
           assignment = :(local __NAME__ = $name)
           pushfirst!(ex.args[2].args, assignment)
           return ex
       end
@name (macro with 1 method)
@tk3369
tk3369 / Curry.md
Created November 3, 2019 05:54
Curry function

From Mason Protter in Slack:

struct FullyCurried end

macro curried(fdef)
    f = fdef.args[1].args[1]
    fargs = fdef.args[1].args[2:end]
    arity = length(fargs)
 body = fdef.args[2]
@tk3369
tk3369 / batman.jl
Created November 17, 2019 09:25
plotting the batman equation
using Plots
function batman(; lw = 4)
let sqrt = x -> x > 0 ? Base.sqrt(x) : NaN
v1(x) = 2*sqrt(-abs(abs(x)-1)*abs(3-abs(x))/((abs(x)-1)*(3-abs(x)))) * (1+abs(abs(x)-3)/(abs(x)-3)) * sqrt(1-(x/7)^2)+(5+0.97(abs(x-.5)+abs(x+.5))-3(abs(x-.75)+abs(x+.75))) * (1+abs(1-abs(x))/(1-abs(x)))
v2(x) = (2.71052 + 1.5 - 0.5 * abs(x) - 1.35526 * sqrt(4-(abs(x)-1)^2)) * sqrt(abs(abs(x)-1)/(abs(x)-1)) + 0.9
v3(x) = (-3)*sqrt(1-(x/7)^2)*sqrt(abs(abs(x)-4)/(abs(x)-4))
v4(x) = abs(x/2)-0.0913722*x^2-3+sqrt(1-(abs(abs(x)-2)-1)^2)
x = -8:0.0005:8