跨功能共享签名约束
发布时间:2020-12-16 06:26:53 所属栏目:大数据 来源:网络整理
导读:给定一个简单的程序来转换为基数: #!perl6my @alphabet = ('0' .. '9','A' .. 'Z','a' .. 'z').flat;sub to-digits(Int $n is copy,Int $b where 2 = * = 62 -- Str) { my @digits; while $n 0 { @digits.push(@alphabet[$n % $b]); $n = $n div $b; } @dig
给定一个简单的程序来转换为基数:
#!perl6 my @alphabet = ('0' .. '9','A' .. 'Z','a' .. 'z').flat; sub to-digits(Int $n is copy,Int $b where 2 <= * <= 62 --> Str) { my @digits; while $n > 0 { @digits.push(@alphabet[$n % $b]); $n = $n div $b; } @digits.reverse.join; } sub from-digits(Str $digits,Int $b where 2 <= * <= 62 --> Int) { my $n = 0; for $digits.comb -> $d { $n = $b * $n + @alphabet.first({ $_ eq $d },:k); } $n; } sub to-base( Str $n,Int $b where 2 <= * <= 62,Int $c where 2 <= * <= 62 --> Str) { to-digits(from-digits($n,$b),$c); } 我发现我在提供的基础上重复我的约束,其中*> = 2&& *< = 62,整个程序四次.查看 my $sig = :(Int $a where $a >= 2 && $a <= 62); 是否有一种方法可以将此签名应用于多个功能和/或如何跨功能共享此约束? 解决方法
事实证明,不,你不能在@moritz的答案中分享多个功能的签名:
Can I use a standalone Signature as a signature in Perl 6?
但是,您可以使用#perl6 Freenode irc中概述的@ zoffix作为 subset Base of Int where 2 <= * <= 62; sub to-digits(Int $n is copy,Base $b) { ... } ... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |