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


Groups > comp.lang.forth > #23634

Re: Duplic8

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Re: Duplic8
Date 2013-06-15 00:37 +0000
Organization A noiseless patient Spider
Message-ID <kpgd0t$pe0$1@dont-email.me> (permalink)
References <kjhge2$ujr$1@dont-email.me>

Show all headers | View raw


WJ wrote:

> > a problem here : I need to define a function 
> > which duplicates every occurance of a given 
> > element  in a given list. Suppose this funtion   
> > was called DUPLIC8, then  the call 
> > 
> >         (DUPLIC8 '(A B C D E B) 'B)) 
> > 
> > should produce (A B B C D E B B).
> 
> Factor:
> 
> USING: locals ;
> 
> :: duplic8 ( seq e -- seq )
>   { } seq [| x |  x e = [ x suffix ] when  x suffix ] each ;
> 
> { "A" "B" "C" "D" "E" "B" } "B" duplic8 .
> 
> { "A" "B" "B" "C" "D" "E" "B" "B" }

Ruby:

def duplicate ary, which
  ary.reduce([]){|a,x|
    a + (if x == which then [x,x] else [x] end)}
end

duplicate [2,3,4,5,3], 3
    ==>[2, 3, 3, 4, 5, 3, 3]

Can this feasibly be done in Forth?
Or is Forth merely a stone-age toy?

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


Thread

Duplic8 "WJ" <w_a_x_man@yahoo.com> - 2013-04-03 15:04 +0000
  Re: Duplic8 "WJ" <w_a_x_man@yahoo.com> - 2013-06-15 00:37 +0000

csiph-web