abrudz / aplcart

A novel approach to finding your way in APL
https://aplcart.info/
Other
133 stars 18 forks source link

Bring element in front of vector #92

Closed AleBeda closed 2 years ago

AleBeda commented 2 years ago

Suggested additional code entry (required)

s (⊃,⊢~⊃) v

Description (required)

Extracts from the right-argument vector the element at the position indicated by the left-argument vector and returns the vector with the extracted element in front (first position).
Example:

    6(⊃,⊢~⊃)⎕A
FABCDEGHIJKLMNOPQRSTUVWXYZ

Known limitations:
It does not work with right arguments with rank higher than 1.
It does not work with non-scalar or non-integer selector as the left argument.

Class (optional)

Tacit

Type (optional)

Dyadic Function

Group (optional)

Category (required)

Data Conversion | Expression

Keywords (optional)

bring extract, first 1st front, element, position middle

TIO (optional)

[Try it online!](https://tio.run/##SyzI0U2pTMzJT/8PBGYKGo@6mnUedS2qA9KaCo/6pjoCAA "APL (Dyalog Unicode) – Try It Online")

Note: By submitting this issue, you permit your contribution to be used without restriction.

abrudz commented 2 years ago

3(⊃,⊢~⊃)'Hello' fails

AleBeda commented 2 years ago

Maybe we can change the description to say that it extracts all of the elements? :-)

abrudz commented 2 years ago

But it doesn't.

AleBeda commented 2 years ago

This should work, but it's ugly: 3(⊃,{⍵/⍨⍺≠(⍳≢⍵)})'Hello' I am not sure if this is worth adding to the APL Cart, at this point. I was not able to make it shorter and tacit, because of the infamous overloading between replicate and reduce and also because there are monadic functions, not conducive to tacit style.

AleBeda commented 2 years ago

I wrote the original tacit function (⊃,⊢~⊃) to be used with vectors of unique values as the right argument. However, while it was useful to me, I realize that it may not be so general to be useful for APL Cart.

abrudz commented 2 years ago

{⍵⌷⍨⊂⍒⍺=⍳≢⍵}

abrudz commented 2 years ago

Should it be generalised to include vector left arguments? {⍵⌷⍨⊂⍒⍺∊⍨⍳≢⍵}

AleBeda commented 2 years ago

This is a nice function. It seems to work already with vectors on the left, although the grade down reorganizes the matches always in the same order regardless the indexes on the left. I suppose that with vectors on the left, the grade down should not be used.

abrudz commented 2 years ago

OK, I'll change it to {⍵⌷⍨⊂⍺∪⍳≢⍵} then.

AleBeda commented 2 years ago

With this modification (which assumes ⎕IO←1) your function seems to work with vector inputs on the left and respect the order of the indexes:

      ⎕IO←1
      (3 7 1){⍵⌷⍨⊂⍒(⌽⍳≢⍺)(×@⍺)⍺∊⍨⍳≢⍵}'Hello world!'
lwHelo orld!
      (1 7 3){⍵⌷⍨⊂⍒(⌽⍳≢⍺)(×@⍺)⍺∊⍨⍳≢⍵}'Hello world!'
Hwlelo orld!
      (7+⍳3){⍵⌷⍨⊂⍒(⌽⍳≢⍺)(×@⍺)⍺∊⍨⍳≢⍵}'Hello world!'
orlHello wd!

To make it work with ⎕IO←0, a 1+ is needed:

      ⎕IO←0
      (2 6 0){⍵⌷⍨⊂⍒(⌽1+⍳≢⍺)(×@⍺)⍺∊⍨⍳≢⍵}'Hello world!'
lwHelo orld!
      (0 6 2){⍵⌷⍨⊂⍒(⌽1+⍳≢⍺)(×@⍺)⍺∊⍨⍳≢⍵}'Hello world!'
Hwlelo orld!
      (7+⍳3){⍵⌷⍨⊂⍒(⌽1+⍳≢⍺)(×@⍺)⍺∊⍨⍳≢⍵}'Hello world!'
orlHello wd!

The version with 1+ also works with ⎕IO←1, so perhaps it is preferable.

      ⎕IO←1
      (3 7 1){⍵⌷⍨⊂⍒(⌽1+⍳≢⍺)(×@⍺)⍺∊⍨⍳≢⍵}'Hello world!'
lwHelo orld!
AleBeda commented 2 years ago

{⍵⌷⍨⊂⍺∪⍳≢⍵}

Ha! this is better! :-)