Filter: Difference between revisions

(Initial FutureBasic task solution added)
 
(9 intermediate revisions by 5 users not shown)
Line 1,566:
auto array := new int[]{1,2,3,4,5};
 
var evens := array.filterBy::(n => n.mod:(2) == 0).toArray();
 
evens.forEach:(printingLn)
}</syntaxhighlight>
Using strong typed collections and extensions:
Line 1,581:
 
array
.filterBy::(int n => n.mod:(2) == 0)
.forEach::(int i){ console.printLine(i) }
}</syntaxhighlight>
{{out}}
Line 2,388:
 
=={{header|langur}}==
Using the filter() function filters by a function or regex and returns ana arraylist of values.
 
<syntaxhighlight lang="langur">val .arrlist = series 7
{{works with|langur|0.11}}
<syntaxhighlight lang="langur">val .arr = series 7
 
writeln " array list: ", .arrlist
writeln "filtered: ", filter ffn{div 2}, .arr</syntaxhighlight>list
}</syntaxhighlight>
 
{{out}}
<pre> array list: [1, 2, 3, 4, 5, 6, 7]
filtered: [2, 4, 6]</pre>
 
Line 4,038:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var arr = [1,2,3,4,5];
 
# Creates a new array
var new = arr.grep {|i| i.is_even %% 2};
say new.dump; # => [2, 4]
 
# Destructive (at variable level)
arr.grep! {|i| i.is_even %% 2};
say arr.dump; # => [2, 4]</syntaxhighlight>
 
=={{header|Slate}}==
Line 4,493:
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn reduce(mut a []int){
fn reduce(mut a []int){
mut last := 0
for e in a {
if e % 2 == 0 {
a[last] = e
last++
}
}
a = a[..last].clone()
}
fn main() {
mut nums := [5, 4, 8, 2, 4, 6, 5, 6, 34, 12, 21]
even := nums.filter(it % 2 == 0)
println('orig: ${nums}')
println('even: ${even}')
reduce(mut nums)
println('dest: ${nums}')
}
}</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
<pre>orig: [5, 4, 8, 2, 4, 6, 5, 6, 34, 12, 21]
even: [4, 8, 2, 4, 6, 6, 34, 12]
dest: [4, 8, 2, 4, 6, 6, 34, 12]</pre>
</pre>
 
=={{header|WDTE}}==
Line 4,538 ⟶ 4,542:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var a = [1, 4, 17, 8, -21, 6, -11, -2, 18, 31]
System.print("The original array is : %(a)")
 
890

edits