2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,5 +1,9 @@
|
|||
;Task:
|
||||
Select certain elements from an Array into a new Array in a generic way.
|
||||
|
||||
|
||||
To demonstrate, select all even numbers from an Array.
|
||||
|
||||
As an option, give a second solution which filters destructively,
|
||||
by modifying the original Array rather than creating a new Array.
|
||||
<br><br>
|
||||
|
|
|
|||
30
Task/Filter/AppleScript/filter-3.applescript
Normal file
30
Task/Filter/AppleScript/filter-3.applescript
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
-- filter :: (a -> Bool) -> [a] -> [a]
|
||||
-- filter :: (a -> Int -> Bool) -> [a] -> [a]
|
||||
-- filter :: (a -> Int -> [a] -> Bool) -> [a] -> [a]
|
||||
on filter(f, xs)
|
||||
script mf
|
||||
property lambda : f
|
||||
end script
|
||||
|
||||
set lst to {}
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to item i of xs
|
||||
if mf's lambda(v, i, xs) then
|
||||
set end of lst to v
|
||||
end if
|
||||
end repeat
|
||||
return lst
|
||||
end filter
|
||||
|
||||
|
||||
-- Ordinary AppleScript predicate function, rather than a script object
|
||||
|
||||
-- isEven :: (a -> Bool)
|
||||
on isEven(x)
|
||||
x mod 2 = 0
|
||||
end isEven
|
||||
|
||||
set lstRange to {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
|
||||
filter(isEven, lstRange)
|
||||
|
|
@ -1,2 +1,2 @@
|
|||
my @a = 1, 2, 3, 4, 5, 6;
|
||||
my @a = 1..6;
|
||||
my @even = grep * %% 2, @a;
|
||||
|
|
|
|||
|
|
@ -1,20 +1,19 @@
|
|||
/*REXX program selects all even numbers from an array ──► a new array.*/
|
||||
parse arg N seed . /*get optional parameters from CL*/
|
||||
if N=='' | N==',' then N=50 /*Not specified? Then use default*/
|
||||
if seed\=='' & seed\==',' then call random ,,seed /*for repeatability.*/
|
||||
old.= /*the OLD array, all null so far.*/
|
||||
new.= /*the NEW array, all null so far.*/
|
||||
do i=1 for N /*gen N random numbers ──► OLD*/
|
||||
old.i=random(1,99999) /*randum number 1 ──► 99999 */
|
||||
/*REXX program selects all even numbers from an array and puts them ──► a new array.*/
|
||||
parse arg N seed . /*obtain optional arguments from the CL*/
|
||||
if N=='' | N=="," then N=50 /*Not specified? Then use the default.*/
|
||||
if datatype(seed,'W') then call random ,,seed /*use the RANDOM seed for repeatability*/
|
||||
old.= /*the OLD array, all are null so far. */
|
||||
new.= /* " NEW " " " " " " */
|
||||
do i=1 for N /*generate N random numbers ──► OLD */
|
||||
old.i=random(1,99999) /*generate random number 1 ──► 99999*/
|
||||
end /*i*/
|
||||
#=0 /*numb. of elements in NEW so far*/
|
||||
do j=1 for N /*process the OLD array elements.*/
|
||||
if old.j//2 \== 0 then iterate /*if element isn't even, skip it.*/
|
||||
#=#+1 /*bump the number of NEW elements*/
|
||||
new.#=old.j /*assign it to the NEW array. */
|
||||
#=0 /*number of elements in NEW (so far).*/
|
||||
do j=1 for N /*process the elements of the OLD array*/
|
||||
if old.j//2 \== 0 then iterate /*if element isn't even, then skip it.*/
|
||||
#=#+1 /*bump the number of NEW elements. */
|
||||
new.#=old.j /*assign the number to the NEW array.*/
|
||||
end /*j*/
|
||||
|
||||
do k=1 for # /*display all the NEW numbers. */
|
||||
say right('new.'k, 20) "=" right(new.k,9) /*show a line*/
|
||||
end /*k*/
|
||||
/*stick a fork in it, we're done.*/
|
||||
do k=1 for # /*display all the NEW numbers. */
|
||||
say right('new.'k, 20) "=" right(new.k,9) /*display a line (an array element). */
|
||||
end /*k*/ /*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
|
|
@ -1,18 +1,17 @@
|
|||
/*REXX pgm find all even numbers from an array, marks a control array. */
|
||||
arse arg N seed . /*get optional parameters from CL*/
|
||||
f N=='' | N==',' then N=50 /*Not specified? Then use default*/
|
||||
f seed\=='' & seed\==',' then call random ,,seed /*for repeatability.*/
|
||||
/*REXX program finds all even numbers from an array, and marks a control array. */
|
||||
parse arg N seed . /*obtain optional arguments from the CL*/
|
||||
if N=='' | N=="," then N=50 /*Not specified? Then use the default.*/
|
||||
if datatype(seed,'W') then call random ,,seed /*use the RANDOM seed for repeatability*/
|
||||
|
||||
do i=1 for N /*gen N random numbers ──► OLD*/
|
||||
@.i=random(1,99999) /*randum number 1 ──► 99999 */
|
||||
end /*i*/
|
||||
.=0 /*numb. of elements in NEW so far*/
|
||||
do j=1 for N /*process the OLD array elements.*/
|
||||
if @.j//2 \==0 then !.j=1 /*mark ! array that it's ¬even.*/
|
||||
end /*j*/
|
||||
do i=1 for N /*generate N random numbers ──► OLD */
|
||||
@.i=random(1,99999) /*generate random number 1 ──► 99999*/
|
||||
end /*i*/
|
||||
!.=0 /*number of elements in NEW (so far).*/
|
||||
do j=1 for N /*process the OLD array elements. */
|
||||
if @.j//2 \==0 then !.j=1 /*mark the ! array that it's ¬even. */
|
||||
end /*j*/
|
||||
|
||||
do k=1 for N /*display all the @ even numbers.*/
|
||||
if !.k then iterate /*if it's marked ¬even, skip it.*/
|
||||
say right('array.'k, 20) "=" right(@.k,9) /*show a line*/
|
||||
end /*k*/
|
||||
/*stick a fork in it, we're done.*/
|
||||
do k=1 for N /*display all the @ even numbers. */
|
||||
if !.k then iterate /*if it's marked as not even, skip it.*/
|
||||
say right('array.'k, 20) "=" right(@.k,9) /*display a even number, filtered array*/
|
||||
end /*k*/ /*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
|
|
@ -1,18 +1,17 @@
|
|||
/*REXX pgm find all even numbers from an array, marks ¬ even numbers. */
|
||||
parse arg N seed . /*get optional parameters from CL*/
|
||||
if N=='' | N==',' then N=50 /*Not specified? Then use default*/
|
||||
if seed\=='' & seed\==',' then call random ,,seed /*for repeatability.*/
|
||||
/*REXX program finds all even numbers from an array, and marks the not even numbers.*/
|
||||
parse arg N seed . /*obtain optional arguments from the CL*/
|
||||
if N=='' | N=="," then N=50 /*Not specified? Then use the default.*/
|
||||
if datatype(seed,'W') then call random ,,seed /*use the RANDOM seed for repeatability*/
|
||||
|
||||
do i=1 for N /*gen N random numbers ──► OLD*/
|
||||
@.i=random(1,99999) /*randum number 1 ──► 99999 */
|
||||
do i=1 for N /*generate N random numbers ──► OLD */
|
||||
@.i=random(1,99999) /*generate a random number 1 ──► 99999 */
|
||||
end /*i*/
|
||||
|
||||
do j=1 for N /*process the OLD array elements.*/
|
||||
if @.j//2 \==0 then @.j= /*mark @ array that it's ¬even.*/
|
||||
do j=1 for N /*process the OLD array elements. */
|
||||
if @.j//2 \==0 then @.j= /*mark the @ array that it's not even*/
|
||||
end /*j*/
|
||||
|
||||
do k=1 for N /*display all the @ even numbers.*/
|
||||
if @.k=='' then iterate /*if it's marked ¬even, skip it.*/
|
||||
say right('array.'k, 20) "=" right(@.k,9) /*show a line*/
|
||||
end /*k*/
|
||||
/*stick a fork in it, we're done.*/
|
||||
do k=1 for N /*display all the @ even numbers. */
|
||||
if @.k=='' then iterate /*if it's marked not even, then skip it*/
|
||||
say right('array.'k, 20) "=" right(@.k,9) /*display a line (an array element). */
|
||||
end /*k*/ /*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
14
Task/Filter/ZX-Spectrum-Basic/filter.zx
Normal file
14
Task/Filter/ZX-Spectrum-Basic/filter.zx
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
10 LET items=100: LET filtered=0
|
||||
20 DIM a(items)
|
||||
30 FOR i=1 TO items
|
||||
40 LET a(i)=INT (RND*items)
|
||||
50 NEXT i
|
||||
60 FOR i=1 TO items
|
||||
70 IF FN m(a(i),2)=0 THEN LET filtered=filtered+1: LET a(filtered)=a(i)
|
||||
80 NEXT i
|
||||
90 DIM b(filtered)
|
||||
100 FOR i=1 TO filtered
|
||||
110 LET b(i)=a(i): PRINT b(i);" ";
|
||||
120 NEXT i
|
||||
130 DIM a(1): REM To free memory (well, almost all)
|
||||
140 DEF FN m(a,b)=a-INT (a/b)*b
|
||||
Loading…
Add table
Add a link
Reference in a new issue