diff options
| author | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2019-11-16 20:59:47 +0100 |
|---|---|---|
| committer | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2019-11-16 20:59:47 +0100 |
| commit | 989f8578f419d5d526ce477043db988c04e95fd8 (patch) | |
| tree | da69e91a193d50f117712eb09a9c32ca3f39653f /challenge-034 | |
| parent | 060e1cdd1339642ddc270b6600339f7d94442296 (diff) | |
| download | perlweeklychallenge-club-989f8578f419d5d526ce477043db988c04e95fd8.tar.gz perlweeklychallenge-club-989f8578f419d5d526ce477043db988c04e95fd8.tar.bz2 perlweeklychallenge-club-989f8578f419d5d526ce477043db988c04e95fd8.zip | |
Solution to challenge 34 problem 1 and 2 in perl 6 by Noud
Diffstat (limited to 'challenge-034')
| -rw-r--r-- | challenge-034/noud/perl6/ch1.p6 | 29 | ||||
| -rw-r--r-- | challenge-034/noud/perl6/ch2.p6 | 27 |
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-034/noud/perl6/ch1.p6 b/challenge-034/noud/perl6/ch1.p6 new file mode 100644 index 0000000000..bafda2340d --- /dev/null +++ b/challenge-034/noud/perl6/ch1.p6 @@ -0,0 +1,29 @@ +# Write a program that demonstrates using hash slices and/or array slices. + +# Hash slices +# +# The idea behind hash slices is that you can assign multiple keys at the same +# time with a hash slice. +# Also see: https://docs.perl6.org/language/hashmap#Hash_slices + +my %h; +%h<a b c d> = ^4; + +say %h; +say %h<a c>; + + +# Slice indexing +# +# Similar, we can use slicing for extracting slices from an array. +# Also see: https://docs.perl6.org/language/list#Range_as_slice + +my @a = ^10; + +say @a[0..2]; +say @a[^2]; +say @a[0..*]; +say @a[0..Inf-1]; +say @a[0..*-1]; +say @a[0..^*-1]; +say @a[0..^*/2]; diff --git a/challenge-034/noud/perl6/ch2.p6 b/challenge-034/noud/perl6/ch2.p6 new file mode 100644 index 0000000000..a57dd0401f --- /dev/null +++ b/challenge-034/noud/perl6/ch2.p6 @@ -0,0 +1,27 @@ +# Write a program that demonstrates a dispatch table. + +# According to wikipedia: A dispatch table is a table of pointers to functions +# or methods. + +sub hello() { + "Hello"; +} + +sub fib($n) { + if ($n < 1) { + return 0; + } + if ($n == 1) { + return 1; + } + return fib($n - 1) + fib($n - 2); +} + +my %dispatch = ( + hello => &hello, + world => sub { "World"; }, + fib => &fib +); + +say %dispatch{"hello"}() ~ " " ~ %dispatch{"world"}(); +say %dispatch{"fib"}(20); |
