Skip to content

Instantly share code, notes, and snippets.

@kipcole9
kipcole9 / type_from_list.ex
Last active Jul 18, 2022
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 [
:animate,
:inanimate,
:personal,
:common,
:feminine,
:masculine,
@kipcole9
kipcole9 / string_index.ex
Created Jul 5, 2022
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
kipcole9 / distance.ex
Last active Mar 29, 2022
Distance graph builder
View distance.ex
defmodule Distance do
@moduledoc """
Ensure that you have the following in your mix.exs
file:
def deps do
{:libgraph, "~> 0.13"}
end
"""
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
View ldml2json.java
>> 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 logicalGroupstrue
#-t timetrue
#-S Sidewaystrue
#-r roottrue
#-c constrainedRestorationtrue
#-i includeComprehensivetrue
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
[]
@kipcole9
kipcole9 / Parser.ex
Last active Jun 21, 2021
Parsing a simple expression grammar with maybe quoted strings
View Parser.ex
defmodule ParseHelpers do
import NimbleParsec
def whitespace() do
ascii_char([?\s])
|> repeat
end
def quote_mark(combinator \\ empty()) do
combinator
@kipcole9
kipcole9 / pig_latin.ex
Last active Jun 2, 2020
Elixir implementation of pig latin translation
View pig_latin.ex
defmodule PigLatin do
# vowels: a, e, i, o, u, y_, x_
@vowels ["a", "e", "i", "o", "u"]
# special constants: "ch", "qu", "squ", "th", "thr", "sch"
@special ["ch", "qu", "squ", "thr", "th", "sch"]
@ay "ay"
defguard is_vowel(c) when c in @vowels
@kipcole9
kipcole9 / decode.ex
Created Jan 2, 2020
Decode a float in Elixir
View decode.ex
defmodule Decode do
@moduledoc """
Extracted from https://github.com/ewildgoose/elixir-float_pp
"""
use Bitwise
@float_bias 1022
############################################################################