Need help parsing the meaning of "i.~"

Viewed 119

I'm trying to understand the solution for day 1 part 2: https://code.jsoftware.com/wiki/Essays/Advent_Of_Code#Part_2

PART2=: >: _1 i.~ +/\ 1 _1 mp '()'=/read'input'

I feel like I understand most of what's going on here but I'm not sure how to interpret the

i.~ 

part. I know what "i." generally does but I'm confused by the "~" here. My understanding is that "~" duplicates the right args to also be on the left. But here we already have a "_1" so I'm not sure how to interpret the semantics of this.

Also any tips on how to track this down myself are greatly appreciated.

1 Answers

It seems like my comments discouraged others from providing the typical official answers in answer format. So I’m reproducing them as an answer proper.

The adverb commute ~ simply flips the arguments to the dyadic verb it’s attached to. So 3 % 5 is three-fifths but 3 %~ 5 is five-thirds (aka 5 % 3).

So, in your verb, the dyad i. is the one you’re familiar with (index of), but the ~ makes the constant _1 its right argument, even though, textually, it appears to its left. Thus, the i. is looking for the first _1 in the result of +/\ 1 _1 mp ….

You can find it spelled out in the NuVoc here or in the original J Vocabulary here. The NuVoc is designed to be more accessible, the Vocabulary canonical.

In the NuVoc, it's expressed as Thus, x u~ y is the same as y u x. The Vocabulary states it more mathematically, with the equivalence assertion x u~ y ↔ y u x (on the right half of the page, since we're talking about dyad i. here, instead of the monad).

Related