Elixir Today: Create a Hollow Square using Elixir

Photo by Edho Pratama on Unsplash

Elixir Today: Create a Hollow Square using Elixir

Pau Riosa's photo
Pau Riosa
·Apr 29, 2022·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Process

  • create a file named "hollow.ex"
  • write the code
hollow = fn number ->
  for i when i < number <- 0..number do
    for j when j < number <- 0..number do
      if i === 0 || i === number - 1 do
        "*"
      else
        if j === 0 || j === number - 1 do
          "*"
        else
          " "
        end
      end
    end
  end
  |> Enum.into("", fn string ->
    string = Enum.join(string)
    "#{string}\n"
  end)
end

IO.puts(hollow.(10))
IO.puts(hollow.(5))
  • run elixir hollow.ex on your terminal.

Result

**********
*        *
*        *
*        *
*        *
*        *
*        *
*        *
*        *
**********

*****
*   *
*   *
*   *
*****

Happy Coding!

 
Share this