Re:FizzBuzz in J, Explained
by 101km on Thursday January 19, 2017 @03:41PM
(#13439278)
Is equivalent to:
In a language with less foreign syntax.
You can make it more familiar by putting the index on the right side of the array with '~':
This is the modulo bit:
[ Parent
| on HN ]
Re:FizzBuzz in J, Explained
by caioariede on Thursday January 19, 2017 @04:04PM
(#13439477)
I think comparing it with other languages may make it easier to grasp. Sometimes it's good to test it in a language you are used to. The selector expression could be represented in Python like this:
a = lambda n: [n % x for x in [15, 3, 5]].index(0)Yep, I started writing this out (first in js, but it doesn't map well, python list comprehensions do) but you beat me to it. :)
Now great, you can get the weird syntax and unroll it if you squint. Why is it written like this? Isn't it wasteful to test against all three cases (15,3,5) instead of returning early?
Probably has something to do with 'thinking in arrays' and how its a cheap comparison and that there's probably matrix multiplication or bit twiddling going on under there. That's the interesting bit to me.
To get the feel for an array language. This fizzbuzz example is actually surprisingly interesting because of that.
Re:FizzBuzz in J, Explained
by caioariede on Thursday January 19, 2017 @04:13PM
(#13439551)
In fact, it's still not the same. In Python, the following expression will raise a ValueError.
> a(2)
In J, it will return the length of the array, as stated in the post.