Skip to content

Instantly share code, notes, and snippets.

@monotykamary
monotykamary / red_black_tree.ex
Created December 25, 2024 03:19
Red Black Tree with Pattern Matching Elixir
defmodule RedBlackTree do
@type color :: :red | :black
@type tree :: nil | {color, any, tree, tree}
@spec insert(tree, any) :: tree
def insert(nil, value), do: {:black, value, nil, nil}
def insert(tree, value) do
{_, new_value, new_left, new_right} = do_insert(tree, value)
{:black, new_value, new_left, new_right}
end
@monotykamary
monotykamary / flow_skiplist.ex
Last active December 25, 2024 03:20
Flow Skiplist in Elixir
defmodule FlowSkiplist do
@chunk_size 1000
def new(data) do
data
|> Flow.from_enumerable()
|> Flow.partition()
|> Flow.reduce(fn -> [] end, fn elem, acc -> [elem | acc] end)
|> Enum.to_list()
|> List.flatten()
@monotykamary
monotykamary / mcp_cline.json
Created December 25, 2024 03:05
MCP Cline
{
"mcpServers": {
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "..."
}
},
"sequential-thinking": {
@monotykamary
monotykamary / validator.ex
Created December 18, 2024 11:33
Validator for multi-sourced Hot Landing Zone
defmodule Nghenhan.MarketDataValidator do
@moduledoc """
Validates market data completeness and quality by reading from the landing zone.
Runs as a separate service that can identify data quality issues without
affecting raw data collection.
"""
use GenServer
require Logger
@monotykamary
monotykamary / distribute_totals_through_x.excel
Created December 18, 2024 11:26
Excel/Sheets distribute value through Indirect count and substitution
=ARRAYFORMULA(
SUM(
IF(
INDIRECT(SUBSTITUTE(ADDRESS(1,COLUMN(),4), "1", "") & "2:" & SUBSTITUTE(ADDRESS(1,COLUMN(),4), "1", "") & COUNTA($A:$A)) = "x",
INDIRECT("$R2:$R" & COUNTA($A:$A)) /
INDIRECT("$Q2:$Q" & COUNTA($A:$A)),
0
)
)
)
@monotykamary
monotykamary / landing_disk_log.ex
Last active December 14, 2024 18:06
Hot Landing Zone with Single-Node Disk Log
defmodule Nghenhan.LandingZone do
@moduledoc """
Hot Landing Zone implementation with fast recovery using segmented logs,
separate index files, and checkpoints.
"""
use GenServer
require Logger
@checkpoint_interval 300_000 # 5 minutes
@monotykamary
monotykamary / devbox.json
Created November 30, 2024 16:02
Devbox env setup for Zed editor contribution
{
"packages": [
"rustup@latest",
"libiconv@latest",
"rustc-wasm32@latest",
"wasmtime@latest",
"cmake@latest"
],
"shell": {
"init_hook": [
@monotykamary
monotykamary / exactly_once.ex
Created November 26, 2024 00:27
Exactly Once Processing
defmodule ExactlyOnce do
use GenServer
require Logger
# State will store processed event IDs and their status
defmodule State do
defstruct processed_events: %{},
processing_timeouts: %{},
retry_interval: 5_000,
processing_timeout: 30_000
@monotykamary
monotykamary / 0_library.ts
Last active November 21, 2024 16:06
Elixir Protocols in TypeScript - Type Safest Version
function createDataType<T extends { type: string }>() {
return new Proxy(
{},
{
get(_, type: string) {
return (data = {}) => ({ type, ...data });
},
}
) as {
[K in T['type']]: (
@monotykamary
monotykamary / decorator.exs
Created November 19, 2024 08:00
FLAME Elixir as a Decorator
defmodule DiscordCollector.FLAMEDecorator do
@moduledoc """
Provides FLAME function decorators that wrap functions to run in FLAME pools.
## Usage
defmodule MyModule do
use DiscordCollector.FLAMEDecorator
@decorate flame(MyApp.Pool)