FAQ
Hi all.   I would like to know the size of a list returned by some expression
while iterating over it, without having to introduce a tmp-var for it.   This
is what I came up with:  scala> List(1,2,3,4,5) match {case l =>
l.zipWithIndex.foreach{case (e, index) => if (index > l.size - 2)
println(s"Doing only for last element: $e"); println(s"Doing anyway $e")}}
  Doing anyway 1
  Doing anyway 2
  Doing anyway 3
  Doing anyway 4
  Doing only for last element: 5
  Doing anyway 5   Is there any better way to do this, using plain Scala-libs?
Thanks.   --
  Andreas Joseph Krogh <andreak@officenet.no>      mob: +47 909 56 963
  Senior Software Developer / CTO - OfficeNet AS - http://www.officenet.no
  Public key: http://home.officenet.no/~andreak/public_key.asc

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Som Snytt at Feb 2, 2014 at 11:45 pm
    scala> is.foldLeft(0, is.size) { case ((i, n), x) =>
    println(if (i+1==n) s"$x last" else x); (i+1,n)}
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10 last
    res2: (Int, Int) = (10,10)

    scala> is.reverse.zipAll(Option(true), -1, false).reverse.foreach {
    case (i, true) => println(s"Done at $i")
    case (i, _) => println(i) }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Done at 10




    On Sun, Feb 2, 2014 at 2:43 PM, Andreas Joseph Krogh
    wrote:
    Hi all.

    I would like to know the size of a list returned by some expression while
    iterating over it, without having to introduce a tmp-var for it.

    This is what I came up with:

    scala> List(1,2,3,4,5) match {case l => l.zipWithIndex.foreach{case (e,
    index) => if (index > l.size - 2) println(s"Doing only for last element:
    $e"); println(s"Doing anyway $e")}}
    Doing anyway 1
    Doing anyway 2
    Doing anyway 3
    Doing anyway 4
    Doing only for last element: 5
    Doing anyway 5

    Is there any better way to do this, using plain Scala-libs?

    Thanks.

    --
    Andreas Joseph Krogh <andreak@officenet.no> mob: +47 909 56 963
    Senior Software Developer / CTO - OfficeNet AS - http://www.officenet.no
    Public key: http://home.officenet.no/~andreak/public_key.asc

    --
    You received this message because you are subscribed to the Google Groups
    "scala-user" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to scala-user+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "scala-user" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupscala-user @
categoriesscala
postedFeb 2, '14 at 10:44p
activeFeb 2, '14 at 11:45p
posts2
users2
websitescala-lang.org
irc#scala

2 users in discussion

Som Snytt: 1 post Andreas Joseph Krogh: 1 post

People

Translate

site design / logo © 2021 Grokbase