Hacker News new | ask | show | jobs
by icen 1701 days ago
/ 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

1 comments

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?

> How else would you want to extend a fold to two arguments?

I don't know that there is one that's useful, but a cartesian product seems like the opposite: a fold is a dimensional reduction (from 1 to 0 at the most basic), a cartesian product is dimensionally preserving (you start with 1 and 1, you end up with 2).

A cartesian product (with a mapping function) might be interpreted as the extension of a map, I don't rightly see how it can be interpreted as the extension of a fold, except in the sense that you can implement essentially any iterative process using folds, but that leads to you every n-dimensional transformation being a possible extension of a fold.

So my answer would mostly be "you would not". You could also define an n-dimensional fold as a fold against a caller-specified dimension, but I don't know how useful that would be (though I guess this sort of folds is used when integrating over time in e.g. reactive interfaces).