home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / LMAP.ICN < prev    next >
Text File  |  1991-07-13  |  2KB  |  76 lines

  1. ############################################################################
  2. #
  3. #    Name:    lmap.icn
  4. #
  5. #    Title:    Map list elements
  6. #
  7. #    Author:    Ralph E. Griswold
  8. #
  9. #    Date:    June 10, 1988
  10. #
  11. ############################################################################
  12. #  
  13. #  The procedure lmap(L1,L2,L3) maps elements of L1 according to L2
  14. #  and L3.  This procedure is the analog for lists of the built-in
  15. #  string-mapping function map(s1,s2,s3). Elements in L1 that are
  16. #  the same as elements in L2 are mapped into the corresponding ele-
  17. #  ments of L3. For example, given the lists
  18. #  
  19. #     L1 := [1,2,3,4]
  20. #     L2 := [4,3,2,1]
  21. #     L3 := ["a","b","c","d"]
  22. #  
  23. #  then
  24. #  
  25. #     lmap(L1,L2,L3)
  26. #  
  27. #  produces a new list
  28. #  
  29. #     ["d","c","b","a"]
  30. #  
  31. #     Lists that are mapped can have any kinds of elements. The
  32. #  operation
  33. #  
  34. #     x === y
  35. #  
  36. #  is used to determine if elements x and y are equivalent.
  37. #  
  38. #     All cases in lmap are handled as they are in map, except that
  39. #  no defaults are provided for omitted arguments. As with map, lmap
  40. #  can be used for transposition as well as substitution.
  41. #  
  42. #  Warning:
  43. #
  44. #     If lmap is called with the same lists L2 and L3 as in
  45. #  the immediately preceding call, the same mapping is performed,
  46. #  even if the values in L2 and L3 have been changed. This improves
  47. #  performance, but it may cause unexpected effects.
  48. #  
  49. #     This ``caching'' of the mapping table based on L2 and L3
  50. #  can be easily removed to avoid this potential problem.
  51. #  
  52. ############################################################################
  53.  
  54. procedure lmap(L1,L2,L3)
  55.    static lmem2, lmem3, lmaptbl, tdefault
  56.    local i, a
  57.  
  58.    initial tdefault := []
  59.  
  60.    if type(a := L1 | L2 | L3) ~== "list" then runerr(108,a)
  61.    if *L2 ~= *L3 then runerr(208,L2)
  62.  
  63.    L1 := copy(L1)
  64.  
  65.    if not(lmem2 === L2 & lmem3 === L3) then {    # if an argument is new, rebuild
  66.       lmem2 := L2                # save for future reference
  67.       lmem3 := L3
  68.       lmaptbl := table(tdefault)        # new mapping table
  69.       every i := 1 to *L2 do            # build the map
  70.          lmaptbl[L2[i]] := L3[i]
  71.       }
  72.    every i := 1 to *L1 do            # map the values
  73.       L1[i] := (tdefault ~=== lmaptbl[L1[i]])
  74.    return L1
  75. end
  76.