interleave/join strings

General APL language issues
Vince|Dyalog
Posts: 439
Joined: Wed Oct 01, 2008 9:39 am

Re: interleave/join strings

Post by Vince|Dyalog »

Hi Jim,

If you are logged in to the Forums, I think that you can edit your post or delete it.

Regards,

Vince
alexeyv
Posts: 56
Joined: Tue Nov 17, 2015 4:18 pm

Re: interleave/join strings

Post by alexeyv »

I'm trying to understand this (,⍤0)S again.
From the documentation: The Rank operator ⍤1 applies function f successively to the sub-arrays in Y specified by k.
Let's apply then the dfn with concatenation to this array or subarrays.

Here is the code:

Code: Select all

    S←'this' 'is' 'a' 'sentence'
    ({' ',⍵}⍤1)S
┌→───────────────────────────┐
│   ┌→───┐ ┌→─┐   ┌→───────┐ │
│   │this│ │is│ a │sentence│ │
│ - └────┘ └──┘ - └────────┘ │
└∊───────────────────────────┘

Ok, the 1-cells of the array is the array itself, (as I understand), so this is ok.
What if k is 0?

Code: Select all

({' ',⍵}⍤0)S
┌→─────────────┐
↓   ┌→───┐     │
│   │this│     │
│ - └────┘     │
│   ┌→─┐       │
│   │is│       │
│ - └──┘       │
│              │
│   a          │
│ - -          │
│   ┌→───────┐ │
│   │sentence│ │
│ - └────────┘ │
└∊─────────────┘
      ⍴({' ',⍵}⍤0)S
┌→──┐
│4 2│
└~──┘

According to the documentation: The major cells of a vector are its elements (0-cells).
So I would expect concatenate applied to each element of the vector. But instead I got a matrix! And the words are not concatenated with the space! How it is happened?
petermsiegel
Posts: 159
Joined: Thu Nov 11, 2010 11:04 pm

Re: interleave/join strings

Post by petermsiegel »

Try this, so that the RANK operator (⍤) applies directly to the CATENATE (,) function:
      ]box on
' ',⍤0⊢'one' 'to' 'three'
┌─┬─────┐
│ │one │
├─┼─────┤
│ │to │
├─┼─────┤
│ │three│
└─┴─────┘

,' ',⍤0⊢'one' 'to' 'three'
┌─┬───┬─┬──┬─┬─────┐
│ │one│ │to│ │three│
└─┴───┴─┴──┴─┴─────┘

' ',⍤1⊢'one' 'to' 'three'
┌─┬───┬──┬─────┐
│ │one│to│three│
└─┴───┴──┴─────┘

etc. As written, your code applies RANK to the enclosing {} DFN, rather than to CATENATE per se.
Marshall|Dyalog

Re: interleave/join strings

Post by Marshall|Dyalog »

Hi Veli-Matti,

I've been working on Dyalog APL for a little while now, and the timings look very different in version 16.0:

      D←' ' ⋄ S←12345⍴'this' 'is' 'a' 'sentence'
D{1↓↑,/,⍺,⍤0⊢⍵}S → 7.4E¯4 | 0% ⎕⎕⎕⎕⎕⎕
D{1↓↑,/,⍺(,⍤0)⍵}S → 7.4E¯4 | 0% ⎕⎕⎕⎕⎕⎕
D{1↓↑,/,⍺∘,⍤0⊢⍵}S → 4.7E¯3 | +538% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
D{1↓∊⍺∘,⍤0⊢⍵}S → 4.2E¯3 | +471% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
D{1↓↑,/,⍺,⍪⍵}S → 8.0E¯4 | +9% ⎕⎕⎕⎕⎕⎕⎕
D{1↓∊⍺,¨⍵}S → 1.4E¯3 | +88% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
D{¯1↓↑,/,⍵,[1.1]⍺}S → 7.4E¯4 | 0% ⎕⎕⎕⎕⎕⎕
D{¯1↓∊(,[1.5]⍵),⍺}S → 5.1E¯4 | -31% ⎕⎕⎕⎕
D{¯1↓∊⍵,[1.5]⊂⍺}S → 4.5E¯4 | -39% ⎕⎕⎕⎕
D←'","'
D{(⍴⍺)↓↑,/,(⊂⍺),⍤0⊢⍵}S → 6.9E¯4 | 0% ⎕⎕⎕⎕
D{(≢⍺)↓↑,/,(⊂⍺)(,⍤0)⍵}S → 7.1E¯4 | +2% ⎕⎕⎕⎕
D{(⍴⍺)↓↑,/,⍺∘,⍤0⊢⍵}S → 6.7E¯3 | +868% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
D{(⍴⍺)↓∊⍺∘,⍤0⊢⍵}S → 5.7E¯3 | +733% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
D{(⍴⍺)↓↑,/,(⊂⍺),⍪⍵}S → 7.5E¯4 | +9% ⎕⎕⎕⎕⎕
D{(⍴⍺)↓∊(⊂⍺),¨⍵}S → 1.4E¯3 | +97% ⎕⎕⎕⎕⎕⎕⎕⎕
D{(-⍴⍺)↓↑,/,⍵,[1.1]⊂⍺}S → 7.0E¯4 | 0% ⎕⎕⎕⎕
D{(-⍴⍺)↓∊(,[1.5]⍵),⊂⍺}S → 5.1E¯4 | -27% ⎕⎕⎕
D{(-⍴⍺)↓∊⍵,[1.5]⊂⍺}S → 4.5E¯4 | -35% ⎕⎕⎕


We've seen roughly the same speedups on all platforms.

The last expression is a slightly faster variant of the one before it, which was the fastest of the original benchmarks.

Both of those expressions actually get to take advantage of two major improvements. First is a huge speedup on enlist, which applies to every argument with small leaves. Second is some special code for catenate which interleaves arrays much more quickly. The result is pretty quick!
Post Reply