Four is the number of letters in the ...: Difference between revisions

→‎{{header|Perl 6}}: Update for updated module
m (→‎{{header|Perl 6}}: Update for module change)
(→‎{{header|Perl 6}}: Update for updated module)
Line 542:
Uses the Lingua::EN::Numbers module to generate both cardinal and ordinal numbers. This module places commas in number words between 3-orders-of-magnitude clusters. E.G. <code>12345678.&ordinal</code> becomes: twelve million, three hundred forty-five thousand, six hundred seventy-eighth. Uses a custom 'no-commas' routine to filter them out for accurate character counts. Generates the 'sentence' lazily so only the words needed are ever calculated and reified.
 
<lang perl6>use Lingua::EN::Numbers; # Version 2.4.0 or higher
no-commas(True);
 
my $index = 1;
my @sentence = flat 'Four is the number of letters in the first word of this sentence, '.words,
{ @sentence[$index++].&alpha.&cardinal, 'in', 'the', |($index.&ordinal.&no-commas ~ ',').words } ... * ;
 
sub alpha ( $str ) { $str.subst(/\W/, '', :g).chars }
sub no-commas ( $str ) { $str.subst(',', '', :g) }
sub count ( $index ) { @sentence[^$index].join(' ').chars ~ " characters in the sentence, up to and including this word.\n" }
 
10,327

edits