voughtdq

voughtdq

Given an integer, produce a list of its digits

I need to turn an integer into a list of digits.

For example, given 1234 as input, the output should be [1, 2, 3, 4].

I am doing this right now, but it looks ugly and might even be inefficient.

  def get_check_digit(digits) when is_binary(digits) or is_integer(digits) do
    digits
    |> to_charlist()
    |> Enum.map(fn char ->
      {integer, []} = :string.to_integer([char])
      integer
    end)
    |> get_check_digit()
  end

  def get_check_digit(digits) when is_list(digits) do
    ...
  end

Any tips?

Marked As Solved

tovarchristian21

tovarchristian21

How about Integer.digits/2 ?

Also Liked

wolf4earth

wolf4earth

Integer.digits/2 is certainly the easiest way to do this but I took a stab at this and wrote two versions:

Make use of codepoints

The first version is super short and basically makes use of the fact that charlists boil down to a list of integers (where the characters have their corresponding ASCII values).

iex> 1234 |> to_charlist() |> Enum.map(& &1 - ?0)
[1, 2, 3, 4]

By converting the number to a charlist, we have a list of codepoints. From here we can subtract the codepoint value of 0 (?0 which is 48) to get the actual numerical value.

Use math and recursion

The second solution uses a combination of integer division and the modulo operation. But see for yourself:

defmodule Digits do
  def digits(number) when number < 0, do: digits(-number)

  def digits(number) when is_integer(number) do
    calc(number, [])
  end

  defp calc(digit, digits)
    when digit < 10,
    do: [digit | digits]

  defp calc(number, digits) do
    digit = Integer.mod(number, 10)

    number
    |> div(10)
    |> calc([digit | digits])
  end
end

IO.inspect Digits.digits(1234), charlists: :as_lists

Here we get a single digit by doing number % 10 (which is Integer.mod/2 in Elixir), then we do the same for the next digit by dividing through 10. If we reach anything smaller than 10 we add it to the list and were done.

voughtdq

voughtdq

Nice! Thanks

rvirding

rvirding

Creator of Erlang

Am I missing something here? :smile: Isn’t :erlang.integer_to_list/1/2 the easiest and fastest? Though it would give a “bad” error value if the argument is not an integer.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement