4 ms·
> 1{::(('FizzBuzz';'Fizz';'Buzz';":) 1337) Fizz Is equivalent to: > ['FizzBuzz', 'Fizz', 'Buzz', 1337][1] Fizz In a language with less foreign syntax
by 101km 10y ago
> 1{::(('FizzBuzz';'Fizz';'Buzz';":) 1337)
Fizz
Is equivalent to:
> ['FizzBuzz', 'Fizz', 'Buzz', 1337][1]
Fizz
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 '~':
(('FizzBuzz';'Fizz';'Buzz';":) 1337){::~1
Fizz
This is the modulo bit:
(0 i.~15 3 5|])
- caioariede 10y agoI 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)
- 101km 10y agoYep, 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.
- caioariede 10y agoIn 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.