Skip to content

Instantly share code, notes, and snippets.

@potatosalad
potatosalad / trigger-otp-24-persistent-term-bug.erl
Created Mar 31, 2021
trigger-otp-24-persistent-term-bug.erl
View trigger-otp-24-persistent-term-bug.erl
N = 8000,
Terms = [{{a, list_to_atom(integer_to_list(I))}, I} || I <- lists:seq(1, N)],
Add = fun Add([]) -> ok; Add([{K, V} | T]) -> persistent_term:put(K, V), Add(T) end,
Len = fun() -> length(persistent_term:get()) end,
Random = fun() -> PTerms = [{X, Y} || {{a, X}, Y} <- persistent_term:get()], lists:nth(rand:uniform(length(PTerms)), PTerms) end,
DelRandom = fun() -> Key = element(1, Random()), persistent_term:erase({a, Key}), Key end,
EraserFun = fun() -> Eraser = fun Eraser() -> receive _ -> Eraser() after 1 -> DelRandom(), Eraser() end end, Eraser() end,
WriterFun = fun() -> Writer = fun Writer() -> receive _ -> Writer() after 1 -> Add(Terms), Writer() end end, Writer() end,
ReportFun = fun() -> Report = fun Report() -> io:format("there are ~w persistent terms~n", [Len()]), receive _ -> Report() after 1000 -> Report() end end, Report() end,
Add(Terms),
View max-subset-sum-no-adjacent.py
# Max Subset Sum No Adjacent
# https://www.algoexpert.io/questions/Max%20Subset%20Sum%20No%20Adjacent
#
# Write a function that takes in an array of positive integers
# and returns the maximum sum of non-adjacent elements in the array.
#
# If a sum can't be generated, the function should return `0`.
#
# Input:
# A = [75, 105, 120, 75, 90, 135]
View toeplitz-matrix-validator.py
# Store 3 hashes:
# When `row == 0`, we do not yet know the maximum length of `col`:
# 1. H[0] = 0 or previous H[1] (hash of all values excluding the final one for the column)
# 2. H[1] = hash((H[1], value)) (hash of all values including the final one for the column)
# 3. self.end = col
# When `row > 0` and `col < self.end`:
# 1. H[1] = hash((H[1], value)) (hash of left-hand values, excluding last)
# 2. H[2] = 0 or hash((H[2], value)) (hash of right-hand values, excluding first)
# When `col == self.end`:
# - Compare H[0] with H[2], if they don't match return False
View cancellable.ts
export interface CancellablePromise<T> extends Promise<T> {
cancel(reason: any): void
}
export class Cancellable {
public promise: Promise<never>
private cancelled: boolean
private reason?: any
constructor() {
View Cargo.toml
[package]
name = "nth-prime"
version = "0.1.0"
edition = "2018"
[dependencies]
futures-preview = "0.3.0-alpha.16"
View bench.exs
n = 1_000_000
bmap = FooBench.start(FooMap, n)
btup = FooBench.start(FooTuple, n)
Benchee.run(%{
"bmap.read" => fn -> FooBench.readall(bmap) end,
"btup.read" => fn -> FooBench.readall(btup) end
}, memory_time: 5)
Benchee.run(%{
View my_cache.ex
defmodule MyCache do
@behaviour :gen_statem
@table :my_cache
@expire_after :timer.seconds(45)
@vacuum_idle_timeout :timer.minutes(5)
@vacuum_dead_timeout :timer.seconds(60)
# Public API functions
View README.md

Build or install nghttp2 (optionally with support for the --interval DURATION flag).

Either:

brew install nghttp2

Or:

View lamport_clock.ex
defmodule :lamport_clock do
@moduledoc ~S"""
# Example
iex> lc0 = :lamport_clock.new()
%:lamport_clock{value: 1}
iex> # Node: A
iex> lc1a = :lamport_clock.increment(lc0)
%:lamport_clock{value: 2}
View quickbench.erl
%% -*- mode: erlang; tab-width: 4; indent-tabs-mode: 1; st-rulers: [70] -*-
%% vim: ts=4 sw=4 ft=erlang noet
-module(quickbench).
%% API
-export([bench/2]).
-export([bench/3]).
-export([compare/3]).
%% Records