Certainly...

unpack=:-.&;:&'-';._2

sample=: unpack S=: {{)n
start-A
start-b
A-c
A-b
b-d
A-end
b-end
}}

Note that I've added a variable to capture the upstream noun which
unpack will process. So, we're looking at this:

   unpack S
+-----+---+
|start|A  |
+-----+---+
|start|b  |
+-----+---+
|A    |c  |
+-----+---+
|A    |b  |
+-----+---+
|b    |d  |
+-----+---+
|A    |end|
+-----+---+
|b    |end|
+-----+---+

Anyways, unpack is a verb of the form F;._2 which means that F would
be applied to each of the lines of S (with the linefeed removed).

   5!:6<'unpack'
((-.&;:)&'-');._2

Here, we could extract the first line and walk through that:

   7{.S
start-A
   (-.&;:)&'-' 7{.S
+-----+-+
|start|A|
+-----+-+

And, if you like, you could inspect this using

   load'debug/dissect'
   dissect {{)n (-.&;:)&'-' 7{.S}}

But let's break this down formally. The sentence in question is formed
using conjunctions.

In other words, we could expand it using J's grammar rules to this:
   (;: 7{.S) -. ;:'-'

or, this:

   (;:'start-A') -. ;:'-'
+-----+-+
|start|A|
+-----+-+

Hopefully this has become clear, or at least close to comprehensible?

The verb ;: tokenizes text. The central verb here is -. which removes
from the sequence on the left any values which appeared in the
sequence on the right.

So... restated in english, unpack tokenzies each line, then removes
the <,'-' tokens leaving us with the other two tokens for each line.

But I also wanted to take this opportunity to show some hints about
how you can read J. Being able to read J is kind of the point here,
and being able to see what code does to relevant example values is a
critical aspect of understanding what the code is doing.

Thanks for the question,

-- 
Raul

On Sun, Jan 2, 2022 at 2:26 PM Brian Schott <[email protected]> wrote:
>
> Raul,
>
> I cannot understand how *unpack* works.
> When I use 9!:3]6 to display *unpack* I get the following.
>
> ((-.&;:)&'-');._2
>
>
> But that does not help me because I cannot process the parenthesized parts
> separately.
>
> The only "part" I can process is
>
> ;: ;. _2 in
>
> where *in* is defined as follows.
>
>
>    in =: 0 : 0
> start-A
> start-b
> A-c
> A-b
> b-d
> A-end
> b-end
> )
>
>
> But then I lose the ability to process the rest of *unpack* because the
> LF's are gone.
>
> So I suspect my problem is a parsing order unknown.
>
> Can you say how *unpack* is parsed, please?
>
> unpack=:-.&;:&'-';._2
>
> On Sat, Jan 1, 2022 at 11:52 PM Raul Miller <[email protected]> wrote:
>
> > https://adventofcode.com/2021/day/12
> >
> > For day 12, we had a "cave map" which was a list of connections
> > between named pairs of rooms.
> >
> > unpack=:-.&;:&'-';._2
> >
> >
> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to