And and Or (SelectionCriterion functions): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
mNo edit summary
Line 1: Line 1:
{{Template:SelectionCriterion:And and Or subtitle}}
{{Template:SelectionCriterion:And and Or subtitle}}


These shared methods each create a new <var>SelectionCriterion</var> object
These [[Notation conventions for methods#Shared members|shared methods]] each create a new <var>SelectionCriterion</var> object
that is an expression used to select the items in a collection.
that is an expression used to select the items in a collection.
Each constructor uses a different logical operator
Each function uses a different logical operator
to form an expression that combines one or more <var>SelectionCriterion</var> objects.
to form an expression that combines one or more <var>SelectionCriterion</var> objects.


Line 29: Line 29:
<td>A <var>SelectionCriterion</var> object variable to contain the new object instance. </td></tr>
<td>A <var>SelectionCriterion</var> object variable to contain the new object instance. </td></tr>
<tr><th><var>%(SelectionCriterion For</var> itemtype)</th>
<tr><th><var>%(SelectionCriterion For</var> itemtype)</th>
<td>This optional specification of the class in parentheses indicates that the method is [[Notation conventions for methods#Shared methods|shared]] and does not operate on a specific instance. If you use this option, however, you must include the collection item type to which the selection expression will be applied, like this: <code>%(selectionCriterion For <i>itemtype</i>):</code> </td></tr>
<td>For a shared function, this optional specification of the class in parentheses denotes a [[Notation conventions for methods#Shared methods|virtual constructor]]. See [[#Usage notes|"Usage notes"]], below, for more information about invoking a <var>SelectionCriterion</var> virtual constructor. </td></tr>
<tr><th>list</th>
<tr><th>list</th>
<td>A comma-separated list of one or more <var>SelectionCriterion</var> objects. </td></tr>
<td>A comma-separated list of one or more <var>SelectionCriterion</var> objects. </td></tr>
Line 37: Line 37:
==Usage notes==
==Usage notes==
<ul>
<ul>
As described in [[Object variables#Virtual Constructor methods|"Virtual Constructor methods"]], <var>And</var> and <var>Or</var> can be invoked with
no method object, with an explicit class specification, or with an object variable of the class,
even if that object is <var>Null</var>:
<p class="code">%selCrit = And(<i>list</i>)
%selCrit = %(SelectionCriterion for float):And(<i>list</i>)
%selCrit = %selCrit:And(<i>list</i>)
</p>
'''Note:'''
As shown in the second of these above, if you explicitly specify the
class name, you must include the item datatype of the collection to be searched, just as on a <var>SelectionCriterion</var> object variable's [[SelectionCriterion class#Declaring a SelectionCriterion object variable|declaration]].
<li>All <var>Or</var> and <var>And</var> <var>SelectionCriterion</var> conditions
<li>All <var>Or</var> and <var>And</var> <var>SelectionCriterion</var> conditions
are short-circuiting conditions.
are short-circuiting conditions.
That is, if any of the conditions in
That is, if any of the conditions in
an <var>Or</var> return True, the subsequent conditions are not evaluated and the
an <var>Or</var> return <code>True</code>, the subsequent conditions are not evaluated and the
<var>Or</var> returns True.
<var>Or</var> returns <code>True</code>.
Similarly, if any of the conditions in an <var>And</var> return
Similarly, if any of the conditions in an <var>And</var> return
False, the subsequent conditions are not evaluated, and the <var>And</var> returns a
<code>False</code>, the subsequent conditions are not evaluated, and the <var>And</var> returns a
False.
<code>False</code>.


Therefore, it is wise to put the most likely conditions first
Therefore, it is wise to put the most likely conditions first

Revision as of 19:55, 1 August 2011

Create selection expression based on logical operator (SelectionCriterion class)


These shared methods each create a new SelectionCriterion object that is an expression used to select the items in a collection. Each function uses a different logical operator to form an expression that combines one or more SelectionCriterion objects.

An Or criterion returns true for a collection item if any of the component SelectionCriterion expressions are true for the item; otherwise it returns false. An And criterion returns true if all of the component SelectionCriterion expressions are true for the item; otherwise it returns false.

The collection searching method that makes use of a SelectionCriterion specifies:

  • Whether to return the first item or item number or all items that satisfy the selection criterion.
  • Where in the collection to begin searching.

Syntax

%selectionCriterion = [%(SelectionCriterion For itemType):]And( list)

%selectionCriterion = [%(SelectionCriterion For itemType):]Or( list)

Syntax terms

%selectionCriterion A SelectionCriterion object variable to contain the new object instance.
%(SelectionCriterion For itemtype) For a shared function, this optional specification of the class in parentheses denotes a virtual constructor. See "Usage notes", below, for more information about invoking a SelectionCriterion virtual constructor.
list A comma-separated list of one or more SelectionCriterion objects.

Usage notes

    As described in "Virtual Constructor methods", And and Or can be invoked with no method object, with an explicit class specification, or with an object variable of the class, even if that object is Null:

    %selCrit = And(list) %selCrit = %(SelectionCriterion for float):And(list) %selCrit = %selCrit:And(list)

    Note: As shown in the second of these above, if you explicitly specify the class name, you must include the item datatype of the collection to be searched, just as on a SelectionCriterion object variable's declaration.

  • All Or and And SelectionCriterion conditions are short-circuiting conditions. That is, if any of the conditions in an Or return True, the subsequent conditions are not evaluated and the Or returns True. Similarly, if any of the conditions in an And return False, the subsequent conditions are not evaluated, and the And returns a False. Therefore, it is wise to put the most likely conditions first in an Or, and it is wise to put the least likely first in an And. For a mix of conditions where some are simply variable references and others require method evaluation, it probably is best to put the variable references first, as these are probably much cheaper to evaluate.

Examples

  1. The following criterion matches numbers less than 70 and greater than or equal to 95:

    %sel = OR(lt(this,70), ge(this,95))

  2. The following criterion matches numbers that are less than or equal to 33 but not 30:

    %sel = AND(le(this, 33), ne(this,30)))

See also