Hacker News new | ask | show | jobs
by masklinn 1703 days ago
The SO answer is easy to follow right until the final operator “/“ where it just goes “magic”. That’s less than helpful as the rest is quite straightforward aside from the syntax.

Its very short description of the two-arguments version make it sound like a fold, but that wouldn’t generalise to what looks like a cartesian product, would it?

1 comments

If I understand it right "*/" results in a function that applies "*" between its arguments. "~" takes the function f(x) on its left and the argument y on its right and turns it into f(y, y).

So the right side of the tilde evaluates to a range 1..10. I think the tilde then turns its left and right into "*/"(1..10, 1..10), resulting in 1..10 * 1..10

/ does different things depending on the arity of the preceding function.

'f/ x' is 'insert' - i.e. reduce, so that '+/ 1+i.10' yields 55.

'x f/ y' is 'table', inserting 'f' between each pair to form a generalised multiplication table.

The documentation for j is unsurprisingly terse, but it is complete and very helpful.

https://code.jsoftware.com/wiki/Vocabulary/slash#dyadic

Ah so the explanation is pretty much just “magic”.
It's no more magical than anything else. It just turns out that j has an operator that does exactly what you want.

Having such operators and functions is far from an uncommon experience in j.

My problem with this is not “J has an operator that does exactly what you want” it’s “J has operators which do entirely different things depending on the arity”, you can’t intuit the behaviour of the binary / from the unary, they are for all intents and purposes unrelated.

As a result, the original mathexchange answer introducing / by explaining its unary behaviour (despite that being used nowhere in the answer) is solely a source of confusion.

The original description is misleading, yes.

The distinguishing between arities is a consistent feature of all of these languages. I find fluency comes quickly to it.

Edit: as for intuition between the two, this is sometimes not obvious, but I think it's a sensible generalisation from the monadic case to the dyadic. How else would you want to extend a fold to two arguments?

> I think the tilde then turns its left and right into "/"(1..10, 1..10), resulting in 1..10 1..10

That is the part which doesn’t make sense to me: `/ v` is `foldl1 () v`, how does that generalise to a cartesian product when applied to >1 sequences?

Dyadic `x / y` is different to monadic `/ y`
Something akin to a map operation maybe?