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


Groups > comp.lang.forth > #44368

Re: Deep counting

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Re: Deep counting
Date 2015-12-27 05:49 +0000
Organization A noiseless patient Spider
Message-ID <n5nu4b$9cu$1@dont-email.me> (permalink)
References <mf4beb$avu$1@dont-email.me>

Show all headers | View raw


WJ wrote:

> Count the number of times an item is found in nested
> lists.
> 
> Oforth:
>  
> func: tally( item, list )
> {| x |
>   0 list
>   forEach: x [
>     x isA( List )
>     ifTrue: [ tally( item, x ) ]
>     else: [ x item == abs ]
>     + ] }
> 
> [0, [2, 3, [4, 2, 8], 2], 7] tally( 2 ) println
>  ===>
> 3
> 
> [0, [2, 3, [4, 2, 8], 2], 7, 2] tally( 2 ) println
>  ===>
> 4

Ruby:

def tally item, array
  array.reduce(0){|count,x|
    count + 
      if x == item
        1
      elsif x.is_a? Array
        tally item, x
      else
        0
      end}
end

tally 2, [0, [2, 3, [4, 2, 8], 2], 7, 2]
 ===>
4

tally [4,2,8], [0, [2, 3, [4, 2, 8], 2], 7, 2]
 ===>
1

-- 
Amazon bans book.  After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunkers-5.html
https://www.youtube.com/watch?v=EEl_1HWFRfo 

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


Thread

Re: Deep counting "WJ" <w_a_x_man@yahoo.com> - 2015-12-27 05:49 +0000

csiph-web