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.




 
 
{def filter
 {lambda {:bool :a}
  {if {S.empty? {S.rest :a}}
   then {:bool {S.first :a}}
   else {:bool {S.first :a}}
        {filter :bool {S.rest :a}}}}}
 
{def even? {lambda {:w} {if {= {% :w 2} 0} then :w else}}}
{def odd?  {lambda {:w} {if {= {% :w 2} 1} then :w else}}}
 
{filter even? {S.serie 1 20}}
-> 2  4  6  8  10  12  14  16  18  20
{filter odd? {S.serie 1 20}}
-> 1  3  5  7  9  11  13  15  17  19
 
 