Skip to content

Instantly share code, notes, and snippets.

kipcole9 / sanitize_string.ex
Last active March 2, 2023 06:48
Sanitize a string using [unicode_set](
View sanitize_string.ex
defmodule Sanitize do
# Unicode sets are defined at
require Unicode.Set
# Defines a guard that is the intersection of alphanumerics and the latin script plus the
# space and underscore characters. Note that the set is resolved at compile time into an
# integer expression and is therefore acceptably performant at runtime.
defguard latin_alphanum(c) when Unicode.Set.match?(c, "[[:Alnum:]&[:script=Latin:][_\\ ]]")
def sanitize_string(<<"">>), do: ""
kipcole9 / accumulate_list.ex
Created December 30, 2022 19:07
Accumulate the sum of list values until a max is reached
View accumulate_list.ex
defmodule Acc do
@moduledoc """
Recurses through a list of maps summing the amount.
If the max is exceeded it returns the list up to this point.
@list [
%{amount: 23, asset: "USD"},
%{amount: 40, asset: "USD"},
kipcole9 / nimble_split.ex
Created December 27, 2022 21:59
Split text at a separators using NimbleParsec
View nimble_split.ex
defmodule NimbleSplit do
@moduledoc """
Split text at a separator using [nimble_parsec](
import NimbleParsec
def test_string do
some text, may also contain character "=", which is part of the separator
can be any number of lines long until the first separator line
kipcole9 / type_from_list.ex
Last active July 18, 2022 23:31
Create a union type from a list
View type_from_list.ex
# Converts a list of atoms into a typespec
type = &Enum.reduce(&1, fn x, acc -> {:|, [], [x, acc]} end)
@grammatical_gender [
kipcole9 / string_index.ex
Created July 5, 2022 00:32
Index a binary at newlines and get a line using the index map
View string_index.ex
defmodule StringIndex do
@newline 10
@doc """
Returns a map of line numbers to
a 2-tuple that is the index at which
the line starts in `string` and the
length of the line.
### Example
kipcole9 / distance.ex
Last active March 29, 2022 00:23
Distance graph builder
View distance.ex
defmodule Distance do
@moduledoc """
Ensure that you have the following in your mix.exs
def deps do
{:libgraph, "~> 0.13"}
View ecto_bitstring_type.ex
if Code.ensure_loaded?(Ecto.Type) do
defmodule Bitstring do
@moduledoc """
Implements the Ecto.Type behaviour for the Postgres
type "bit varying"
use Ecto.Type
>> org.unicode.cldr.tool.GenerateProductionData
[-s, /Users/kip/Development/cldr_repo/common, -d, /Users/kip/Development/cldr_staging_data/common]
#-s sourceDirectory ≔ /Users/kip/Development/cldr_repo/common
#-d destinationDirectory ≔ /Users/kip/Development/cldr_staging_data/common
#-l logicalGroups ≝ true
#-t time ≝ true
#-S Sideways ≝ true
#-r root ≝ true
#-c constrainedRestoration ≝ true
#-i includeComprehensive ≝ true
View channel.ex
defmodule Channel do
@input [{0, 190}, {1, 0}, {2, 0}, {3, 0}, {6, 220}, {9, 90}]
@doc """
Map a list of {index, value} pairs
into a binary, filling in any gaps in
the index sequence with <<0>> as
the list is built.
View extract_delimited_words.ex
defmodule Extract do
@moduledoc """
Split into words only when the words have a "%" as a prefix
or a suffix
# Empty string returns empty list
def words("") do