This channel is intended for people just starting with the Raku Programming Language (raku.org). Logs are available at irclogs.raku.org/raku-beginner/live.html
Set by lizmat on 8 June 2022.
03:34 stanrifkin_ joined 03:37 stanrifkin left 04:12 undoencalma joined 04:30 undoencalma left
Matou Hi ! I'm learning Raku ; following the exercism.org track. There's an exercise called Bob where I have to match an input string and give another string in answer. raku unit role Bob; method hey { my Bool $is_question = self.trim-trailing.ends-with('?'); my Bool $is_yelling = self.contains(/<:Lu>/) && (self eq self.uc()); given self { # self being Bob, the string that's passed to hey ... 07:14
when $is_question && $is_yelling { "Calm down, I know what I'm doing!" } when $is_question { "Sure." } when $is_yelling { "Whoa, chill out!" } when !self || !trim(self) { "Fine. Be that way!" } default { "Whatever." } } } I pass all tests ! But the unit tests look like this : cmp-ok( ( hey ":) ?" does Bob: ), "eq", "Sure.", # ... ) and I'm
not sure I understand what that "does Bob:" mean. But since the 'hey' method has no parameter it looks like "Bob is the string" , that's why I had to match self against strings. Which looks strange from my oldstyle OOP background (in Java I'd have expected a static method (String) -> (String)) .. so is this "rakuish" or do I miss something ?
07:19 lizmat joined 07:34 lizmat left 08:32 lizmat joined 08:47 lizmat left
rcmlz To my understanding you attach the role „Bob“ to the String-Object „:) ?“ such that the String „:) ?“ now has a method „hey“ 08:53
So „:) ?“ is now an enhanced String object that has an additional method. 08:55
docs.raku.org/language/objects#Mixins_of_roles 08:58
With the Bob-Role you can „bobify“ instances of any object you like (I guess) or use it when creating classic inheritance of classes to „bobify“ your classes directly. 09:05
So to my understanding a role is more flexible/powerfull with regards to re-use as you do not need to design and maintain a (complex) class hierachy … 09:09
and „self.contains“ works, because .containes converts automatically to string docs.raku.org/type/Cool#method_contains 09:15
Welemtam Thanks a lot @rcmlz ; it makes sense now! 13:40
antononcube @Welemtam TIMTOWTDI, etc., but why are you using a role of this exercise? Does the exercise require using roles? 13:46
Welemtam Yes the exercise defines "unit role Bob;" and then provides that empty method hey() with no parameter. So I wondered why things were turned out like that. 13:49
antononcube Good luck! 13:50
Welemtam unit tests are also prewritten with that "does" syntax. Thanks ! 80 exercises to go 😂
antononcube I am not that intereste with those exercises. But I am interested to know which of the current LLM models can pass them how many of them. 13:51
With or without manual (i.e. human) tweaking. 13:52
nahita3882 in case the syntax is a little cryptic here is my attempt to light perl hey ":) ?" does Bob: is equivalent to perl hey ":) ?" does Bob: because whitespace is insignificant there; and it's equivalent to perl (":) ?" does Bob).hey() because indirect invocation is in action; to quote the linked page > Please note that in notes $trip: "Almost there" we are using indirect 15:54
invocant syntax, which puts first the method name, then the object, and then, separated by a colon, the arguments: method invocant: arguments. We can use this syntax whenever it feels more natural than the classical period-and-parentheses one. It works exactly in the same way. So it mixes-in the role Bob to the object represented by ":) ?". Mixin is in-place but still returns the affected object. In comparison, but
would also mix it but make it not in-place and IMHO better here because there is no need for an inplace operation as the affected object is thrown away anyway. Then the method "hey" is called on the mixed in object which is now of type (Str+{Bob}). Noting that now first the Bob is looked up, then whatever the base type (Str in this case) has in its method resolution order, for a method invocation. To prove some of the
things i said: perl >>> [13] > ("s" does role R {}).^roles ((R) (Stringy)) # R first >>> ("s" does role R {}).WHAT (Str+{R}) # It's no longer a plain Str; it is all that + more
20:08 lizmat joined 20:50 lizmat left
Welemtam As an experiment I put my question to GPT 4o (which is often quite accurate imo) - interestingly it gives the right answer, at the end mostly, but says quite the opposite when trying to explain the "does" syntax (by telling there must be a multi sub in between to be able to call "hey"). chatgpt.com/share/67fad2ca-12b4-80...5f4439fdb9 21:06
23:25 undoencalma joined 23:26 undoencalma left 23:58 stanrifkin_ left