ryanzidago
What do you think of my solution for converting integers to roman numerals?
What do you think of my solution for converting integers to roman numerals?
defmodule RomanNumerals do
import IO, only: [iodata_to_binary: 1]
@doc """
Convert the number to a roman number.
"""
@spec numeral(pos_integer) :: String.t()
def numeral(0), do: ""
def numeral(1), do: "I"
def numeral(2), do: "II"
def numeral(3), do: "III"
def numeral(4), do: "IV"
def numeral(5), do: "V"
def numeral(6), do: "VI"
def numeral(7), do: "VII"
def numeral(8), do: "VIII"
def numeral(9), do: "IX"
def numeral(10), do: "X"
def numeral(50), do: "L"
def numeral(100), do: "C"
def numeral(500), do: "D"
def numeral(1000), do: "M"
def numeral(n) when n < 40, do: [numeral(10), numeral(n - 10)] |> iodata_to_binary()
def numeral(n) when n < 50, do: [numeral(10), numeral(50), numeral(n - 40)] |> iodata_to_binary()
def numeral(n) when n < 90, do: [numeral(50), numeral(n - 50)] |> iodata_to_binary()
def numeral(n) when n < 100, do: [numeral(10), numeral(100), numeral(n - 90)] |> iodata_to_binary()
def numeral(n) when n < 400, do: [numeral(100), numeral(n - 100)] |> iodata_to_binary()
def numeral(n) when n < 500, do: [numeral(100), numeral(500), numeral(n - 400)] |> iodata_to_binary()
def numeral(n) when n < 900, do: [numeral(500), numeral(n - 500)] |> iodata_to_binary()
def numeral(n) when n < 1000, do: [numeral(100), numeral(1000), numeral(n - 900)] |> iodata_to_binary()
def numeral(n) when n < 5000, do: [numeral(1000), numeral(n - 1000)] |> iodata_to_binary()
end
Most Liked
kokolegorille
NobbZ
Also, the spec does make the function appear unbound towards positive infinity.
In reality the spec should be numeral(0..4999) :: String.t().
In general, I do consider this appraoch as quite noisy, and also since iodata_to_binary/1 is used on each recursion step, nothing is gained by it. It might even perform worse than just using <>/2.
Last but not least, I’m not a friend of not using mix format.
4
ryanzidago
Popular in Questions
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
New
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Other popular topics
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









