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

[REBOL] Re: Bidirectional value mapping.

From: Christian:Ensel:GMX at: 3-Nov-2003 21:10

Hi Bruno, doing it in the SELECT-way is nice but you should consider using it with the /SKIP-refinement. Without this you may end up with surprising results if one value may occur in value-1 and value-2 position as in ;------------------------------------
>> that's-life: [cats birds cats birds insects birds] >> who-hunts?: func ['animal] [select that's-life animal] >> who-hunts? birds
== cats
>> who-hunts? insects
== birds
>> prey-of?: func ['animal] [select that's-life animal] >> prey-of? cats
== birds
>> prey-of? birds
== cats ; OOPS! ;------------------------------------ The following design doesn't suffer from this difficulty and avoids duplication of values, too, at the cost of a little bit more maintenance work: ;------------------------------------ color-names: [red green blue] color-values: [255.0.0 0.255.0 0.0.255] select-color-by-name: func [name [word!] /local color] [ if color: find color-names name [pick color-values index? color] ] select-color-by-values: func [value [tuple!] /local color] [ if color: find color-values value [pick color-names index? color] ] append-color: func [name [word!] value [tuple!]] [ append color-names color append color-values color ] remove-color: func [name [word!] value [tuple!]] [ remove find color-names name remove find color-values value ] ;------------------------------------ A lot of improvements can be made to this, especially the REMOVE-COLOR is somewhat bad designed in respect to data-integrity (try REMOVE-COLOR RED 0.255.0 - Hey, I'm color-blind ;-) But what I like about the design this approach that it is easily expandable in cases you have to deal with tuples of data of length greater than just 2. HTH, Christian