samedi 4 juillet 2020

How to implement this in functional programming using Function

I have a scenario which in imperative programming would like this

processEvent(Event event) {
  if ( isEventOfSpecificType(event) ){ .............................................. 1

     Result result = findSomeData(event.someProperty()) ............................. 2
     if(result.someStatus == somevalue) { ........................................... 3

         saveEvent(event, result) // save to DB with properties from both objects ... 4
         String referenceID = createSomeRecord(event, result) // Will do API call ... 5
         writeToLogFile(event, result, referenceID) // write to a logfile ........... 6
     }
  }
}

I am trying to wrap my head around functional programming in JAVA using Function, Predicate, Consumer, and Supplier.

My first thought: use streams to take advantage of filter and map, but since there is no collection in this scenario it didn't seem appropriate to create a stream for one object.

My second thought: to use Function and curry, but I am lost. Not able to get my head around it since I need all the three objects in my last step.

How can this be done in a functional way. What are some of the best references to understand this topic.

Aucun commentaire:

Enregistrer un commentaire