by makemethrowaway · 1y ago · 0 comments
The BEAM's grown up along with Erlang and so the culture and optimisations are built up all around function application, list processing, recursion, and pattern matching etc.
https://github.com/llaisdy/beam_languages is a decent list of the diverse languages that have been implemented on it, but nothing quite like 'regular for and while loops'.
The BEAM itself is a plain aul register machine though, so it could be done!
for : Int -> Int -> Int -> (Int -> msg) -> List msg
for start stop step action =
let
range =
if step > 0
then List.range start (step) (stop - 1)
else if step < 0
then List.range stop (abs step) (start - 1) |> List.reverse
else []
in
List.map action range
It should be just as trivial to write this in Gleam.