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. |
|||
stevied | is there a way to type constrain the elemens in a List that is passed to a method? | 00:17 | |
do I do some kind of for loop in the signature? | |||
ok, this worked: `List:D $items where { $items.all ~~ Str}` | 00:29 | ||
01:16
Khwarizmi joined
|
|||
Khwarizmi | hello there, I have a few questions | 01:16 | |
readyready15728 | Switched over from IRC | 01:40 | |
So anyway, my first question is how familiar should I be with Perl 5 if I want to do interop with CPAN packages | 01:41 | ||
CPAN is still way huger than Raku's equivalent | |||
01:42
Khwarizmi left
|
|||
SmokeMachine | You shouldn’t need to be familiar with Perl… only for translating the module usage to Raku… | 02:03 | |
Khwarizmi: 👆 | 02:10 | ||
03:16
deoac joined,
deoac left
03:20
deoac26 joined
04:00
deoac26 left
05:59
frost joined
07:04
frost left
07:11
frost joined
07:44
kjp left
07:51
kjp joined
11:39
frost left
12:29
frost joined
13:59
frost left
|
|||
stevied | how do I loop over a hash sorted by key? I'm trying `for self.items.sort.kv -> $k, $v ` but it's not working | 16:33 | |
sienet_ja_LSD[m] | I have done at least for %hash.sort( { .keys })>>.kv -> ($k, $v) | 16:36 | |
Nemokosch | I'm not at a (proper) keyboard but still... there must be a better way | 17:45 | |
Util | <@563790557831495691> `self.items.sort.kv` does not work, because .sort is returning a list of Pair objects, so `.kv` is operating on a List, and you get (0, FirstPair, 1, SecondPair ...). | 19:10 | |
`%hash.sort( { .keys })` , I think is a typo; `.key` is needed, not `.keys`. | |||
Also, the default .sort automatically sorts a List of Pairs by .key first. | |||
The syntax for unpacking a Pair is awkward and hard to remember, but it is optimal for iterating over a sorted hash: | |||
`for %hash.sort -> ( :key($some_variable_for_key), :value($some_variable_for_value) ) { ... }` | |||
Also fine (and easier to remember) is: | |||
`for %hash.sort { my ($k,$v) = .kv; ... }` | 19:12 | ||
`%hash.sort».kv -> ($k, $v) {...}` is also a fine solution; I have avoided it in code that beginners might use, due to the advanced concepts in hyper-dot method combined with easily-confused use of .kv to mean "unpack a single Pair" instead of "get many keys and values from a single `thing` like a Hash or Array or List". | 19:18 | ||
`%hash.sort -> (:$key, :$value) {...}` | 19:33 | ||
Oh! If you don't mind using exactly $key and $value as variable names, you can do this: `%hash.sort -> (:$key, :$value) {...}` | |||
stevied | Ah yes. The latter with the hyper operator is what I have used in the path. That is more understandable to me. For whatever reason I couldn’t come up with it. | 19:34 | |
Nemokosch | (:$key, :$value) isn't bad either for flexibility | 19:43 | |
Util | <@297037173541175296> Indeed so. Also, when the loop is small, and especially when key and value are accessed no more than once each, I often don't unpack them at all, leaving them in $_ : | 19:55 | |
for %hash.sort { do_foo_with(.key); do_bar_with(.value); } | |||
Nemokosch | 👍 | 20:04 | |
stevied | ok, got another little problem: I've got a callable in an attribute: `has &.action;` | 20:08 | |
i'm trying to execute the callable with $obj.action but that doesn't seem to work | |||
Util | <@563790557831495691> Does this work? `$obj.action();` | 20:10 | |
stevied | doesn't look like it | ||
the action is just a simple one: `action => { say 'action' }` | |||
the raw dump of the object: | 20:14 | ||
``` | |||
Simple::Option.new(display-string => "Option 1", option-number => 1, submenu => Simple::Menu.new(menuID => 2, option-format => "\%d - \%s", selection => Str, validated-selection => Str, option-separator => "\n", prompt => Array[Str].new("\nMake selection: "), error-msg => "\nSorry, invalid entry. Try again. ", options => {"1" => Simple::Option.new(display-string => "one", option-number => 1, submenu = | |||
-> ;; $_? is raw = OUTER::<$_> { #`(Block|4385121823648) ... } | |||
``` | |||
the action appears to be there | |||
MasterDuke | m: class A { has &.a = -> { say "action" } }; A.new.a()() | 20:16 | |
camelia | action | ||
stevied | hmmm | 20:18 | |
MasterDuke | m: class A { has &.a = -> { say "action" } }; my &b = A.new.a(); b() # making it a little more obvious what's happening | ||
camelia | action | ||
MasterDuke | m: class A { has &.a = -> { say "action" } }; my &b = A.new.a; b() # first set of parens aren't needed this way | 20:19 | |
camelia | action | ||
stevied | ok, that helped | 20:22 | |
yeah, needed the doubled parens | 20:25 | ||
thanks! | |||
MasterDuke | np | ||
stevied | alright, cool. I think I'm ready to drop an honest to god useful module to raku.land | 20:26 | |
thanks for everyone's help! | 20:27 | ||
Util | So, instead of `$obj.action()`, you can just add a dot: `$obj.action.();`. Cool! | ||
stevied | gist.github.com/sdondley/a5bdfc368...c887d9b7cf | 20:28 | |
Nemokosch | Why was the double paren needed though? | ||
stevied | there it is! behold! | ||
Util | <@297037173541175296> A plain `$obj.action()` was just like `$obj.action`; the parens were part of the method call. | 20:29 | |
Nemokosch | Well, why isn't it just a method call, really | ||
MasterDuke | it was, to get the callable. to call the callable requires another step though | 20:30 | |
Nemokosch | It's more like a method than a higher order function... | ||
Util | Adding another set of parens, or placing a dot between action and parens, means that the final set of parens is an parameterless call to whatever sub is pointed to by the the executed code-so-far. | ||
Nemokosch | So ()() is _not_ two consecutive calls? | 20:31 | |
Util | First one is running `action` to get the contents of the `&.action` attribute. Second `()` is a call to that code. | 20:32 | |
Nemokosch | The first part is confusing | 20:33 | |
stevied | I was surpised to see there were no modules in raku.land for generating menus. hopefully people find this useful | 20:34 | |
Nemokosch | Is the first function call for any property? | ||
Util | If that is not what is wanted, you can change the way the `action` method works: `has &!.action; method action () { &!.action.() }` | 20:35 | |
Then, `$obj.action`, with or without parens, will call whatever sub is stored in &!action, and the outside world cannot get a pointer to the sub itself. | |||
Nemokosch | If you want to access a $.var, does it also boil down to a function call? | ||
Util | <@297037173541175296> From outside the class, if `var` is a public attribute, then the *method* call (not a sub) of `$obj.var` retrieves the contents of that attribute. So, yes, if I understood your question. | 20:37 | |
Nemokosch | So the first call is for the property resolution and the second call is the actual function call | 20:39 | |
This makes sense eventually | |||
20:39
cine joined
|
|||
Util | <@297037173541175296> Yes, exactly. | 20:41 | |
Nemokosch | 🎉 | 20:44 | |
20:47
cine left
|
|||
stevied | github.com/sdondley/MenuSimple/blo...Simple.iml | 21:10 | |
is that file generated by raku or comma? | |||
it's an *iml file | |||
looks like it's from intellij. Should I keep it out of the repo? | 21:12 | ||
MasterDuke | yeah, i've added *.iml to my .gitignore | 21:29 | |
stevied | ok, thanks | 21:43 | |
ok, now to write the docs. I guess it's time to look to see if there's any automation tools for that that might speed things up. Anything out there? | 21:45 | ||
i've used mi6 in the past. | 21:49 | ||
I think that generated a README from the docs. | |||
i probably should have init'ed the module with that instead of comma | 21:50 | ||
23:41
lucs left
23:46
lucs joined
|