Skip to content

Commit 096670f

Browse files
committed
add some more nqp examples
1 parent 09bf927 commit 096670f

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

examples/sub-lines2words.nqp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env nqp
2+
3+
# create a line => words splitting function
4+
my $line := ' # one two ';
5+
6+
say("\$line: '$line'");
7+
8+
my @w := words($line);
9+
10+
say("'$line' => words");
11+
for @w {
12+
say(" word: $_");
13+
}
14+
15+
sub words($line) {
16+
my @arr := nqp::split(' ', $line);
17+
my @words := [];
18+
for @arr {
19+
my $s := $_;
20+
# remove any whitespace
21+
$s := subst($s, /\s+/, '', :global);
22+
next if !$s;
23+
@words.push($s);
24+
}
25+
26+
return @words;
27+
}

examples/test-ws.nqp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env nqp
2+
3+
# test regexes for deleting and normalizing whitespace in text
4+
# interspersed with comments
5+
my $txt := 'blah # comment
6+
# comment
7+
8+
blah';
9+
10+
my $rx := /
11+
<!after \S>
12+
\h*
13+
[
14+
| [\h* '#' \N* \n]
15+
| [\h* \n]
16+
]*
17+
\h*
18+
/;
19+
20+
my $s := subst($txt, $rx, ' ');
21+
say("original text = '$txt'");
22+
say("new text = '$s'");

examples/use-classes.nqp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/env nqp
2+
3+
# based on blog post by Andrew Shitov:
4+
#
5+
# https://perl6.online/2018/01/09/what-does-nqpgetattr-do/
6+
#
7+
# class C {
8+
# has $.attr is rw;
9+
# }
10+
#
11+
# my $o := nqp::create(C);
12+
# $o.attr = 'other value';
13+
# nqp::say(nqp::getattr($o, C, '$!attr')); # other value
14+
15+
class Foo {
16+
has $!line;
17+
has @!contents;
18+
19+
method set-line($value) {
20+
$!line := $value;
21+
}
22+
23+
method set-contents(@value) {
24+
@!contents := @value;
25+
}
26+
27+
method add2contents($value) {
28+
@!contents.push($value);
29+
}
30+
31+
method empty-contents() {
32+
@!contents := [];
33+
}
34+
35+
# note the empty parens are required in the declaration
36+
method show-line() {
37+
say(" \$!line: '$!line'");
38+
}
39+
40+
# note the empty parens are required in the declaration
41+
method show-contents() {
42+
say(" \@!contents:");
43+
if !nqp::elems(@!contents) {
44+
say(" [empty]");
45+
return;
46+
}
47+
for @!contents {
48+
say(" $_");
49+
}
50+
}
51+
}
52+
53+
my $line1 := ' # text ';
54+
my $line2 := ' text ';
55+
56+
my $o1 := nqp::create(Foo);
57+
$o1.set-line($line1);
58+
my $o2 := nqp::create(Foo);
59+
$o2.set-line($line2);
60+
61+
my $L1 := nqp::getattr($o1, Foo, '$!line');
62+
my $L2 := nqp::getattr($o2, Foo, '$!line');
63+
say("\$o1.line: '$L1'");
64+
say("\$o2.line: '$L2'");
65+
66+
my @arr := nqp::list(
67+
'elem 0',
68+
'elem 1',
69+
);
70+
71+
$o1.set-contents(@arr);
72+
73+
my @list := nqp::getattr($o1, Foo, '@!contents');
74+
say("\$o1's contents:");
75+
for @list {
76+
say(" $_");
77+
}
78+
79+
$o1.add2contents('another elem');
80+
@list := nqp::getattr($o1, Foo, '@!contents');
81+
say("\n\$o1's contents after an update:");
82+
for @list {
83+
say(" $_");
84+
}
85+
86+
$o1.empty-contents;
87+
@list := nqp::getattr($o1, Foo, '@!contents');
88+
say("\n\$o1's contents after emptying:");
89+
if !nqp::elems(@list) {
90+
say(" \$o1's \@contents are empty");
91+
}
92+
else {
93+
for @list {
94+
say(" $_");
95+
}
96+
}
97+
98+
my $o3:= nqp::create(Foo);
99+
$o3.set-contents(@arr);
100+
$o3.set-line('line 3');
101+
102+
$o3.show-contents;
103+
$o3.show-line;

examples/use-hashes.nqp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env nqp
2+
3+
# create a hash
4+
my %h := nqp::hash(
5+
'k1', 0,
6+
'k2', 1,
7+
);
8+
9+
# iterate over the hash
10+
for %h {
11+
my $k := nqp::iterkey_s($_);
12+
my $v := nqp::iterval($_);
13+
say("key $k => val $v");
14+
}
15+
16+
my $k1 := 'k1';
17+
my $k2 := 'k2';
18+
19+
# return value of a key
20+
my $v := nqp::atkey(%h, $k1);
21+
say("value of key $k1 is $v");
22+
23+
# check existence of a key
24+
25+
if nqp::existskey(%h, $k2) {
26+
say("key $k2 exists");
27+
}
28+
29+
30+
31+

0 commit comments

Comments
 (0)