Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Transpose

From: al::bri::xtra::co::nz at: 6-Oct-2000 1:11

Joel wrote:
> Finally, I'd considered handling the structure-traversing issues
separately from the data-manipulation issues, by defining (this version is VERY quick and dirty):
> >> transpose: function [b [block!]] [w i s r] [ > [ r: make block! w: accum length? b/1 map b :length? :min > [ repeat i w [ > [ s: make block! length? b > [ foreach c b [append/only s c/:i] > [ append/only r s > [ ] > [ r > [ ] > >> transpose [[1 2 3 4 5] ["a" "b" "c" "d" "e"]] > == [[1 "a"] [2 "b"] [3 "c"] [4 "d"] [5 "e"]] > >> transpose [[1 2 3] [14 15 16] [27 28 29]] > == [[1 14 27] [2 15 28] [3 16 29]]
I've cleaned it up a bit: [ Rebol [ Name: 'Transpose Title: "Transpose" File: %"Transpose.r" Author: "Andrew Martin" eMail: [Al--Bri--xtra--co--nz] Date: 5/October/2000 Enhancement: 'Transpose Acknowledgements: "Joel Neely" Example: [ transpose [[1 2 3 4 5] ["a" "b" "c" "d" "e"]] ; [[1 "a"] [2 "b"] [3 "c"] [4 "d"] [5 "e"]] transpose [[1 2 3] [14 15 16] [27 28 29]] ; [[1 14 27] [2 15 28] [3 16 29]] ] ] Transpose: function [ [catch] "Transposes Matrices" Matrix [block!] ][ Results Width Height Column ][ Results: make block! Width: length? Matrix/1 Height: length? Matrix repeat Index Width [ Column: make block! Width foreach Row Matrix [ insert/only tail Column Row/:Index ] insert/only tail Results Column ] Results ] ]
>> transpose [[1 2 3 4 5] ["a" "b" "c" "d" "e"]]
== [[1 "a"] [2 "b"] [3 "c"] [4 "d"] [5 "e"]]
>> ; [[1 "a"] [2 "b"] [3 "c"] [4 "d"] [5 "e"]] >> transpose [[1 2 3] [14 15 16] [27 28 29]]
== [[1 14 27] [2 15 28] [3 16 29]]
>> ; [[1 14 27] [2 15 28] [3 16 29]] > Critiques/comments/debugging welcome!
I totally agree. :-) Andrew Martin ICQ: 26227169 http://members.nbci.com/AndrewMartin/