Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

61 lines (37 sloc) 1.64 kb

NAME

PAST::Pattern::Closure - A PAST::Pattern subclass for predicate functions.

SYNOPSIS

    pir::load_bytecode('PAST/Pattern.pbc');
    my &isOdd := sub ($_) { $_ % 2 };
    my $pattern := PAST::Val.new(:value(PAST::Pattern::Closure.new(&isOdd));
    my $/ := $past ~~ $pattern;
    say('$past ' ~ ($/ ?? 'had' !! 'did not have' ~ ' an odd :value.');
    say('The value was ' ~ $<value>.from());

DESCRIPTION

A PAST::Pattern::Closure matches any pattern for which its associated subroutine or other invokable object returns a true result.

PAST::Pattern::Closure is PAST::Pattern

A PAST::Pattern::Closure contains a predicate subroutine or other object that can be invoked. This predicate sub is used to determine whether or not an object matches the pattern. If the predicate, when called with the object to be matched, produces a true result, the object matches the pattern. Otherwise, it does not.

PAST::Pattern::patternize produces a PAST::Pattern::Closure when called with an object which provides the invokable role.

Attributes

code

The code attribute holds the predicate invokable object that is used to determine whether the pattern matches an object.

Methods

new(code)

Creates a PAST::Pattern::Closure with code as its associated predicate.

code([value])

If value is provided, set the code attribute of the pattern to value. Otherwise, return the current value of the code attribute.

ACCEPTSEXACTLY(node)

Used internally by the inherited ACCEPTS method to determine whether a node exactly matches the pattern.

Jump to Line
Something went wrong with that request. Please try again.