- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Preserving Seq types
Mon, 2011-12-19, 11:20
I'd like to have a method that takes a sequence (or monad) of some sort, and returns a sequence of the same sort:
def foo(list: Seq[T]): Seq[T] = list.filter(stuff)
If you give it a list, you get a list back; if you give it an option, you get an option, and so on. Is there any type trickery that makes this possible?
def foo(list: Seq[T]): Seq[T] = list.filter(stuff)
If you give it a list, you get a list back; if you give it an option, you get an option, and so on. Is there any type trickery that makes this possible?
Mon, 2011-12-19, 11:41
#2
Re: Preserving Seq types
Sure you can, use the same tricks as the collections framework.
But it can get a bit involved...
import collection.generic.CanBuildFrom
def foo[CC[x] <: Seq[x], T](xs: CC[T])(implicit cbf: CanBuildFrom[CC[T],T,CC[T]]): CC[T] = xs.map(identity)(collection.breakOut)
scala> foo(List(1,2,3)) res1: List[Int] = List(1, 2, 3)
On 19 December 2011 10:20, Marcus Downing <marcus.downing@gmail.com> wrote:
--
Kevin Wright
mail: kevin.wright@scalatechnology.com
gtalk / msn : kev.lee.wright@gmail.com quora: http://www.quora.com/Kevin-Wrightgoogle+: http://gplus.to/thecoda
kev.lee.wright@gmail.com twitter: @thecoda
vibe / skype: kev.lee.wrightsteam: kev_lee_wright
"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra
But it can get a bit involved...
import collection.generic.CanBuildFrom
def foo[CC[x] <: Seq[x], T](xs: CC[T])(implicit cbf: CanBuildFrom[CC[T],T,CC[T]]): CC[T] = xs.map(identity)(collection.breakOut)
scala> foo(List(1,2,3)) res1: List[Int] = List(1, 2, 3)
On 19 December 2011 10:20, Marcus Downing <marcus.downing@gmail.com> wrote:
I'd like to have a method that takes a sequence (or monad) of some sort, and returns a sequence of the same sort:
def foo(list: Seq[T]): Seq[T] = list.filter(stuff)
If you give it a list, you get a list back; if you give it an option, you get an option, and so on. Is there any type trickery that makes this possible?
--
Kevin Wright
mail: kevin.wright@scalatechnology.com
gtalk / msn : kev.lee.wright@gmail.com quora: http://www.quora.com/Kevin-Wrightgoogle+: http://gplus.to/thecoda
kev.lee.wright@gmail.com twitter: @thecoda
vibe / skype: kev.lee.wrightsteam: kev_lee_wright
"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra
Mon, 2011-12-19, 11:51
#3
Re: Preserving Seq types
this does only solve the type problem though.. option does not extend traversable, so you'll also need an implicit conversion form option to traversable for this to work.
and M should be "M <: Traversable[T]", but you'll figure that out ;)
-------- Original-Nachricht --------
> Datum: Mon, 19 Dec 2011 11:29:24 +0100
> Von: "Dennis Haupt"
> An: scala-user@googlegroups.com, scala-user@googlegroups.com
> Betreff: Re: [scala-user] Preserving Seq types
> i know, i know!
>
> def foo[T, M[T]](somethingWithSomething: M[T]): M[T] = {
> null.asInstanceOf[M[T]]
> }
>
> val bar: Some[String] = foo(Some("String"))
>
>
> -------- Original-Nachricht --------
> > Datum: Mon, 19 Dec 2011 02:20:09 -0800 (PST)
> > Von: Marcus Downing
> > An: scala-user@googlegroups.com
> > Betreff: [scala-user] Preserving Seq types
>
> > I'd like to have a method that takes a sequence (or monad) of some sort,
> > and returns a sequence of the same sort:
> >
> > def foo(list: Seq[T]): Seq[T] = list.filter(stuff)
> >
> > If you give it a list, you get a list back; if you give it an option,
> you
> > get an option, and so on. Is there any type trickery that makes this
> > possible?
Mon, 2011-12-19, 12:51
#4
Re: Preserving Seq types
Isn't the cast dangerous? Can I be absolutely certain that the type will always be correct?
Mon, 2011-12-19, 13:01
#5
Re: Preserving Seq types
the cast is in my code because i return null and for some reason i need to cast it (in contrast to java where null can just be assigned to any reference). in a real world use case, you should not need one.
traversable.filter(...) or whatever you want to do inside foo already returns the correct type (or should)
-------- Original-Nachricht --------
> Datum: Mon, 19 Dec 2011 03:45:53 -0800 (PST)
> Von: Marcus Downing
> An: scala-user@googlegroups.com
> Betreff: Re: [scala-user] Preserving Seq types
> Isn't the cast dangerous? Can I be absolutely certain that the type will
> always be correct?
Mon, 2011-12-19, 13:11
#6
Re: Preserving Seq types
So were you suggesting this:
def foo[T, M[T]](somethingWithSomething: M[T]): M[T] = {
sys.error("Todo")
}
val bar: Some[String] = foo(Some("String"))
?
Because that makes much more sense to me than casting nulls :)
On 19 December 2011 11:59, Dennis Haupt wrote:
> the cast is in my code because i return null and for some reason i need to cast it (in contrast to java where null can just be assigned to any reference). in a real world use case, you should not need one.
> traversable.filter(...) or whatever you want to do inside foo already returns the correct type (or should)
>
> -------- Original-Nachricht --------
>> Datum: Mon, 19 Dec 2011 03:45:53 -0800 (PST)
>> Von: Marcus Downing
>> An: scala-user@googlegroups.com
>> Betreff: Re: [scala-user] Preserving Seq types
>
>> Isn't the cast dangerous? Can I be absolutely certain that the type will
>> always be correct?
Mon, 2011-12-19, 13:21
#7
Re: Preserving Seq types
yes
-------- Original-Nachricht --------
> Datum: Mon, 19 Dec 2011 12:02:28 +0000
> Von: Alec Zorab
> An: Dennis Haupt
> CC: scala-user@googlegroups.com
> Betreff: Re: [scala-user] Preserving Seq types
> So were you suggesting this:
>
> def foo[T, M[T]](somethingWithSomething: M[T]): M[T] = {
> sys.error("Todo")
> }
>
> val bar: Some[String] = foo(Some("String"))
>
> ?
>
> Because that makes much more sense to me than casting nulls :)
>
> On 19 December 2011 11:59, Dennis Haupt wrote:
> > the cast is in my code because i return null and for some reason i need
> to cast it (in contrast to java where null can just be assigned to any
> reference). in a real world use case, you should not need one.
> > traversable.filter(...) or whatever you want to do inside foo already
> returns the correct type (or should)
> >
> > -------- Original-Nachricht --------
> >> Datum: Mon, 19 Dec 2011 03:45:53 -0800 (PST)
> >> Von: Marcus Downing
> >> An: scala-user@googlegroups.com
> >> Betreff: Re: [scala-user] Preserving Seq types
> >
> >> Isn't the cast dangerous? Can I be absolutely certain that the type
> will
> >> always be correct?
Mon, 2011-12-19, 17:01
#8
Re: Preserving Seq types
In general, not possible. See:
https://issues.scala-lang.org/browse/SI-5001
On 12/19/2011 02:20 AM, Marcus Downing wrote:
> I'd like to have a method that takes a sequence (or monad) of some sort,
> and returns a sequence of the same sort:
>
> def foo(list: Seq[T]): Seq[T] = list.filter(stuff)
>
> If you give it a list, you get a list back; if you give it an option,
> you get an option, and so on. Is there any type trickery that makes this
> possible?
i know, i know!
def foo[T, M[T]](somethingWithSomething: M[T]): M[T] = {
null.asInstanceOf[M[T]]
}
val bar: Some[String] = foo(Some("String"))
-------- Original-Nachricht --------
> Datum: Mon, 19 Dec 2011 02:20:09 -0800 (PST)
> Von: Marcus Downing
> An: scala-user@googlegroups.com
> Betreff: [scala-user] Preserving Seq types
> I'd like to have a method that takes a sequence (or monad) of some sort,
> and returns a sequence of the same sort:
>
> def foo(list: Seq[T]): Seq[T] = list.filter(stuff)
>
> If you give it a list, you get a list back; if you give it an option, you
> get an option, and so on. Is there any type trickery that makes this
> possible?