Settings

Theme

The Panda Language: No Loops, No Ifs, Just Fun

pandalang.org

5 points by jiangplus 6 years ago · 7 comments

Reader

eesmith 6 years ago

FWIW, here's the modern Python equivalent to "1..10.odd.sqr.lt(50)"

  >>> [s for i in range(50) if i&1 and (s:=i*i) < 50]
  [1, 9, 25, 49]
And here's my interpretation of the C equivalent:

      #include <stdio.h>
        
      int main() {
        int i, s;
        for(i = 1; i <= 10; i++) {
          if (i & 1 && (s = i*i) < 50) {
              printf("%d ", s);
          }
        }
        return 0;
      }
Not quite a-la 1972, but then again the example 1972 code from that page wouldn't compile then either - variables had to be declared at the start of the function.

I wonder what the APL looks like. I hacked this solution:

      (x<50)/x←(1=2|x)/x←((⍳10)*2)
but I'm guessing the real solution would be 1/3 the size.
  • gandercrews 6 years ago

    rewritten apl version w/ lambdas and commute (vs assignment & parens):

              {⍵/⍨⍵<50}{2*⍨⍵/⍨1=2|⍵}⍳10
        1 9 25 49
    
    About as short as I can make it, and while its longer than the pandas solution, the primitives are significantly more general (sqr, odd - why include these as language primitives?).
    • eesmith 6 years ago

      Thanks! It's been a long time since I tried APL. All I could remember was ⍳ and right-to-left evaluation. The rest was through monkeying around with Rosetta Code examples.

      As to your "why" question - I'm assuming to make a neat demo. Something like:

        >>> from math import cos, sin
        >>> [s for i in range(50) if i&1 and cos(s:=i*i) < sin(i)]
        [1, 9, 49, 225, 361, 441, 625, 1089, 1521, 1681, 2025, 2209]
      
      seems more difficult to pull off in a pipeline API.
      • gandercrews 6 years ago

        nah its pretty straightforward w/ higher order fns.

        one of many possible solutions:

                  {(1○⍺)>2○⍵} {(⍹ ⍵)/⍨⍵ ⍶ ⍹ ⍵} {⍵*2} {⍵/⍨2|⍵}⍳50
            1 9 49 225 361 441 625 1089 1521 1681 2025 2209
        • eesmith 6 years ago

          Oh, sorry, I was too clever by half. I should have been more specific - hard to pull off in Panda.

          Thanks for an APL solution in any case!

Gravityloss 6 years ago

Reminds me of Ruby.

  • boygobbo 6 years ago

    In case anyone's interested, here's a Ruby version:

      (1..10).select(&:odd?).map{|x| x*x }.select{|x| x < 50 }

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection