Skip to content

Instantly share code, notes, and snippets.

View kbridge's full-sized avatar

kq kbridge

  • Sunnydale
View GitHub Profile
@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 / 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 / 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 / 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 / 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)

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.