This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub modify-copy-and-return(@a is copy, @b is copy) { | |
my @buffer; | |
@buffer.append: @a; | |
@buffer.append: @b; | |
@buffer.push: [1,2]; | |
dd @buffer; | |
# Array @buffer = [[1, 2], [3, 4], [3, 4], [5, 6], [1, 2]] | |
True, @buffer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my @sets = <ab bc cd>.map: { .comb.Set }; | |
my $first = @sets.first({ $_ (==) <a b>.Set }); | |
my $second = @sets.first({ $_ (==) <b c>.Set }); | |
my $last = @sets.first({ $_ != <a b>.Set && $_ != <b c>.Set }); | |
say [$first, $second, $last]; | |
# outputs | |
# [Set(a b) Set(b c) (Any)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use experimental :cached; | |
sub postfix:<!>(Int:D $n) { | |
[*] 2..$n; | |
} | |
multi sub is-factorial(0) { True } | |
multi sub is-factorial(1) { True } | |
multi sub is-factorial(Int:D $n where * >= 2 --> Bool) { | |
sub is-factorial-recursive(Int:D $n, Int:D $d --> Bool) is cached { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
// Using De Morgan's Law each of these logic gates can be implemented using one or more NAND gate. | |
const nand = (p, q) => !(p && q); | |
const not = p => nand(p, p); | |
const and = (p, q) => not(nand(p, q)); | |
const or = (p, q) => nand(nand(p, p), nand(q, q)); | |
const nor = (p, q) => not(or(p, q)); | |
const xor = (p, q) => nand(nand(p, nand(p, q)), nand(q, nand(p, q))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
multi sub floyds-triangle(1 --> List) { 1 .. 1, } | |
multi sub floyds-triangle(Int:D $n where * > 1 --> List) { | |
my $nth-triangle-number = $n * ($n + 1) div 2; | |
return floyds-triangle2($n - 1), $nth-triangle-number - $n ^.. $nth-triangle-number; | |
} | |
say floyds-triangle(5); | |
# (((((1..1) 1^..3) 3^..6) 6^..10) 10^..15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl6 | |
class Stack { | |
has Int @!store; | |
method push(Int:D $elem --> Nil) { | |
push @!store, $elem; | |
} | |
method pop(--> Int) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl6 | |
my @dates = <May 15>, <May 16>, <May 19>, <June 17>, <June 18>, <July 14>, <July 16>, <August 14>, <August 15>, <August 17>; | |
sub day (@date) { @date[1] } | |
sub month (@date) { @date[0] } | |
sub tell($data) { | |
@dates.grep({ day($_) eq $data or month($_) eq $data }); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub search(Int $x, Int $y, Int $max-depth is copy = 25000 --> List) { | |
state %cache{Set}; | |
my @queue = (($x, $y, Nil, Nil),); | |
while @queue and --$max-depth > 0 { | |
my ($x, $y, $px, $py) = @queue.shift(); | |
%cache{Set($x, $y)} = [$px, $py]; | |
if $x == $y { | |
my @path; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> my %h{Array}; | |
{} | |
> %h{[1,2]} = [3,4]; | |
Type check failed in binding to parameter 'key'; expected Array but got Int (1) | |
in block <unit> at <unknown file> line 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl6 | |
my @alphabet = ('0' .. '9', 'A' .. 'Z', 'a' .. 'z').flat; | |
sub to-digits(Int $n is copy, Int $b where $b >= 2 && $b <= 62 --> Str) { | |
my @digits; | |
while $n > 0 { | |
@digits.push(@alphabet[$n % $b]); | |
$n = $n div $b; | |
} |
NewerOlder