There are definitely things that can be improved in ReScript and we’re actually working hard to improve them (and we welcome contributions too by the way), one of them is building better primitives to iterate things, there has been some experiments ongoing around for of/in, but it’s not an easy task to come up with a simple yet powerful syntax/semantics.
But at the same time, this pain is not really a surprise because it comes from one of the main design principles of rescript where things are immutable by default, which brings a lot of benefits and you can’t really have a cake and eat it, can you? You’d likely not use a while loop in such cases in rescript because since things are immutable you wouldn’t be able to return something out of the read chunk unless you do allocate a variable for this (or only apply side effects of course). You’d likely just use a recursive function and if you happen to use this pattern often, you’d write a helper function for it:
module Reader: {
type t<'a> = unit => option<'a>
let reduce: (t<'a>, 'b, ('b, 'a) => 'b) => 'b
let forEach: (t<'a>, 'a => unit) => unit
} = {
type t<'a> = unit => option<'a>
let reduce = (t, init, f) => {
let rec aux = acc =>
switch t() {
| Some(data) => aux(f(acc, data))
| None => acc
}
aux(init)
}
let forEach = (t, f) => reduce(t, (), ((), a) => f(a))
}
Playground link
I’m not saying it’s better or worse than a while loop, it’s just more idiomatic in rescript and it’s hard to design a language that is great at everything and with any style of programming.
We’re definitely all ears when it comes to listening to our users’ feedback and improving the warts of rescript, so keep them coming!