Skip to content

Instantly share code, notes, and snippets.

View eksperimental's full-sized avatar

Eksperimental eksperimental

  • Available for hire
  • Remote
View GitHub Profile
@eksperimental
eksperimental / enum_fetch.ex
Last active July 16, 2016 02:01
Enum.fetch/2 Optimizations
defmodule EnumFetchHelpers do
## fetch
def fetch_list([], _index),
do: :error
def fetch_list([head | _], 0),
do: {:ok, head}
def fetch_list([_ | tail], index),
do: fetch_list(tail, index - 1)
@eksperimental
eksperimental / enum_slice_bench.exs
Last active July 16, 2016 12:48
Enum.slice rewrite
defmodule Data do
@slice_counts [0, 1, 100, 990, ]
@terms [
:range, :range_single, :range_big, :range_huge,
:list, :list_empty, :list_single, :list_big, :list_huge,
:map, :map_empty, :map_single, :map_big, :map_huge,
:struct, :struct_empty, :struct_single, :struct_big, :struct_huge,
]
@eksperimental
eksperimental / PROPOSAL.md
Last active August 11, 2016 15:27
## Introducing is_kind/2, and operators: is, is_not, is_any, are, are_not, are_any

Introducing is_kind/2, and operators: is, is_not, is_any, are, are_not, are_any

Guards clauses are a key feature in Elixir. Researching how to make it easier for developers to define guards, has led me to two enhancement proposal. This is the first one, which will allow developers to write guards, guard-safe macros and regular expressions in a more natural and succinct way.

TLDR;

The following macro is allowed in guards:

  • is_kind(term, kind) determines if a given term is of a certain kind.

as well as the following operators:

  • term is kinds determines if term is each kind in kinds.
@eksperimental
eksperimental / guard_helpers.ex
Last active August 13, 2016 13:07 — forked from christhekeele/guard_helpers.ex
A defguard macro written for Elixir v0.11.something a while back. I don't remember anything breaking at the time. Written for a library that was supposed to help AST transformations, in part by creating guards for particular AST constructs.
defmodule Guard.Helpers do
@moduledoc """
Tools for creating custom guards.
"""
@doc """
Creates a macro that's aware of its presence in a guard.
Taken from https://github.com/elixir-lang/elixir/blob/df8b216357e023e4ef078be396fed6b873d6a938/lib/elixir/lib/kernel.ex#L1601-L1615,
@eksperimental
eksperimental / record_file.exs
Created September 23, 2016 14:43
How to parse a text file with pattern matching, and converted to a nested list
# https://elixirforum.com/t/newbie-needs-help-parsing-a-file/1762
defmodule RecordFile do
def read(file) do
{:ok, data} = File.read(file)
data
|> String.split("\n")
|> filter
end
defmodule ForLoop do
@doc ~S"""
Iterates over `term`, while `condition` between `term` and `final` is truthy,
applying `fun`. It returns the accumulated terms.
- `term` is any that will be iterated over.
- `final_term` is the element that will be run against `term` in `condition`.
- `condition` can take 1 argument: `term`, or two arguments: `term` and `final`.
- `fun` is run every time a `condition` is evaluated to truthy.
- `transform` is applied to `term` at the end of every loop, before the next iteration.
@eksperimental
eksperimental / gist:701103fc9e723fddc0bfeb757ecb1e7d
Created May 25, 2017 20:00
What modules implement the Enumerable and Collectable protocols in Elixir
Collectable
elixir$ ag "defimpl\s+Collectable" -G ".ex$"
HashDict
lib/elixir/lib/hash_dict.ex
247:defimpl Collectable, for: HashDict do
IO.Stream
lib/elixir/lib/io/stream.ex
35: defimpl Collectable do
File.Stream
@eksperimental
eksperimental / typespec_example.ex
Last active July 2, 2020 18:36
Broken types in Elixir Issue #10140
# Downloaded from: https://gist.github.com/eksperimental/55f1e207ab5878a668668546f57a3f90
#
# Lists all the types that have the problem reported in
# https://github.com/elixir-lang/elixir/issues/10140
# create an Elixir project, and save this file in lib/typespec_example.ex
# Start IEx with: iex -S mix
# then run: TypespecExample.types()
# and paste the output into the shell.
defmodule TypespecExample do
@eksperimental
eksperimental / kernel_to_boolean.ex
Last active December 27, 2020 07:47
Kernel.to_boolean/1
defmodule Kernel do
@doc """
Converts any expression to boolean.
Returns `true` if `expr` is truthy (ie. it does not evaluate to `nil`, nor `false`),
otherwise returns the `false`.
Allowed in guard tests.
"""
@spec to_boolean(Macro.t) :: boolean
@eksperimental
eksperimental / variadic.exs
Last active March 19, 2021 14:13
Build anonymous functions with a variable length of arguments
defmodule Variadic do
@moduledoc """
Solution for https://elixirforum.com/t/defining-an-anonymous-function-of-dynamic-arity-not-variadic/38228
"""
defmacro spread_combine(h, f, g) do
quote bind_quoted: [h: h, f: f, g: g, module: __CALLER__.module],
location: :keep do
{:arity, f_arity} = Function.info(f, :arity)
{:arity, g_arity} = Function.info(g, :arity)