FIND/in - a pattern I use a lot

Carl Sassenrath, CTO
REBOL Technologies
1-Apr-2009 15:56 GMT

Article #0185
Main page || Index || Prior Article [0184] || Next Article [0186] || 7 Comments || Send feedback

I often store records as blocks. The blocks follow a standard format. For example, if you look at the R3 chat message file, you will see such a format.

So, this common code pattern appears a lot. I often use it for finding a user in a user database:

foreach user users [  ; or similar FORALL loop
    if user/3 = name [return user]

You will recognize this as a linear search. I use it in cases where its not worth building a hash (map!) on the target field, normally because it's rarely needed, or the database is small.

It's time to abstract this type of search. The key elements are:

  • Search block of blocks
  • A specific field of each
  • That equals a given value

So, it could be something like:

user: find/in users 3 name

And user is probably the index into the users block, allowing us to do things like:

remove find/in users 3 name

The method could be extended to blocks of objects:

rec: find/in objects word value

The same as:

foreach obj objects [  ; or similar FORALL loop
    if obj/:word = value [return obj]

This is one of those cases where you begin to ask, is this more of a native or a mezzanine function?

Of course, another approach would be to add a function that is a bit similar to an SQL select, but it's also more work, and I'm not sure how much use it would get.

Let me know your thoughts.


Updated 21-Sep-2017 - Edit - Copyright REBOL Technologies -