In Cool§
See primary documentation in context for method words
method words(Cool: |c)
Coerces the invocant (or first argument, if it is called as a subroutine) to Str
, and returns a list of words that make up the string. Check Str.words
for additional arguments and its meaning.
say <The quick brown fox>.words.join('|'); # OUTPUT: «The|quick|brown|fox»say <The quick brown fox>.words(2).join('|'); # OUTPUT: «The|quick»
Cool
is the base class for many other classes, and some of them, like Match
, can be converted to a string. This is what happens in this case:
say ( "easy come, easy goes" ~~ m:g/(ea\w+)/).words(Inf);# OUTPUT: «(easy easy)»say words( "easy come, easy goes" ~~ m:g/(ea\w+)/ , ∞);# OUTPUT: «(easy easy)»
The example above illustrates two of the ways words
can be invoked, with the first argument turned into invocant by its signature. Inf
is the default value of the second argument, so in both cases (and forms) it can be simply omitted.
Only whitespace (including no-break space) counts as word boundaries
say <Flying on a Boeing 747>.words.join('|'); # OUTPUT: «Flying|on|a|Boeing|747»
In this case, "Boeing 747" includes a (visible only in the source) no-break space; words
still splits the (resulting) Str
on it, even if the original array only had 4 elements:
say <Flying on a Boeing 747>.join('|'); # OUTPUT: «Flying|on|a|Boeing 747»
Please see Str.words
for more examples and ways to invoke it.
In IO::Path§
See primary documentation in context for method words
method words(IO::Path: : = True, : = 'utf8', : = ["\x0A", "\r\n"], |c --> Seq)
Opens the invocant and returns its words.
The behavior is equivalent to opening the file specified by the invocant, forwarding the :$chomp
, :$enc
, and :$nl-in
arguments to IO::Handle.open
, then calling IO::Handle.words
on that handle, forwarding any of the remaining arguments to that method, and returning the resultant Seq
.
NOTE: words are lazily read. The handle used under the hood is not closed until the returned Seq
is fully reified, and this could lead to leaking open filehandles. It is possible to avoid leaking open filehandles using the $limit
argument to cut down the Seq
of words to be generated.
my := bag 'my-file.txt'.IO.words;say "Most common words: ", .sort(-*.value).head: 5;
In Supply§
See primary documentation in context for method words
method words(Supply: --> Supply)
Creates a supply that will emit the characters coming in word for word from a supply that's usually created by some asynchronous I/O operation.
my = Supply.from-list("Hello Word!".comb);my = .words;.tap(); # OUTPUT: «HelloWord!»
In IO::CatHandle§
See primary documentation in context for method words
method words(IO::CatHandle: = Inf, : --> Seq)
Same as IO::Handle.words
(including the caveat about more data read than needed to make some number of words). Note that a boundary between source handles is considered to be word boundary.
(my = 'foo'.IO).spurt: 'foo bar';(my = 'bar'.IO).spurt: 'meow';IO::CatHandle.new(, ).words.raku.say;# OUTPUT: «("foo", "bar", "meow").Seq»
Note: if :$close
is False
, fully-consumed handles are still going to be closed.
In Str§
See primary documentation in context for routine words
multi method words(Str: )multi method words(Str:)
Returns a list of non-whitespace bits, i.e. the same as a call to $input.comb( / \S+ /, $limit )
would.
Examples:
say "a\nb\n".words.raku; # OUTPUT: «("a", "b").Seq»say "hello world".words.raku; # OUTPUT: «("hello", "world").Seq»say "foo:bar".words.raku; # OUTPUT: «("foo:bar",).Seq»say "foo:bar\tbaz".words.raku; # OUTPUT: «("foo:bar", "baz").Seq»
It can also be used as a subroutine, turning the first argument into the invocant. $limit
is optional, but if it is provided (and not equal to Inf
), it will return only the first $limit
words.
say words("I will be very brief here", 2); # OUTPUT: «(I will)»
In IO::Handle§
See primary documentation in context for routine words
multi words(IO::Handle = , = Inf, : --> Seq)multi method words(IO::Handle: = Inf, : --> Seq)
Similar to Str.words
, separates the handle's stream on contiguous chunks of whitespace (as defined by Unicode) and returns a Seq
of the resultant "words." Takes an optional $limit
argument that can be a non-negative Int
, Inf
, or Whatever
(which is interpreted to mean Inf
), to indicate only up-to $limit
words must be returned. If Bool
:$close
named argument is set to True
, will automatically close the handle when the returned Seq
is exhausted. Subroutine form defaults to $*ARGFILES
, if no handle is provided.
Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode
exception being thrown.
my := bag .words;say "Most common words: ", .sort(-*.value).head: 5;
NOTE: implementations may read more data than necessary when a call to .words
is made. That is, $handle.words(2)
may read more data than two "words" worth of data and subsequent calls to read methods might not read from the place right after the two fetched words. After a call to .words
, the file position should be treated as undefined.