Filter: Difference between revisions
→{{header|langur}}
(Add emacs lisp) |
Langurmonkey (talk | contribs) |
||
(29 intermediate revisions by 15 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
evens.forEach
}</syntaxhighlight>
Using strong typed collections and extensions:
Line 1,270 ⟶ 1,581:
array
.filterBy::(int n => n.mod
.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,818 ⟶ 2,257:
{{Out}}
<syntaxhighlight lang="javascript">[2, 4, 6, 8]</syntaxhighlight>
=={{header|Joy}}==
<syntaxhighlight lang="joy">[1 2 3 4 5 6 7 8 9 10] [2 rem null] filter.</syntaxhighlight>
=={{header|jq}}==
Line 1,946 ⟶ 2,388:
=={{header|langur}}==
Using the filter() function filters by a function or regex and returns
<syntaxhighlight lang="langur">val .list = series 7
writeln "
writeln "filtered: ", filter
</syntaxhighlight>
{{out}}
<pre>
filtered: [2, 4, 6]</pre>
Line 2,989 ⟶ 3,431:
=={{header|Q}}==
<syntaxhighlight lang="q">x where 0=x mod 2</syntaxhighlight>
=={{header|QBASIC}}==
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.x}}
{{works with|Visual Basic for DOS|1.0}}
{{works with|PDS|7.x}}
===Using two arrays===
This version uses two arrays.
<syntaxhighlight lang="qbasic">
' OPTION EXPLICIT
' Filter
' This program selects certain elements from an array into a new array in a generic way
' Var
' $DYNAMIC
TYPE regSub
aNum AS INTEGER
END TYPE
CONST Even = 2
CONST Uneven = 1
CONST cFile = "DUMMY$$$.$$$"
CONST False = 0, True = NOT False
DIM t AS INTEGER
DIM t2 AS INTEGER
DIM f AS INTEGER
DIM i AS INTEGER
DIM iFlag AS INTEGER
DIM iGetWhat AS INTEGER
DIM iArray%(1 TO 1)
DIM iArray2%(1 TO 1)
DIM rSub AS regSub
' Initialize vars
iFlag = False
f = FREEFILE
iGetWhat = Even
RANDOMIZE TIMER
t = INT(RND * 300) + 1
REDIM iArray%(1 TO t)
' Main program cycle
OPEN cFile FOR OUTPUT AS #f
CLOSE
OPEN cFile FOR RANDOM AS #f LEN = LEN(rSub)
CLS
PRINT "Select items in an array into a new array in a generic way."
PRINT "Base array:"
FOR i = 1 TO t
iArray%(i) = INT(RND * 2000) + 1
PRINT iArray%(i);
IF (iArray%(i) MOD 2) = 0 AND iGetWhat = Even THEN
iFlag = True
ELSEIF (iArray%(i) MOD 2) <> 0 AND iGetWhat = Uneven THEN
iFlag = True
END IF
IF iFlag THEN
rSub.aNum = iArray%(i)
PUT #f, , rSub
iFlag = False
END IF
NEXT i
' Redims the array
t2 = LOF(f) / LEN(rSub)
REDIM iArray2%(1 TO t2)
FOR i = 1 TO t2
GET #f, i, rSub
iArray2%(i) = rSub.aNum
NEXT i
CLOSE #f
KILL cFile
PRINT
' Shows the result
IF t2 > 0 THEN
PRINT "Selected items from the array (total:"; t2; "of"; t; "):"
FOR i = 1 TO t2
PRINT iArray2%(i);
NEXT i
END IF
PRINT
PRINT "End of program."
END</syntaxhighlight>
{{out}}
The output can change as the size of the base array and its values varies on each run.
<pre>
Select items in an array into a new array in a generic way.
Base array:
1426 770 1686 1472 385 1212 909 656 776 707 918 1646 1258 1406 887 42
Selected items from the array (total: 12 of 16 ):
1426 770 1686 1472 1212 656 776 918 1646 1258 1406 42
End of program.
</pre>
===Using one array===
Extra points: This version uses one array.
<syntaxhighlight lang="qbasic">
' OPTION EXPLICIT
' Filter
' This program selects certain elements from an array into a new array in a generic way
' Extra points: Destroys the original values in the array
' Var
' $DYNAMIC
TYPE regSub
aNum AS INTEGER
END TYPE
CONST Even = 2
CONST Uneven = 1
CONST cFile = "DUMMY$$$.$$$"
CONST False = 0, True = NOT False
DIM t AS INTEGER
DIM t2 AS INTEGER
DIM f AS INTEGER
DIM i AS INTEGER
DIM iFlag AS INTEGER
DIM iGetWhat AS INTEGER
DIM iArray%(1 TO 1)
DIM rSub AS regSub
' Initialize vars
iFlag = False
t = INT(RND * 300) + 1
f = FREEFILE
iGetWhat = Even
REDIM iArray%(1 TO t)
' Main program cycle
OPEN cFile FOR OUTPUT AS #f
CLOSE
OPEN cFile FOR RANDOM AS #f LEN = LEN(rSub)
CLS
RANDOMIZE TIMER
PRINT "Select items in an array into a new array in a generic way."
PRINT "Base array:"
FOR i = 1 TO t
iArray%(i) = INT(RND * 2000) + 1
PRINT iArray%(i);
IF (iArray%(i) MOD 2) = 0 AND iGetWhat = Even THEN
iFlag = True
ELSEIF (iArray%(i) MOD 2) <> 0 AND iGetWhat = Uneven THEN
iFlag = True
END IF
IF iFlag THEN
rSub.aNum = iArray%(i)
PUT #f, , rSub
iFlag = False
END IF
NEXT i
' Redims the array
t = LOF(f) / LEN(rSub)
REDIM iArray%(1 TO t)
FOR i = 1 TO t
GET #f, i, rSub
iArray%(i) = rSub.aNum
NEXT i
CLOSE #f
KILL cFile
PRINT
' Shows the result
t2 = UBOUND(iArray%)
IF t2 > 0 THEN
PRINT "Selected items from the array (total:"; t2; "of"; t; "):"
FOR i = 1 TO t2
PRINT iArray%(i);
NEXT i
END IF
PRINT
PRINT "End of program."
END
</syntaxhighlight>
{{out}}
The output can change as the size of the base array and its values varies on each run.
<pre>
Select items in an array into a new array in a generic way.
Base array:
1426 770 1686 1472 385 1212 909 656 776 707 918 1646 1258 1406 887 42
Selected items from the array (total: 12 of 16 ):
1426 770 1686 1472 1212 656 776 918 1646 1258 1406 42
End of program.
</pre>
=={{header|R}}==
Line 3,070 ⟶ 3,716:
===using two arrays===
This example uses two arrays. The '''random''' BIF is used to generate the numbers.
<syntaxhighlight lang="rexx">/*REXX program selects all
If datatype(seed,'W') Then
End
End
End </syntaxhighlight>
{{out|output|text= when using the input of: <tt> , 12345 </tt>}}
The '''12345''' is the '''random''' BIF ''seed'' so that the random numbers can be repeated when re-running the REXX program.
<pre>
new.1 = 17520
Line 3,124 ⟶ 3,762:
new.23 = 44360
</pre>
===Using a control array===
<syntaxhighlight lang="rexx">
Call random,,seed /* use RANDOM seed for repeatability*/
End
If
even.j=1 /* turn on the even bit */
End
If even.k Then
End</syntaxhighlight>
<pre>
x.3 = 52754
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 ''new'' array.
The result is a sparse array.
This method doesn't need as much memory
<syntaxhighlight lang="rexx">/*REXX program
Call random,,seed /* use RANDOM seed for repeatability*/
End
Do j=1 To n /* process the elements of x.* */
If
Drop
End
Do k=1 To n /* display all the numbers */
If
Say right('x.'k,20) '=' right(x.k,9)
End</syntaxhighlight>
For the following input: <tt> 20 12345 </tt>
{{out|output|text= is the same as the 2<sup>nd</sup> REXX version.}} <br><br>
=={{header|Ring}}==
Line 3,189 ⟶ 3,830:
return bArray
</syntaxhighlight>
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪
DUP SIZE {} 2 ROT '''FOR''' j OVER j GET + 2 '''STEP'''
≫ ''''FILTR'''' STO
≪
LIST→ → len
≪ {} len 1 '''FOR''' j
'''IF''' j 2 MOD '''THEN''' SWAP DROP '''ELSE''' + '''END'''
-1 '''STEP'''
≫ ≫ ''''FILTD'''' STO
|
'''FILTR''' ''( { a1 a2.. an } -- { a1 a2.. an } { a2 a4... a2k } )''
selective copy
'''FILTD''' ''( { a1 a2.. an } -- { a2 a4... a2k } )''
Destructive version : put the array in the stack...
... and make a new one by picking one out of two items
|}
{{in}}
<pre>
{ 1 2 3 4 5 6 7 8 9 } FILTR
{ 1 2 3 4 5 6 7 8 9 } FILTD
</pre>
{{out}}
<pre>
3: { 1 2 3 4 5 6 7 8 9 }
2: { 2 4 6 8 }
1: { 2 4 6 8 }
</pre>
=={{header|Ruby}}==
Line 3,356 ⟶ 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
say new
# Destructive (at variable level)
arr.grep! {|i| i.is_even
say arr
=={{header|Slate}}==
Line 3,811 ⟶ 4,493:
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">
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>
{{out}}
<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>
=={{header|WDTE}}==
Line 3,856 ⟶ 4,542:
=={{header|Wren}}==
<syntaxhighlight lang="
System.print("The original array is : %(a)")
|