Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!uwm.edu!bionet!agate!ucbvax!MTS.cc.Wayne.edu!Paul_Abrahams From: Paul_Abrahams@MTS.cc.Wayne.edu Newsgroups: comp.lang.icon Subject: Using the ``map'' function Message-ID: <315814@MTS.cc.Wayne.edu> Date: 3 Apr 91 23:20:20 GMT Sender: daemon@ucbvax.BERKELEY.EDU Distribution: inet Organization: The Internet Lines: 36 In doing the index for my latest book, I need to sort the entries using a nonstandard collating sequence. I therefore need a ``keytrans'' function that transforms a sorting key to a key that uses the correct collating sequence. Here's the obvious way to do it in Icon: procedure keytrans(key) static collator initial {collator := ...} # printable chars in collating sequence order return map(key, collator, &ascii[33:128]) end This is simple but slow since the map must be regenerated for each key. Here's the other way: procedure keytrans(key) static collator, lookup initial { collator := ... lookup = map(&ascii[33:128], collator, &ascii[33:128]) } local c, retval retval := "" every c := !key do retval ||:= lookup[ord(c) - 31] return retval end This is inelegant but fast. Is there any way I can have my cake and eat it too? I wouldn't mind a small loss in speed, but the difference here seems pretty big. Paul Abrahams Abrahams@mts.cc.wayne.edu