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


Groups > comp.lang.forth > #20350 > unrolled thread

SP-Forth: reduce or fold a list

Started by"WJ" <w_a_x_man@yahoo.com>
First post2013-03-06 18:11 +0000
Last post2013-03-06 23:50 +0000
Articles 4 — 2 participants

Back to article view | Back to comp.lang.forth


Contents

  SP-Forth: reduce or fold a list "WJ" <w_a_x_man@yahoo.com> - 2013-03-06 18:11 +0000
    Re: SP-Forth: reduce or fold a list "Charles Childers" <crc@retroforth.org> - 2013-03-06 16:42 -0500
    Re: SP-Forth: reduce or fold a list "WJ" <w_a_x_man@yahoo.com> - 2013-03-06 23:24 +0000
      Re: SP-Forth: reduce or fold a list "WJ" <w_a_x_man@yahoo.com> - 2013-03-06 23:50 +0000

#20350 — SP-Forth: reduce or fold a list

From"WJ" <w_a_x_man@yahoo.com>
Date2013-03-06 18:11 +0000
SubjectSP-Forth: reduce or fold a list
Message-ID<kh80t5$ec8$1@dont-email.me>
REQUIRE list-all ~ygrek/lib/list/all.f
list ALSO!
REQUIRE RANDOMIZE ~af/lib/random.f

0 VALUE biglist

: reduce-test
  \ Build list of 9999 random integers.
  %[ 9999 0 DO  5000 CHOOSE %  LOOP ]%
  TO biglist
  S" Length of list is " TYPE  biglist length .
  \ Sum the list.
  0 biglist ['] + iter
  CR S" Sum of list is " TYPE  .
  biglist free
;

reduce-test
Length of list is 9999
Sum of list is 25009246  Ok

[toc] | [next] | [standalone]


#20358

From"Charles Childers" <crc@retroforth.org>
Date2013-03-06 16:42 -0500
Message-ID<op.wtjnwvkt6ef20b@denney-2466a6df>
In reply to#20350
Parable:

"Build an array of 9999 integers"
[ #1 #9999 range-expand ] array-from-quote

"Get the length of the array"
[ array-length ] sip

"And sum them"
#0 [ + ] array-reduce

This will work in Retro in the near future: I'm currently rewriting the  
array
functions to make them more flexible and backporting functionality from  
Parable
in the process.

-- crc

[toc] | [prev] | [next] | [standalone]


#20372

From"WJ" <w_a_x_man@yahoo.com>
Date2013-03-06 23:24 +0000
Message-ID<kh8j7q$q72$1@dont-email.me>
In reply to#20350
WJ wrote:

> REQUIRE list-all ~ygrek/lib/list/all.f
> list ALSO!
> REQUIRE RANDOMIZE ~af/lib/random.f
> 
> 0 VALUE biglist
> 
> : reduce-test
>   \ Build list of 9999 random integers.
>   %[ 9999 0 DO  5000 CHOOSE %  LOOP ]%
>   TO biglist
>   S" Length of list is " TYPE  biglist length .
>   \ Sum the list.
>   0 biglist ['] + iter
>   CR S" Sum of list is " TYPE  .
>   biglist free
> ;
> 
> reduce-test
> Length of list is 9999
> Sum of list is 25009246  Ok


Factor:

USING: random ; 

9999 iota [ drop 5000 random ] map
"Length of list is " write dup length .
0 [ + ] reduce
"Sum of list is " write .

Length of list is 9999
Sum of list is 25051634

[toc] | [prev] | [next] | [standalone]


#20376

From"WJ" <w_a_x_man@yahoo.com>
Date2013-03-06 23:50 +0000
Message-ID<kh8knb$19b$1@dont-email.me>
In reply to#20372
WJ wrote:

> WJ wrote:
> 
> > REQUIRE list-all ~ygrek/lib/list/all.f
> > list ALSO!
> > REQUIRE RANDOMIZE ~af/lib/random.f
> > 
> > 0 VALUE biglist
> > 
> > : reduce-test
> >   \ Build list of 9999 random integers.
> >   %[ 9999 0 DO  5000 CHOOSE %  LOOP ]%
> >   TO biglist
> >   S" Length of list is " TYPE  biglist length .
> >   \ Sum the list.
> >   0 biglist ['] + iter
> >   CR S" Sum of list is " TYPE  .
> >   biglist free
> > ;
> > 
> > reduce-test
> > Length of list is 9999
> > Sum of list is 25009246  Ok
> 
> 
> Factor:
> 
> USING: random ; 
> 
> 9999 iota [ drop 5000 random ] map
> "Length of list is " write dup length .
> 0 [ + ] reduce
> "Sum of list is " write .
> 
> Length of list is 9999
> Sum of list is 25051634

"reduce" isn't even needed; one can simply use "each"
(equivalent to "iter" in the SP-Forth code):

0 { 1 3 5 7 } [ + ] each .
16

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web