0

I'm new to Scala and trying to write some programs to get better at it. I wrote a flow (version 1) that is very Java-like and I'm trying to write it using higher order functions (version 2).

version 1:

    val entry: Option[Int] = getEntry()

    if (entry.isDefined) {
      val cachedEntry = entry.get
      if (cachedEntry.state.isActive) {
        return cachedEntry
      } else {
        Cache.invalidateCachedEntry(cachedEntry)
      }
    }

    Cache.createNewEntry()

version 2:

    val entry: Option[Int] = getEntry()

    entry.filter(_.state.isActive).orElse((() => {
      Cache.invalidateCachedEntry _
      Option(Cache.createNewEntry())
    })()).get

I'm not sure if this is the correct approach or there is a better way of doing this?

5
  • can you post the structure of getEntryResponse. Other your explanation is wrong. Because, above you define that entry is optional Int, but later you cal .state.isActive. Commented Apr 8, 2020 at 4:11
  • The two don't appear to be quite the same. If entry is None then V1 does not call Cache.invalidateCachedEntry() but in V2 it does. Commented Apr 8, 2020 at 4:15
  • what do you need to return as a result at the very end? Commented Apr 8, 2020 at 4:18
  • @Bob 1) that was a typo, it should've been cachedEntry.state.isActive. I've updated the question. 2) I need to return an integer value (returned by createNewEntry(), not an optional value) Commented Apr 8, 2020 at 5:53
  • @jwvh you are right, that is different. Though invalidateCachedEntry simply removes the cachedEntry from the map so it should be fine either way (should be just a no-op). Commented Apr 8, 2020 at 5:54

2 Answers 2

3

Let's consider following scenerio:

case class Entry(state: AnyState)
case class AnyState(isActive: Boolean = true)

object Cache {
  def invalidateCachedEntry(entry: Entry): Unit = println("cleaned")
}

def getEntry: Option[Entry] = Some(Entry(AnyState()))

val optEntry: Option[Entry] = getEntry

val result: Option[Entry] = optEntry match {
  case Some(entry) if entry.state.isActive =>
    entry // do something
     println("did something")
     Some(entry)
  case Some(entry) =>
    Cache.invalidateCachedEntry(entry)
    None
  case _ =>
   println("Entry not found")
   None
}


This would be a one scenario. In general you should return something. But sometimes you don't have enough information. Such cases you can return Option and if you want to throw an error you can use Either

Sign up to request clarification or add additional context in comments.

Comments

1

I prefer using match for clarity:

getEntry() match {
  case Some(entry) if entry.state.isActive => entry
  case opt => opt.foreach(Cache.invalidateCachedEntry); Cache.createNewEntry()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.