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
VendorID tpep_pickup_datetime tpep_dropoff_datetime passenger_count trip_distance RatecodeID store_and_fwd_flag PULocationID DOLocationID payment_type fare_amount extra mta_tax tip_amount tolls_amount improvement_surcharge total_amount
1 2018-12-01 00:28:22 2018-12-01 00:44:07 2 2.50 1 N 148 234 1 12 0.5 0.5 3.95 0 0.3 17.25
1 2018-12-01 00:52:29 2018-12-01 01:11:37 3 2.30 1 N 170 144 1 13 0.5 0.5 2.85 0 0.3 17.15
2 2018-12-01 00:12:52 2018-12-01 00:36:23 1 .00 1 N 113 193 2 2.5 0.5 0.5 0 0 0.3 3.8
1 2018-12-01 00:35:08 2018-12-01 00:43:11 1 3.90 1 N 95 92 1 12.5 0.5 0.5 2.75 0 0.3 16.55
1 2018-12-01 00:21:54 2018-12-01 01:15:13 1 12.80 1 N 163 228 1 45 0.5 0.5 9.25 0 0.3 55.55
1 2018-12-01 00:00:38 2018-12-01 00:29:26 1 18.80 1 N 132 97 1 50.5 0.5 0.5 10.35 0 0.3 62.15
1 2018-12-01 00:59:39 2018-12-01 01:09:07 1 1.00 1 N 246 164 1 7.5 0.5 0.5 0.44 0 0.3 9.24
1 2018-12-01 00:19:19 2018-12-01 00:22:19 1 .30 1 N 161 163 4 4 0.5 0.5 0 0 0.3 5.3
@tk3369
tk3369 / perf_cartesian_product.md
Created November 23, 2019 20:45
Performance result - cartesian product of two arrays
julia> foo(x,y) = reshape(collect(Iterators.product(x,y)), :, 1)
foo (generic function with 1 method)

julia> bar(x,y) = [(i,j) for j in y for i in x]
bar (generic function with 1 method)

julia> x = rand(100); y = rand(100);

julia> @btime foo($x,$y);
@tk3369
tk3369 / tmux.conf
Created November 18, 2019 06:15
tmux config
# start window number from 1
set -g base-index 1
# rebind prefix hot-key from C-b to C-j
set -g prefix C-j
unbind-key C-b
bind-key C-j send-prefix
# use vi binding
setw -g mode-keys vi
@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
@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 / 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 / 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

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 / 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)
@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