- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Sum of length of three Option[Array[T]]
Fri, 2012-01-13, 10:20
Dear all,
I have three val which are Option[Array[T]]. What is the best way to sum their length? Clearly a None has length 0 :)
Thank you very much
Best Regards
Edmondo
I have three val which are Option[Array[T]]. What is the best way to sum their length? Clearly a None has length 0 :)
Thank you very much
Best Regards
Edmondo
Fri, 2012-01-13, 11:01
#2
Re: Sum of length of three Option[Array[T]]
I oversimplified, this is tipically my case.
trait Stuff[T<:Numeric] { def returnSomething:Int}class LotOfStuff(val doubles:Option[Stuff[Double]],val ints:Option[Stuff[Int]], val longs:Option[Stuff[Long]]) { // don't work, they are options val total = doubles.returnSomething + ints.returnSomething + longs.returnSomething;}
How do I use flatmap? I need to concatenate the three options in a List before...right?
Best Regards
2012/1/13 Dennis Haupt <h-star@gmx.de>
trait Stuff[T<:Numeric] { def returnSomething:Int}class LotOfStuff(val doubles:Option[Stuff[Double]],val ints:Option[Stuff[Int]], val longs:Option[Stuff[Long]]) { // don't work, they are options val total = doubles.returnSomething + ints.returnSomething + longs.returnSomething;}
How do I use flatmap? I need to concatenate the three options in a List before...right?
Best Regards
2012/1/13 Dennis Haupt <h-star@gmx.de>
flatmap(e => e).map(_.length).sum
what exactly are you doing, btw?
-------- Original-Nachricht --------
> Datum: Fri, 13 Jan 2012 10:19:59 +0100
> Von: Edmondo Porcu <edmondo.porcu@gmail.com>
> An: scala-user <scala-user@googlegroups.com>
> Betreff: [scala-user] Sum of length of three Option[Array[T]]
> Dear all,
> I have three val which are Option[Array[T]]. What is the best way to sum
> their length? Clearly a None has length 0 :)
>
> Thank you very much
> Best Regards
>
> Edmondo
Fri, 2012-01-13, 11:51
#3
Re: Sum of length of three Option[Array[T]]
You want to send a function into the option to work on the "stuff"
inside it, right?
the name for that is map, so you're looking at
val doublesSomething = doubles.map(_.returnSomething) //this is of
type Option[Int]
val intsSomething = //same thing
//etc
so now you can do a couple of things, but the interesting ones involve
scalaz, which I'm not going to recommend to you until you've got a
solid handle on the less abstract ways to handle things.
I think the simplest solution is likely to be
doublesSomething.getOrElse(0) + intSomethings.getOrElse(0) +
longsSomethings.getOrElse(0)
On 13 January 2012 09:59, Edmondo Porcu wrote:
> I oversimplified, this is tipically my case.
>
> trait Stuff[T<:Numeric] {
> def returnSomething:Int
> }
> class LotOfStuff(val doubles:Option[Stuff[Double]],val
> ints:Option[Stuff[Int]], val longs:Option[Stuff[Long]]) {
> // don't work, they are options val total = doubles.returnSomething +
> ints.returnSomething + longs.returnSomething;
> }
>
> How do I use flatmap? I need to concatenate the three options in a List
> before...right?
>
>
> Best Regards
>
>
> 2012/1/13 Dennis Haupt
>>
>> flatmap(e => e).map(_.length).sum
>>
>> what exactly are you doing, btw?
>>
>> -------- Original-Nachricht --------
>> > Datum: Fri, 13 Jan 2012 10:19:59 +0100
>> > Von: Edmondo Porcu
>> > An: scala-user
>> > Betreff: [scala-user] Sum of length of three Option[Array[T]]
>>
>> > Dear all,
>> > I have three val which are Option[Array[T]]. What is the best way to sum
>> > their length? Clearly a None has length 0 :)
>> >
>> > Thank you very much
>> > Best Regards
>> >
>> > Edmondo
>
>
Fri, 2012-01-13, 12:21
#4
Re: Sum of length of three Option[Array[T]]
val doubles = Option( Array(1.0, 2.0) )
val ints = Option( Array(0, -1) )
val longs = Option( Array(5000000000L) )
List(doubles, ints, longs).flatMap(_.map(_.length)).sum // Returns 5
--Rex
On Fri, Jan 13, 2012 at 4:59 AM, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
val ints = Option( Array(0, -1) )
val longs = Option( Array(5000000000L) )
List(doubles, ints, longs).flatMap(_.map(_.length)).sum // Returns 5
--Rex
On Fri, Jan 13, 2012 at 4:59 AM, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
I oversimplified, this is tipically my case.
trait Stuff[T<:Numeric] { def returnSomething:Int}class LotOfStuff(val doubles:Option[Stuff[Double]],val ints:Option[Stuff[Int]], val longs:Option[Stuff[Long]]) { // don't work, they are options val total = doubles.returnSomething + ints.returnSomething + longs.returnSomething;}
How do I use flatmap? I need to concatenate the three options in a List before...right?
Best Regards
2012/1/13 Dennis Haupt <h-star@gmx.de>
flatmap(e => e).map(_.length).sum
what exactly are you doing, btw?
-------- Original-Nachricht --------
> Datum: Fri, 13 Jan 2012 10:19:59 +0100
> Von: Edmondo Porcu <edmondo.porcu@gmail.com>
> An: scala-user <scala-user@googlegroups.com>
> Betreff: [scala-user] Sum of length of three Option[Array[T]]
> Dear all,
> I have three val which are Option[Array[T]]. What is the best way to sum
> their length? Clearly a None has length 0 :)
>
> Thank you very much
> Best Regards
>
> Edmondo
flatmap(e => e).map(_.length).sum
what exactly are you doing, btw?
-------- Original-Nachricht --------
> Datum: Fri, 13 Jan 2012 10:19:59 +0100
> Von: Edmondo Porcu
> An: scala-user
> Betreff: [scala-user] Sum of length of three Option[Array[T]]
> Dear all,
> I have three val which are Option[Array[T]]. What is the best way to sum
> their length? Clearly a None has length 0 :)
>
> Thank you very much
> Best Regards
>
> Edmondo