2|.(2|.a),4
2007/8/23, Fuchs Ira [EMAIL PROTECTED]:
Very elementary question:
Is there a primitive verb which will insert a value in a list? That
is, given an index, a new value , and a list, the verb would insert
the value in the list after the index location.
for example if a=:2 3 5 6
Very elementary question:
Is there a primitive verb which will insert a value in a list? That
is, given an index, a new value , and a list, the verb would insert
the value in the list after the index location.
for example if a=:2 3 5 6 and I want to insert a 4 after in the 3rd
position,
On 8/23/07, Fuchs Ira [EMAIL PROTECTED] wrote:
Is there a primitive verb which will insert a value in a list?
No. And, unless you are prepared to combine arguments
(perhaps inserted values and locations), there cannot be:
the operation you describe requires three arguments.
For example:
4 (2)}1 1 2 1#a
2007/8/23, Fuchs Ira [EMAIL PROTECTED]:
Very elementary question:
Is there a primitive verb which will insert a value in a list? That
is, given an index, a new value , and a list, the verb would insert
the value in the list after the index location.
for example if a=:2 3
Here's an alternative that doesn't require numeric lists:
insertA=: 13 : '(x{.1{y),(0{y),x}.1{y'
3 insertA 4;2 3 5 6
2 3 5 4 6
3 insertA 4 4.1 4.2 4.3;2 3 5 6
2 3 5 4 4.1 4.2 4.3 6
3 insertA 'XXX';'ABCDEF'
ABCXXXDEF
3 insertA ('BBB';'BB');'AAA';'AA';'A';'B';'CCC'
I had thought I'd written insert so it would accept list
arguments for insertion. I did not.
Here's a version which works like I had originally
intended:
insert=:1 :0
:
b=.1#~1 j.m e.~i.#y
((-.b)expand x)+b expand y
)
For example, if I wanted to insert a 4 after both the
second position in a
I wrote:
ins=:1 :0
:
b=.1#~1 j.m e.~i.#y
x (I.-.b)} b expand y
)
Or, equivalently:
ins1=:1 :0
:
b=.1#~1 j.m e.~i.#y
x (m+1+i.#m)} b expand y
)
--
Raul
--
For information about J forums see
On 8/23/07, Raul Miller [EMAIL PROTECTED] wrote:
ins=:1 :0
:
b=.1#~1 j.m e.~i.#y
x (I.-.b)} b expand y
)
Brian Schott pointed out to me that this (and my variant ins1) assume
that m is in ascending order (which I was assuming, since I don't
normally do this kind of thing with indices
This is very helpful. Thanks to all who replied. The ins, ins1 and
insert verbs do what I wanted (and more)..now if only I could
decipher how they work :-)
--
For information about J forums see
How about using /: as in
ins=: 1 : 0
:
(/:(1+j.m),i.#y){x,y
)
remove the 1+ to work in offset mode
/D
--- Raul Miller [EMAIL PROTECTED] wrote:
I had thought I'd written insert so it would accept list
arguments for insertion. I did not.
Here's a version which works like I had originally
The definitions are getting shorter but not more scrutable, at least
to me. Dan, if you could perhaps parse this for me, it would be most
instructive. Actually, are there any tools in J that would help
someone learning the language to figure out how this works?
Dan,
I get better results by leaving out j. from your
solution if m can be a vector of the same length as x.
ins=: 1 : 0
:
(/:(1+m),i.#y){x,y
)
0 4(1 3 ins) 2 3 5 6
2 3 0 5 6 4
0 4(3 1 ins) 2 3 5 6
2 3 4 5 6 0
On Thu, 23 Aug 2007, Dan Baronet wrote:
+ How about using /: as
Here's an explanation of the following:
ins=:1 :0
:
b=.1#~1 j.m e.~i.#y
(x/:m) (I.-.b)} b expand y
)
First off 1 :0 defines an adverb, and the part after the line that's
just a : is the body of the dyad that that adjective produces. If that's
not clear, and looking up adverb and dyad in J's
Here is a verb which does the same thing as Dan's
adverb if the left hand argument is the positions and the
righthand argument is the inserts followed by the original
list.
insv =: ]{~ :@[/:@,[EMAIL PROTECTED]#
1 1 3 insv 0 2 4 , 2 3 5 6
2 3 0 2 5 6 4
On Thu, 23 Aug 2007, Brian
On 8/23/07, Dan Baronet [EMAIL PROTECTED] wrote:
How about using /: as in
ins=: 1 : 0
:
(/:(1+j.m),i.#y){x,y
)
That's an interesting idea -- I have not played with /: on complex
arguments before (it looks like the rules for sorting complex
arguments are analogous to rules for sorting arrays
That's an interesting idea -- I have not played with /: on complex
arguments before (it looks like the rules for sorting complex
arguments are analogous to rules for sorting arrays of two element
lists.
See:
http://www.jsoftware.com/help/dictionary/d422.htm
On 8/23/07, Dan Baronet [EMAIL PROTECTED] wrote:
How about using /: as in
ins=: 1 : 0
:
(/:(1+j.m),i.#y){x,y
)
I think that j. is spurious. In other words, I think you meant:
ins=: 1 : 0
:
(/:(1+m),i.#y){x,y
)
Note also that Dan's code has a significant advantage over mine:
his does
ins=: 1 : '(-m)|.,'
_1 (3) ins 1 2 3
1 2 3 _1
_1 (0) ins 1 2 3
_1 1 2 3
_1 (1) ins i.3 4
8 9 10 11
_1 _1 _1 _1
0 1 2 3
4 5 6 7
--- Fuchs Ira [EMAIL PROTECTED] wrote:
Very elementary question:
Is there a primitive verb which will insert a value in a list? That
18 matches
Mail list logo