2

I want to do something like this in raku, the idea is that while I'm in a given block I want to match a wildcard to a variable to use it inside the condition, is it possible?

given @my-array {
  when @[*element] {
    say *element;
  }
  when @[] {
    say "nothing :(";
  }
}
  • 3
    I'm guessing you know your code makes no sense at all as Raku code but intend it to be read as a sort of pseudocode to help us guess what you mean. FWIW, it hasn't worked for me! Does "bind a wildcard value to a variable" mean variable binding as part of [pattern matching](https://en.wikipedia.org/wiki/Pattern_matching#:~:text=match%20absolutely%20any%20argument%20and%20bind%20it%20to%20name%20n%20to%20be%20used%20in%20the%20rest%20of%20the%20definition)? Or something to do with text/regexes? Something else? Perhaps you can provide an example of what you mean in some other language you know? – raiph Apr 24 '22 at 01:27
  • Hi, thanks for your comment, I'm talking about binding as part of pattern matching, and yes indeed that code snippet lacks context to make sense as it is a part of a convoluted given block some scopes above, the idea is still there as I want to capture the wildcard part of the match to a variable in the scope. – Luis Alfonso Buelvas Betancour Apr 24 '22 at 06:05
  • 5
    It will help if you either show the whole program in the original language, or just go to pseudocode and write everything there. For starters, given/when are inverted. `*tale` makes no sense. Is it a regex? `Deps` and `Gemini::Response` are not defined. Please try and golf your question to something that can be answered. – jjmerelo Apr 24 '22 at 09:24
  • 2
    If you *are* just talking about functional programming pattern matching then `given`/`when` may not be what you need. (Or it might. You've not given enough information to be able to tell. But for this comment I'm going to assume not.) See, for example, my two answers, one using `when`, the other using functional programming pattern matching, in [this SO](https://stackoverflow.com/questions/49814220/does-pattern-match-in-raku-have-guard-clause) and/or the various answers in [this SO](https://stackoverflow.com/questions/66233465/haskell-like-pattern-matching-in-raku). Are any of these relevant? – raiph Apr 24 '22 at 22:13
  • 2
    Raku supports two sorts of abstract data structure indexing: [`Associative`](https://docs.raku.org/type/Associative) (think "associative arrays" aka dictionaries) and [`Positional`](https://docs.raku.org/type/Positional) (think regular arrays, indexed by integer offset). For `Associative` wildcarding and binding, see SO links in my prior comment. For `Positional`, see the [Rosettacode Raku entry for *Red-Black tree balancing*](http://rosettacode.org/wiki/Pattern_matching#Raku). These two sorts are combined in Raku's parsing/regex mechanisms and can be in user defined data/patterns/binding. – raiph Apr 25 '22 at 10:23
  • 1
    Hi @Luis. My comments thus far have been an attempt to engage with you to figure out what you were asking so we might arrive at a useful answer. This is another attempt with that in mind. "is `[X]` possible?" If `[X]` refers to something some other PL can do, then yes, because [Raku(do) are Turing complete, with 100% meta programmable syntax and semantics, including at compile-time](https://gist.github.com/raiph/849a4a9d8875542fb86df2b2eda89296). Would such a generic answer be acceptable to you? Aiui, your Q ought be answerable (and preferably answered) or deleted/closed. Please advise. TIA. – raiph May 04 '22 at 15:56
  • I'd like to thank you all for your answers, but I really can't seem to be able to pick one to be THE answer, the green button wont appear :/ – Luis Alfonso Buelvas Betancour May 04 '22 at 15:59
  • I think people were waiting for some more clarifications from you (perhaps you could describe, what your example should do/print) before putting their comments as answers. Only answers can be picked on solution, not comments. – Sebastian May 04 '22 at 16:11
  • @Sebastian , oh thanks, didn't know, I'd swear one could to it this way – Luis Alfonso Buelvas Betancour May 04 '22 at 16:29
  • 1
    I think your intent is very clear now! – Sebastian May 04 '22 at 16:40
  • @Sebastian Thanks for trying to help Luis. :) I see Luis edited their Q. I have tried again to understand what Luis is after but I still see many wildly different interpretations, smothered in caveats, and I have no confidence any of them are what Luis means. You've written "I think your intent is very clear now!", so perhaps you can help? If you could provide an answer, or at least a comment containing an example in some language you know, and Luis confirmed you had nailed it, that would be great. (And please, someone, @-mention me so I know; I only just saw your comments from May 4th today!) – raiph May 18 '22 at 20:04
  • @raiph I am not so sure anymore myself. Luis, could you make an example with the contents of @ my-array and the expected output, please? – Sebastian May 18 '22 at 20:32

0 Answers0