A block act just like a lambda function (an anonymous proc ?) but with side
effects ?
One syntax caveat is the increase of the indentation level. With if, while, for
statements and blocks in between, the code gets quickly too much indented.
Your examples are great, but I am unsure about the second one. Isn't it bad
style to "force" side-effects ? You would get a runtime error if you applied
your fold on an immutable sequence (a sequence of const/let variables), isn't
it ?
For the first example, what is the advantage over a proc exactly ? With a proc
you are forced to explain what you do by naming the function. I explicitly gave
the result to the special variable, but we are not forced to. I have added
generics for type restriction of the result, that is too much for this example.
proc getFirstTwoCharacters[T, U](nameFile: string): tuple[T, U] =
let t = readFile(nameFile).split("\n\n") # no point of letting `t` to
linger to the end of the proc
result = (frobnicate(t[0]), t[1])
var (foo, bar) = getFirstTwoCharacters[typeof(frobnicate('a')),
char]("input.txt")
Run