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

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Factor-talk mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to