Skip to content

Commit 7d48d29

Browse files
ryn1xegonSchiele
authored andcommitted
add perl6 code for chapters 1-4 (egonSchiele#49)
1 parent dd0100e commit 7d48d29

File tree

11 files changed

+194
-0
lines changed

11 files changed

+194
-0
lines changed

.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
11
.DS_Store
2+
*.swp
3+
index.data
4+
*~
5+
html/*.html
6+
html/css/style.css
7+
html/css/style.css.map
8+
html/perl6.xhtml
9+
html/routine/
10+
html/type/
11+
html/op/
12+
html/language/
13+
html/programs/
14+
html/syntax/
15+
html/images/type-graph*
16+
html/js/search.js
17+
.precomp
18+
precompiled
19+
assets/assetpack.db
20+
assets/cache
21+
.sass-cache/
22+
html/css/style.css.map
23+
html/links.txt
24+
xt/aspell.pws
25+
highlights/node_modules
26+
**/npm-debug.log
27+
highlights/atom-language-perl6/
28+
.DS_store
29+
highlights/package-lock.json
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env perl6
2+
use v6.c;
3+
4+
sub binary-search(@arr, $item){
5+
# low and high keep track of which part of the array you'll search in.
6+
my $low = 0;
7+
my $high = @arr.elems - 1;
8+
9+
# While you haven't narrowed it down to one element ...
10+
while $low <= $high {
11+
# ... check the middle element
12+
my $mid = ($low + $high) div 2;
13+
my $guess = @arr[$mid];
14+
# Found the item.
15+
if $guess ~~ $item {
16+
return $mid;
17+
}
18+
# The guess was too high.
19+
if $guess > $item {
20+
$high = $mid - 1;
21+
}
22+
# The guess was too low.
23+
else {
24+
$low = $mid + 1;
25+
}
26+
}
27+
# Item doesn't exist
28+
return Nil;
29+
}
30+
31+
my @arr = 1, 3, 5, 7, 9;
32+
say binary-search(@arr, 3); # => 1
33+
34+
# We use 'Nil' to indicate that the item wasn't found.
35+
say binary-search(@arr, -1); # => Nil
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env perl6
2+
use v6.c;
3+
4+
# Finds the smallest value in an array
5+
sub findSmallest(@arr) {
6+
# Stores the smallest value
7+
my $smallest = @arr[0];
8+
# Stores the index of the smallest value
9+
my $smallest-index = 0;
10+
for 1..^@arr.elems {
11+
if @arr[$_] < $smallest {
12+
$smallest = @arr[$_];
13+
$smallest-index = $_;
14+
}
15+
}
16+
return $smallest-index;
17+
}
18+
19+
# Sort array
20+
sub selectionSort(@arr) {
21+
my @newArr;
22+
for ^@arr.elems {
23+
# Finds the smallest element in the array and adds it to the new array
24+
my $smallest = findSmallest(@arr);
25+
@newArr.append: @arr.splice($smallest, 1);
26+
}
27+
return @newArr;
28+
}
29+
30+
my @unsorted-array = 5, 3, 6, 2, 10;
31+
say selectionSort(@unsorted-array);

03_recursion/perl6/01_countdown.p6

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env perl6
2+
use v6.c;
3+
4+
# base case
5+
multi countdown(0) { say 0 }
6+
7+
# recursive case
8+
multi countdown($i where { $i > 0 }) {
9+
say $i;
10+
countdown $i-1;
11+
}
12+
13+
countdown 5;

03_recursion/perl6/02_greet.p6

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env perl6
2+
use v6.c;
3+
4+
sub greet2($name) {
5+
say "how are you, $name?";
6+
}
7+
8+
sub bye() {
9+
say "ok bye!";
10+
}
11+
12+
sub greet($name) {
13+
say "hello, $name!";
14+
greet2 $name;
15+
say "getting ready to say bye...";
16+
bye;
17+
}
18+
19+
greet "adit";

03_recursion/perl6/03_factorial.p6

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env perl6
2+
use v6.c;
3+
4+
multi fact(1) { 1 }
5+
multi fact($x where { $x > 1 }) { $x * fact($x-1) }
6+
7+
say fact(5);

04_quicksort/perl6/01_loop_sum.p6

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env perl6
2+
use v6.c;
3+
4+
sub sum(@arr) {
5+
my $total = 0;
6+
for @arr {
7+
$total += $_;
8+
}
9+
return $total;
10+
}
11+
12+
my @arr = 1, 2, 3, 4;
13+
say sum(@arr);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env perl6
2+
use v6.c;
3+
4+
multi sum([]) { 0 };
5+
multi sum(@arr) { @arr[0] + sum(@arr[1..*]) }
6+
7+
my @arr = 1,2,3,4,5;
8+
say sum(@arr);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env perl6
2+
use v6.c;
3+
4+
multi count([]) { 0 }
5+
multi count(@arr) { 1 + count(@arr[1..*]) }
6+
7+
my @arr = 1,2,3,4,5;
8+
say count(@arr);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env perl6
2+
use v6.c;
3+
4+
sub my-max(@arr) {
5+
if @arr.elems == 2 { return (@arr[0] > @arr[1]) ?? @arr[0] !! @arr[1] }
6+
my $sub-max = my-max(@arr[1..*]);
7+
return (@arr[0] > $sub-max) ?? @arr[0] !! $sub-max;
8+
}
9+
10+
my @arr = 1,2,3,4,5;
11+
say my-max(@arr);

0 commit comments

Comments
 (0)