Skip to content

Filter ​

Generated from the engine's own definition: compute-engine's symbol, which we neither extend nor document by hand.

Return the elements of the collection for which the predicate function returns True. Equivalent to [x for x in xs if p(x)].

✓ checked against that system · ! checked, with disagreements (hover a chip)Fungrim entryWolfram LanguageSelect✓
Filter(collection<T>, predicate: (T) any -> boolean) -> collection where Tas compute-engine declares it
Filter(predicate)the operator form, Filter(p)(xs)=Filter(xs,p)\mathrm{Filter}(p)(xs) = \mathrm{Filter}(xs, p).
Typed signatures
TypePackageApplies
(any) -> anycombinatoricsreplaces compute-enginealways
(collection<T>, predicate: (T) any -> boolean) -> collection where Tcompute-enginealways

Domain: Compute engine

Examples

Basic #

Keeps the elements that satisfy the predicate

live ↗
InFilter([1,2,4,7,6,2],IsEven)\mathrm{Filter}(\bigl\lbrack1, 2, 4, 7, 6, 2\bigr\rbrack, \mathrm{IsEven})Out[2,4,6,2]\bigl\lbrack2, 4, 6, 2\bigr\rbrack

The complementary predicate keeps the rest

live ↗
InFilter([1,2,4,7,6,2],IsOdd)\mathrm{Filter}(\bigl\lbrack1, 2, 4, 7, 6, 2\bigr\rbrack, \mathrm{IsOdd})Out[1,7]\bigl\lbrack1, 7\bigr\rbrack
live ↗
InFilter(IsEven)⊲[1,2,4,7,6,2]\mathrm{Filter}(\mathrm{IsEven})\lhd\bigl\lbrack1, 2, 4, 7, 6, 2\bigr\rbrackOut[2,4,6,2]\bigl\lbrack2, 4, 6, 2\bigr\rbrack
Scope #

Keeps the pairs that mention x

live ↗
InFilter([[1,y],[2,x],[3,x],[4,z],[5,x],[6,y]],_1⁡↦Contains(_1⁡,x))\mathrm{Filter}(\bigl\lbrack\bigl\lbrack1, y\bigr\rbrack, \bigl\lbrack2, x\bigr\rbrack, \bigl\lbrack3, x\bigr\rbrack, \bigl\lbrack4, z\bigr\rbrack, \bigl\lbrack5, x\bigr\rbrack, \bigl\lbrack6, y\bigr\rbrack\bigr\rbrack, \operatorname{\_1}↦\mathrm{Contains}(\operatorname{\_1}, x))Out[[2,x],[3,x],[5,x]]\bigl\lbrack\bigl\lbrack2, x\bigr\rbrack, \bigl\lbrack3, x\bigr\rbrack, \bigl\lbrack5, x\bigr\rbrack\bigr\rbrack

A different test selects a single pair

live ↗
InFilter([[1,y],[2,x],[3,x],[4,z],[5,x],[6,y]],_1⁡↦Contains(_1⁡,z))\mathrm{Filter}(\bigl\lbrack\bigl\lbrack1, y\bigr\rbrack, \bigl\lbrack2, x\bigr\rbrack, \bigl\lbrack3, x\bigr\rbrack, \bigl\lbrack4, z\bigr\rbrack, \bigl\lbrack5, x\bigr\rbrack, \bigl\lbrack6, y\bigr\rbrack\bigr\rbrack, \operatorname{\_1}↦\mathrm{Contains}(\operatorname{\_1}, z))Out[[4,z]]\bigl\lbrack\bigl\lbrack4, z\bigr\rbrack\bigr\rbrack

An anonymous test keeps elements greater than 2; duplicates and order preserved

live ↗
InFilter([1,2,4,7,6,2],_1⁡↦2<_1⁡)\mathrm{Filter}(\bigl\lbrack1, 2, 4, 7, 6, 2\bigr\rbrack, \operatorname{\_1}↦2\lt\operatorname{\_1})Out[4,7,6]\bigl\lbrack4, 7, 6\bigr\rbrack
Applications #

Numbers that are 1 mod both 3 and 5 are those 1 mod 15

live ↗
InFilter(1..100,_1⁡↦_1⁡ mod 3=1∧_1⁡ mod 5=1)\mathrm{Filter}(1..100, \operatorname{\_1}↦\operatorname{\_1}\bmod3=1∧\operatorname{\_1}\bmod5=1)Out[1,16,31,46,61,76,91]\bigl\lbrack1, 16, 31, 46, 61, 76, 91\bigr\rbrack
Properties #

Evens in a range

live ↗
InFilter(1..10,IsEven)\mathrm{Filter}(1..10, \mathrm{IsEven})Out[2,4,6,8,10]\bigl\lbrack2, 4, 6, 8, 10\bigr\rbrack
About
Examples8
Checked againstWolfram Language 8 agree
CrosswalkFungrim entry ×13Wolfram Language

Implementation

mappedmathlib4packages/oracle/src/mappings.ts

The elements where the predicate holds; a predicate that is not a Bool or decidable Prop fails to elaborate.