Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide some form of for #13

Open
calebzulawski opened this issue Mar 7, 2023 · 1 comment
Open

Provide some form of for #13

calebzulawski opened this issue Mar 7, 2023 · 1 comment

Comments

@calebzulawski
Copy link
Collaborator

Without mutability, a for loop probably has limited use, but it can be confusing when it does come up:

^Array:from <| Iter:map (fn :dir -> fs:create-dir $dir) $dirs

would probably be easier to understand as something like

for :dir in $dirs { fs:create-dir $dir }

The scoping of :dir is a little bit unusual compared to the rest of the language, which might be a complication. Something like

for $dirs do (:dir -> fs:create-dir $dir)

is a little more in line with the rest of the language, but slightly more verbose and doesn't flow "grammatically".

@afranchuk
Copy link
Collaborator

Just a side note: the example you provided (creating directories) typically might be better to do in parallel rather than sequenced. But I get the underlying motivation of course!

for :dir in $dirs { fs:create-dir $dir }

This form would need to be dedicated syntax as things currently exist, though there were potential plans to allow arbitrary setters. Though the semantics of the scope to which dir pertains isn't easy to handle, the original intention was for the setters to correspond to the outer scope.

Otherwise there's a few ways I can think of that would handle this:

  • std:Iter:do - Iter:do <| Iter:map fs:create-dir $dirs, but probably far more practical as Iter:do fs:create-dir $dirs
    do = fn :f :iter -> { ^Array:from <| Iter:map $f $iter; () }
    
  • for function - for $dirs do fs:create-dir
    for = fn :iter do :f -> { ^Array:from <| Iter:map $f $iter; () }
    
  • for function with context (late-bindings) - for $dirs { fs:create-dir $?it }
    for = fn :iter :block -> { ^Array:from <| Iter:map (fn :it -> late-bind { it } $block) $iter; () }
    

Obviously these all end up with fairly similar implementations. It might be appropriate to add more than one, too (for instance, Iter:do has argument ordering that is compatible with typical iterator piping).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants