sam-w
sam-w

Reputation: 7687

Given an element in a sequence, how to get the previous element?

Let's say I have a Scala list List("apple", "orange", "banana", "chinese gooseberry")*. I want to search this list and return either the previous item in the list in relation to an item I already have.

For example: getPrevious(fruit: String, fruits: List[String]): Option[String] should return

Easily done imperatively, but how can I do this in an elegant functional manner? The best I can come up with is the following:

def previous(fruit: String, fruits: List[String]): Option[String] =
  fruits.sliding(2)
  .filter { case List(previous, current) => current == fruit }
  .toList
  .headOption
  .map { case List(previous, current) => previous }

It works, but it isn't elegant or efficient. I particularly hate converting the filter iterator toList. How can I improve it?

(*as an aside, is List the best collection to use for a sliding iteration?)

Upvotes: 6

Views: 3033

Answers (3)

stew
stew

Reputation: 11366

I think this is a case where just straight up recursion and pattern matching is both efficient and easier to read:

@annotation.tailrec
def getPrevious(fruit: String, fruits: List[String]): Option[String] = fruits match  {
  case Nil               => None
  case x :: `fruit` :: _ => Some(x)
  case _ :: xs           => getPrevious(fruit, xs)
}

Upvotes: 8

Michael Zajac
Michael Zajac

Reputation: 55569

Here's a shorter version using collectFirst:

def previous(fruit: String, fruits: List[String]): Option[String] =
    fruits.sliding(2).collectFirst{ case List(previous, `fruit`) => previous}

Note the backticks surrounding fruit to match the parameter value. Using collectFirst will also stop at the first match, rather than running through the entire iterator with filter.

Upvotes: 14

Kigyo
Kigyo

Reputation: 5768

The first simple solution that comes to mind is:

import scala.util.Try
def previous(fruit: String, fruits: List[String]) = Try(fruits(fruits.indexOf(fruit) - 1)).toOption

There should certainly be more efficient ones.

Upvotes: 1

Related Questions