raku

How does Perl 6 evaluate truthiness?

左心房为你撑大大i 提交于 2019-12-23 06:48:03
问题 In reading about Perl 6, I see a feature being trumpeted about, where you no longer have to do: return "0 but true"; ...but can instead do: return 0 but True; If that's the case, how does truth work in Perl 6? In Perl 5, it was pretty simple: 0, "", and undef are false, everything else is true. What are the rules in Perl 6 when it comes to boolean context? 回答1: Perl 6 evaluates truth now by asking the object a question instead of looking at its value. The value is not the object. It's

How can I slice a string like Python does in Perl 6?

ぃ、小莉子 提交于 2019-12-22 09:36:53
问题 In Python, I can splice string like this: solo = A quick brown fox jump over the lazy dog solo[3:5] I know substr and comb is enough, I want to know if it's possible, However. Should I use a role to do this? 回答1: How to slice a string Strings (represented as class Str ) are seen as single values and not positional data structures in Perl 6, and thus can't be indexed/sliced with the built-in [ ] array indexing operator. As you already suggested, comb or substr is the way to go: my $solo = "A

Using percent sign as part of the name for a prefix operator

北城余情 提交于 2019-12-22 07:41:54
问题 I thought that %of would be more readable and concise than percent-of for a function name. Here is working code using the longer name. #!/bin/env perl6 # Quick stats from gene_exp.diff file sub percent-of { return sprintf('%.1f', (100 * $^a/ $^b).round(0.1)); } my $total = first-word-from("wc -l gene_exp.diff ") -1; # subtract one for the header my $ok = first-word-from "grep -c OK gene_exp.diff"; my $yes = first-word-from "grep -c yes gene_exp.diff"; put '| total | OK | OK % | yes | yes % |

how to pass a class method as argument to another method of the class in perl 6

北城余情 提交于 2019-12-22 07:06:13
问题 I have a script like the below. Intent is to have different filter methods to filter a list. Here is the code. 2 3 class list_filter { 4 has @.my_list = (1..20); 5 6 method filter($l) { return True; } 7 8 # filter method 9 method filter_lt_10($l) { 10 if ($l > 10) { return False; } 11 return True; 12 } 13 14 # filter method 15 method filter_gt_10($l) { 16 if ($l < 10) { return False; } 17 return True; 18 } 19 20 # expecting a list of (1..10) to be the output here 21 method get_filtered_list

Perl6 hyper » operator doesn't work like map

我的未来我决定 提交于 2019-12-22 04:47:09
问题 As far as I undertand it, the hyper operator » is a shortcut for map() . Why does the following return two different results and in the second example .sum seems not to be applied? say ([1,2], [2, 2], [3, 3]).map({.sum}); # (3 4 6) say ([1,2], [2, 2], [3, 3])».sum; # ([1 2] [2 2] [3 3]) 回答1: Hyperops descend recursively into sublists. Also they are candidates for autothreading (NYI) what means their operations are out of order. Also there was a bug that is corrected with https://github.com

Does Perl 6 have an infinite Int?

*爱你&永不变心* 提交于 2019-12-22 04:27:13
问题 I had a task where I wanted to find the closest string to a target (so, edit distance) without generating them all at the same time. I figured I'd use the high water mark technique (low, I guess) while initializing the closest edit distance to Inf so that any edit distance is closer: use Text::Levenshtein; my @strings = < Amelia Fred Barney Gilligan >; for @strings { put "$_ is closest so far: { longest( 'Camelia', $_ ) }"; } sub longest ( Str:D $target, Str:D $string ) { state Int $closest

Can I use a standalone Signature as a signature in Perl 6?

寵の児 提交于 2019-12-22 03:48:40
问题 I was playing around with a Perl 6 implementation of a command-line program that takes several switches. The signature to MAIN was quite involved and a bit messy. I wondered if there was a way to define the signature somewhere else and tell the subroutine what to use: # possibly big and messy signature my $sig; BEGIN { $sig = :( Int $n, Int $m ) }; multi MAIN ( $sig ) { put "Got $n and $m"; } MAIN doesn't see the variables in the signature even though the signature is set before MAIN compiles

How does a Perl 6 object find a multi method that might be in a parent class or role?

…衆ロ難τιáo~ 提交于 2019-12-22 03:24:27
问题 Consider this example where a subclass has a multi method with no signature and one with a slurpy parameter: class Foo { multi method do-it { put "Default" } multi method do-it ( Int $n ) { put "Int method" } multi method do-it ( Str $s ) { put "Str method" } multi method do-it ( Rat $r ) { put "Rat method" } } class Bar is Foo { multi method do-it { put "Bar method" } multi method do-it (*@a) { put "Bar slurpy method" } } Foo.new.do-it: 1; Foo.new.do-it: 'Perl 6'; Foo.new.do-it: <1/137>; Foo

How can I completely flatten a Perl 6 list (of lists (of lists) … )

两盒软妹~` 提交于 2019-12-22 01:34:31
问题 I was wondering about how I could completely flatten lists and things that contain them. Among other things, I came up with this solution that slips things that have more than one element and puts them back, or takes things with one element after slipping it. This is a bit different than How do I “flatten” a list of lists in perl 6?, which doesn't completely flat because the task is to restructure. But, maybe there's a better way. my @a = 'a', ('b', 'c' ); my @b = ('d',), 'e', 'f', @a; my @c

How do you add a method to an existing class in Perl 6?

我与影子孤独终老i 提交于 2019-12-21 07:11:42
问题 The Int class has a method is_prime , so I figured, just for giggles, I'd like to add some other methods to Int for some of my hobby projects that do number theory stuff. I thought I could do something like this: class Int { method is-even (Int:D $number ) returns Bool:D { return False if $number % 2; return True; } } say 137.is-even; But that doesn't work: ===SORRY!=== P6opaque: must compose before allocating I don't know if this means that I can't do that or that I'm doing it incorrectly. I