Filter: Difference between revisions

11,452 bytes added ,  1 month ago
 
(20 intermediate revisions by 11 users not shown)
Line 21:
<pre>
[2, 4, 6, 8, 10]
</pre>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program filterdes64.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
/************************************/
/* Initialized data */
/************************************/
.data
szMessResult: .asciz "Start array : "
szMessResultFil: .asciz "Filter array : "
szMessResultdest: .asciz "Same array : "
szMessStart: .asciz "Program 64 bits start.\n"
szCarriageReturn: .asciz "\n"
szFiller: .asciz " "
.align 4
arrayNumber: .quad 1,2,3,4,5,6,7,8,9,10
.equ LGARRAY, (. - arrayNumber) / 8
/************************************/
/* UnInitialized data */
/************************************/
.bss
.align 4
arrayNumberFil: .skip 8 * LGARRAY // result array
sZoneConv: .skip 24
/************************************/
/* code section */
/************************************/
.text
.global main
main:
ldr x0,qAdrszMessStart // display start message
bl affichageMess
ldr x0,qAdrszMessResult // display message
bl affichageMess
ldr x5,qAdrarrayNumber // start array address
mov x4,#0 // index
1:
ldr x0,[x5,x4,lsl #3] // load a value
ldr x1,qAdrsZoneConv
bl conversion10 // décimal conversion
ldr x0,qAdrsZoneConv
bl affichageMess // display value
ldr x0,qAdrszFiller
bl affichageMess
add x4,x4,#1 // increment index
cmp x4,#LGARRAY // end array ?
blt 1b // no -> loop
ldr x0,qAdrszCarriageReturn
bl affichageMess
ldr x6,qAdrarrayNumberFil // adrress result array
mov x4,#0 // index
mov x3,#0 // index result
2:
ldr x0,[x5,x4,lsl #3] // load a value
tst x0,#1 // odd ?
bne 3f
str x0,[x6,x3,lsl #3] // no -> store in result array
add x3,x3,#1 // and increment result index
3:
add x4,x4,#1 // increment array index
cmp x4,#LGARRAY // end ?
blt 2b // no -> loop
ldr x0,qAdrszMessResultFil
bl affichageMess
mov x4,#0 // init index
4: // display filter result array
ldr x0,[x6,x4,lsl #3]
ldr x1,qAdrsZoneConv
bl conversion10
ldr x0,qAdrsZoneConv
bl affichageMess
ldr x0,qAdrszFiller
bl affichageMess
add x4,x4,#1
cmp x4,x3
blt 4b
ldr x0,qAdrszCarriageReturn
bl affichageMess
// array destruction
mov x4,#0 // index
mov x3,#0 // index result
5:
ldr x0,[x5,x4,lsl #3] // load a value
tst x0,#1 // odd ?
bne 7f
cmp x3,x4 // index = no store
beq 6f
str x0,[x5,x3,lsl #3] // store in free item on same array
6:
add x3,x3,#1 // and increment result index
7:
add x4,x4,#1 // increment array index
cmp x4,#LGARRAY // end ?
blt 5b // no -> loop
ldr x0,qAdrszMessResultdest
bl affichageMess
mov x4,#0 // init index
8: // display array
ldr x0,[x5,x4,lsl #3]
ldr x1,qAdrsZoneConv
bl conversion10
ldr x0,qAdrsZoneConv
bl affichageMess
ldr x0,qAdrszFiller
bl affichageMess
add x4,x4,#1
cmp x4,x3
blt 8b
ldr x0,qAdrszCarriageReturn
bl affichageMess
 
100: // standard end of the program
mov x0, #0 // return code
mov x8, #EXIT // request to exit program
svc 0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrszMessStart: .quad szMessStart
qAdrarrayNumber: .quad arrayNumber
qAdrszMessResult: .quad szMessResult
qAdrarrayNumberFil: .quad arrayNumberFil
qAdrszMessResultFil: .quad szMessResultFil
qAdrszMessResultdest: .quad szMessResultdest
qAdrsZoneConv: .quad sZoneConv
qAdrszFiller: .quad szFiller
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeARM64.inc"
 
 
</syntaxhighlight>
{{Out}}
<pre>
Program 64 bits start.
Start array : 1 2 3 4 5 6 7 8 9 10
Filter array : 2 4 6 8 10
Same array : 2 4 6 8 10
</pre>
 
Line 480 ⟶ 635:
{{Out}}
<pre>{0, 2, 4, 6, 8, 10}</pre>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program filterdes.s */
 
/************************************/
/* Constantes */
/************************************/
/* for constantes see task include a file in arm assembly */
.include "../constantes.inc"
 
/************************************/
/* Initialized data */
/************************************/
.data
szMessResult: .asciz "Start array : "
szMessResultFil: .asciz "Filter array : "
szMessResultdest: .asciz "Same array : "
szMessStart: .asciz "Program 32 bits start.\n"
szCarriageReturn: .asciz "\n"
.align 4
arrayNumber: .int 1,2,3,4,5,6,7,8,9,10
.equ LGARRAY, (. - arrayNumber) / 4
/************************************/
/* UnInitialized data */
/************************************/
.bss
.align 4
arrayNumberFil: .skip 4 * LGARRAY @ result array
sZoneConv: .skip 24
/************************************/
/* code section */
/************************************/
.text
.global main
main:
ldr r0,iAdrszMessStart @ display start message
bl affichageMess
ldr r0,iAdrszMessResult @ display message
bl affichageMess
ldr r5,iAdrarrayNumber @ start array address
mov r4,#0 @ index
1:
ldr r0,[r5,r4,lsl #2] @ load a value
ldr r1,iAdrsZoneConv
bl conversion10 @ décimal conversion
add r1,r1,r0 @ compute address end number
add r1,#2 @ add two characters
mov r0,#0 @ for limit the size of display number
strb r0,[r1] @ to store a final zero
ldr r0,iAdrsZoneConv
bl affichageMess @ display value
add r4,r4,#1 @ increment index
cmp r4,#LGARRAY @ end array ?
blt 1b @ no -> loop
ldr r0,iAdrszCarriageReturn
bl affichageMess
ldr r6,iAdrarrayNumberFil @ adrress result array
mov r4,#0 @ index
mov r3,#0 @ index result
2:
ldr r0,[r5,r4,lsl #2] @ load a value
tst r0,#1 @ odd ?
streq r0,[r6,r3,lsl #2] @ no -> store in result array
addeq r3,r3,#1 @ and increment result index
add r4,r4,#1 @ increment array index
cmp r4,#LGARRAY @ end ?
blt 2b @ no -> loop
ldr r0,iAdrszMessResultFil
bl affichageMess
mov r4,#0 @ init index
3: @ display filter result array
ldr r0,[r6,r4,lsl #2]
ldr r1,iAdrsZoneConv
bl conversion10
add r1,r1,r0
add r1,#2
mov r0,#0
strb r0,[r1]
ldr r0,iAdrsZoneConv
bl affichageMess
add r4,r4,#1
cmp r4,r3
blt 3b
ldr r0,iAdrszCarriageReturn
bl affichageMess
@ array destruction
mov r4,#0 @ index
mov r3,#0 @ index result
4:
ldr r0,[r5,r4,lsl #2] @ load a value
tst r0,#1 @ even ?
bne 6f
cmp r3,r4 @ index = no store
beq 5f
str r0,[r5,r3,lsl #2] @ store in free item on same array
5:
add r3,r3,#1 @ and increment result index
6:
add r4,r4,#1 @ increment array index
cmp r4,#LGARRAY @ end ?
blt 4b @ no -> loop
ldr r0,iAdrszMessResultdest
bl affichageMess
mov r4,#0 @ init index
7: @ display array
ldr r0,[r5,r4,lsl #2]
ldr r1,iAdrsZoneConv
bl conversion10
add r1,r1,r0
add r1,#2
mov r0,#0
strb r0,[r1]
ldr r0,iAdrsZoneConv
bl affichageMess
add r4,r4,#1
cmp r4,r3
blt 7b
ldr r0,iAdrszCarriageReturn
bl affichageMess
 
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc 0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrszMessStart: .int szMessStart
iAdrarrayNumber: .int arrayNumber
iAdrszMessResult: .int szMessResult
iAdrarrayNumberFil: .int arrayNumberFil
iAdrszMessResultFil: .int szMessResultFil
iAdrszMessResultdest: .int szMessResultdest
iAdrsZoneConv: .int sZoneConv
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language ARM assembly*/
.include "../affichage.inc"
 
</syntaxhighlight>
{{Out}}
<pre>
Program 32 bits start.
Start array : 1 2 3 4 5 6 7 8 9 10
Filter array : 2 4 6 8 10
Same array : 2 4 6 8 10
</pre>
 
=={{header|Arturo}}==
Line 1,255 ⟶ 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,270 ⟶ 1,581:
 
array
.filterBy::(int n => n.mod:(2) == 0)
.forEach::(int i){ console.printLine(i) }
}</syntaxhighlight>
{{out}}
Line 1,298 ⟶ 1,609:
<pre>
(2 4 6 8 10)
</pre>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
writeLine(range(1, 11).filter(<int i|i % 2 == 0))
</syntaxhighlight>
{{out}}
<pre>
[2,4,6,8,10]
</pre>
 
Line 1,555 ⟶ 1,875:
filter (fn x => x%2 == 0) as
</syntaxhighlight>
 
=={{header|FutureBasic}}==
Even elements from array added to new array.
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn EvenNumbersFromArrayToNewArray
CFArrayRef numbersArray = @[@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12]
PredicateRef isEvenPred = fn PredicateWithFormat( @"modulus:by:(SELF, 2) == 0" )
CFArrayRef evensArray = fn ArrayFilteredArrayUsingPredicate( numbersArray, isEvenPred )
NSLog( @"Array of odd and even numbers before sort:\n\t%@\n", numbersArray )
NSLog( @"New array of even numbers after sort:\n\t%@\n", evensArray )
end fn
fn EvenNumbersFromArrayToNewArray
NSLogScrollToTop
</syntaxhighlight>
Destructive version: Odd elemnts removed from mutable array.
<syntaxhighlight>
local fn OddNumbersRemovedFromArray
CFMutablearrayRef mutableNumbersArray = fn MutableArrayWithArray( @[@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12] )
NSLog( @"Mutable array of odd and even numbers before sort:\n\t%@\n", mutableNumbersArray )
PredicateRef isEvenPred = fn PredicateWithFormat( @"modulus:by:(SELF, 2) == 0" )
MutableArrayFilterUsingPredicate( mutableNumbersArray, isEvenPred )
NSLog( @"Mutable array with odd numbers removed:\n\t%@\n", mutableNumbersArray )
end fn
 
fn OddNumbersRemovedFromArray
NSLogScrollToTop
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre style="height:20ex;">
 
Array of odd and even numbers before sort:
(
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12
)
 
New array of even numbers after sort:
(
2,
4,
6,
8,
10,
12
)
 
Mutable array of odd and even numbers before sort:
(
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12
)
 
Mutable array with odd numbers removed:
(
2,
4,
6,
8,
10,
12
)
</pre>
 
 
 
 
=={{header|Gambas}}==
Line 1,709 ⟶ 2,118:
 
<syntaxhighlight lang="idl">result = array[where(NOT array AND 1)]</syntaxhighlight>
 
=={{header|Insitux}}==
<syntaxhighlight lang="insitux">> (filter even? [0 1 2 3 4])
[0 2 4]
 
> (var x [0 1 2 3 4])
[0 1 2 3 4]
 
> (var! x (filter even?)) ;replaces variable x by applying implicit closure
[0 2 4]
</syntaxhighlight>
 
=={{header|J}}==
Line 1,743 ⟶ 2,163:
 
(That said, note that in a highly parallel computing environment the destruction either happens after the filtering or you have to repeatedly stall the filtering to ensure that some sort of partially filtered result has coherency.)
 
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
fn filter<T>(anon array: [T], anon filter_function: fn(anon value: T) -> bool) throws -> [T] {
mut result: [T] = []
for value in array {
if filter_function(value) {
result.push(value)
}
}
return result
}
 
fn main() {
mut numbers: [i64] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let filtered = filter(numbers, fn(anon x: i64) -> bool => x % 2 == 0)
println("{}", filtered)
}
</syntaxhighlight>
 
=={{header|Java}}==
Line 1,949 ⟶ 2,388:
 
=={{header|langur}}==
Using the filter() function filters by a function or regex and returns ana arraylist of values.
 
<syntaxhighlight lang="langur">val .list = 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 3,277 ⟶ 3,716:
===using two arrays===
This example uses two arrays. &nbsp; The &nbsp; '''random''' &nbsp; BIF is used to generate the numbers.
<syntaxhighlight lang="rexx">/*REXX program selects all even numbers from an array and puts them ──► a new array.*/
parse/* arginto Na seednew array. /*obtain optional arguments from the CL */
ifParse N==''Arg |n N==","seed then N= 50. /*Not specified?obtain optional Thenarguments usefrom the default.CL*/
ifIf datatype(seed,n=='W')|n=="," Then n=50 then call random ,,seed /* Not specified? use the RANDOM seeddefault for repeatability*/
If datatype(seed,'W') Then
old.= /*the OLD array, all are null so far. */
new.= Call random,,seed /* " NEW " " " " " use "RANDOM seed for repeatability*/
doDo i=1 For forn N /* generate N random numbers ──► OLD */
old.i= random(1, 99999) /* generate random number 1 ──► 99999 */
End
end /*i*/
#m= 0 /* number of elements in NEW (so far). */
doDo j=1 To for N n /* process the elements of the OLD array */
ifIf old.j//2 \== 0 Then then iterate Do /* if element isn'tis even, then skip it. */
#m= # m+ 1 /* bump the number of NEW elements.elemens */
new.#m= old.j /* assign the number to the NEW array. */
end /*j*/End
End
 
doDo k=1 For for # m /* display all the NEW numbers. */
saySay right('new.'k, 20) "'="' right(new.k, 9) /*display a line (an array element). */
End </syntaxhighlight>
end /*k*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> , &nbsp; 12345 </tt>}}
Programming note: &nbsp; the REXX statement
The '''12345''' is the '''random''' BIF &nbsp; ''seed'' &nbsp; so that the random numbers can be repeated when re-running the REXX program.
<syntaxhighlight lang="rexx"> if old.j//2 \== 0 then iterate</syntaxhighlight>
could've been replaced with
<syntaxhighlight lang="rexx"> if old.j//2 then iterate</syntaxhighlight>
but that would've assumed the numbers are integers &nbsp; (no matter what form they're expressed in).<br>
As it happens, the REXX program uses the numbers generated from the &nbsp; '''random''' &nbsp; BIF, &nbsp; which are integers.
 
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> , &nbsp; 1234567 </tt>}}
 
The '''1234567''' is the '''random''' BIF &nbsp; ''seed'' &nbsp; so that the random numbers can be repeated when re-running the REXX program.
<pre>
new.1 = 17520
Line 3,331 ⟶ 3,762:
new.23 = 44360
</pre>
===Using a control array===
 
===usingThis oneversion array withuses a control array=== (even.*)
<syntaxhighlight lang="rexx">
This version uses a control array, which isn't fully populated &nbsp; (in REXX terms, a sparse array.)
<syntaxhighlight lang="rexx">/*REXX program findsuses alla even numbers from ancontrol array, to andtell which markselements aars control arrayeven. */
parseParse argArg Nn seed . /* obtain optional arguments from the CL*/
ifIf Nn=='' | Nn=="," Then then Nn= 50 /* Not specified? Then use the default. */
ifIf datatype(seed,'W') then call random ,,seed /*use the RANDOM seed for repeatability*/Then
Call random,,seed /* use RANDOM seed for repeatability*/
 
doDo i=1 For forn N /* generate n random numbers N random numbers ──► OLD */
@x.i= random(1, 99999) /* generate random number 1 ──► 99999 */
End
end /*i*/
!even.= 0 /* all even bits are off /*number of elements in NEW (so far).*/
doDo j=1 To for N n /* process the elements OLDof x.* array elements. */
If if @x.j//2 \==0 Then then !.j= 1 /* if element is /*markeven, thethen ! array that it's ¬even. */
even.j=1 /* turn on the even bit */
end /*j*/
End
 
doDo k=1 To for N n /* display all the numbers @ even numbers. */
If even.k Then if !.k then iterate /* that are even /*if it's marked as not even, skip it.*/
saySay right('arrayx.'k, 20) "'="' right(@x.k, 9) /*display a even number, filtered array*/
End</syntaxhighlight>
end /*k*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
For{{out|output|text=&nbsp; thewhen followingusing the input of: &nbsp; &nbsp; <tt> ,20 &nbsp; 123456712345 </tt>}}
<pre>
 
x.3 = 52754
{{out|output|text=&nbsp; Output is the same as the 1<sup>st</sup> REXX version &nbsp; (using two arrays).}}<br>
x.5 = 94296
 
x.6 = 2068
 
x.13 = 71494
x.14 = 71628
x.15 = 47404
x.19 = 92502
x.20 = 24808</pre>
===using one array, destructive===
This version just uses one array to perform the filtering instead of creating a &nbsp; ''new'' &nbsp; array.
The result is a sparse array.
 
This method doesn't need as much memory to hold the sparse array.
<syntaxhighlight lang="rexx">/*REXX program findssets all elements evencontaining odd numbers fromto anblank array, and marks the not even numbers.*/
parseParse argArg Nn seed . /* obtain optional arguments from the CL*/
ifIf Nn=='' | Nn=="," Then then Nn= 50 /* Not specified? Then use the default. */
ifIf datatype(seed,'W') then call random ,,seed /*use the RANDOM seed for repeatability*/Then
Call random,,seed /* use RANDOM seed for repeatability*/
 
doDo i=1 For forn N /* generate N random numbers ──► OLD */
@x.i= random(1, 99999) /* generate a random number 1 ──► 99999 */
End
end /*i*/
Do j=1 To n /* process the elements of x.* */
 
If do x.j=1 for N //2<>0 Then /*process theif element OLDis not arrayeven, then elements. */
Drop if @x.j//2 \==0 then @.j= /*mark thedelete it @ array that it's not even*/
End
end /*j*/
Do k=1 To n /* display all the numbers */
 
If do datatype(x.k)=1'NUM' Then for N /* that are even /*display all the @ even numbers. */
Say right('x.'k,20) '=' right(x.k,9)
if @.k=='' then iterate /*if it's marked not even, then skip it*/
End</syntaxhighlight>
say right('array.'k, 20) "=" right(@.k, 9) /*display a line (an array element). */
For the following input: &nbsp; &nbsp; <tt> 20 12345 </tt>
end /*k*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; is the same as the 2<sup>nd</sup> REXX version.}} <br><br>
For the following input: &nbsp; &nbsp; <tt> , 1234567 </tt>
 
{{out|output|text=&nbsp; is the same as the 1<sup>st</sup> REXX version &nbsp; (using two arrays).}} <br><br>
 
=={{header|Ring}}==
Line 3,604 ⟶ 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,059 ⟶ 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]
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,104 ⟶ 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)")
 
885

edits