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 / 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 / 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 / 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 / 1_enum_fetch_benchmarking_report.md
Last active July 6, 2016 06:15
Enum.fetch/2 optimization for maps, negative indexes, out of bound indexes, empty enumerables, and enumerables with only one element.

Optimize Enum.fetch/2 for maps, negative indexes, out of bound indexes, empty enumerables, and enumerables with only one element.

This function has been highly-optimized, through thorough benchmarking.

There were serious issues when dealing with big lists and maps when an out of bound or a negative index was given. Ex. fetching an out-of-bound index in a 1,000-element map, was reduced to 0.18% of the original item. Or same case with a 1,000-element list time was reduced to a 10,5%.

  • Enumerables are no longer reversed when dealing with negative indexes.
defmodule EnumRandomOriginal do
def random(enumerable) do
case Enum.take_random(enumerable, 1) do
[] -> raise Enum.EmptyError
[e] -> e
end
end
end
defmodule EnumRandomNew do
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 / make-test-file
Last active November 10, 2015 17:05
A script to ease development of the Elixir programming code base. Allowing the developer to run selected tests.
#!/bin/sh
# make-test-file
# https://git.io/make-test-file
#
# DESCRIPTION:
# make-test-file is a script to ease development of the Elixir programming code
# base. Allowing the developer to run selected tests.
#
# INTRODUCTION:
@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 / .bash_funcs
Last active April 4, 2021 00:48
ag-l1 : ag – searches files and list them with the line of the first match. Useful to combine with with xargs and your favorite text editor.
#######################################################################
# `ag` list file with first match line number
# Returns a list of files including the first line matched in the file
# emulates the non-implemented option: `ag --files-with-matches-line-number`
# for `ag` (the silver searcher) https://github.com/ggreer/the_silver_searcher
# feature request: https://github.com/ggreer/the_silver_searcher/issues/715
# Usage: just simple use it as you would use `ag -l`, but ommit the -l option
# $ ag-l1 "(pattern)?.*"
# Installation: add this function to your ~/.bash_funcs file
# Download: https://git.io/ag-l1
@eksperimental
eksperimental / check_not_linked_exdoc.sh
Last active September 4, 2015 03:02
Check for references to exiting modules in the documentation, that is actually not being liked by ExDoc, in the documentation for the Elixir project
#!/bin/bash
# ./check_not_linked_exdoc.sh – http://git.io/check_not_linked_exdoc.sh
#
# Description:
# Check for references to exiting modules in the documentation, that is actually not being liked
# by ExDoc.
#
# Instructions
# Run from the root folder in the elixir project – https://github.com/elixir-lang/elixir/