On Mon, 28 May 2012 14:24:42 +0000, Josh Grams wrote:
Here's an example involving cleanup:
You have an image, and the task is to retrieve the image's timestamp
using OCR. To do this, there are two words available.
: img2int ( img n1 n2 n3 n4 -- n f ) ... ;
: ymdhm ( y m d h m -- time ) ... ;
The word IMG2INT takes 4 coordinates on the image, which enclose an
integer, crops the image to those coordinates, runs the OCR program and
parses the resulting string into an integer. The return value is the
integer and a flag indicating success/failure.
YMDHM takes 5 integers (year, month, day, hour, minute) and converts this
to an internal time format. A time of 0 could indicate an OCR failure.
The images come from different sources, so the coordinates n1..n4 would
vary by image source.
Here's a solution using the shortcircuit operators:
: | ` if ` true ` exit ` then ; immediate
: -> ` not ` if ` exit ` then ; immediate
: year ( img n1 n2 n3 n4 -- m f )
img2int | drop 0 false ;
: month ( img n1 n2 n3 n4 -- m f )
img2int | drop drop 0 false ;
: day ( img n1 n2 n3 n4 -- m f )
img2int | drop drop drop 0 false ;
: hour ( img n1 n2 n3 n4 -- m f )
img2int | drop drop drop drop 0 false ;
: minute ( img n1 n2 n3 n4 -- m f )
img2int | drop drop drop drop drop 0 false ;
\ Gets timestamps from image source XYZ
: timestamp-xyz { img } ( img -- time )
img 91 13 158 35 year ->
img 158 13 194 35 month ->
img 194 13 233 35 day ->
img 233 13 268 35 hour ->
img 277 13 310 35 minute ->
ymdhm ;
\ Gets timestamps from image source JKL
: timestamp-jkl { img } ( img -- time )
img 170 13 245 40 year ->
img 245 13 289 40 month ->
img 289 13 331 40 day ->
img 331 13 372 40 hour ->
img 381 13 420 40 minute ->
ymdhm ;
As you can see the cleanup is done in the words like YEAR, MONTH etc, and
factoring it this way looks natural IMHO.
Yet, as others have noted, passing the flag is an inefficiency, since we
re-test the same flag downstream.
Cheers,
Arnold