Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #44065

Re: Unfeasible?

From Bernd Paysan <bernd.paysan@gmx.de>
Newsgroups comp.lang.forth
Subject Re: Unfeasible?
Date 2015-12-04 03:23 +0100
Organization A noiseless patient Spider
Message-ID <n3qtba$6cm$1@dont-email.me> (permalink)
References <n3q89d$9d1$1@dont-email.me>

Show all headers | View raw


WJ wrote:

> One has a function "get_list" that produces a list.
> He has another function "test" that determines whether
> a member of the list passes muster.
> He has a function "foo" that is used to transform
> an element of the list.
> 
> The task is to iterate through the list while
> printing each element with its index and collecting
> the transformations of the elements that fit the bill.
> 
> Wouldn't this be painful in the Toy Language (ANS Forth)?
> 
> Ruby:
> 
> def get_list; ('aa'..'zz').step(36).to_a ; end
> def test s; s == s.reverse ; end
> def foo x; x.upcase ; end
> 
> get_list.each_with_index.
>   grep->((x,i)){printf "%2d - %s\n", i, x; test x}{|x,_| foo x}

No pain at all.  Forth:

: .aa ( n -- )  26 /mod 'a' + emit 'a' + emit ;
: reversible? ( n -- flag ) 26 /mod = ;
: gen 0 26 dup * 0 DO
     dup 2 .r ."  - " i .aa cr
     i reversible? IF  i ,  THEN  1+
  36 +LOOP  drop ;
: .gen ( -- ) here gen here swap ?DO  i @ .aa space cell +LOOP ;

cr .gen 
 0 - aa
 1 - bk
 2 - cu
 3 - ee
 4 - fo
 5 - gy
 6 - ii
 7 - js
 8 - lc
 9 - mm
10 - nw
11 - pg
12 - qq
13 - sa
14 - tk
15 - uu
16 - we
17 - xo
18 - yy
aa ee ii mm qq uu yy  ok

There's no need to first generate a list, and then to filter that list.  
Just generate the numbers and filter them right when they have been 
generated.

-- 
Bernd Paysan
"If you want it done right, you have to do it yourself"
net2o ID: kQusJzA;7*?t=uy@X}1GWr!+0qqp_Cn176t4(dQ*
http://bernd-paysan.de/

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Unfeasible? "WJ" <w_a_x_man@yahoo.com> - 2015-12-03 20:22 +0000
  Re: Unfeasible? Julian Fondren <julian.fondren@gmail.com> - 2015-12-03 16:17 -0800
    Re: Unfeasible? Paul Rubin <no.email@nospam.invalid> - 2015-12-03 17:47 -0800
      Re: Unfeasible? Julian Fondren <julian.fondren@gmail.com> - 2015-12-03 19:48 -0800
        Re: Unfeasible? Paul Rubin <no.email@nospam.invalid> - 2015-12-04 21:13 -0800
          Re: Unfeasible? Julian Fondren <julian.fondren@gmail.com> - 2015-12-04 23:48 -0800
            Re: Unfeasible? Paul Rubin <no.email@nospam.invalid> - 2015-12-05 00:11 -0800
              Re: Unfeasible? Julian Fondren <julian.fondren@gmail.com> - 2015-12-05 00:35 -0800
                Re: Unfeasible? Paul Rubin <no.email@nospam.invalid> - 2015-12-05 00:40 -0800
    Re: Unfeasible? hughaguilar96@gmail.com - 2015-12-04 20:53 -0800
  Was: Unfeasible? Is: Infeasible rickman <gnuarm@gmail.com> - 2015-12-03 19:18 -0500
  Re: Unfeasible? "WJ" <w_a_x_man@yahoo.com> - 2015-12-04 01:11 +0000
    Re: Unfeasible? rickman <gnuarm@gmail.com> - 2015-12-03 20:50 -0500
  Re: Unfeasible? Bernd Paysan <bernd.paysan@gmx.de> - 2015-12-04 03:23 +0100
  Re: Unfeasible? "WJ" <w_a_x_man@yahoo.com> - 2015-12-04 05:55 +0000
  Re: Unfeasible? Melzzzzz <mel@zzzzz.com> - 2015-12-06 01:01 +0100

csiph-web