2017年5月11日木曜日

開発環境

Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)のPart 1(Starting with the basics)、Chapter 9(Arrays and Lists)の Exercise: Other Ways to Modify an Array.を取り組んでみる。

Exercise: Other Ways to Modify an Array.

コード(Emacs)

#!/usr/bin/env perl6
# -*- coding: utf-8 -*-

sub my-pop(@array where @array > 0) {
    my $result = splice @array, @array.elems - 1, 1;
    return $result[0];
}

sub my-unshift(@array, $item) {
    splice @array, 0, 0, $item;
    return @array;
}

sub my-push(@array, $item) {
    splice @array, @array.elems, 0, $item;
    return @array;
}

sub my-delete(@array, $i where 0 <= $i < @array.elems) {
    my $result = splice @array, $i, 1, Any;
    return $result[0];
}


my @array1 = 1..10;
my @array2 = 1..10;

say @array1;
say @array2;

say 'pop';
say pop @array1;
say @array1;
say my-pop @array2;
say @array2;

say 'unshift';
say unshift @array1, 0;
say @array1;
say my-unshift @array2, 0;
say @array2;

say 'push';
say push @array1, 10;
say @array1;
say my-push @array2, 10;
say @array2;

say 'delete';
say @array1[2]:delete;
say @array1;
say my-delete(@array2, 2);
say @array2;

入出力結果(Terminal, REPL)

$ ./sample_other.pl
[1 2 3 4 5 6 7 8 9 10]
[1 2 3 4 5 6 7 8 9 10]
pop
10
[1 2 3 4 5 6 7 8 9]
10
[1 2 3 4 5 6 7 8 9]
unshift
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
push
[0 1 2 3 4 5 6 7 8 9 10]
[0 1 2 3 4 5 6 7 8 9 10]
[0 1 2 3 4 5 6 7 8 9 10]
[0 1 2 3 4 5 6 7 8 9 10]
delete
2
[0 1 (Any) 3 4 5 6 7 8 9 10]
2
[0 1 (Any) 3 4 5 6 7 8 9 10]
$

0 コメント:

コメントを投稿

Comments on Google+: