Raku Land

WhereList

github:raku-community-modules

Build Status

NAME

WhereList - Simpler where constraints for items of lists

SYNOPSIS

use WhereList;

subset StrArray of Array where all-items Str, *.chars  3, *.contains: any <o a e>;

say [<foo bar meow>] ~~ StrArray; # OUTPUT: «True␤»
say ['dddrrrrrrrrr'] ~~ StrArray; # OUTPUT: «False␤»
say [<ha ha ha ha >] ~~ StrArray; # OUTPUT: «False␤»
say [<ooh come onn>] ~~ StrArray; # OUTPUT: «True␤»

class Foo {
    has @.bar where all-items any Str|Nil|Int:D, * === Any
}

sub foo (
    @meows where all-items(/meow/), # <-- use parens to avoid gobbling of params that follow
    @barks where all-items(/woof/) = ['woof'], # <-- or to add defaults
) {
    
}

DESCRIPTION

Type-constraining elements of list parameters, attributes, and subsets can be done with where clauses, however, they can quickly get overly complex when you want to constraint using multiple requirements. This module addresses that problem!

EXPORTED SUBROUTINES

&all-items

Defined as:

    sub all-items (+@matchers)

See SYNOPSIS for sample use.

Takes a list of matchers (anything that can be fed to .grep). Returns a Callable that a where clause can use to check whether all items in a list match all of these matchers. Matchers will be checked in the order provided, short-circuiting as soon as a matcher fails. If an exception occurs during matching, it will be turned into a Failure, gracefully failing the type check.

Notes and tips:


REPOSITORY

Fork this module on GitHub: https://github.com/raku-community-modules/WhereList

BUGS

To report bugs or request features, please use https://github.com/raku-community-modules/WhereList/issues

AUTHOR

Zoffix Znet (http://perl6.party/)

LICENSE

You can use and distribute this module under the terms of the The Artistic License 2.0. See the LICENSE file included in this distribution for complete details.

The META6.json file of this distribution may be distributed and modified without restrictions or attribution.