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
@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 / 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);
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 / JMG.md
Last active December 9, 2019 00:20
Julian Master Game

The Julian Master Game

The following is a set of problems that I organized with some friends at work in 2018.

Day 1 - π

This is an interesting one because the infinite sequence below converges to a number that relates to π. There are several ways to prove that although I like this one the most https://www.youtube.com/watch?v=d-o3eB9sfls.

1/1 + 1/4 + 1/9 + 1/16 + … = π / 6

Suppose that we have an array of bytes that comes from a binary file. For simplicity sake, let's say we have just 8 64-bit integers loaded into memory, so we should have 64 bytes in the array.

Here's how we can generate a sample byte array:

julia> x = rand(1:100, 8)
8-element Array{Int64,1}:
  3
@tk3369
tk3369 / capture.md
Created February 16, 2020 20:50
How to use `@capture` with variable expression

How to use @capture with variable expression

The @capture macro from MacroTools expects one to pass the pattern directly when the macro is called. From Slack, someone asked if the pattern can be stored in a variable and then passed to the macro. I could make it work with the following "hack":

julia> code
:(foo(bar + xyz))

julia> pattern
:(phi1_(x_ + y_))
@tk3369
tk3369 / channel_play.jl
Created May 10, 2020 00:30
Composing with channels
# sum worker
function sum_worker(source)
target = Channel()
task = @async while true
args = take!(source)
args === nothing && break
result = sum(args)
push!(target, result)
end
return (output = target, task = task)
@tk3369
tk3369 / df_separate_func.jl
Created August 29, 2020 17:25
Sample implementation of `separate` function for DataFrames.jl
using DataFrames
using Test
# Tidyr match-up
abstract type FillStrategy end
struct FillMissing <: FillStrategy end
struct FillRight <: FillStrategy end
struct FillLeft <: FillStrategy end
@tk3369
tk3369 / bounce.jl
Created September 8, 2020 05:59
Bounce text in a terminal!
using TerminalUserInterfaces
const TUI = TerminalUserInterfaces
function bounce(str; delay = 0.08)
TUI.initialize()
try
terminal = TUI.Terminal()
width, height = TUI.terminal_size()
len = length(str)
@tk3369
tk3369 / .screenrc
Last active October 18, 2020 06:06
screenrc
termcapinfo xterm Z0=\E[?3h:Z1=\E[?3l:is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;4;6l
wall Welcome to screen
# See http://www.gnu.org/software/screen/manual/html_node/String-Escapes.html
hardstatus alwayslastline
# #hardstatus string '%{= mK}%-Lw%{= KW}%50>%n%f* %t%{= mK}%+Lw%< %{= kG}%-=%D %d %M %Y %c:%s%{-}'
#hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m %{W}%c %{g}]'
hardstatus string '%{= wr}[%{B}%S%{r}][%= %{= wr}%?%-Lw%?%{b}(%{B}%n*%f%t%?(%u)%?%{b})%{b}%?%+Lw%?%?%= %{r}][%{B} %Y-%m-%d %c %{r}]'