Fl4m3Ph03n1x

Fl4m3Ph03n1x

How to improve this code?

Background

I have come up with a solution to the Caeser Cipher challenge in Exercism.io. This solution works and passes all tests, but I am still not happy.

Code

defmodule RotationalCipher do
  @doc """
  Given a plaintext and amount to shift by, return a rotated string.

  Example:
  iex> RotationalCipher.rotate("Attack at dawn", 13)
  "Nggnpx ng qnja"
  """
  @spec rotate(text :: String.t(), shift :: integer) :: String.t()
  def rotate(text, shift) do
    text
    |> String.to_charlist()
    |> Enum.map( fn char ->
      cond do
        is_lower_case?( char ) -> rotate_lower_case( char, shift )
        is_upper_case?( char ) -> rotate_upper_case( char, shift )
        true -> char
      end
    end )
    |> to_string()
  end

  defp is_lower_case?( char ), do: char >= 97 && char <= 122
  defp rotate_lower_case( char, shift ), do: 97 + rem( char - 71 + shift, 26 )

  defp is_upper_case?( char ), do: char >= 65 && char <= 90
  defp rotate_upper_case( char, shift ), do: 65 + rem( char - 39 + shift, 26 )
end

So, given a string and a shift, I have to shift all the letters. Punctuation, numbers and spaces remain the same.

I achieve this by converting the string to a charlist, mapping over it, and then converting the result into a string again. But in my eyes, there is room for better.

Possible improvements

  1. I had to define is_upper_case? and is_lower_case? for characters. Isn’t there a built in function that already does this?
  2. In my Enum.map I have a function that I would like to extract into a defp, for the sake of clarity. But I still don’t know how to do it and I only found answer that tell me how to use anonymous functions with the terse syntax. How would I call an already defined function that takes more than 1 parameter inside an Enum.map ?
  3. I am using a conditional, even though I have the feeling I shouldn’t be using one yet. The exercise is supposed to be about string only but I am already using control flow. I have the feeling I am over complicating. Perhaps there is a simpler solution?

Let me know what you guys think!

Marked As Solved

malloryerik

malloryerik

Turns all the solutions use some advanced feature…

Kip’s is amazing with the binary pattern matching, which goes into the string itself like it has superpowers. Here’s my more pedestrian beginner’s attempt, with run-of-the-mill guards and very basic pattern matching – no advanced features. Everything here is in the first parts of the Getting Started guide on elixir-lang.org. It does seem easy to read.

defmodule RotationalCipher do
  def rotate(text, shift) do
    text
    |> to_charlist()
    |> Enum.map(&handle(&1, shift))
    |> to_string()
  end

  def handle(char, shift) when char in ?a..?z, do: spin(char, shift, ?z)
  def handle(char, shift) when char in ?A..?Z, do: spin(char, shift, ?Z)
  def handle(char, _shift), do: char

  def spin(char, shift, z) when char + shift > z, do: char + shift - 26
  def spin(char, shift, _z), do: char + shift
end

Everything you’d think of as conditional logic has been moved to the function headers, using guards in this case. Of course you could write a more traditionally conditional version:

defmodule RotationalCipher2 do
  def rotate(text, shift) do
    text
    |> to_charlist()
    |> Enum.map(&handle(&1, shift))
    |> to_string()
  end

  def handle(char, shift) do
    cond do
      char in ?a..?z -> spin(char, shift, ?z)
      char in ?A..?Z -> spin(char, shift, ?Z)
      true -> char
    end
  end

  def spin(char, shift, z) do
     if(char + shift > z, do: char + shift - 26, else: char + shift)
  end
end

I find the first version more direct.

By the way, AstonJ in this forum has been recommending Dave Thomas’ Elixir for Programmers video course. I tried it and have found it an excellent addition to books because you (or at least I) get a sense of workflow and state of mind along with a great introduction to OTP.

Also Liked

kip

kip

ex_cldr Core Team

@Fl4m3Ph03n1x, I think this is a good case for binary pattern matching.

defmodule Caesar do
  defguard is_lower?(char) when char in ?a..?z
  defguard is_upper?(char) when char in ?A..?Z

  @spec rotate(text :: String.t() | integer, shift :: integer) :: String.t()
  def rotate(<< char :: integer-size(8), rest :: binary >>, shift)  do
    << rotate(char, shift) :: integer-size(8), rotate(rest, shift) :: binary >>
  end

  def rotate("", _shift), do: ""

  def rotate(char, shift) when is_lower?(char), do: ?a + rem( char - ?G + shift, 26 )
  def rotate(char, shift) when is_upper?(char), do: ?A + rem( char - ?' + shift, 26 )
  def rotate(char, _shift), do: char
end
NobbZ

NobbZ

No, there is not.

Enum.map(list, &your_defp_fun(&1, shift))

At least when I understand you correctly.

Don’t take the abscense of the control flow label too strict. those labels are relatively new and they haven’t been applied thoroughly yet. This is a problem common to most tracks.


Have you considered using a Range and charliterals? eg. char in ?a..?z.

But in general I think it looks quite good already.

datadrover

datadrover

Completely agree with the comment about Dave Thomas’s video course. I also think that Lance Halvorsen’s book, should be on the reading list of anyone wanting to deploy a basic understanding of Elixir to the construction of larger applications.

Getting back to to the topic, this version was inspired by the treatment of comprehensions on bitstrings in the Dave Thomas book (p108 in my 2016 1.3 edition):

defmodule Caesar do

@spec rotate(text :: String.t(), shift :: integer) :: String.t()

def rotate(text, shift), do: List.to_string(for <<ch <- text>>, do: caesar_code(ch, shift))

defp caesar_code(ch, shift) when ch in ?a..?z, do: rotate_code(ch, shift, ?z)

defp caesar_code(ch, shift) when ch in ?A..?Z, do: rotate_code(ch, shift, ?Z)

defp caesar_code(ch, _shift), do: ch

defp rotate_code(ch, shift, z), do: if ch + shift > z, do: ch + shift - 26, else: ch + shift

end

It’s not as elegant as kip’s solution, but it is easy to follow.

Last Post!

datadrover

datadrover

Completely agree with the comment about Dave Thomas’s video course. I also think that Lance Halvorsen’s book, should be on the reading list of anyone wanting to deploy a basic understanding of Elixir to the construction of larger applications.

Getting back to to the topic, this version was inspired by the treatment of comprehensions on bitstrings in the Dave Thomas book (p108 in my 2016 1.3 edition):

defmodule Caesar do

@spec rotate(text :: String.t(), shift :: integer) :: String.t()

def rotate(text, shift), do: List.to_string(for <<ch <- text>>, do: caesar_code(ch, shift))

defp caesar_code(ch, shift) when ch in ?a..?z, do: rotate_code(ch, shift, ?z)

defp caesar_code(ch, shift) when ch in ?A..?Z, do: rotate_code(ch, shift, ?Z)

defp caesar_code(ch, _shift), do: ch

defp rotate_code(ch, shift, z), do: if ch + shift > z, do: ch + shift - 26, else: ch + shift

end

It’s not as elegant as kip’s solution, but it is easy to follow.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
fayddelight
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
joaquinalcerro
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
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New

We're in Beta

About us Mission Statement