Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Ada
- Clojure
- C++
- C++
- C#
- C#
- D
- D
- Dart
- Elixir
- Elixir
- Erlang
- Erlang
- Fortran
- Go
- Go
- Go
- Go
- Groovy
- Haskell
- JS
- Java
- Java
- Kotlin
- Lisp
- Lua
- Lua
- Obj-C
- PHP
- Pascal
- Perl
- Python
- Python
- Python
- Ruby
- Rust
- Scala
- Scheme
- Smalltalk
for Item of X loop
if P (Item) then
Y.Append (Item);
end if;
end loop;
(def y (filter p x))
y = for item <- x, p.(item), do: item
y = Enum.filter(x, p)
Y = lists:filter(P, X).
Y = [I || I <- X, P(X)].
y = pack(x,mask=p(x))
n := 0
for _, v := range x {
if p(v) {
n++
}
}
y := make([]T, 0, n)
for _, v := range x {
if p(v) {
y = append(y, v)
}
}
This makes 2 passes: one to count the number n of elements to be kept, and one to copy the elements in the target slice created with the correct size n.
This is efficient if p is cheap and x is small.
This is efficient if p is cheap and x is small.
func filter[S ~[]T, T any](x S, p func(T) bool) S {
var y S
for _, v := range x {
if p(v) {
y = append(y, v)
}
}
return y
}
filter is a generic function with a type parameter T
y := make([]T, 0, len(x))
for _, v := range x{
if p(v){
y = append(y, v)
}
}
For item type T.
Note that this allocates memory for the new slice y.
Warning: y is allocated with as much memory as x, which may in some cases be wasteful.
Note that this allocates memory for the new slice y.
Warning: y is allocated with as much memory as x, which may in some cases be wasteful.
y = filter p x
val y = x.filter(p)
(setf y (remove-if-not p x))
y = {}
for _, v in ipairs(x) do
if p(v) then y[#y+1] = v end
end
local y={}
for _,v in ipairs(x) do
if p(v)==true then
table.insert(y,v)
end
end
function p($element) { /* .... */ }
$y = array_filter ($x, "p");
Filtering code goes to function p which is called for each element of array $x,
and if function returns true, that element goes to array $y.
and if function returns true, that element goes to array $y.
function Filter(vv:integer):boolean;
begin
result := vv mod 2= 0;
end;
type TFilter=function(v:integer):boolean;
function FilteredArray(const x:TBoundArray;p:TFilter):TBoundArray;
var
Idx: Integer;
v : Integer;
begin
setlength(result,high(x)+1);
Idx := 0;
for v in x do
if p(v) then
begin
result[Idx] := v;
inc(Idx);
end;
setlength(result,Idx);
end;
[...]
y := FilteredArray(x,@Filter);
The function Filter defines a demo-filter.
This is/can be passed to the FilteredArray-function.
TBoundArray is a standard type for array of integer;
On some implementation's (e.G: delphi) you have to omit the @ before Filter.
This is/can be passed to the FilteredArray-function.
TBoundArray is a standard type for array of integer;
On some implementation's (e.G: delphi) you have to omit the @ before Filter.
# You can use a subroutine as your predicate
@primes_less_than_100 = grep { is_prime($_) } 1 .. 99;
# You can also write your predicate inline
@odd_numbers = grep { $_%2 == 1 } 1 .. 99;
y = [element for element in x if p(element)]
List comprehensions tend to be more readable than filter function
y = [*filter(p, x)]
y = x.select(&:p)
select is also aliased to find_all.
val y = x.filter(p)
(define y (filter p x))
y := x select: [:item | item p].