Search code examples
j

Apply a verb to the contents of a boxed list at a specific index


Consider:

   [xs=. 'hi';'hello';'foo'
┌──┬─────┬───┐
│hi│hello│foo│
└──┴─────┴───┘

I seek the tacit conjunction adjust such that:

(,&' world' adjust 1) xs

produces:

┌──┬───────────┬───┐
│hi│hello world│foo│
└──┴───────────┴───┘

I'm open to other approaches as well if they would fit better for this problem.


Solution

  • I did take a different approach creating the result with arguments to verbs rather than conjunctions. I was able to keep the arguments relatively adjustable, so that they are easy to manipulate without changing the structure. I do this by creating a dyadic verb where the box to be selected is the left argument, the list of boxes is the left argument, and the suffix to be added is embedded in the verb.

       suffix=.''&;
       1 (] ,each (suffix ' world') {~ (= i.@#)) xs
    +--+-----------+---+
    |hi|hello world|foo|
    +--+-----------+---+
       2 (] ,each (suffix 'die') {~ (= i.@#)) xs
    +--+-----+------+
    |hi|hello|foodie|
    +--+-----+------+
       0 (] ,each (suffix ' there') {~ (= i.@#)) xs
    +--------+-----+---+
    |hi there|hello|foo|
    +--------+-----+---+
    

    The completely primitive tacit version could look like this.

       0 (] ,each ('';' there') {~ (= i.@#)) xs
    +--------+-----+---+
    |hi there|hello|foo|
    +--------+-----+---+