2017年5月28日日曜日

開発環境

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 11(Case Study: Data Structure Selection)の Random Words、Exercise: 6.を取り組んでみる。

Exercise: 6.

コード(Emacs)

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

my $skip = True;
sub process-line (Str $line is copy) {
    # if defined index $line, "*END*THE SMALL PRINT!" {
    if defined index $line, "*** START OF THIS PROJECT GUTENBERG EBOOK EMMA ***" {
        $skip = False;
        return;
    }
    return if $skip;
    $line ~~ s:g/<[-'/]>/ /;
    $line ~~ s:g/<[;:,!?.()"_`”‘“’]>//;
    $line = $line.lc;
    return $line.words;
}
my $emma = bag (process-line $_ for 'emma.txt'.IO.lines);

for 1..10 {
    say $emma.pick(10);
}

for $emma.pick(10) {
    say $_;
}

say 'KEYS';
for $emma.pick(10).keys {
    say $_;
}
say 'VALUES';
for $emma.pick(10).values {
    say $_;
}

say "KV1";
for $emma.pick(10).kv {
    say $_;
}
say "KV2";
for $emma.pick(10).kv -> $k, $v {
    say "$k => $v";
}

say "PAIRS";
for $emma.pick(10).pairs {
    say $_;
}

say "BAG";
for $emma.pick(10) {
    say "$_\($emma{$_}\)";
}

入出力結果(Terminal, REPL)

$ ./sample6.pl
(was emma without painful without going i clear when quite)
(likewise it was not reanimation room should elton emma links)
(love over the all that thought was proper to from)
(aunt over of in was giving there the batess done)
(never scouted her the or a grievous and spirits knows)
(have impossible it long agreeable first sheep of barouche an)
(be i her of a a with having is what)
(yards proposal believed own that it we consideration us he)
(know so conveniently seriousness out was as bates before speaking)
(true nothing word highbury elegant her sucklings protected notice for)
glad
highbury
good
indeed
23rd
hesitate
by
she
have
hardly
KEYS
0
1
2
3
4
5
6
7
8
9
VALUES
that
or
followed
hopes
of
are
what
when
to
and
KV1
0
to
1
will
2
an
3
thank
4
any
5
than
6
or
7
electronic
8
usual
9
of
KV2
0 => beginning
1 => again
2 => i
3 => came
4 => will
5 => i
6 => lament
7 => for
8 => evening
9 => less
PAIRS
0 => have
1 => my
2 => not
3 => an
4 => little
5 => and
6 => of
7 => being
8 => it
9 => punishment
BAG
was(2401)
all(866)
party(98)
to(5321)
a(3187)
cautioned(1)
yards(6)
her(2483)
civility(10)
morrow(33)
$

0 コメント:

コメントを投稿

Comments on Google+: