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 / julia-getting-started.md
Last active June 22, 2021 20:59
Getting started with Julia

Setup

  • Visit Julia home page to download pre-built binaries and access documentation
  • Install VSCode, and install Julia Language extension
  • For an interactive environment, try Pluto

Essential packages

Install these packages in your root environment:

@tk3369
tk3369 / v2v_to_matrix.jl
Created May 22, 2021 23:44
Performance of converting vector-of-vector to matrix
using BenchmarkTools
g(y) = [[y[j], 2y[j]] for j in 1:length(y)]
f8(x) = reduce(hcat, x)'
function f11(v::Vector{Vector{T}}) where T
Mat = Matrix{T}(undef, length(v), length(v[1]))
@inbounds for row in 1:length(v)
for col in 1:length(v[1])
@tk3369
tk3369 / gist:392987f91a75abdd69cc2b5ff3f68e9e
Created May 14, 2021 14:39
hojbot disconnected 2021-05-14 08:27 UTC
β”Œ Error: Gateway error
β”‚ time = 2021-05-14T08:27:03.245
β”‚ conn = 20
β”‚ exception =
β”‚ IOError: stream is closed or unusable
β”‚ Stacktrace:
β”‚ [1] check_open(x::Sockets.TCPSocket)
β”‚ @ Base ./stream.jl:386
β”‚ [2] ssl_unsafe_write(ctx::MbedTLS.SSLContext, buf::Ptr{UInt8}, nbytes::UInt64)
β”‚ @ MbedT

My test code:

using TypeDBClient
import TypeDBClient.grakn.protocol as P

client = TypeDBClient.CoreClient("127.0.0.1", 1729)
session = TypeDBClient.CoreSession(client, "social_network", P.Session_Type.DATA)
transaction = TypeDBClient.CoreTransaction(session, session.sessionID, P.Transaction_Type.READ, TypeDBClient.TypeDBOptions())
result = TypeDBClient.match(transaction, raw"""
    match $loc has name "French Lick";

Set up test data

julia> bs = rand(Bool, 100000);

Using if/else:

julia> function foo(bs)

I built a cache that invalidates based on expiration time but then I realized that ExpiringCaches.jl (and few others) exist :-p

Just capturing what I did:

"""
    TimedCache

A time-based cache. To construct a new cache, provie the types for key and value
and a retention period. It is lazy - when a cache entry expires, it is not removed
@tk3369
tk3369 / grakn-error.md
Created April 18, 2021 01:20
grakn error - Invalid protobuf byte sequence

I ran this code:

using Revise, GraknClient, UUIDs
using GraknClient: CoreTransaction, Proto, GraknOptions, bytes

client = GraknClient.CoreClient("127.0.0.1", 1729)
session = GraknClient.CoreSession(client, "phone_calls", GraknClient.grakn.protocol.Session_Type.DATA, GraknClient.GraknOptions())
txn = CoreTransaction(session, session.sessionID, Proto.Transaction_Type.READ, GraknOptions())
@startuml
class Test
@enduml
@tk3369
tk3369 / error.md
Created March 28, 2021 17:44
gRPC does not support streaming interfaces?

Error when calling transaction with a Channel argument:

julia> channel = Channel{GraknClient.grakn.protocol.Transaction_Client}(1)

julia> txn = GraknClient.transaction(client.core_stub.blockingStub, gRPCController(), channel)
ERROR: MethodError: no method matching meta(::Type{Channel{GraknClient.grakn.protocol.Transaction_Client}})
Closest candidates are:
  meta(::ProtoBuf.ProtoMeta, ::Type, ::Vector{Pair{Symbol, Union{String, Type}}}, ::Vector{Symbol}, ::Vector{Int64}, ::Dict{Symbol, Any}) at /Users/tomkwong/.julia/packages/ProtoBuf/NJbMU/src/codec.jl:576
  meta(::ProtoBuf.ProtoMeta, ::Type, ::Vector{Pair{Symbol, Union{String, Type}}}, ::Vector{Symbol}, ::Vector{Int64}, ::Dict{Symbol, Any}, ::Vector{Symbol}) at /Users/tomkwong/.julia/packages/ProtoBuf/NJbMU/src/codec.jl:576
@tk3369
tk3369 / JavaScriptEmoji.jl
Created March 13, 2021 19:46
Generate javascript code for mapping emoji short name to unicode
module JavaScriptEmoji
export download_emojis, parse_emojis, gen_javascript
import JSON
function download_emojis()
url = "https://raw.githubusercontent.com/iamcal/emoji-data/master/emoji.json"
return JSON.parsefile(download(url))
end