# Applying a function on 2 elements in a Traversable

5 replies
edmondo1984
Joined: 2011-09-14,
Dear All,how do I have a f (A,A) => B and I want to apply to each 2 adjacent elements in a Traversble[A]
How do I do it?
traversable.sliding(2,1). and then?
Best RegardsEdmondo

Sciss
Joined: 2008-12-17,
Re: Applying a function on 2 elements in a Traversable

the problem of sliding is you get a Seq[Seq[...]]] and not Seq[Tuple2[...]], so you more fiddling. but i could think of this:

traversable.zip(traversable.drop(1)).map(f.tupled)

On 11 Jan 2012, at 13:44, Edmondo Porcu wrote:

> Dear All,
> how do I have a f (A,A) => B and I want to apply to each 2 adjacent elements in a Traversble[A]
>
> How do I do it?
>
> traversable.sliding(2,1). and then?
>
> Best Regards
> Edmondo
>
>

dcsobral
Joined: 2009-04-23,
Re: Applying a function on 2 elements in a Traversable

On Wed, Jan 11, 2012 at 11:44, Edmondo Porcu wrote:
> Dear All,
> how do I have a f (A,A) => B and I want to apply to each 2 adjacent elements
> in a Traversble[A]
>
> How do I do it?
>
> traversable.sliding(2,1). and then?

sequence sliding 2 map { case Seq(a, b) => f(a, b) }

If you want it on Iterable, then you have to do this:

iterable sliding 2 map (i => f(i.head, i.last))

There's no sliding on Traversable.

Sciss
Joined: 2008-12-17,
Re: Applying a function on 2 elements in a Traversable

traversable.zip(traversable.tail).map(f.tupled)

On 11 Jan 2012, at 14:04, Sciss wrote:

> the problem of sliding is you get a Seq[Seq[...]]] and not Seq[Tuple2[...]], so you more fiddling. but i could think of this:
>
> traversable.zip(traversable.drop(1)).map(f.tupled)
>
> On 11 Jan 2012, at 13:44, Edmondo Porcu wrote:
>
>> Dear All,
>> how do I have a f (A,A) => B and I want to apply to each 2 adjacent elements in a Traversble[A]
>>
>> How do I do it?
>>
>> traversable.sliding(2,1). and then?
>>
>> Best Regards
>> Edmondo
>>
>>
>

H-star Development
Joined: 2010-04-14,
Re: Applying a function on 2 elements in a Traversable

time to add "slideTuple2..n" to my enrichment lib

-------- Original-Nachricht --------
> Datum: Wed, 11 Jan 2012 14:06:33 +0000
> Von: Sciss
> An: Sciss
> CC: Edmondo Porcu , scala-user
> Betreff: Re: [scala-user] Applying a function on 2 elements in a Traversable

> traversable.zip(traversable.tail).map(f.tupled)
>
> On 11 Jan 2012, at 14:04, Sciss wrote:
>
> > the problem of sliding is you get a Seq[Seq[...]]] and not
> Seq[Tuple2[...]], so you more fiddling. but i could think of this:
> >
> > traversable.zip(traversable.drop(1)).map(f.tupled)
> >
> > On 11 Jan 2012, at 13:44, Edmondo Porcu wrote:
> >
> >> Dear All,
> >> how do I have a f (A,A) => B and I want to apply to each 2 adjacent
> elements in a Traversble[A]
> >>
> >> How do I do it?
> >>
> >> traversable.sliding(2,1). and then?
> >>
> >> Best Regards
> >> Edmondo
> >>
> >>
> >
>

Sciss
Joined: 2008-12-17,
Re: Applying a function on 2 elements in a Traversable

yeah, i have missed that, too, i think it's a fairly common case...

On 11 Jan 2012, at 14:09, Dennis Haupt wrote:

> time to add "slideTuple2..n" to my enrichment lib
>
> -------- Original-Nachricht --------
>> Datum: Wed, 11 Jan 2012 14:06:33 +0000
>> Von: Sciss
>> An: Sciss
>> CC: Edmondo Porcu , scala-user
>> Betreff: Re: [scala-user] Applying a function on 2 elements in a Traversable
>
>> traversable.zip(traversable.tail).map(f.tupled)
>>
>> On 11 Jan 2012, at 14:04, Sciss wrote:
>>
>>> the problem of sliding is you get a Seq[Seq[...]]] and not
>> Seq[Tuple2[...]], so you more fiddling. but i could think of this:
>>>
>>> traversable.zip(traversable.drop(1)).map(f.tupled)
>>>
>>> On 11 Jan 2012, at 13:44, Edmondo Porcu wrote:
>>>
>>>> Dear All,
>>>> how do I have a f (A,A) => B and I want to apply to each 2 adjacent
>> elements in a Traversble[A]
>>>>
>>>> How do I do it?
>>>>
>>>> traversable.sliding(2,1). and then?
>>>>
>>>> Best Regards
>>>> Edmondo
>>>>
>>>>
>>>
>>