Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rewrote module to be more moduler, updated README and tests

  • Loading branch information...
commit 9b666f0e15fc70805631ddcb688623da6102b25e 1 parent 8c49c79
@thundergnat authored Steve Schulze committed
Showing with 428 additions and 384 deletions.
  1. +201 −201 LICENSE
  2. +149 −130 README
  3. +25 −25 lib/Sort/Naturally.pm6
  4. +53 −28 t/01-basic.t
View
402 LICENSE
@@ -1,201 +1,201 @@
- The Artistic License 2.0
-
- Copyright (c) 2000-2006, The Perl Foundation.
-
- Everyone is permitted to copy and distribute verbatim copies
- of this license document, but changing it is not allowed.
-
-Preamble
-
-This license establishes the terms under which a given free software
-Package may be copied, modified, distributed, and/or redistributed.
-The intent is that the Copyright Holder maintains some artistic
-control over the development of that Package while still keeping the
-Package available as open source and free software.
-
-You are always permitted to make arrangements wholly outside of this
-license directly with the Copyright Holder of a given Package. If the
-terms of this license do not permit the full use that you propose to
-make of the Package, you should contact the Copyright Holder and seek
-a different licensing arrangement.
-
-Definitions
-
- "Copyright Holder" means the individual(s) or organization(s)
- named in the copyright notice for the entire Package.
-
- "Contributor" means any party that has contributed code or other
- material to the Package, in accordance with the Copyright Holder's
- procedures.
-
- "You" and "your" means any person who would like to copy,
- distribute, or modify the Package.
-
- "Package" means the collection of files distributed by the
- Copyright Holder, and derivatives of that collection and/or of
- those files. A given Package may consist of either the Standard
- Version, or a Modified Version.
-
- "Distribute" means providing a copy of the Package or making it
- accessible to anyone else, or in the case of a company or
- organization, to others outside of your company or organization.
-
- "Distributor Fee" means any fee that you charge for Distributing
- this Package or providing support for this Package to another
- party. It does not mean licensing fees.
-
- "Standard Version" refers to the Package if it has not been
- modified, or has been modified only in ways explicitly requested
- by the Copyright Holder.
-
- "Modified Version" means the Package, if it has been changed, and
- such changes were not explicitly requested by the Copyright
- Holder.
-
- "Original License" means this Artistic License as Distributed with
- the Standard Version of the Package, in its current version or as
- it may be modified by The Perl Foundation in the future.
-
- "Source" form means the source code, documentation source, and
- configuration files for the Package.
-
- "Compiled" form means the compiled bytecode, object code, binary,
- or any other form resulting from mechanical transformation or
- translation of the Source form.
-
-
-Permission for Use and Modification Without Distribution
-
-(1) You are permitted to use the Standard Version and create and use
-Modified Versions for any purpose without restriction, provided that
-you do not Distribute the Modified Version.
-
-
-Permissions for Redistribution of the Standard Version
-
-(2) You may Distribute verbatim copies of the Source form of the
-Standard Version of this Package in any medium without restriction,
-either gratis or for a Distributor Fee, provided that you duplicate
-all of the original copyright notices and associated disclaimers. At
-your discretion, such verbatim copies may or may not include a
-Compiled form of the Package.
-
-(3) You may apply any bug fixes, portability changes, and other
-modifications made available from the Copyright Holder. The resulting
-Package will still be considered the Standard Version, and as such
-will be subject to the Original License.
-
-
-Distribution of Modified Versions of the Package as Source
-
-(4) You may Distribute your Modified Version as Source (either gratis
-or for a Distributor Fee, and with or without a Compiled form of the
-Modified Version) provided that you clearly document how it differs
-from the Standard Version, including, but not limited to, documenting
-any non-standard features, executables, or modules, and provided that
-you do at least ONE of the following:
-
- (a) make the Modified Version available to the Copyright Holder
- of the Standard Version, under the Original License, so that the
- Copyright Holder may include your modifications in the Standard
- Version.
-
- (b) ensure that installation of your Modified Version does not
- prevent the user installing or running the Standard Version. In
- addition, the Modified Version must bear a name that is different
- from the name of the Standard Version.
-
- (c) allow anyone who receives a copy of the Modified Version to
- make the Source form of the Modified Version available to others
- under
-
- (i) the Original License or
-
- (ii) a license that permits the licensee to freely copy,
- modify and redistribute the Modified Version using the same
- licensing terms that apply to the copy that the licensee
- received, and requires that the Source form of the Modified
- Version, and of any works derived from it, be made freely
- available in that license fees are prohibited but Distributor
- Fees are allowed.
-
-
-Distribution of Compiled Forms of the Standard Version
-or Modified Versions without the Source
-
-(5) You may Distribute Compiled forms of the Standard Version without
-the Source, provided that you include complete instructions on how to
-get the Source of the Standard Version. Such instructions must be
-valid at the time of your distribution. If these instructions, at any
-time while you are carrying out such distribution, become invalid, you
-must provide new instructions on demand or cease further distribution.
-If you provide valid instructions or cease distribution within thirty
-days after you become aware that the instructions are invalid, then
-you do not forfeit any of your rights under this license.
-
-(6) You may Distribute a Modified Version in Compiled form without
-the Source, provided that you comply with Section 4 with respect to
-the Source of the Modified Version.
-
-
-Aggregating or Linking the Package
-
-(7) You may aggregate the Package (either the Standard Version or
-Modified Version) with other packages and Distribute the resulting
-aggregation provided that you do not charge a licensing fee for the
-Package. Distributor Fees are permitted, and licensing fees for other
-components in the aggregation are permitted. The terms of this license
-apply to the use and Distribution of the Standard or Modified Versions
-as included in the aggregation.
-
-(8) You are permitted to link Modified and Standard Versions with
-other works, to embed the Package in a larger work of your own, or to
-build stand-alone binary or bytecode versions of applications that
-include the Package, and Distribute the result without restriction,
-provided the result does not expose a direct interface to the Package.
-
-
-Items That are Not Considered Part of a Modified Version
-
-(9) Works (including, but not limited to, modules and scripts) that
-merely extend or make use of the Package, do not, by themselves, cause
-the Package to be a Modified Version. In addition, such works are not
-considered parts of the Package itself, and are not subject to the
-terms of this license.
-
-
-General Provisions
-
-(10) Any use, modification, and distribution of the Standard or
-Modified Versions is governed by this Artistic License. By using,
-modifying or distributing the Package, you accept this license. Do not
-use, modify, or distribute the Package, if you do not accept this
-license.
-
-(11) If your Modified Version has been derived from a Modified
-Version made by someone other than you, you are nevertheless required
-to ensure that your Modified Version complies with the requirements of
-this license.
-
-(12) This license does not grant you the right to use any trademark,
-service mark, tradename, or logo of the Copyright Holder.
-
-(13) This license includes the non-exclusive, worldwide,
-free-of-charge patent license to make, have made, use, offer to sell,
-sell, import and otherwise transfer the Package with respect to any
-patent claims licensable by the Copyright Holder that are necessarily
-infringed by the Package. If you institute patent litigation
-(including a cross-claim or counterclaim) against any party alleging
-that the Package constitutes direct or contributory patent
-infringement, then this Artistic License to you shall terminate on the
-date that such litigation is filed.
-
-(14) Disclaimer of Warranty:
-THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
-IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
-WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
-NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL
-LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL
-BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
-DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF
-ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ The Artistic License 2.0
+
+ Copyright (c) 2000-2006, The Perl Foundation.
+
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+Preamble
+
+This license establishes the terms under which a given free software
+Package may be copied, modified, distributed, and/or redistributed.
+The intent is that the Copyright Holder maintains some artistic
+control over the development of that Package while still keeping the
+Package available as open source and free software.
+
+You are always permitted to make arrangements wholly outside of this
+license directly with the Copyright Holder of a given Package. If the
+terms of this license do not permit the full use that you propose to
+make of the Package, you should contact the Copyright Holder and seek
+a different licensing arrangement.
+
+Definitions
+
+ "Copyright Holder" means the individual(s) or organization(s)
+ named in the copyright notice for the entire Package.
+
+ "Contributor" means any party that has contributed code or other
+ material to the Package, in accordance with the Copyright Holder's
+ procedures.
+
+ "You" and "your" means any person who would like to copy,
+ distribute, or modify the Package.
+
+ "Package" means the collection of files distributed by the
+ Copyright Holder, and derivatives of that collection and/or of
+ those files. A given Package may consist of either the Standard
+ Version, or a Modified Version.
+
+ "Distribute" means providing a copy of the Package or making it
+ accessible to anyone else, or in the case of a company or
+ organization, to others outside of your company or organization.
+
+ "Distributor Fee" means any fee that you charge for Distributing
+ this Package or providing support for this Package to another
+ party. It does not mean licensing fees.
+
+ "Standard Version" refers to the Package if it has not been
+ modified, or has been modified only in ways explicitly requested
+ by the Copyright Holder.
+
+ "Modified Version" means the Package, if it has been changed, and
+ such changes were not explicitly requested by the Copyright
+ Holder.
+
+ "Original License" means this Artistic License as Distributed with
+ the Standard Version of the Package, in its current version or as
+ it may be modified by The Perl Foundation in the future.
+
+ "Source" form means the source code, documentation source, and
+ configuration files for the Package.
+
+ "Compiled" form means the compiled bytecode, object code, binary,
+ or any other form resulting from mechanical transformation or
+ translation of the Source form.
+
+
+Permission for Use and Modification Without Distribution
+
+(1) You are permitted to use the Standard Version and create and use
+Modified Versions for any purpose without restriction, provided that
+you do not Distribute the Modified Version.
+
+
+Permissions for Redistribution of the Standard Version
+
+(2) You may Distribute verbatim copies of the Source form of the
+Standard Version of this Package in any medium without restriction,
+either gratis or for a Distributor Fee, provided that you duplicate
+all of the original copyright notices and associated disclaimers. At
+your discretion, such verbatim copies may or may not include a
+Compiled form of the Package.
+
+(3) You may apply any bug fixes, portability changes, and other
+modifications made available from the Copyright Holder. The resulting
+Package will still be considered the Standard Version, and as such
+will be subject to the Original License.
+
+
+Distribution of Modified Versions of the Package as Source
+
+(4) You may Distribute your Modified Version as Source (either gratis
+or for a Distributor Fee, and with or without a Compiled form of the
+Modified Version) provided that you clearly document how it differs
+from the Standard Version, including, but not limited to, documenting
+any non-standard features, executables, or modules, and provided that
+you do at least ONE of the following:
+
+ (a) make the Modified Version available to the Copyright Holder
+ of the Standard Version, under the Original License, so that the
+ Copyright Holder may include your modifications in the Standard
+ Version.
+
+ (b) ensure that installation of your Modified Version does not
+ prevent the user installing or running the Standard Version. In
+ addition, the Modified Version must bear a name that is different
+ from the name of the Standard Version.
+
+ (c) allow anyone who receives a copy of the Modified Version to
+ make the Source form of the Modified Version available to others
+ under
+
+ (i) the Original License or
+
+ (ii) a license that permits the licensee to freely copy,
+ modify and redistribute the Modified Version using the same
+ licensing terms that apply to the copy that the licensee
+ received, and requires that the Source form of the Modified
+ Version, and of any works derived from it, be made freely
+ available in that license fees are prohibited but Distributor
+ Fees are allowed.
+
+
+Distribution of Compiled Forms of the Standard Version
+or Modified Versions without the Source
+
+(5) You may Distribute Compiled forms of the Standard Version without
+the Source, provided that you include complete instructions on how to
+get the Source of the Standard Version. Such instructions must be
+valid at the time of your distribution. If these instructions, at any
+time while you are carrying out such distribution, become invalid, you
+must provide new instructions on demand or cease further distribution.
+If you provide valid instructions or cease distribution within thirty
+days after you become aware that the instructions are invalid, then
+you do not forfeit any of your rights under this license.
+
+(6) You may Distribute a Modified Version in Compiled form without
+the Source, provided that you comply with Section 4 with respect to
+the Source of the Modified Version.
+
+
+Aggregating or Linking the Package
+
+(7) You may aggregate the Package (either the Standard Version or
+Modified Version) with other packages and Distribute the resulting
+aggregation provided that you do not charge a licensing fee for the
+Package. Distributor Fees are permitted, and licensing fees for other
+components in the aggregation are permitted. The terms of this license
+apply to the use and Distribution of the Standard or Modified Versions
+as included in the aggregation.
+
+(8) You are permitted to link Modified and Standard Versions with
+other works, to embed the Package in a larger work of your own, or to
+build stand-alone binary or bytecode versions of applications that
+include the Package, and Distribute the result without restriction,
+provided the result does not expose a direct interface to the Package.
+
+
+Items That are Not Considered Part of a Modified Version
+
+(9) Works (including, but not limited to, modules and scripts) that
+merely extend or make use of the Package, do not, by themselves, cause
+the Package to be a Modified Version. In addition, such works are not
+considered parts of the Package itself, and are not subject to the
+terms of this license.
+
+
+General Provisions
+
+(10) Any use, modification, and distribution of the Standard or
+Modified Versions is governed by this Artistic License. By using,
+modifying or distributing the Package, you accept this license. Do not
+use, modify, or distribute the Package, if you do not accept this
+license.
+
+(11) If your Modified Version has been derived from a Modified
+Version made by someone other than you, you are nevertheless required
+to ensure that your Modified Version complies with the requirements of
+this license.
+
+(12) This license does not grant you the right to use any trademark,
+service mark, tradename, or logo of the Copyright Holder.
+
+(13) This license includes the non-exclusive, worldwide,
+free-of-charge patent license to make, have made, use, offer to sell,
+sell, import and otherwise transfer the Package with respect to any
+patent claims licensable by the Copyright Holder that are necessarily
+infringed by the Package. If you institute patent litigation
+(including a cross-claim or counterclaim) against any party alleging
+that the Package constitutes direct or contributory patent
+infringement, then this Artistic License to you shall terminate on the
+date that such litigation is filed.
+
+(14) Disclaimer of Warranty:
+THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
+IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
+WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
+NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL
+LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
+DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
279 README
@@ -1,130 +1,149 @@
-Name
-
-Sort::Naturally
-
-Synopsis
-
-Sort strings containing a mix of letters and digits in an order more natural for
-human readers.
-
- use v6;
- use Sort::Naturally;
-
- my @a = <1 11 100 14th 2 210 21 30 3rd d1 d10 D2 D21 d3 aid Are ANY>;
-
- say @a.nsort.join(' ');
- # or
- say @a.sort( { $^a ncmp $^b } ).join(' ');
-
-
-Or, sort a list of dotted quad notation IP addresses:
-
- use v6;
- use Sort::Naturally;
- # generate an array of random IP addresses
- my @ips = ((0..255).roll(4).join('.')for 0..99);
- .say for nsort @ips;
-
-
-Description
-
-Sort::Naturally sorts groups of consecutive digits by order of magnitude then
-lexically.
-
-Similar though not identical to the Perl 5 Sort::Naturally. When sorting strings
-that contain digits, will sort the groups of digits by "order of magnitude",
-then lexically by lowercased terms. Order of magnitude is something of a
-simplification. Sort::Naturally does't try to interpret or evaluate a group of
-digits as a number, it just counts how many digits are in each group and uses
-that as its order of magnitude.
-
-The implications are:
-
- It doesn't understand the (non)significance of leading zeros; 0010, 0100 and
- 1000 are all treated as being of the same order of magnitude and will all be
- sorted to be after 20 and 200.
-
- It doesn't understand floating point numbers; the numbers before and after a
- decimal point are treated as two separate groups of digits.
-
- It doesn't understand or deal with scientific or exponential notation.
-
-However, that also means:
-
- You are not limited by number magnitude. It will sort arbitrarily large
- numbers with ease regardless of the capability of your hardware/OS.
-
- It is quite speedy. (For liberal values of speedy.) Since it doesn't need
- to interpret numbers by value it eliminates a lot of code that would do that.
-
-Sort::Naturally could have been modified to ignore leading zeros, and in fact I
-experimented with that bit, but then ran into issues with sorting zip codes and
-phone numbers. Just remember, it is for sorting strings, not numbers. It makes
-some attempt at treating groups of digits in a kind of numbery way, but they are
-still strings. If you truly want to sort numbers, use a numeric sort.
-
-Usage
-
-Sort::Naturally exposes two primary routines.
-
-C<nsort> is the primary sorting routine. May be called either as a method or a sub.
-C<@array.nsort> or C<nsort @array>.
-
-C<ncmp> is to be used in sort blocks. Useful when you need to do secondary
-sorts.
-
-Say you have a hash containg the words in a document with the keys being the
-number of times each appears. You could sort by word frequency, then naturally
-as follows:
-
- ("%words{$_}, $_").say
- for sort {%words{$^b} <=> %words{$^a} || $^a ncmp $^b}, %words.keys;
-
-Note: using a sort block with an arity > 1 will disable the default Schwartzian
-Transform and may be very slow. If that is an issue either do a manual
-Schwartzian Transform or some kind of caching of terms.
-
-***IMPORTANT CAVEAT***
-As it uses perl6s' sort behind the scenes, Sort::Naturally does a stable sort.
-Therefore terms that evaluate to the same string will be returned in the order
-they were seen. For example: C<say <perl6 Perl6 PERL6 pErL6>.nsort.join(' ');>
-will return "perl6 Perl6 PERL6 pErL6". If this is unacceptable and you need to
-reliably sort uppercase before lower case, filter the list through a standard
-sort first: C<say <perl6 Perl6 PERL6 pErL6>.sort.nsort.join(' ');> returns
-"PERL6 Perl6 pErL6 perl6".
-
-Backward Compatibility
-
-Perl 5 Sort::Naturally has an odd convention in that numbers at the beginning of
-strings are sorted in ASCII order (digits sort before letters) but numbers
-embedded inside strings are sorted in non-ASCII order (digits sort after
-letters). While this is just plain strange in my opinion, some people may rely
-on this behaviour so perl6 Sort::Naturally has "p5 compatibility mode" routines.
-These are analogues of the primary routines prepended with p5.
-
-C<p5nsort()> and C<p5ncmp>. Used identically to the p6 versions
-
- say <foo12z foo foo13a fooa Foolio Foo12a foolio foo12 foo12a 9x
- 14>.p5nsort.join(' ');
-
-yeilds:
-
-9x 14 foo fooa Foolio foolio foo12 Foo12a foo12a foo12z foo13a
-
-rather than:
-
-9x 14 foo foo12 Foo12a foo12a foo12z foo13a fooa Foolio foolio
-
-Bugs
-
-Probably. None known.
-See caveats listed above.
-
-Author
-
-Stephen Schulze (often seen lurking on perlmonks.org and #perl6 IRC as
-thundergnat)
-
-License
-
-Licensed under The Artistic 2.0; see LICENSE.
+NAME
+
+Sort::Naturally
+
+
+SYNOPSIS
+
+ use v6;
+ use Sort::Naturally;
+
+ # sort strings containing a mix of letters and digits sensibly
+ my @a =
+ <1 11 100 14th 2 210 21 30 3rd d1 Any any d10 D2 D21 d3 aid Are ANY >;
+
+ say ~@a.nsort;
+ # or
+ say @a.sort( { $^a ncmp $^b } ).join(' ');
+
+@a.nsort yields:
+
+ 1 2 3rd 11 14th 21 30 100 141 210 aid ANY Any any Are d1 D2 d3 d10 D21
+
+compared to @a.sort:
+
+ 1 100 11 141 14th 2 21 210 30 3rd ANY Any Are D2 D21 aid any d1 d10 d3
+
+
+
+ # or sort a list of dotted quad notation IP addresses:
+ my @ips = ((0..255).roll(4).join('.')for 0..99);
+
+ .say for nsort @ips;
+
+
+DESCRIPTION
+
+Similar though not identical to the Perl 5 Sort::Naturally. When sorting strings
+that contain groups of digits, will sort the groups of consecutive digits by
+"order of magnitude", then lexically by lowercased terms. Order of magnitude is
+something of a simplification. Sort::Naturally does't try to interpret or
+evaluate a group of digits as a number, it just counts how many digits are in
+each group and uses that as its order of magnitude.
+
+The implications are:
+
+ It doesn't understand the (non)significance of leading zeros in numbers;
+ 0010, 0100 and 1000 are all treated as being of the same order of magnitude
+ and will all be sorted to be after 20 and 200. This is the correct behavior
+ for strings of digits where leading zeros are significant, like zip codes or
+ phone numbers.
+
+ It doesn't understand floating point numbers; the groups of digits before
+ and after a decimal point are treated as two separate numbers for sorting
+ purposes.
+
+ It doesn't understand or deal with scientific or exponential notation.
+
+However, that also means:
+
+ You are not limited by number magnitude. It will sort arbitrarily large
+ numbers with ease regardless of the math capability of your hardware/OS.
+
+ It is quite speedy. (For liberal values of speedy.) Since it doesn't need
+ to interpret numbers by value it eliminates a lot of code that would do that.
+
+Sort::Naturally could have been modified to ignore leading zeros, and in fact I
+experimented with that bit, but ran into issues with strings where leading zeros
+WERE significant. Just remember, it is for sorting strings, not numbers. It
+makes some attempt at treating groups of digits in a kind of numbery way, but
+they are still strings. If you truly want to sort numbers, use a numeric sort.
+
+
+USAGE
+
+Sort::Naturally has at its heart a sorting block modifier transformation
+routine: &naturally. This performs a tranform of the sort terms so they will end
+up in the natural order.
+
+C<nsort> is meant to be used the primary natural sorting routine. Syntactic
+sugar for C<sort( { .&naturally } )>. May be called either as a method or a sub:
+C<@array.nsort> or C<nsort @array>.
+
+C<ncmp> is meant to be used in sort blocks. Syntactic sugar for C<sort( {
+$^a.naturally cmp $^b.naturally } )>. Useful when you need to do secondary
+sorts.
+
+Say you have a hash containg the words in a document with the keys being the
+number of times each appears. You could sort by word frequency, then naturally
+as follows:
+
+ ("%words{$_}, $_").say
+ for sort {%words{$^b} <=> %words{$^a} || $^a ncmp $^b}, %words.keys;
+
+Note: using a sort block with an arity > 1 will disable the default Schwartzian
+Transform and may be very slow. If that is an issue either do a manual
+Schwartzian Transform or some kind of caching of terms.
+
+
+BACKWARD COMPATIBILITY
+
+Perl 5 Sort::Naturally has an odd convention in that numbers at the beginning of
+strings are sorted in ASCII order (digits sort before letters) but numbers
+embedded inside strings are sorted in non-ASCII order (digits sort after
+letters). While this is just plain strange in my opinion, some people may rely
+on or prefer this behavior so perl6 Sort::Naturally has "p5 compatibility mode"
+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:
+
+
+ (' sort:',<foo12z foo foo13a fooa Foolio Foo12a foolio foo12 foo12a 9x 14>\
+ .sort).join(' ').say;
+ (' nsort:',<foo12z foo foo13a fooa Foolio Foo12a foolio foo12 foo12a 9x 14>\
+ .nsort).join(' ').say;
+ ('p5nsort:',<foo12z foo foo13a fooa Foolio Foo12a foolio foo12 foo12a 9x 14>\
+ .p5nsort).join(' ').say;
+
+yeilds:
+
+ sort: 14 9x Foo12a Foolio foo foo12 foo12a foo12z foo13a fooa foolio
+ nsort: 9x 14 foo foo12 Foo12a foo12a foo12z foo13a fooa Foolio foolio
+ p5nsort: 9x 14 foo fooa Foolio foolio foo12 Foo12a foo12a foo12z foo13a
+
+
+
+BUGS
+
+Tests and the p5* routines will fail under locales that specify lower case
+letters to sort before upper case. (EBCDIC locales notably). They will still
+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 lexical scope. More an annoyance than a bug.
+
+
+AUTHOR
+
+Stephen Schulze (often seen lurking on perlmonks.org and #perl6 IRC as
+thundergnat)
+
+LICENSE
+
+Licensed under The Artistic 2.0; see LICENSE.
+
View
50 lib/Sort/Naturally.pm6
@@ -1,25 +1,25 @@
-use v6;
-use MONKEY_TYPING;
-my $VERSION = 0.01;
-
-augment class Any {
- our multi method nsort is export { self.list.sort( {
- .lc.subst( /(\d+)/, -> $/ { 0 ~ $0.chars.chr ~ $0 }, :g ) } )
- };
-
- our multi method p5nsort is export { self.list.sort( {
- .lc.subst( /^(\d+)/, -> $/ { 0 ~ $0.chars.chr ~ $0
- } ).subst( /(\D)(\d+)/, -> $/ { $0 ~ 'z{' ~ $1.chars.chr ~ $1 }, :g) } )
- };
-}
-
-our sub infix:<ncmp>($a, $b) {
- [leg] ($a, $b)>>.lc>>.subst(/(\d+)/, -> $/ { 0 ~ $0.chars.chr ~ $0 }, :g)
-};
-
-our sub infix:<p5ncmp>($a, $b) {
- [leg] ($a, $b)>>.lc>>.subst( /^(\d+)/, -> $/ { 0 ~ $0.chars.chr ~ $0
- } )>>.subst( /(\D)(\d+)/, -> $/ { $0 ~ 'z{' ~ $1.chars.chr ~ $1 }, :g)
-};
-
-
+module Sort::Naturally:ver<0.1.0>;
+use v6;
+use MONKEY_TYPING;
+
+augment class Any {
+ method nsort is export(:standard) { self.list.sort( { .&naturally } ) };
+ method p5nsort is export(:p5) { self.list.sort( { .&p5naturally } ) };
+}
+
+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 naturally ($a) is export(:standard) {
+ $a.lc.subst(/(\d+)/, ->$/ { 0 ~ $0.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 ...>
+ # lookaround implemented yet. 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
+}
View
81 t/01-basic.t
@@ -1,28 +1,53 @@
-use v6;
-use Sort::Naturally;
-use Test;
-
-plan *;
-
-my @test;
-my @nsorted;
-
-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");
-
-@test = <2 210 21 30 3rd d1 d10 D2 D21 d3 aid Are ANY 1 11 100 14th>;
-@nsorted = <1 2 3rd 11 14th 21 30 100 210 aid ANY Are d1 D2 d3 d10 D21>;
-
-is(~@test.sort.nsort, ~@nsorted, "calling nsort as method yeilds correct order");
-is(~nsort(@test.sort), ~@nsorted, "calling nsort as subroutine yeilds correct order");
-is(~@test.sort.sort( { $^a ncmp $^b } ), ~@nsorted, "calling ncmp in a sort block yeilds correct order");
-
-my @p5test = <foo12z foo foo13a fooa Foolio Foo12a foolio foo12 foo12a 9x 14>;
-my @p5sorted = <9x 14 foo fooa Foolio foolio foo12 Foo12a foo12a foo12z foo13a>;
-
-is(~@p5test.sort.p5nsort, ~@p5sorted, "calling p5nsort as method yeilds correct order");
-is(~p5nsort(@p5test.sort), ~@p5sorted, "calling p5nsort as subroutine yeilds correct order");
-is(~@p5test.sort.sort( { $^a p5ncmp $^b } ), ~@p5sorted, "calling p5ncmp in a sort block yeilds correct order");
-
-
+use v6;
+use Sort::Naturally;
+use Test;
+
+
+plan 12;
+
+my @test;
+my $nsorted = '';
+
+# does it deal with empty array in a sane fashhion?
+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");
+
+
+# does it return the terms in the expected oorder?
+@test = <2 210 21 30 3rd d1 d10 D2 D21 d3 aid Are any ANY 1 Any 11 100 14th>;
+$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 yeilds expected order");
+is(~@test.pick(+@test).nsort, $nsorted,
+ "calling nsort as method yeilds expected order");
+is(~nsort(@test.pick(+@test)), $nsorted,
+ "calling nsort as subroutine yeilds expected order");
+is(~@test.pick(+@test).sort( { $^a ncmp $^b } ), $nsorted,
+ "calling ncmp in a sort block yeilds expected order");
+
+
+# do the compatibility routines return terms in the expected order?
+my @p5test = <foo12z foo foo13a fooa Foolio Foo12a foolio foo12 foo12a 9x 14>;
+my $p5sorted = '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 } ),
+ $p5sorted, "calling &p5naturally in a sort block yeilds expected order");
+is(~@p5test.pick(+@p5test).p5nsort, $p5sorted,
+ "calling p5nsort as method yeilds expected order");
+is(~p5nsort(@p5test.pick(+@p5test)), $p5sorted,
+ "calling p5nsort as subroutine yeilds expected order");
+is(~@p5test.pick(+@p5test).sort( { $^a p5ncmp $^b } ),
+ $p5sorted, "calling p5ncmp in a sort block yeilds expected order");
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.