lundi 26 juin 2017

How should i decouple branches in Scala(FP)?

Currently i'm scrapping a url, based on url value i will need to scrap page A or B(maybe i will need C,D,E and more), to compose my object, it's like:

case class X( val1 : ..., val2: ..., a: Optional[A] = None , b: Optional[B] = None)
case class A(...)
case class B(...)

So i have this functions:

def scrapBody() : Future[TagNode] // referent to HtmlCleaner
def scrapA(): A = { ws.url(...).post(...).map(scrapBody).map(produceA) }
def scrapX : X = { 
 ...
 //Scrap val1, val2 ...
 val condition = ...
 condition match{
 case a if ... => scrapA() // Optional
 case b if ... => scrapB() // Optional
 case _ => None   
 }
 return X// 
}

// The main body
for {
  x <- ws.url(...).post(...).map(scrapBody).map(getAorB)
} yield x

I can't figure out, how i unwrap A and B from Future, if i use a onComplete i cannot return Some or None, and i didn't see a way using Neither, the value of these traits it's just in the end of a chain of computations . So how should i handle conditional branches in a functional way? Also wanna know if i'm using composition in a right way.

Thanks.

Aucun commentaire:

Enregistrer un commentaire