DEV Community

Pau Riosa
Pau Riosa

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

3 2

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay