Raku / problem-solving

🦋 Problem Solving, a repo for handling problems that require review, deliberation and possibly debate
Artistic License 2.0
70 stars 16 forks source link

List should have `.do` method as Supply #122

Open FCO opened 5 years ago

FCO commented 5 years ago

Supply has the .do method that should be used only for its side-effects, it works kind of as a map that always return $_. But lists do not have that method. That would make sense to exist that method on lists.

FCO commented 5 years ago

https://colabti.org/irclogger/irclogger_log/raku?date=2019-10-21#l103

jnthn commented 5 years ago

I presume on all Iterables rather than simply on list?

How would we choose to define it on items?

FCO commented 5 years ago

Yes, Iterables...

Sorry, what do you mean with “choose to define it on items”?

Sent with GitHawk

jnthn commented 5 years ago

Does 42.do(*.say) evaluate to 42 or (42,).Seq?

FCO commented 5 years ago

I think 42 would be more useful... but I’m not sure...

Sent with GitHawk

FCO commented 5 years ago

It would behave as a given that returns the unchanged value...

Sent with GitHawk

FCO commented 5 years ago

Writing Red I found an example that could be using .do on scalar:

method begin {
    my $trans = self.new-connection;
    $trans.prepare(Red::AST::BeginTransaction.new).map: *.execute;
    $trans
}

could be just

method begin {
    self.new-connection.do: {
        .prepare(Red::AST::BeginTransaction.new).map: *.execute;
    }
}
ab5tract commented 2 months ago

I was just thinking about this, though I had envisioned it a bit differently.

Basically I wonder if this would fit @FCO 's vision:

# this is actually broken, what 
# we actually want is to ensure 
# that the signature to &block 
# put is raw on its param.
# that way we only affect 
# mutations, whereas this stores 
# the output of side effects as 
# well

method do(&block) { 
   for self.list -> $_ is raw    
      { $_ = &block($_) } 
   }
}

Basically providing a method version of a for where the idea is strictly related to side effects and/or mutation of elements.

I would actually have gone with 'each' for this, but it seems that Supply.do is already a thing. Also, 'do' fits the Scalar case better, a use case I find quite compelling.

But maybe I'm misunderstanding @FCO's intent here.