Permalink
Browse files

Fixed attribute tags to present standards, enabled code that was NYI

  • Loading branch information...
1 parent 4691add commit 575ea30d7a387f1107943bd5cc1e18d7ebde8145 @thundergnat committed Aug 28, 2012
Showing with 51 additions and 28 deletions.
  1. +1 −1 META.info
  2. +11 −3 README
  3. +14 −21 lib/Sort/Naturally.pm6
  4. +25 −3 t/01-basic.t
View
@@ -1,6 +1,6 @@
{
"name" : "Sort::Naturally",
- "version" : "0.1.1",
+ "version" : "0.1.2",
"description" : "Provides several routines to ease natural sorting.",
"depends" : [],
"source-type" : "git",
View
@@ -116,6 +116,7 @@ routines. These are analogues of the primary routines prepended with p5.
C<p5naturally()>, C<p5nsort()> and C<p5ncmp>. Used identically to the p6
versions.
+
for comparison:
@@ -132,7 +133,16 @@ yields:
nsort: 9x 14 foo foo12 Foo12a foo12a foo12z foo13a fooa Foolio foolio
p5nsort: 9x 14 foo fooa Foolio foolio foo12 Foo12a foo12a foo12z foo13a
-
+
+By default, only the nsort routines are imported when you use Sort::Naturally.
+If you want to import the p5 compatabilty routines instead, add the p5 tag.
+
+use Sort::Naturally :p5;
+
+This will load the p5 compatibility routines only. If you want botht the standard
+and compatabilty routines imported, use:
+
+use Sort::Naturally :ALL;
BUGS
@@ -142,8 +152,6 @@ sort consistently, just not in the order advertised. I can probably implement
some kind of run time check to modify the behavior based on current locale.
I'll look into it more seriously later if necessary.
-Load on demand is not working yet. By default all methods and subroutines are
-imported into the GLOBAL namespace. More an annoyance than a bug.
AUTHOR
@@ -1,37 +1,30 @@
-module Sort::Naturally:ver<0.1.1>;
+module Sort::Naturally:ver<0.1.2>;
use v6;
use MONKEY_TYPING;
augment class Any {
- multi method nsort is export(:standard) { self.list.flat.sort( { .&naturally } ) };
- multi method p5nsort is export(:p5) { self.list.flat.sort( { .&p5naturally } ) };
+ method nsort (*@) { self.list.flat.sort( { .&naturally } ) };
+ method p5nsort (*@) { self.list.flat.sort( { .&p5naturally } ) };
}
-
-# Work-around multi subs nsort and p5nsort. Shouldn't be necessary but
+# Export the subroutines manually. Shouldn't be necessary but
# module load order can affect class method exporting and make them unfindable.
-multi sub nsort(*@a) is export(:standard) { @a.nsort; };
-multi sub p5nsort(*@a) is export(:p5) { @a.p5nsort };
-
+sub nsort(*@a) is export( :ALL, :DEFAULT ) { @a.list.flat.sort( { .&naturally } ) }
+sub p5nsort(*@a) is export( :ALL, :p5 ) { @a.list.flat.sort( { .&p5naturally } ) }
# Sort modifier block routines
-sub infix:<ncmp>($a, $b) is export(:standard) {$a.&naturally cmp $b.&naturally}
-sub infix:<p5ncmp>($a, $b) is export(:p5) {$a.&p5naturally cmp $b.&p5naturally}
-
+sub infix:<ncmp>($a, $b) is export( :ALL, :DEFAULT ) { $a.&naturally cmp $b.&naturally }
+sub infix:<p5ncmp>($a, $b) is export( :ALL, :p5 ) { $a.&p5naturally cmp $b.&p5naturally }
-# core routines to actually do the transformation for sorting
+# Core routines to actually do the transformation for sorting
-sub naturally ($a) is export(:standard) {
- $a.lc.subst(/(\d+)/, ->$/ { 0 ~ $0.chars.chr ~ $0 }, :g) ~ "\x0" ~ $a
+sub naturally ($a) is export( :ALL, :DEFAULT ) {
+ $a.lc.subst(/(\d+)/, ->$/ { "0{$0.gist.chars.chr}$0" }, :g) ~ "\x0$a"
}
-sub p5naturally ($a) is export(:p5) {
- $a.lc.subst(/^(\d+)/, -> $/ { 0 ~ $0.chars.chr ~ $0 } )\
- # Less than awesome use of captures, but rakudo doesn't have
- # <?after ...> lookbehind implemented yet for .subst(). Really should be:
- # .subst(/<?after \D>(\d+)/, -> $/ { 'z{' ~ $0.chars.chr ~ $0 }, :g)
- .subst(/(\D)(\d+)/, -> $/ { $0 ~ 'z{' ~ $1.chars.chr ~ $1 }, :g)
- ~ "\x0" ~ $a
+sub p5naturally ($a) is export( :ALL, :p5 ) {
+ $a.lc.subst(/^(\d+)/, -> $/ { "0{$0.gist.chars.chr}$0" } )\
+ .subst(/<?after \D>(\d+)/, -> $/ { 'z{' ~"{$0.gist.chars.chr}$0" }, :g) ~ "\x0$a"
}
View
@@ -1,6 +1,6 @@
use v6;
use Test;
-use Sort::Naturally;
+use Sort::Naturally :ALL;
plan 22;
@@ -10,21 +10,27 @@ my $nsorted = '';
# does it deal with Nil in a sane fashion?
is(~Nil.sort( { .&naturally } ), $nsorted,
"calling &naturally in a sort block on Nil is ok");
+
is(~Nil.nsort, $nsorted,
"calling nsort as method on Nil is ok");
+
is(~nsort(Nil), $nsorted,
"calling nsort as subroutine on Nil is ok");
-is(~Nil.sort( { $^a ncmp $^b } ), $nsorted,
- "calling ncmp in a sort block on Nil is ok");
+
+is(~Nil.sort( { $^a ncmp $^b } ), $nsorted, "calling ncmp in a sort block on Nil is ok");
# does it deal with empty array in a sane fashion?
+
is(~@test.sort( { .&naturally } ), $nsorted,
"calling &naturally in a sort block on an empty array is ok");
+
is(~@test.nsort, $nsorted,
"calling nsort as method on an empty array is ok");
+
is(~nsort(@test), $nsorted,
"calling nsort as subroutine on an empty array is ok");
+
is(~@test.sort( { $^a ncmp $^b } ), $nsorted,
"calling ncmp in a sort block on an empty array is ok");
@@ -35,41 +41,57 @@ $nsorted = '1 2 3rd 11 14th 21 30 100 210 aid ANY Any any Are d1 D2 d3 d10 D21';
# randomize list for each test
# could conceivably fail under some locales
+
is(~@test.pick(+@test).sort( { .&naturally } ), $nsorted,
"calling &naturally in a sort block yields expected order");
+
is(~@test.pick(+@test).nsort, $nsorted,
"calling nsort as method yields expected order");
+
is(~nsort(@test.pick(+@test)), $nsorted,
"calling nsort as subroutine yields expected order");
+
is(~nsort(<2 210 21 30 3rd d1 d10 D2 D21 d3 aid Are
any ANY 1 Any 11 100 14th>), $nsorted,
"calling nsort as subroutine on a list gives expected order");
+
is(~@test.pick(+@test).sort( { $^a ncmp $^b } ), $nsorted,
"calling ncmp in a sort block yields expected order");
+
is(~(@test[10..18],@test[0..9]).nsort, $nsorted,
"nsort as method flattens its arguments and sorts as expected");
+
is(~nsort(@test[10..18],@test[0..9]), $nsorted,
"nsort as subroutine flattens its arguments and sorts as expected");
+
# do the compatibility routines return terms in the expected order?
my @p5test = <foo12z foo foo13a fooa Foolio Foo12a foolio foo12 foo12a 9x 14>;
my $p5nsorted = '9x 14 foo fooa Foolio foolio foo12 Foo12a foo12a foo12z foo13a';
# randomize list for each test
# could conceivably fail under some locales
+
is(~@p5test.pick(+@p5test).sort( { .&p5naturally } ),
$p5nsorted, "calling &p5naturally in a sort block yields expected order");
+
is(~@p5test.pick(+@p5test).p5nsort, $p5nsorted,
"calling p5nsort as method yields expected order");
+
is(~p5nsort(@p5test.pick(+@p5test)), $p5nsorted,
"calling p5nsort as subroutine yields expected order");
+
is(~p5nsort(<foo12z foo foo13a fooa Foolio Foo12a
foolio foo12 foo12a 9x 14>), $p5nsorted,
"calling p5nsort as subroutine gives same order as method");
+
is(~@p5test.pick(+@p5test).sort( { $^a p5ncmp $^b } ),
$p5nsorted, "calling p5ncmp in a sort block yields expected order");
+
is(~(@p5test[6..10],@p5test[0..5]).p5nsort, $p5nsorted,
"p5nsort as method flattens its arguments and sorts as expected");
+
is(~p5nsort(@p5test[6..10],@p5test[0..5]), $p5nsorted,
"p5nsort as subroutine flattens its arguments and sorts as expected");
+

0 comments on commit 575ea30

Please sign in to comment.