everyday: Development Log

01.23.2022 - WebAssembly/A quick guide

WebAssembly is a new type of code that can be loaded and run by the JavaScript VM on web browsers. It can also be run natively on the computer by other WASM runtimes like Wasmer.

WebAssembly is fast and could provide a near-native performance for several reasons...


01.22.2022 - Rust/Notes about Vector and memory allocation

A Vector in Rust is fundamentally a (pointer, capacity, length) triplet. For example, a Vec<Char> containing two elements 'a' and 'b', with the capacity of 4, can be visualized as the following diagram:

The pointer will always be pointed to the head of contiguous elements allocated on the heap....


01.21.2022 - Math/Count number of digits with Logarithm

Suppose that a number $n$ has $d$ digits, then:

$$ 10^{d-1} \leq n \lt 10^{d} $$...


01.20.2022 - Rust/Blanket Implementation

By using generic parameters, you can impl a trait for any type that satisfies some trait bounds, for example, implement trait ToString for every type T that implemented the Display trait:

This technique is called blanket implementation. It's being used widely in Rust standard library....


01.19.2022 - Rust/The Never Type

Rust has a special type ! to imply that there is no return value. Usually called never type.

It allows us to write code like this...


01.18.2022 - Rust/Lifetime Elision Rules

Before Rust 1.0, writing code that returns a reference without any lifetime annotation wouldn't compile. Because Rust does not know what's the lifetime of the returned reference:

In later versions, the Rust compiler can automatically analyze and figure out the lifetime. It is called lifetime elision and is based on a set of rules. For now, these rules are applied for the fn and impl blocks. In the future, more elision rules will be added....


01.17.2022 - JavaScript/Console Assert Command

The built-in console come with the console.assert method, which is pretty useful to write some test in your code.

This method has the syntax of...


01.16.2022 - Rust/Dealing with Integer Overflow

Overflow can happen when doing integer arithmetics in Rust, for example:

Rust handle overflow differently between Debug and Release mode...


01.15.2022 - Rust/Deref and DerefMut

The traits std::ops::Deref and std::ops::DerefMut are used for explicit dereference operations (immutable or mutable), like:

If a type T implements Deref<Target=U> or DerefMut<Target=U> like this...


01.14.2022 - Rust/Implement external traits for external types

One of the rules for working with traits in Rust is: You are not allowed to implement a trait on a type if either the trait or the type is not local to your crate.

For example, within your program, both the trait fmt::Display and type Vec are external because they're defined in the standard library. So, you cannot do something like...


01.13.2022 - Comparing floating-point numbers

Due to rounding errors, most floating-point numbers cannot be represented precisely in the computer. This led to some weird situations like:

Different languages handle this calculation differently....


01.12.2022 - Zig/How ArenaAllocator works

ArenaAllocator is one of Zig's built-in allocators. It takes another allocator and wraps some code around to allow you to allocate memory without the need to free them individually. Every allocated memory will be freed at once when you call ArenaAllocator.deinit().

This post will not go into the details of how memory allocation happens but will focus on how ArenaAllocator manages the memory buffers to be free in a single call....


01.11.2022 - Zig/Build system

There are 4 build modes in Zig, each can be enabled via the arguments -O ReleaseSafe, -O ReleaseSmall, and -O ReleaseFast when running zig run or zig test.

Depending on which mode is enabled, runtime safety checks (things like number overflow check,...) and optimizations will be enabled or disabled...


01.10.2022 - Zig/Handling errors

A function in Zig can return either an error or an actual value, most of the time you will see something like this:

The return type of these functions is the combination of an error set type and an expected return type, and the ! binary operator, also called an error union type...


01.09.2022 - Zig/JSON in 5 minutes

Zig has built-in support for JSON via the std.json module.

To convert any object into a JSON string, we use std.json.stringify(<input>, <options>, <output stream>)...


01.08.2022 - Zig/Case Study: Implementing a Generic Stack

Implementing a stack is one of my favorite tests when working with a new language because it will show us a lot about the language's ergonomics.

We are going to make this stack a generic data structure. By Zig's convention, it will be a function that takes a type comptime T for its elements and returns a type like this...


01.07.2022 - Zig/Cross-compile C/C++ and Zig programs

Zig came with a built-in C/C++ compiler zig cc and zig c++. So you can compile and run your C/C++ code with Zig!

For example, let's compile this simple C program...


01.06.2022 - Zig/Comptime

One of the unique features of Zig is the ability to let us control what happens in our code, during both runtime and compile time.

For optimization, Zig implicitly performs some evaluations at compile-time, for example, in the case of const....


01.05.2022 - Zig/Where data is stored and how to choose an allocator

Zig has no runtime, hence, no automatic memory management. It's the developer's responsibility to manage the allocation/deallocation of memories. It's important to know how memory allocation works, where your data is stored, and what to do with them.

Where data is stored in a Zig program?...


01.04.2022 - Zig/Strings in 5 minutes

Just like C/C++, you can define a string in Zig with an array of bytes or as a null-terminated string literal.

You can reference to any byte in the string just like an array...


01.03.2022 - React/Render a component to any DOM node with React Portals

In React, you can mount a component into any DOM node instead of just the current parent component, with the help of ReactDOM.createPortal([content], [node] ).

It's very helpful when working with modal dialogs or tooltips....


01.02.2022 - TypeScript/Type Intersection

Let's say, we are building a component to display some images (with lazy loading):

And this component takes some properties (ImageProps) that came from the two imaginary types...


01.01.2022 - TypeScript/Non-null assertion operator

Sometimes, you'll run into a case where TS keeps complaining about a possibly undefined value. For example:

The proper way to handle this error is actually to check if a is valid before returning any values...


12.31.2021 - Next.js/Handle Optional Subpath

In Next.js, you can define a dynamic route by creating a file at /pages/post/[id].tsx. And this will catch all URLs like /post/foo, /post/bar,...

But if you want to catch URLs like /post, you will have to create another file at /pages/post/index.tsx....


12.30.2021 - Data Structures/Binary Search Tree/Delete node

Algorithm to delete a node from a BST:



12.29.2021 - Rust/GUI/Druid/Custom Widget

To create a custom widget in Druid, we create a new struct and implement Widget trait. Or create a function that returns an impl Widget.

Widget trait defined a few lifecycle methods for a Druid's widget...


12.28.2021 - Data Structures/Binary Tree/In-order Traversal with Stack

This one is a bit more compex than the pre-order (in term of using stack).



12.27.2021 - Algorithms/Find Palindrome

When solving palindrome problems, we need to avoid sliding window solution (a.k.a brute force), where we try to generate every substring and check to find the palindrome.

The good approach to go for is the expandable solution....


12.26.2021 - Algorithms/Partition a number to sum of smaller ones

The array x is to keep track of all possible integers we can find, the array t is to keep the sum t[i] of every x number from x[1] to x[i].

Now, let's write a Generator algorithm with Backtracking....


12.25.2021 - Algorithms/Generate repeated combinatorial

The algorithm is simple:

It can be implemented as following...


12.24.2021 - Scala/Partial Applied Function

Like currying, if a function takes some arguments and we invoke that function without all of the arguments, a new function will be created so we can call it with the remaining functions later on.

In Scala, it is called Partial Applied Function, and we can call it with the place holder like this...