TIP #393: Add -command Option to lsearch


TIP:393
Title:Add -command Option to lsearch
Version:$Revision: 1.2 $
Author:Pawel Salawa <pawelsalawa at gmail dot com>
State:Draft
Type:Project
Tcl-Version:8.7
Vote:Pending
Created:Monday, 25 April 2011

Abstract

This document describes new option for lsearch named -command that lets the developer define a custom comparision algorithm for searching for an element of a list.

Rationale

The -command option would be very useful when someone wants to look for element on list, that contains objects (TclOO, Itcl, etc), or simply Tcl dicts. Specifying a command lets a developer to decide what data from objects in a list is important for the comparison.

This is effectively introducing the -command option from lsort to lsearch.

Specification

The option -command takes additional argument, which is a name of a command prefix that implements the comparison. The command prefix will have two extra arguments added during evaluation, being (1) the element of the list and (2) the pattern that is being sought. Thus, a suitable command might be defined like:

    proc searchCommand {listElement pattern} {...}

And used like:

    lsearch -command searchCommand $list $pattern

The command must return a boolean value, where true means that the element matches the pattern. Since the -command option specifies a comparision method, therefor it's treated the same way as -exact, -glob, -regexp and -sorted options are. It also causes some options (e.g., -integer, -nocase) to be ignored.

Reference Implementation

http://sqlitestudio.pl/tcl/patches/tip-393-lsearch-command.patch

Patch made against 8.6.0 beta.

Copyright

This document has been placed in the public domain.


Powered by Tcl[Index] [History] [Edit] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format (experimental)] [RTF Format (experimental)]

TIP AutoGenerator - written by Donal K. Fellows