Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -0,0 +1,11 @@
filter pred lst:
]
for value in copy lst:
if pred @value:
@value
[
even x:
= 0 % x 2
!. filter @even [ 0 1 2 3 4 5 6 7 8 9 ]

View file

@ -0,0 +1,16 @@
local :lst [ 0 1 2 3 4 5 6 7 8 9 ]
filter-destructively pred lst:
local :tmp []
while lst:
pop-from lst
if pred dup:
push-to tmp
else:
drop
while tmp:
push-to lst pop-from tmp
filter-destructively @even lst
!. lst

View file

@ -0,0 +1,63 @@
/* NetRexx */
options replace format comments java crossref symbols nobinary
numeric digits 5000
-- =============================================================================
class RFilter public
properties indirect
filter = RFilter.ArrayFilter
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method main(args = String[]) public static
arg = Rexx(args)
RFilter().runSample(arg)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) public
sd1 = Rexx[]
sd2 = Rexx[]
say 'Test data:'
sd1 = makeSampleData(100)
display(sd1)
setFilter(RFilter.EvenNumberOnlyArrayFilter())
say
say 'Option 1 (copy to a new array):'
sd2 = getFilter().filter(sd1)
display(sd2)
say
say 'Option 2 (replace the original array):'
sd1 = getFilter().filter(sd1)
display(sd1)
return
-- ---------------------------------------------------------------------------
method display(sd = Rexx[]) public static
say '-'.copies(80)
loop i_ = 0 to sd.length - 1
say sd[i_] '\-'
end i_
say
return
-- ---------------------------------------------------------------------------
method makeSampleData(size) public static returns Rexx[]
sd = Rexx[size]
loop e_ = 0 to size - 1
sd[e_] = (e_ + 1 - size / 2) / 2
end e_
return sd
-- =============================================================================
class RFilter.ArrayFilter abstract
-- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
method filter(array = Rexx[]) public abstract returns Rexx[]
-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
class RFilter.EvenNumberOnlyArrayFilter extends RFilter.ArrayFilter
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
method filter(array = Rexx[]) public returns Rexx[]
clist = ArrayList(Arrays.asList(array))
li = clist.listIterator()
loop while li.hasNext()
e_ = Rexx li.next
if \e_.datatype('w'), e_ // 2 \= 0 then li.remove()
end
ry = Rexx[] clist.toArray(Rexx[clist.size()])
return ry

View file

@ -0,0 +1,28 @@
(subscriptrange):
filter_values: procedure options (main); /* 15 November 2013 */
declare a(20) fixed, b(*) fixed controlled;
declare (i, j, n) fixed binary;
a = random()*99999; /* fill the array with random elements from 0-99998 */
put list ('Unfiltered values:');
put skip edit (a) (f(6));
/* Loop to count the number of elements that will be filtered */
n = 0;
do i = 1 to hbound(a);
n = n + filter(a(i));
end;
allocate b(n);
j = 0;
do i = 1 to hbound(a);
if filter(a(i)) then do; j = j + 1; b(j) = a(i); end;
end;
put skip list ('Filtered values:');
put skip edit (b) (f(6));
filter: procedure (value) returns (bit(1));
declare value fixed;
return (iand(abs(value), 1) = 0);
end filter;
end filter_values;

View file

@ -0,0 +1,20 @@
/*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 */
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. */
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.*/

View file

@ -0,0 +1 @@
if old.j//2 \== 0 then iterate

View file

@ -0,0 +1 @@
if old.j//2 then iterate

View file

@ -0,0 +1,18 @@
/*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.*/
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 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.*/

View file

@ -0,0 +1,18 @@
/*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.*/
do i=1 for N /*gen N random numbers ──► OLD*/
@.i=random(1,99999) /*randum 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.*/
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.*/

View file

@ -1,26 +0,0 @@
/*REXX program selects all even numbers from an array ──► a new array. */
numeric digits 210000 /*handle past 1m for Fibonacci.*/
old.= /*the OLD array, all null so far.*/
new.= /*the NEW array, all null so far.*/
do j=-40 to 40; old.j=fib(j); end /*put 81 Fibonacci numbs ==> OLD */
news=0 /*numb. of elements in NEW so far*/
/*══════════════════════════════════════════════════════════════════════*/
do k=-40 while old.k\=='' /*process the OLD array elements.*/
if old.k//2 \== 0 then iterate /*if element isn't even, skip it.*/
news=news+1 /*bump the number of NEW elements*/
new.news=old.k /*assign it to the NEW array. */
end
/*══════════════════════════════════════════════════════════════════════*/
do j=1 for news /*display all the NEW numbers. */
say 'new.'j "=" new.j
end
exit /*stick a fork in it, we're done.*/
/*─────────────────────────────────────FIB subroutine (non-recursive)───*/
fib: procedure; parse arg n; na=abs(n); if na<2 then return na /*special*/
a=0; b=1
do j=2 to na; s=a+b; a=b; b=s; end
if n>0 | na//2==1 then return s /*if positive or odd negative... */
else return -s /*return a negative Fib number. */

View file

@ -3,6 +3,3 @@ unset e[@]
for ((i=0;i<${#a[@]};i++)); do
[ $((a[$i]%2)) -eq 0 ] && e[$i]="${a[$i]}"
done
echo "${a[@]}"
echo "${e[@]}"

View file

@ -0,0 +1,2 @@
a=(1 2 3 4 5)
read -a e -d\n < <(printf '%s\n' "${a[@]}" | grep '[02468]$')

View file

@ -0,0 +1,2 @@
echo "${a[@]}"
echo "${e[@]}"