Skip to content

Instantly share code, notes, and snippets.

View Jason5Lee's full-sized avatar

Jason5Lee Jason5Lee

View GitHub Profile
@Jason5Lee
Jason5Lee / kotlin-abandoned-coroutines.md
Created July 27, 2019 04:58
An example about abandoning coroutine in Kotlin may not execute the finally clause immediately

In Kotlin, a coroutine can be abandoned by not resuming it. If a coroutine is abandoned while suspending in a try-block with finally, the finally clause will not be executed until the coroutine is collected.

For example, the following Kotlin code

val seqWithFinally = sequence {
    try {
        yield(Unit)
        yield(Unit)
@Jason5Lee
Jason5Lee / opaque-type.ts
Last active February 22, 2020 02:14
TypeScript's opaque type.
type USD = number & { readonly __brand: unique symbol }
type EUR = number & { readonly __brand: unique symbol }
type Currency = USD | EUR;
function addCurrency<T extends Currency>(a: T, b: T): T {
return (a + b) as T;
}
addCurrency(5 as USD, 6 as USD);
@Jason5Lee
Jason5Lee / async-await-no-stackoverflow.md
Last active October 29, 2023 11:11
Using async/await to avoid stack overflow error.

Inspired by elizarov/DeepRecursiveFunction.kt.

Normally, when recursively evaluating the depth of a deep tree, it will result in stack overflow error. But, by using async/await, it keeps its stack on the heap, and evaluates correct result without error.

C#

public static class Program
{
@Jason5Lee
Jason5Lee / rust-non-blocking-drop.rs
Last active July 9, 2020 06:11
Non-blocking drop objects by a background thread, inspired by the "GC thread" concept in GC languages.
// [dependencies]
// lazy_static = "1.4"
// crossbeam-channel = "0.4"
use crossbeam_channel::{unbounded, SendError, Sender};
use lazy_static::lazy_static;
use std::thread;
use std::time::Duration;
lazy_static! {
@Jason5Lee
Jason5Lee / async-helloworld.hs
Created July 13, 2020 05:06
Haskell helloworld example of asynchronous IO.
import Control.Concurrent
import Control.Concurrent.Async
delayHello n = do
threadDelay (n * 100)
let hello = show n ++ ": Hello World!"
putStrLn hello
delayHello :: Int -> IO ()
delayHello n = do
@Jason5Lee
Jason5Lee / rust_monad.rs
Created February 10, 2021 15:11 — forked from rust-play/playground.rs
Code shared from the Rust Playground
/// Warning: the following code is made for experiment.
/// If you cause losses due to the use of this code in production, I am not responsible.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct List<T = ()>(pub Vec<T>);
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Id<T = ()>(pub T);
pub trait HKT<T> {
type Actual;
@Jason5Lee
Jason5Lee / booklist.md
Last active July 27, 2022 05:51 — forked from swlaschin/booklist.md
Some recommended books for improving as a software developer

Some recommended books for improving as a software developer

Most software books are too language specific and go out of date too quickly. What I find has stayed with me are books about bigger concepts, such as systems thinking and complexity, and also so-called "soft skills" such as management and psychology.

User experience

These are all really about developing empathy for other people :)

  • "The Design of Everyday Things" by Donald Norman