Skip to content

Instantly share code, notes, and snippets.

View kbridge's full-sized avatar

kq kbridge

  • Sunnydale
View GitHub Profile

Understanding the Phases Applicative

While I was researching how to do level-order traversals of a binary tree in Haskell, I came across a library called tree-traversals which introduced a fancy Applicative instance called Phases. It took me a lot of effort to understand how it works. Although I still have some unresolved issues, I want to share my journey.

Note: I was planning to post this article on Reddit. But I gave up because it was too long so here might be a better place.

See the discussion.

Note: This article is written in a beginner-friendly way. Experts may find it tedious.

@kbridge
kbridge / a.hs
Created February 20, 2024 09:40
demostrate monad transformers and forever
import Control.Monad
import Control.Monad.Trans.Class -- from `transformers`
import Control.Monad.Trans.Maybe -- from `transformers`
import Data.IORef
main :: IO ()
main = do
i <- newIORef (0 :: Int)
void $ runMaybeT $ forever $ do
n <- lift (readIORef i)
@kbridge
kbridge / rtti_does_more.cpp
Created January 25, 2024 19:11
RTTI does more than type-checking
#include <iostream>
#include <type_traits>
class Bird
{
public:
virtual ~Bird() = default;
};
class IFly
@kbridge
kbridge / cpp23_support.cpp
Last active January 24, 2024 11:44
play with new features added in cpp23. tested in VS2022 Version 17.8.2.
#include <iostream>
#include <version>
#ifdef __cpp_lib_print
# include <print>
#endif
#ifdef __cpp_lib_expected
# include <expected>
# include <system_error>
@kbridge
kbridge / VerifyOnStack.cpp
Created January 19, 2024 12:11
method extracted from v8 source code. MSVC/Windows only.
#include <windows.h>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <intrin.h>
#include <stdint.h>
@kbridge
kbridge / fold.cpp
Created January 4, 2024 15:35
demostratse fold expressions introduced C++17 (requires C++20 to run :D)
#include <format>
#include <iostream>
#include <string>
#include <string_view>
class Factor
{
public:
Factor(std::string_view representation) :
m_representation(representation)
@kbridge
kbridge / get_random_string.cpp
Created January 3, 2024 11:56
demonstrate the usage of c++ standard library random facilities
#include <iostream>
#include <random>
#include <string>
std::string get_random_string(size_t n)
{
std::random_device device;
std::default_random_engine::result_type seed = device();
std::default_random_engine engine(seed);
std::uniform_int_distribution<int> distribution('a', 'z');
@kbridge
kbridge / puzzle.ml
Last active December 14, 2023 17:51
ocaml implementation of a puzzle operator
module Puzzle = struct
let (+) a b =
let rec work accum base remain =
if remain = 0
then accum
else work
(Int.add accum (remain mod b * base))
(base * 10)
(remain / b)
in
@kbridge
kbridge / winrt.c
Created December 3, 2023 18:38
demo: call windows runtime from pure c
// please link with "runtimeobject.lib"
#include <roapi.h>
#include <winstring.h>
#include <windows.foundation.h>
#include <stdio.h>
#include <stdlib.h>
#!/bin/bash
# the uninstall script corresponding to the install script here:
# https://clojure.org/guides/install_clojure#_linux_instructions
set -e
prefix_dir=/usr/local
rm -rfv $prefix_dir/lib/clojure