Skip to content

Commit ea288b4

Browse files
committed
almost ready for release
1 parent 694e330 commit ea288b4

File tree

9 files changed

+180
-2
lines changed

9 files changed

+180
-2
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{{$NEXT}}
2+
- Add new sub str2intlist
23

34
4.0.1 2025-07-22T08:10:23-05:00
45
- Fix erroneous placement of exported subs

META6.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"provides": {
1919
"Text::Utils": "lib/Text/Utils.rakumod",
2020
"Text::Utils::Classes": "lib/Text/Utils/Classes.rakumod",
21+
"Text::Utils::Str2IntList": "lib/Text/Utils/Str2IntList.rakumod",
2122
"Text::Utils::Subs": "lib/Text/Utils/Subs.rakumod",
2223
"Text::Utils::Vars": "lib/Text/Utils/Vars.rakumod"
2324
},

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ The module contains several routines to make text handling easier for module and
3939
<th>Name</th> <th>Notes</th>
4040
</tr></thead>
4141
<tbody>
42-
<tr> <td>commify</td> <td></td> </tr> <tr> <td>count-substrs</td> <td></td> </tr> <tr> <td>list2text</td> <td></td> </tr> <tr> <td>normalize-string</td> <td>alias &#39;normalize-text&#39;</td> </tr> <tr> <td>sort-list</td> <td></td> </tr> <tr> <td>split-line</td> <td>aliases &#39;splitstr&#39;, &#39;split-str&#39;</td> </tr> <tr> <td>strip-comment</td> <td>alias &#39;strip&#39;</td> </tr> <tr> <td>wrap-paragraph</td> <td>&#39;width&#39; is in PS points</td> </tr> <tr> <td>wrap-text</td> <td>&#39;width&#39; is in number of chars</td> </tr>
42+
<tr> <td>commify</td> <td></td> </tr> <tr> <td>count-substrs</td> <td></td> </tr> <tr> <td>list2text</td> <td></td> </tr> <tr> <td>normalize-string</td> <td>alias &#39;normalize-text&#39;</td> </tr> <tr> <td>sort-list</td> <td></td> </tr> <tr> <td>split-line</td> <td>aliases &#39;splitstr&#39;, &#39;split-str&#39;</td> </tr> <tr> <td>str2intlist</td> <td></td> </tr> <tr> <td>strip-comment</td> <td>alias &#39;strip&#39;</td> </tr> <tr> <td>wrap-paragraph</td> <td>&#39;width&#39; is in PS points</td> </tr> <tr> <td>wrap-text</td> <td>&#39;width&#39; is in number of chars</td> </tr>
4343
</tbody>
4444
</table>
4545

@@ -57,6 +57,8 @@ The module contains several routines to make text handling easier for module and
5757

5858
* [split-line](#split-line)
5959

60+
* [str2intlist](#str2intlist)
61+
6062
* [strip-comment](#strip-comment)
6163

6264
* [wrap-paragraph](#wrap-paragraph)
@@ -272,6 +274,17 @@ sub split-line(
272274

273275
[Links list](#Links)
274276

277+
### str2intlist
278+
279+
Converts a string of integer numbers separated by one or more spaces to a List of integers. Such a List can then be converted to a Set for easy numerical manipulation. (Leading zeros are removed in the process.)
280+
281+
my $s = "01 2 3";
282+
my @i = str2intlist $s;
283+
my $intset = @i.Set;
284+
say $intset.keys; # OUTPUT: «(3 2 1)␤»
285+
286+
[Links list](#Links)
287+
275288
### strip-comment
276289

277290
Strip the comment from an input text line, save comment if requested, normalize returned text by default.

docs/README.rakudoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ list2text
4444
normalize-string | alias 'normalize-text'
4545
sort-list
4646
split-line | aliases 'splitstr', 'split-str'
47+
str2intlist
4748
strip-comment | alias 'strip'
4849
wrap-paragraph | 'width' is in PS points
4950
wrap-text | 'width' is in number of chars
@@ -57,6 +58,7 @@ wrap-text | 'width' is in number of chars
5758
=item L<normalize-string|#normalize-string>
5859
=item L<sort-list|#sort-list>
5960
=item L<split-line|#split-line>
61+
=item L<str2intlist|#str2intlist>
6062
=item L<strip-comment|#strip-comment>
6163
=item L<wrap-paragraph|#wrap-paragraph>
6264
=item L<wrap-text|#wrap-text>
@@ -349,6 +351,22 @@ sub split-line(
349351

350352
L<Links list|#Links>
351353

354+
=head3 str2intlist
355+
356+
Converts a string of integer numbers separated by one or more spaces
357+
to a List of integers. Such a List
358+
can then be converted to a Set for easy numerical manipulation.
359+
(Leading zeros are removed in the process.)
360+
361+
=begin code
362+
my $s = "01 2 3";
363+
my @i = str2intlist $s;
364+
my $intset = @i.Set;
365+
say $intset.keys; # OUTPUT: «(3 2 1)␤»
366+
=end code
367+
368+
L<Links list|#Links>
369+
352370
=head3 strip-comment
353371

354372
Strip the comment from an input text line, save comment if requested,

lib/Text/Utils.rakumod

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,42 @@ sub count-substrs(
163163

164164
} # count-substrs
165165

166+
#-----------------------------------------------------------------------
167+
#| Purpose : Convert a string of numbers to a List of Ints
168+
#| Params : String, :$no-zeroes...<
169+
#| Returns : List of Integers
170+
sub str2intlist(
171+
# from ChatGPT
172+
Str $s,
173+
:$no-zeros = True,
174+
:$no-negatives = True,
175+
:$only-positives = True,
176+
:$debug,
177+
--> List
178+
) is export(:str2intlist) {
179+
180+
my @words = $s.words;
181+
# validate tokens
182+
for @words -> $tok {
183+
unless $tok ~~ /^ '-'? \d+ $/ {
184+
die "Non-numeric token found: |$tok|";
185+
}
186+
}
187+
188+
# normalize to Ints
189+
my @ints = @words.map({ .Int });
190+
if $only-positives {
191+
@ints .= grep({ $_ != 0}) if $no-zeros;
192+
}
193+
else {
194+
@ints .= grep({ $_ != 0}) if $no-zeros;
195+
@ints .= grep({ $_ >= 0}) if $no-negatives;
196+
}
197+
198+
# Ints unique and sorted
199+
@ints.unique.sort({ $^a <=> $^b }).List;
200+
} # sub str2intlist
201+
166202
#-----------------------------------------------------------------------
167203
#| Purpose : Strip comments from an input text line, save comment if
168204
#| requested, normalize returned text if requested

lib/Text/Utils/Str2IntList.rakumod

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
unit module PB-Lottery::Str2IntList;
2+
3+
# moved to its own module
4+
sub str2intlist(
5+
# from ChatGPT
6+
Str $s,
7+
:$no-zeros = True,
8+
:$no-negatives = True,
9+
:$only-positives = True,
10+
:$debug,
11+
--> List
12+
) is export {
13+
14+
my @words = $s.words;
15+
# validate tokens
16+
for @words -> $tok {
17+
unless $tok ~~ /^ '-'? \d+ $/ {
18+
die "Non-numeric token found: |$tok|";
19+
}
20+
}
21+
22+
# normalize to Ints
23+
my @ints = @words.map({ .Int });
24+
if $only-positives {
25+
@ints .= grep({ $_ != 0}) if $no-zeros;
26+
}
27+
else {
28+
@ints .= grep({ $_ != 0}) if $no-zeros;
29+
@ints .= grep({ $_ >= 0}) if $no-negatives;
30+
}
31+
32+
# Ints unique and sorted
33+
@ints.unique.sort({ $^a <=> $^b }).List;
34+
} # end of sub str2intlist

t/17b-strip-comment.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use Test;
22

33
use File::Temp;
44

5-
use Text::Utils :ALL; #:strip-comment :normalize-string;
5+
use Text::Utils :ALL;
66

77
my $debug = 0; # output files are placed in local dir "tmp"
88

t/17c-str2intlist.t

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use Test;
2+
3+
use Text::Utils :ALL;
4+
5+
my $set5s = set(1..69);
6+
my $setpb = set(1..26);
7+
8+
isa-ok $set5s, Set, "isa-ok Set";
9+
isa-ok $setpb, Set, "isa-ok Set";
10+
11+
# with a set of 5 numbers, ensure:
12+
# valid sets
13+
my @valid = [
14+
"69 4 3 2 1",
15+
"68 4 67 2 66",
16+
"1 3 4 68 69",
17+
];
18+
19+
for @valid -> $s {
20+
my $set = set str2intlist $s;
21+
isa-ok $set, Set, "isa-ok Set";
22+
}
23+
24+
done-testing;
25+
=finish
26+
27+
# invalid sets
28+
my @invalid = [
29+
"69 4 3 2 4", # dup numbers
30+
"70 4 67 2 66", # number too large
31+
"69 4 3 0 4", # number too small
32+
];
33+
34+
done-testing;

t/19-str2intlist.t

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use Test;
2+
3+
use Text::Utils :ALL;
4+
5+
my $set5s = set(1..69);
6+
my $setpb = set(1..26);
7+
8+
isa-ok $set5s, Set, "isa-ok Set";
9+
isa-ok $setpb, Set, "isa-ok Set";
10+
11+
# with a set of 5 numbers, ensure:
12+
# valid sets
13+
my @valid = [
14+
"69 4 3 2 1",
15+
"68 4 67 2 66",
16+
"1 3 4 68 69",
17+
];
18+
19+
for @valid -> $s {
20+
my $set = set str2intlist $s;
21+
isa-ok $set, Set, "isa-ok Set";
22+
}
23+
24+
# from the README
25+
my $s = "01 2 3";
26+
my @i = str2intlist $s;
27+
isa-ok @i, List, "a List";
28+
my $intset = @i.Set;
29+
isa-ok $intset, Set, "a Set";
30+
say $intset.keys;
31+
done-testing;
32+
=finish
33+
34+
# invalid sets
35+
my @invalid = [
36+
"69 4 3 2 4", # dup numbers
37+
"70 4 67 2 66", # number too large
38+
"69 4 3 0 4", # number too small
39+
];
40+
41+
done-testing;

0 commit comments

Comments
 (0)