Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Concise foreach expression

by LanX (Saint)
on Aug 29, 2014 at 17:00 UTC ( [id://1099018]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Concise foreach expression
in thread Concise foreach expression

> Could you explain what you mean there?

I was talking about chaining maps or fors to simulate nested foreach loops.

I.o.w. list comprehensions are very hard to implement in Perl without nested blocks.

e.g. try to implement this Python example for "Pythagorean triples" w/o nesting

>>> [(x,y,z) for x in range(1,30) for y in range(x,30) for z in range( +y,30) if x**2 + y**2 == z**2] [(3, 4, 5), (5, 12, 13), (6, 8, 10), (7, 24, 25), (8, 15, 17), (9, 12, + 15), (10, 24, 26), (12, 16, 20), (15, 20, 25), (20, 21, 29)]

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

Replies are listed 'Best First'.
Re^4: Concise foreach expression
by AppleFritter (Vicar) on Aug 29, 2014 at 17:21 UTC
    Ah, yes, I see what you mean.
      FWIW

      I once implemented a ( Monad style ;-) approach to emulate this

       @results = take { [$a,$b,$c] if $a**2 +$b**2 ==$c**2 } from {1..30} $a from {1..$a} $b from {1...$b} $c

      take(&$) and from(&$$) where just ordinary functions passing iterators around.

      The problem I couldn't solve was a DRY scoping of the variables, to be strict I needed to surround the whole expression with

      { my($a,$b,$c); ... }

      my declarations don't effect the scope of the same line. :(

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      update

      hmm maybe this syntax is acceptable?

      from { 1..30} my $a from {1..$a} my $b from { 1...$b} my $c; @results = take { [$a,$b,$c] if $a**2 +$b**2 ==$c**2 }

      when called from void context from() could put the iterator into $_ , and take() could read it again from $_ if called with just one block arg...

      never mind doesn't work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1099018]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-03-28 10:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found