/categories/elixir/index.xml

Enum Methods comparison in Ruby/ Javascript and Elixir

Enum Methods comparison in Ruby/ Javascript and Elixir

all?

The all? method in Ruby returns a boolean value (true/false) if all the objects in the list either return true or satisfies the block condition.

Ruby

Enumerable#all?

No block:

>> [ true, "dog", "bear", 19 ].all?
=> true
>> [ nil, true, "hello" ].all?
=> false

With a block:

>> %w(cat dog squirrel bear).all? { |word| word.length >= 3 }
=> true
>> %w(cat dog squirrel bear).all? { |word| word.length <= 3 }
=> false

Elixir

Enum.all? No block:

>> Enum.all?([ true, "dog", "bear", 19 ])
true
>> Enum.all?([ nil, true, "hello" ])
false

With a block/fn:

>> Enum.all?(["cat", "dog", "squirrel", "bear"], fn word -> String.length(word) >= 3 end)
=> true
>> Enum.all?(["cat", "dog", "squirrel", "bear"], fn word -> String.length(word) <= 3 end)
=> false

Javascript

Array.prototype.every()

Only with callback support:

> [ true, "dog", "bear", 19 ].every(x => x)
true
> [null, true, "hello" ].every(x => x)
false

> ["cat", "dog", "squirrel", "bear"].every(word => word.length >= 3)
true
> ["cat", "dog", "squirrel", "bear"].every(word => word.length <= 3)
false

Read More

Creating Elixir Protocol with defprotocol

Creating Elixir Protocol with defprotocol

Elixir has a very flexible feature called Protocols. These can be used for helper utilities modules.

defprotocol Double do
  def double(input)
end

defimpl Double, for: Integer do
  def double(int) do
    int * 2
  end
end

defimpl Double, for: List do
  def double(list) do
    list ++ list
  end
end

Hoping I can do more on how this can be used in a phoenix app.

Read More

Elixir's get nexted key and value in nested Map

Elixir’s get nexted key and value in nested Map

Although it is not super intuitive since it does not belong to the Map module, Elixir has a built in get_in function which is used to retrieve a nested value in nested maps using an array of keys.

movie_map = %{ movie: %{ title: "Deadpool"} }
title = get_in(movie_map, [:movie, :title])
=> nil
# When value is not present, returns nil
description = get_in(movie_map, [:movie, :description])
=> nil
director_name = get_in(movie_map, [:movie, :cast, :director, :first_name])
=> nil

Read More

Chaining Elixir's "with" context

Chaining Elixir’s “with” context

Elixir has a powerful withconstruct that isn’t availabe in ruby. This is an elegant pattern for a language. It can be used to pull out individual values or it can be chained.

Individual value example (yes, a bit contrived):

def divide(values) do
  with {:ok, dividend} <- Map.fetch(values, :dividend),
       {:ok, divisor} <- Map.fetch(values, :divisor) do
    {:ok, dividend / divisor}
  end
end

Read More

Building a Baby Age Calculuator with Phoenix/Elixir LiveView

In this article I share how I was able to create a Baby Age Calculator using Phoenix/Elixir LiveView.

If you just want to know how old your child is, jump over to babyweeks.herokuapp.com. If you want to download the source code, goto github.com/monkseal/babyweeks-phx-liveview

Building a Baby Age Calculuator with Phoenix/Elixir LiveView

LiveView - Phoenix/Elixir’s Javascript killer?

As described here and here — “Phoenix LiveView is an exciting new library which enables rich, real-time user experiences with server-rendered HTML. LiveView powered applications are stateful on the server with bidrectional communication via WebSockets, offering a vastly simplified programming model compared to JavaScript alternatives.”

If you don’t want to write Javascript to have a real-time web application, Phoenix LiveView provides an interesting alternative with server-rendered HTML.

Being one to avoid bold pronouments, I won’t commit myself to say that LiveView will deal a death blow to javascript in your app. For one, you may already have a lot Javascript in your app. Maybe some if you even like. However, if you are interested in managing state on the server, LiveView is quite compelling.

Read More