I'll run through a perhaps shallow description of my approach -
hopefully that will show you enough of what's going on to also help
understand R.E. Boss's approach. My hope here is that looking at this
from a different perspective will help you build your understanding...
a=. 10 345 64 5642 11 5
,.&.":&.>a
This has two sentences with six "words" in the second sentence. But,
also if you run
load 'debug/dissect'
dissect ',.&.":&.>a'
then you'll see a chain of five verbs (some directly from the original
expression, some derived by the original expression) chaining between
the argument value and the result value. So let's run through both of
those vocabularies:
> would unbox things in a box, but that's not why it's being used
here. Instead, we're using it because it has rank 0, and because its
inverse (or "obverse") is to box things.
&. appears twice in the original expression, so counts as two words
there. It also is not a verb and is shown as a verb by dissect. What
this does is run the verb on its right on the data, and then run the
verb on its left on that result (with a free set of parenthesis around
any completed adverb or conjunction chain on its left). And then -
this is the important part - it runs the inverse of the verb on its
right (no free parenthesis there, and for good reason) on the result
of the verb on its left.
Normally, documentation will use u to represent the left verb and v to
represent the right verb, and y to represent the data on the right.
So, with that shorthand, what &. does is: (v inv) u v y
": turns numbers into a sequence of characters which represent that
number. For the purpose of this expression, it's good on numbers up to
15 digits. If you want the expression to work on bigger numbers, be
sure to use extended precision (put an 'x' after the last digit of one
of the numbers). But, also, the inverse of ": is ". which turns
character sequences back into numbers.
,. turns a list into a column. This breaks up digits like '345'
into separate rows, so that when they're turned back into numbers each
becomes a separate number.
Putting this back together, the expression works like this on a single number:
< ". ,. ": y
Or, packaging things up to show how the rank 0 effect of the
expression kicks in:
(3 :'< ". ,. ": y' "0) a
(The parenthesis are not strictly necessary, but if you replace a with
a literal number you'll be glad you have them. Without them, the 0 in
"0 would become a part of that literal number and you'd get an error.
With the parenthesis, it just works:
(3 :'< ". ,. ": y' "0) 10 345 64 5642 11 5
┌───┬─────┬───┬───────┬───┬─┐
│1 0│3 4 5│6 4│5 6 4 2│1 1│5│
└───┴─────┴───┴───────┴───┴─┘
And there are other ways you could rewrite this expression. For
example, in the dissect view of things, you'll see >^:_1 where I have
used < (they do the same operation).
But that's enough of me talking - you should try working with some of
these expressions so that you can be a bit more comfortable with what
they do (and your finger memory should help your mental memory, later
on, if you try things out).
I hope this helps,
--
Raul
On Tue, Feb 27, 2018 at 2:27 AM, Skip Cave <[email protected]> wrote:
> Thanks to Raul & R. E. Boss for solutions to my Separating Digits problem.
> Either solution works well for my application. However, understanding how
> either of them works, is still a challenge for me.
>
> a=. 10 345 64 5642 11 5
>
>
> Raul's solution:
>
> ,.&.":&.> a
> ┌───┬─────┬───┬───────┬───┬─┐
> │1 0│3 4 5│6 4│5 6 4 2│1 1│5│
> └───┴─────┴───┴───────┴───┴─┘
>
>
> My first problem with this is the > which should *unbox* an already
>
> unboxed a, but instead somehow boxes it.
>
>
> Not only that, but I know ": turns numerics into literal text characters.
>
>
> datatype > ":&.> a
>
> literal
>
> However in this case, after unboxing the ,. the literals are somehow turned
> back into
>
> integers.
>
>
> datatype > ,.&.":&.> a
>
> integer
>
>
> ?? magic ??
>
>
> R. E. Boss' solution
>
>
> 10#.^:_1 &.> a
>
> ┌───┬─────┬───┬───────┬───┬─┐
> │1 0│3 4 5│6 4│5 6 4 2│1 1│5│
> └───┴─────┴───┴───────┴───┴─┘
>
>
> I have the same issue with the > which should *unbox* an already
>
> unboxed a, but instead somehow boxes it.
>
>
> Then I can see that this:
>
> 10#.^:_1 a
>
> 0 0 1 0
>
> 0 3 4 5
>
> 0 0 6 4
>
> 5 6 4 2
>
> 0 0 1 1
>
> 0 0 0 5
>
>
> is the same as this:
>
> 10 10 10 10 #: a
>
> 0 0 1 0
>
> 0 3 4 5
>
> 0 0 6 4
>
> 5 6 4 2
>
> 0 0 1 1
>
> 0 0 0 5
>
> So this:
>
> 10#.^:_1 &.> a
>
> ┌───┬─────┬───┬───────┬───┬─┐
>
> │1 0│3 4 5│6 4│5 6 4 2│1 1│5│
>
> └───┴─────┴───┴───────┴───┴─┘
>
> Should be the same as this:
>
>
>
> 10 10 10 10 #: &.> a
>
> |length error
>
> | 10 10 10 10 #:&.>a
>
>
> But it's not! ??
>
>
>
> Skip
>
>
>
>
>
>
> Skip Cave
> Cave Consulting LLC
>
> On Sun, Feb 25, 2018 at 1:17 PM, Skip Cave <[email protected]> wrote:
>
>> how can I take a vector of integers:
>>
>> a=. 10 345 64 5642 11 5
>>
>> and separate them into their individual digits (integers):
>>
>> sep a
>>
>> ┌───┬─────┬───┬───────┬───┬─┐
>>
>> │1 0│3 4 5│6 4│5 6 4 2│1 1│5│
>>
>> └───┴─────┴───┴───────┴───┴─┘
>>
>> ?
>>
>> Skip
>>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm