andmap - Maple Programming Help

andmap

determine whether a predicate is true of all operands of an expression

ormap

determine whether a predicate is true of some operands of an expression

 Calling Sequence andmap(p, expr, ...) ormap(p, expr, ...)

Parameters

 p - predicate returning either true or false expr - expression ... - (optional) other arguments to pass to p

Description

 • The procedures andmap and ormap determine whether a predicate p returns true, false, or FAIL for all or some operands of an expression expr.
 • If expr is atomic, both andmap(p, expr, ...) and ormap(p, expr, ...) are equivalent to p(expr, ... ).
 • In general, andmap(p, expr, ...) returns true if p(opnd, ...) is true for all operands opnd of expr, returns false if any call to p returns false, and returns FAIL when calls to p lead to at least one FAIL result and any number of other true and FAIL results.
 • Similarly, ormap(p, expr, ...) returns false if p(opnd, ...) is false for all operands opnd of expr, true when any call to p returns true, and FAIL when calls to p lead to at least one FAIL result and any number of other false and FAIL results.
 • Both andmap and ormap have short-circuit ("McCarthy") semantics, which means that an answer is returned as soon as it can be determined. The predicate only evaluates at the operands of the expression expr until the result can be determined. The order in which the operands are examined is not specified. You should not rely on side effects of the predicate p.
 • For a table or array, p is applied to each entry of the table or array.
 • Since strings are atomic expressions in Maple, you cannot map a procedure over a string by using andmap and ormap. However, the StringTools package contains the exports AndMap and OrMap that provide this functionality.

 • The andmap and ormap commands are thread safe as of Maple 15, provided that evaluating the expression p is thread safe.

Examples

 > andmap( type, [ 1, 2, 3, 4, 5 ], 'integer' );
 ${\mathrm{true}}$ (1)
 > ormap( type, [ 1, 2, 3, 4, 5 ], 'integer' );
 ${\mathrm{true}}$ (2)
 > andmap( type, [ 1, 2, 3, 4, 5 ], 'even' );
 ${\mathrm{false}}$ (3)
 > ormap( type, [ 1, 2, 3, 4, 5 ], 'even' );
 ${\mathrm{true}}$ (4)
 > andmap( isprime, [ 2, 3, 5, 7 ] );
 ${\mathrm{true}}$ (5)
 > andmap( isprime, [ 2, 3, 5, 8 ] );
 ${\mathrm{false}}$ (6)
 > ormap( isprime, { 2, 3, 5, 8 } );
 ${\mathrm{true}}$ (7)
 > t := table( [ a = 1, b = 2, c = 3 ] ):
 > andmap( type, eval(t,1), 'integer' );
 ${\mathrm{true}}$ (8)
 > andmap( type, eval(t,1), 'even' );
 ${\mathrm{false}}$ (9)
 > ormap( type, eval(t,1), 'even' );
 ${\mathrm{true}}$ (10)
 > e := expand( Int(randpoly(x),x) );
 ${e}{≔}{-}{7}{}\left({\int }{{x}}^{{5}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{x}\right){+}{22}{}\left({\int }{{x}}^{{4}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{x}\right){-}{55}{}\left({\int }{{x}}^{{3}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{x}\right){-}{94}{}\left({\int }{{x}}^{{2}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{x}\right){+}{87}{}\left({\int }{x}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{x}\right){-}{56}{}\left({\int }{1}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{x}\right)$ (11)
 > andmap( hastype, e, 'specfunc( anything, Int )' );
 ${\mathrm{true}}$ (12)

This examples illustrates a technique for quickly destructuring a record.

 > RecordSlots := proc( r::record )       if not type( [ args[ 2 .. nargs ] ], 'list( symbol )' ) then               error "arguments after the first must be of type symbol'"       end if;       andmap( e -> member( cat( e ), r, e ), [ args[ 2 .. nargs ] ] ) end proc:
 > r := Record( 'a' = 2, 'b' = 3, 'c' = Array( 1 .. 5 ) );
 ${r}{≔}{\mathrm{Record}}{}\left({a}{=}{2}{,}{b}{=}{3}{,}{c}{=}\left[\begin{array}{ccccc}{0}& {0}& {0}& {0}& {0}\end{array}\right]\right)$ (13)
 > RecordSlots( r, 'a', 'b', 'c' );
 ${\mathrm{true}}$ (14)
 > a, b, c;
 ${2}{,}{3}{,}\left[\begin{array}{ccccc}{0}& {0}& {0}& {0}& {0}\end{array}\right]$ (15)
 > andmap( (a)->if( a < 0, FAIL, a mod 7 = 0), [ 7, 14 ] );
 ${\mathrm{true}}$ (16)
 > andmap( (a)->if( a < 0, FAIL, a mod 7 = 0), [ 7, 14, -7 ] );
 ${\mathrm{FAIL}}$ (17)
 > andmap( (a)->if( a < 0, FAIL, a mod 7 = 0), [ 7, 14, -7, 3 ] );
 ${\mathrm{false}}$ (18)
 > ormap( (a)->if( a < 0, FAIL, a mod 7 = 0), [ 7, 14, -7 ] );
 ${\mathrm{true}}$ (19)
 > ormap( (a)->if( a < 0, FAIL, a mod 7 = 0), [ 1, 3 ] );
 ${\mathrm{false}}$ (20)
 > ormap( (a)->if`( a < 0, FAIL, a mod 7 = 0), [ 1, 3, -5 ] );
 ${\mathrm{FAIL}}$ (21)

Compatibility

 • The andmap and ormap commands were updated in Maple 2017.