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

Sum of length of three Option[Array[T]]

4 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
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
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Sum of length of three Option[Array[T]]

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

edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
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>
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

Alec Zorab
Joined: 2010-05-18,
User offline. Last seen 42 years 45 weeks ago.
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
>
>

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
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:
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


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