|
From: Slava P. <sl...@fa...> - 2007-04-12 07:56:08
|
Hi all, Eric Mertens (glguy on IRC) has submitted a patch which introduces a refactoring of various sequence search words. Previously, they would all return -1 if the element was not found in the sequence: CHAR: x "hello" index . ==> -1 Now, they return f: CHAR: x "hello" index . ==> f The rationale for this change is that -1 is a somewhat arbitrary magic number. Using 'f' is more idiomatic, because instead of doing something like 'dup -1 = [ ... ] [ drop ] if' you can just write '[ ... ] when*'. Everything in core/ has been updated, but nothing in apps/ or libs/ has been touched. Any contributors who want to make the changes themselves should let me know and go ahead and do so; I will update the remaining modules myself, or maybe Eric will decide to do it. A little bit of history: The 'index' word was originally implemented in Java Factor by calling String.indexOf(), which returns -1 if the character is not part of the string. This was carried over into the current Factor implementation. This is not really idiomatic in a language such as Factor. Java's type system is rather limited, necessitating hacks like this: Factor is dynamically typed, so we are free to write words whose return type is "integer or f". Even statically typed languages can solve this in an elegant way: Haskell for example would use a return type of Maybe Int for this type of operation, with the nullary Nothing data constructor taking the place of Factor's 'f' value. Slava |