develooper Front page | perl.perl6.language | Postings from February 2005

Re: Fwd: Junctive puzzles.

Thread Previous | Thread Next
From:
Damian Conway
Date:
February 10, 2005 15:46
Subject:
Re: Fwd: Junctive puzzles.
Message ID:
420BF23A.8040909@conway.org
Rod Adams wrote:

> The overall impression I'm getting here is that we need some syntax for 
> saying:
> 
> $x = any(1..1000) such_that is_prime($x);

In standard Perl 6 that'd be:

   $x = any(grep {is_prime $^x} 1..1000);

or, if you prefer your constraints postfixed:

   $x = any( (1..1000).grep({is_prime $^x}) );

If you really wanted a "such that" operator you could certainly create one 
yourself:

   multi sub *infix:<such_that> (Junction $j, Code $constraint) {
     return $j.type.new(grep $constraint, $j.values);
   }

   $x = any(1..1000) such_that {is_prime $^x};

Though, personally, I think a C<.where> method with an adverbial block might 
be neater:

   multi method Junction::where (Junction $j: *&constraint) {
     return $j.type.new(grep &constraint, $j.values);
   }

   $x = any(1..1000).where:{is_prime $^x};

   # or...

   $x = where any(1..1000) {is_prime $^x};




> We then can say that any junction stored in a var stays constant, until 
> explicitly reassigned. Just like every other kind of thing we store.

Yep. That's exactly what we'll be saying!


> Philosophy Question:
> 
> What's the difference between a junction and an array containing all the 
> possible values of the junction?

Junctions have an associated boolean predicate that's preserved across 
operations on the junction. Junctions also implicitly distribute across 
operations, and rejunctify the results.


> So, on that train of thought, would this make sense:
> 
> if $x == @x.any {...}
> if $x == @x.none {...}

Probably. It's entirely possible that, in addition to being built-in list 
operators, C<all>, C<any>, C<one>, and C<none> are also multimethods on 
Scalar, Array, and List.

Damian

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About