27.26 Filtered

Filtered( list, func )

Filtered returns a new list that contains those elements of the list list for which the unary function func returns true. The order of the elements in the result is the same as the order of the corresponding elements of list. If an element, for which func returns true appears several times in list it will also appear the same number of times in the result. list may contain holes, they are ignored by Filtered. func must return either true or false for every element of list, otherwise an error is signalled.

    gap> Filtered( [1..20], IsPrime );
    [ 2, 3, 5, 7, 11, 13, 17, 19 ]
    gap> Filtered( [ 1, 3, 4, -4, 4, 7, 10, 6 ], IsPrimePowerInt );
    [ 3, 4, 4, 7 ]
    gap> Filtered( [ 1, 3, 4, -4, 4, 7, 10, 6 ],
    >              n -> IsPrimePowerInt(n) and n mod 2 <> 0 );
    [ 3, 7 ] 

The result is a new list, that is not identical to any other list. The elements of that list however are identical to the corresponding elements of the argument list (see Identical Lists).

Sublist (see Sublist) allows you to extract elements of a list according to indices given in another list.

Previous Up Top Next
Index

GAP 3.4.4
April 1997