Skip to content

Instantly share code, notes, and snippets.

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
@kipcole9
kipcole9 / nimble_split.ex
Created December 27, 2022 21:59
Split text at a separators using NimbleParsec
defmodule NimbleSplit do
@moduledoc """
Split text at a separator using [nimble_parsec](https://hex.pm/packages/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
kipcole9 / Map.Helpers
Last active October 24, 2023 22:13
Helpers for Elixir Maps: underscore, atomise and stringify map keys
defmodule Map.Helpers do
@moduledoc """
Functions to transform maps
"""
@doc """
Convert map string camelCase keys to underscore_keys
"""
def underscore_keys(nil), do: nil
@kipcole9
kipcole9 / function_clause.ex
Created October 23, 2019 11:04
Debug Elixir Function Clause errors
defmodule FunctionClause do
@moduledoc """
Format function clauses using Exception.blame/3
"""
@doc """
Given a `module`, `function`, and `args` see
if that function clause would match or not match.
This is useful for helping diagnose function
@kipcole9
kipcole9 / decode.ex
Created January 2, 2020 13:05
Decode a float in Elixir
defmodule Decode do
@moduledoc """
Extracted from https://github.com/ewildgoose/elixir-float_pp
"""
use Bitwise
@float_bias 1022
############################################################################
@kipcole9
kipcole9 / sanitize_string.ex
Last active March 2, 2023 06:48
Sanitize a string using [unicode_set](https://hex.pm/packages/unicode_set)
defmodule Sanitize do
# Unicode sets are defined at https://unicode-org.github.io/icu/userguide/strings/unicodeset.html
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
kipcole9 / accumulate_list.ex
Created December 30, 2022 19:07
Accumulate the sum of list values until a max is reached
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
kipcole9 / type_from_list.ex
Last active July 18, 2022 23:31
Create a union type from a list
# Converts a list of atoms into a typespec
type = &Enum.reduce(&1, fn x, acc -> {:|, [], [x, acc]} end)
@grammatical_gender [
:animate,
:inanimate,
:personal,
:common,
:feminine,
:masculine,
@kipcole9
kipcole9 / string_index.ex
Created July 5, 2022 00:32
Index a binary at newlines and get a line using the index map
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
kipcole9 / distance.ex
Last active March 29, 2022 00:23
Distance graph builder
defmodule Distance do
@moduledoc """
Ensure that you have the following in your mix.exs
file:
def deps do
{:libgraph, "~> 0.13"}
end
"""