Scala vs Java programming language

Moderator: Concepts and Technologies for DS and BDP

rb76jaty
Neuling
Neuling
Beiträge: 1
Registriert: 10. Jul 2017 19:13

Scala vs Java programming language

Beitrag von rb76jaty »

In Excercise 4, we have to use .flatMap or .filter to simplify the given code. However, the syntax used in the solution is very different from the one observed on the slides and I'm having trouble understanding which syntax I should focus on. This also makes it difficult to digest the contents learned in class.
How would the following code be written using the syntax on the slides (Lecture 8)?

Code: Alles auswählen

(IO.read("filename.json")
  flatMap JSON.parse
  filter { !_.hasProperty(Ignore) }
  map someComplexComputation
  onComplete {
    case Success(result) =>
      println(s"final result: $result")
    case Failure(_) =>
      println("could not compute result")
   }
)

JLP
Moderator
Moderator
Beiträge: 35
Registriert: 19. Apr 2010 15:45

Re: Scala vs Java programming language

Beitrag von JLP »

The code should be syntactically similar to what was shown in the lecture; except maybe for the infix notation of some combinators. The same code using dots and brackets looks like the following:

Code: Alles auswählen

(IO.read("filename.json")
  .flatMap(JSON.parse)
  .filter { !_.hasProperty(Ignore) }
  .map(someComplexComputation)
  .onComplete {
    case Success(result) =>
      println(s"final result: $result")
    case Failure(_) =>
      println("could not compute result")
   }
 )

Benutzeravatar
Malou
Windoof-User
Windoof-User
Beiträge: 26
Registriert: 19. Jun 2016 17:54

Re: Scala vs Java programming language

Beitrag von Malou »

Hey,

I was wondering if following code could work as well... Since this is the structure seen in the lecture:

Code: Alles auswählen

val content = IO.read("testEx04.json")
val processed = content.flatMap { content =>
    val json = JSON.parse(content)
    json.map { json =>
            val result = Future {
                someComplexComputation(json.filter{ !json.hasProperty(Ignore) }
            }
            result.map { result =>
                println(s"final result: $result")
            }
    }
}
processed.onComplete {
    case Failure(_) => 
        println("could not compute result")
}
I hope I'm not too late to have an answer before the exam :roll:

Thank's in advance

Malou

salvaneschi
Moderator
Moderator
Beiträge: 49
Registriert: 29. Mär 2013 23:51

Re: Scala vs Java programming language

Beitrag von salvaneschi »

" was wondering if following code could work as well... "

Did you try to run it?

Benutzeravatar
Malou
Windoof-User
Windoof-User
Beiträge: 26
Registriert: 19. Jun 2016 17:54

Re: Scala vs Java programming language

Beitrag von Malou »

salvaneschi hat geschrieben:Did you try to run it?
Yes, but I couldn't run it because IO.read and JSON.parse are methods of an external library (not mentioned in the exercise). Maybe akka?

Anyway, I'm pretty sure that it would work (since I've done some other tests) except for the filter part:

Code: Alles auswählen

someComplexComputation(json.filter{ !json.hasProperty(Ignore) }
So the proposed solution is a better option in this case I think.

Malou

Antworten

Zurück zu „Archiv“