Add some sugar into Elixir

Let’s get a look at the package, which makes then code in pipelines more readable.

I’m sure, most of you use pipelines, somebody does with Kernel’s functions like ==, >= , ++ and etc.

You know, these functions look ugly (When uses with Kernel namespace), especially in pipelines.

There are some examples (from production projects):

Example #1

def date_and_month(datetime) do
Timex.format!(datetime, "{D} ")
|> Kernel.<>(@month_labels[datetime.month])
end

Example #2

def adult?(params) do
params
|> Map.get(:age)
|> String.to_integer()
|> Kernel.>=(@adult_age)
end

I’ve seen a project, that has module with wrappers for Kernel’s comparing functions: >, >=, <, <=, ==, !=

Wrappers was called gt?, gteq?, lt?, lteq?, eq?, not_eq? respectively.

The main reason why I’ve created package BeautyKernel — We need to have some standards for aliasing Kernel functions in projects. Custom modules with aliases are mistake!

Usage of BeautyKernel

The package is useful in pipelines, if you’re tired of ugly Kernel calls, like shown below:

defmodule Foo do
def bar do
list_of_data
|> elvis_function()
|> Kernel.++(additional_data)
end
end

becomes beauty ❤, just look:

defmodule Foo do
import BeautyKernel, only: :functions
  def bar do
list_of_data
|> elvis_function()
|> list_concat(additional_data)
end
  def zar? do
# usage without import
data
|> elvis()
|> BeautyKernel.gteq?(42)
end
end

list_concat is BeautyKernel’s alias for Kernel.++

Existing aliases


Installation

The package can be installed by adding beauty_kernel to your list of dependencies in mix.exs:

def deps do
[
{:beauty_kernel, "~> 0.1.0"}
]
end

Github — https://github.com/SofaKing18/beauty_kernel
Hex.pm — https://hex.pm/packages/beaty_kernel/0.1.0

If you have any suggestions or requests, be sure to write about them in the comments