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

[REBOL] Beginner's automatic type conversion

From: laurent::giroud::libertysurf::fr at: 23-Sep-2002 1:10

Hello everyone, I am still a beginner and would like to have some experts comments on Rebol code I managed to write recently. This is a very simple example but since all the reflexions on the list are always very insightful (at least to me !) I thought it could be a good thing to have my code criticized by people who know what they are talking about ;) What I am doing is very simple, I get string data and store it this way : trans: [ ["09:33" "12.30" "1 270"] ; [string! string! string!] ["09:34" "12.31" "4 450"] ... ] and need to convert it (for faster manipulation later on) into : [ [9:33 12.30 1270] ; [time! decimal! integer!] [9:34 12.31 4450] ... ] My original conversion code was : foreach t trans [ [change trans reduce to-time first trans to-decimal second trans to-integer trim/all third trans ] ] not very elegant but works fine ;) However, I thought that it would be more flexible to act as if I did not know in advance what the type of elements was. Since Rebol is already capable to guess the correct type for data it encounters within scripts or the console I thought that it was possible to find a way to have Rebol guess the correct type by itself. I went into much trouble to write a working solution but finally got this code to do the job : (beware line cuts) foreach t trans [ for i 1 length? t 1 [ do rejoin ["t/" i ": to-" type? do trim/all t/:i " " t/:i] ] ] it uses the 'do function to have rebol guess the correct type ("do t/:i") from the string then builds the code to convert the current element to its new value and executes this code using 'do again. This is very short but it took me almost three hours to write this :( I still struggle a lot to write code like this which executes statements it has itself generated and there seems to be so many ways to do the same thing in Rebol that I am never quite sure that I found a good one ;) So all comments will be more than welcome ! Best regards, Laurent -- Laurent Giroud [laurent--giroud--libertysurf--fr]