This will help you understand when to use Recursion or Enum with Elixir

postelxpro

Elxpro

Posted on June 19, 2022

This will help you understand when to use Recursion or Enum with Elixir

Greeting

https://www.youtube.com/watch?v=kyOv8XbvqAs

Hello!!! Welcome to #FullStackElxpro

Do you want to learn Elixir in three months? https://elxpro.com/sell

Here we discuss strategies and tips for your Elixir learning journey. This is from scratch until your first wave as an elixir developer

I am Gustavo and today's theme is: Recursion or Enum with Elixir

What is the difference between Recursion and Enum?

ENUM: A set of algorithms for enumerating collections.

Recursion: is a problem-solving method that involves breaking a problem into smaller and smaller sub-problems until you get to a problem small enough that it can be solved trivially. Usually, recursion consists of a function that calls itself. While it may not seem like much, recursion allows us to write elegant solutions to problems that might otherwise be very difficult to program.

What are the most significant benefits of using Recursion?

All my posts are always related to my experience, and how I use it daily, and always my biggest one is to explain and give you the situations that I went through for you to make your decisions on a daily basis. What I have as an advantage with recursion is having full control of your most technical view and knowing how to break it down into small steps and steps (which in the beginning is not so simple and requires a lot of practice) but we are going to have an example today very practice. you can give good names to variables using a variety of patterns-matchings, called independent functions.

What's worth more? Do Recursion or use Enum?

that is an interesting question and the most interesting thing to think about here are 3 points:

  1. Performance
  2. Code reading
  3. Troubleshooting.

Performance

The issue of performance is always important on large scale, processing thousands of rows and where it can complicate your recursion. What is there for you to know more about asymptotic analysis. **big O notation **and knowing something to improve.

ps: some points and data structure in elixir is an interesting topic to cover but that's for another article.

The issue of Enum, when I compare performance it without data perseverance can be noticed, however sometimes I have much more control in recursion and outputs and you learn railroad techniques and you learn about this topic.

Code reading

I believe recursion can be fatal and make code from other developers difficult to read.

The same goes for Enum, but for an Elixir developer (or not) the enum is much more comfortable.

Troubleshooting

Recursion in this case has always been my favorite because I have control over TDD techniques and don't need to try and error what is the best function to call from Enum. But this has been my experience, using pair programming has been the best solution and other devs have followed along very well. But reading to others without the context was complicated to refactor or fix bugs…

How did you come to that conclusion?

As a developer, I have no right or wrong of Enum or Recursion and for a long time I use more recursion, because for me it is easier to break the problem into small pieces and to facilitate the use of TDD.

Only I came across that people don't recognize me the code when using.

A Benchmark if necessary and also someone else reviewing your code to give you feedback on what is best for your animal's context.

What is important to remember is that under the hood Enum uses recursion and not knowing how to use recursion can put you in difficult decision making situations.

Where to start?

Change Calculator
Imagine a scenario where you have 5 dollars, and you spent 0.99 cents, and you have to return change with coins of:

  • 1 cent
  • 5 cents
  • 10 cents
  • 25 cents
  • 50 cents
  • Dollars

Image description

The function execute(5, 0.99) will receive the values ​​and the first calculation allows for subtraction. And since we're talking about non-integers, let's use the float to round or value:

Image description

Our next iteration is very simple. Just convert this value to String which is to the left of the point and our dollar value to the right and where we have to calculate the change.

Image description

Now to calculate the cents we have to do 3 things.

Make sure we only take two decimal places
Convert to String
And create a refusal with a default value of an array counting the change coins, subtracting the value, by the value of the largest coin, and incrementing a contact like an example below:

Image description

And the final result will be:

defmodule ChangeCalculator do
  # 1c, 5c, 10c, 25c, 50c
  def execute(amount, value_spent) do
    (amount - p)
    |> Float.round(2)
    |> _get_usd
    |> _get_cents
  end

  defp _get_usd(value) do
    value
    |> Float.to_string()
    |> String.split(".")
    |> then(fn [real, cents] ->
      real = String.to_integer(real)
      {real, cents}
    end)
  end

  defp _get_cents({real, cents}) do
    cents =
      cents
      |> String.split_at(2)
      |> then(fn {cents, _} -> String.to_integer(cents) end)

    _convert_cents_to_currency(cents) ++ [real]
  end

  defp _convert_cents_to_currency(cents) do
    _check_currency(cents, [0, 0, 0, 0, 0])
  end

  defp _check_currency(0, array), do: array

  defp _check_currency(value, [one_c, five_c, ten_c, t_c, fifty_c]) when value >= 50 do
    _check_currency(value - 50, [one_c, five_c, ten_c, t_c, fifty_c + 1])
  end

  defp _check_currency(value, [one_c, five_c, ten_c, t_c, fifty_c]) when value >= 25 and value < 50 do
    _check_currency(value - 25, [one_c, five_c, ten_c, t_c + 1, fifty_c])
  end

    defp _check_currency(value, [one_c, five_c, ten_c, t_c, fifty_c]) when value >= 10 and value < 25 do
    _check_currency(value - 10, [one_c, five_c, ten_c + 1, t_c, fifty_c])
  end

  defp _check_currency(value, [one_c, five_c, ten_c, t_c, fifty_c]) when value >= 5 and value < 10 do
    _check_currency(value - 5, [one_c, five_c + 1, ten_c, t_c, fifty_c])
  end

  defp _check_currency(value, [one_c, five_c, ten_c, t_c, fifty_c]) when value >= 1 and value < 5 do
    _check_currency(value - 1, [one_c + 1, five_c, ten_c, t_c, fifty_c])
  end

end

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
postelxpro
Elxpro

Posted on June 19, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related