----------
zip←{↓⍉↑⍵} ⎕ml<2
----------
Suggested by Peter Donnelly, [zip] takes an N-vector of M-vectors and returns
the transpose: M-vector of N-vectors. It is named after a functional programming
idiom, which returns a list-of-pairs from a pair-of-lists.
zip'ABCDEFGHIJKLM' 'abcdefghijklm' ⍝ zip vectors.
Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm
disp pairs
┌→───────────────────┬─────────────────────────┬───────────────────────┐
│┌→────────┬────────┐│┌→──────────┬───────────┐│┌→──────────┬─────────┐│
││┌→──┬───┐│┌→──┬──┐│││┌→────┬───┐│┌→───┬────┐│││┌→───┬────┐│┌→──┬───┐││
│││one│unu│││two│du│││││three│tri│││four│kvar│││││five│kvin│││six│ses│││
││└──→┴──→┘│└──→┴─→┘│││└────→┴──→┘│└───→┴───→┘│││└───→┴───→┘│└──→┴──→┘││
│└────────→┴───────→┘│└──────────→┴──────────→┘│└──────────→┴────────→┘│
└───────────────────→┴────────────────────────→┴──────────────────────→┘
disp zip pairs
┌→──────────────────────────────────┬────────────────────────────────┐
│┌→────────┬───────────┬───────────┐│┌→───────┬───────────┬─────────┐│
││┌→──┬───┐│┌→────┬───┐│┌→───┬────┐│││┌→──┬──┐│┌→───┬────┐│┌→──┬───┐││
│││one│unu│││three│tri│││five│kvin│││││two│du│││four│kvar│││six│ses│││
││└──→┴──→┘│└────→┴──→┘│└───→┴───→┘│││└──→┴─→┘│└───→┴───→┘│└──→┴──→┘││
│└────────→┴──────────→┴──────────→┘│└───────→┴──────────→┴────────→┘│
└──────────────────────────────────→┴───────────────────────────────→┘
disp zip¨pairs
┌→───────────────────┬─────────────────────────┬───────────────────────┐
│┌→────────┬────────┐│┌→───────────┬──────────┐│┌→─────────┬──────────┐│
││┌→──┬───┐│┌→──┬──┐│││┌→────┬────┐│┌→──┬────┐│││┌→───┬───┐│┌→───┬───┐││
│││one│two│││unu│du│││││three│four│││tri│kvar│││││five│six│││kvin│ses│││
││└──→┴──→┘│└──→┴─→┘│││└────→┴───→┘│└──→┴───→┘│││└───→┴──→┘│└───→┴──→┘││
│└────────→┴───────→┘│└───────────→┴─────────→┘│└─────────→┴─────────→┘│
└───────────────────→┴────────────────────────→┴──────────────────────→┘
disp zip¨zip pairs
┌→────────────────────────────────┬──────────────────────────────┐
│┌→───────────────┬──────────────┐│┌→─────────────┬─────────────┐│
││┌→──┬─────┬────┐│┌→──┬───┬────┐│││┌→──┬────┬───┐│┌→─┬────┬───┐││
│││one│three│five│││unu│tri│kvin│││││two│four│six│││du│kvar│ses│││
││└──→┴────→┴───→┘│└──→┴──→┴───→┘│││└──→┴───→┴──→┘│└─→┴───→┴──→┘││
│└───────────────→┴─────────────→┘│└─────────────→┴────────────→┘│
└────────────────────────────────→┴─────────────────────────────→┘
disp zip zip¨ pairs
┌→──────────────────────────────────┬────────────────────────────────┐
│┌→────────┬────────────┬──────────┐│┌→───────┬──────────┬──────────┐│
││┌→──┬───┐│┌→────┬────┐│┌→───┬───┐│││┌→──┬──┐│┌→──┬────┐│┌→───┬───┐││
│││one│two│││three│four│││five│six│││││unu│du│││tri│kvar│││kvin│ses│││
││└──→┴──→┘│└────→┴───→┘│└───→┴──→┘│││└──→┴─→┘│└──→┴───→┘│└───→┴──→┘││
│└────────→┴───────────→┴─────────→┘│└───────→┴─────────→┴─────────→┘│
└──────────────────────────────────→┴───────────────────────────────→┘
----------
cat←{↑,/⍵} ⎕ml<2
----------
[cat] concatenates the items in a vector (or higher rank array). It differs from
enlist (∊), in that the concatenation occurs only at the top level of the array.
disp pairs
┌→───────────────────┬─────────────────────────┬───────────────────────┐
│┌→────────┬────────┐│┌→──────────┬───────────┐│┌→──────────┬─────────┐│
││┌→──┬───┐│┌→──┬──┐│││┌→────┬───┐│┌→───┬────┐│││┌→───┬────┐│┌→──┬───┐││
│││one│unu│││two│du│││││three│tri│││four│kvar│││││five│kvin│││six│ses│││
││└──→┴──→┘│└──→┴─→┘│││└────→┴──→┘│└───→┴───→┘│││└───→┴───→┘│└──→┴──→┘││
│└────────→┴───────→┘│└──────────→┴──────────→┘│└──────────→┴────────→┘│
└───────────────────→┴────────────────────────→┴──────────────────────→┘
disp cat pairs
┌→────────┬────────┬───────────┬───────────┬───────────┬─────────┐
│┌→──┬───┐│┌→──┬──┐│┌→────┬───┐│┌→───┬────┐│┌→───┬────┐│┌→──┬───┐│
││one│unu│││two│du│││three│tri│││four│kvar│││five│kvin│││six│ses││
│└──→┴──→┘│└──→┴─→┘│└────→┴──→┘│└───→┴───→┘│└───→┴───→┘│└──→┴──→┘│
└────────→┴───────→┴──────────→┴──────────→┴──────────→┴────────→┘
disp cat¨pairs
┌→───────────────┬─────────────────────┬───────────────────┐
│┌→──┬───┬───┬──┐│┌→────┬───┬────┬────┐│┌→───┬────┬───┬───┐│
││one│unu│two│du│││three│tri│four│kvar│││five│kvin│six│ses││
│└──→┴──→┴──→┴─→┘│└────→┴──→┴───→┴───→┘│└───→┴───→┴──→┴──→┘│
└───────────────→┴────────────────────→┴──────────────────→┘
disp cat cat pairs
┌→──┬───┬───┬──┬─────┬───┬────┬────┬────┬────┬───┬───┐
│one│unu│two│du│three│tri│four│kvar│five│kvin│six│ses│
└──→┴──→┴──→┴─→┴────→┴──→┴───→┴───→┴───→┴───→┴──→┴──→┘
------------
combinations
------------
disp pairs
┌→───────────────────┬─────────────────────────┬───────────────────────┐
│┌→────────┬────────┐│┌→──────────┬───────────┐│┌→──────────┬─────────┐│
││┌→──┬───┐│┌→──┬──┐│││┌→────┬───┐│┌→───┬────┐│││┌→───┬────┐│┌→──┬───┐││
│││one│unu│││two│du│││││three│tri│││four│kvar│││││five│kvin│││six│ses│││
││└──→┴──→┘│└──→┴─→┘│││└────→┴──→┘│└───→┴───→┘│││└───→┴───→┘│└──→┴──→┘││
│└────────→┴───────→┘│└──────────→┴──────────→┘│└──────────→┴────────→┘│
└───────────────────→┴────────────────────────→┴──────────────────────→┘
disp zip cat pairs
┌→────────────────────────────┬──────────────────────────┐
│┌→──┬───┬─────┬────┬────┬───┐│┌→──┬──┬───┬────┬────┬───┐│
││one│two│three│four│five│six│││unu│du│tri│kvar│kvin│ses││
│└──→┴──→┴────→┴───→┴───→┴──→┘│└──→┴─→┴──→┴───→┴───→┴──→┘│
└────────────────────────────→┴─────────────────────────→┘
disp cat¨zip zip¨pairs
┌→────────────────────────────┬──────────────────────────┐
│┌→──┬───┬─────┬────┬────┬───┐│┌→──┬──┬───┬────┬────┬───┐│
││one│two│three│four│five│six│││unu│du│tri│kvar│kvin│ses││
│└──→┴──→┴────→┴───→┴───→┴──→┘│└──→┴─→┴──→┴───→┴───→┴──→┘│
└────────────────────────────→┴─────────────────────────→┘
Technical note:
Notice that [cat] applies _mix_ rather than _first_ to its reduction. This has
the advantage of allowing it to accommodate higher rank arrays.
disp mat
┌→───────────┬────────────┬───────────┐
↓┌→──┬──────┐│┌→──┬──────┐│┌→──┬─────┐│
││red│pepper│││red│letter│││red│alert││
│└──→┴─────→┘│└──→┴─────→┘│└──→┴────→┘│
├───────────→┼───────────→┼──────────→┤
│┌→───┬────┐ │┌→───┬─────┐│┌→───┬───┐ │
││blue│moon│ ││blue│suede│││blue│sky│ │
│└───→┴───→┘ │└───→┴────→┘│└───→┴──→┘ │
└───────────→┴───────────→┴──────────→┘
disp cat mat
┌→───┬──────┬────┬──────┬────┬─────┐
↓red │pepper│red │letter│red │alert│
├───→┼─────→┼───→┼─────→┼───→┼────→┤
│blue│ moon │blue│suede │blue│ sky │
└───→┴─────→┴───→┴─────→┴───→┴────→┘
disp zip mat
┌→────────────┬──────────────┬───────────┐
↓ ┌→──┬────┐ │ ┌→──┬────┐ │┌→──┬────┐ │
│ │red│blue│ │ │red│blue│ ││red│blue│ │
│ └──→┴───→┘ │ └──→┴───→┘ │└──→┴───→┘ │
├────────────→┼─────────────→┼──────────→┤
│┌→─────┬────┐│┌→─────┬─────┐│┌→────┬───┐│
││pepper│moon│││letter│suede│││alert│sky││
│└─────→┴───→┘│└─────→┴────→┘│└────→┴──→┘│
└────────────→┴─────────────→┴──────────→┘
Back to: contents