This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Implicitly converting a TupleN[A, .... , A] to a Traversable[A]

3 replies
Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
I can write an implicit conversion from a Pair[A, A] to a Traversable[A] and then extend this to the other tuples. 
 - Is there some reason why this is a bad idea? - Is there some reason why the standard library does not provider this conversion already? 
I keep finding that I want to partition a collection, apply some function to either side of the partition (thankyou, scalaz Bifunctor) and then join the partition back up again to get a single result. E.g.
List[A] -> partition -> (List[A], List[A]) -> bimap -> (B, B) -> treat as a list of B's (find, exists, sum etc)
Obviously I can see that not all of Traversable's methods make any sense for a Tuple but this still seems like a shame to me.
Chris
Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Implicitly converting a TupleN[A, .... , A] to a Traversab
Is there a use case for Tuple3 and on?

On Fri, Jul 30, 2010 at 10:01 AM, christopher marshall <oxbow_lakes [at] hotmail [dot] com> wrote:
I can write an implicit conversion from a Pair[A, A] to a Traversable[A] and then extend this to the other tuples. 
 - Is there some reason why this is a bad idea? - Is there some reason why the standard library does not provider this conversion already? 
I keep finding that I want to partition a collection, apply some function to either side of the partition (thankyou, scalaz Bifunctor) and then join the partition back up again to get a single result. E.g.
List[A] -> partition -> (List[A], List[A]) -> bimap -> (B, B) -> treat as a list of B's (find, exists, sum etc)
Obviously I can see that not all of Traversable's methods make any sense for a Tuple but this still seems like a shame to me.
Chris

Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
RE: Implicitly converting a TupleN[A, .... , A] to a Traversabl
I'm sure that it's every bit as reasonable to end up with a (A,A,A) which needs to be viewed as Traversable as a (A, A). In the end I used a Scalaz conversion to Tuple2W which contains a fold method:
Tuple2W[A, B].fold(Function2[A, B, C]) : C

This was just what I wanted
Chris
Date: Sun, 1 Aug 2010 17:24:01 -0400
Subject: Re: [scala-debate] Implicitly converting a TupleN[A, .... , A] to a Traversable[A]
From: naftoligug [at] gmail [dot] com

Is there a use case for Tuple3 and on?

On Fri, Jul 30, 2010 at 10:01 AM, christopher marshall <oxbow_lakes [at] hotmail [dot] com> wrote:
I can write an implicit conversion from a Pair[A, A] to a Traversable[A] and then extend this to the other tuples. 
 
Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Implicitly converting a TupleN[A, .... , A] to a Traversab

For the record, Scalaz also has conversion from TupleN[A, ... A] =>
IndexedSeq[A].

(1, 2, 3).toIndexedSeq

This isn't implicit.

-jason

On Mon, Aug 2, 2010 at 9:24 AM, christopher marshall
wrote:
> I'm sure that it's every bit as reasonable to end up with a (A,A,A) which
> needs to be viewed as Traversable as a (A, A). In the end I used a Scalaz
> conversion to Tuple2W which contains a fold method:
>
> Tuple2W[A, B].fold(Function2[A, B, C]) : C
>
> This was just what I wanted
> Chris

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland