DEV Community

Pau Riosa
Pau Riosa

Posted on • Updated on • Originally published at paugramming.com

Elixir Today: Create a Hollow Square using Elixir

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))

Enter fullscreen mode Exit fullscreen mode
  • run elixir hollow.ex on your terminal.

Result

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

*****
*   *
*   *
*   *
*****
Enter fullscreen mode Exit fullscreen mode

Happy Coding!

Top comments (0)