News for hackers. Stuff that matters.
Re:Array Languages: R vs. APL by cl3misch on Thursday March 21, 2024 @05:06PM (#39784407)
> what if we just generate all products from the set of numbers 2:n and exclude those as "not prime" from all the numbers up to n?

It's fun to translate terse APL to somewhat terse numpy. The result still can be very compact and you can parse it easily if you're used to looking at numpy:

    s = arange(2, 50); p = outer(s, s).ravel(); sorted(set(s) - set(p))
Re:Array Languages: R vs. APL by jonocarroll on Thursday March 21, 2024 @05:29PM (#39784629)
What's interesting there is that numpy is inspired (more than a little) by APL and aims to bring that 'array' thinking to python. I agree that thinking in this 'array' way helps to better construct a solution in any language, so I'm leaning towards 'designing' with APL glyphs, even if that's not the language I'm implementing the thing in.
Re:Array Languages: R vs. APL by nerdponx on Thursday March 21, 2024 @05:40PM (#39784733)
If it takes any inspiration from APL, it would be mostly indirect, via Matlab.
Re:Array Languages: R vs. APL by jonocarroll on Thursday March 21, 2024 @06:05PM (#39784987)
I've seen the connection made here https://dev.to/bakerjd99/numpy-another-iverson-ghost-9mc though the link to the source quote is broken. In all fairness, Matlab is itself inspired by APL.
Re:Array Languages: R vs. APL by bruturis on Thursday March 21, 2024 @05:45PM (#39784783)
Analogous code in J,

  /:~ s -. p [ p =: s*/s [ s=: 2+i.48
An exercise for numpy, test that GCD(x,y) * LCM(x,y) = x*y using 1000 random numbers in the range 0..99 for x e y.

  test =:  (* = *. * +.) & ?
  *./ test~  1000 # 100
Re:Array Languages: R vs. APL by cl3misch on Friday March 22, 2024 @06:44AM (#39789305)
Thanks, that was fun.

    def d(x): N = arange(1, x + 1); return N[x % N == 0]
    def m(x, n): return x * arange(1, n + 1)
    def gcd(x, y): return max(set(d(x)) & set(d(y)))
    def lcm(x, y): return min(set(m(x, y)) & set(m(y, x)))
    def test(x, y): return gcd(x, y) * lcm(x, y) == x * y
    all([test(x, y) for (x, y) in randint(1, 100, (1000, 2))])  # True
I am not a good golfer. Now I want to look at the codegolf stackexchange for this...
Re:Array Languages: R vs. APL by bear8642 on Friday March 22, 2024 @08:56PM (#39796388)
Equivalently in APL:

  ⎕io←0               ⍝ to match J
  test ← (× = ∨ × ∧) ∘ ?
  ∧⌿ test⍨ 1000⍴100