scalaz - Scala iterate over a set of objects -


I have a sealed feature that has been applied by 3 objects

  Seal attribute MyTract {. ..} Object extends A MyTrait {...} Object B extends MyTrait {...} Object C extends MyTrait {...}  

I am using Scalez's Verification Mechanism in which the methods of Object A, B and C can be reverted to a valid type. There is a logic in Objects A, B and C and I want to apply this argument sequentially, that is, I want to implement A first and the result of A and check on the basis of it, decide whether I just want to call B or just return valid results, I want to repeat it until I hit C, after which I just return whatever I get as a result of calling C Not.

Currently I have a stable approach where I am saying first, check the utility method and results and then call B

  def applied (request : Request): Valid [result] = {val vResultA = run (request, a) val vResultB = if ((request, c) other vResultB}  

What a better way to do this (Request, br) Other vResultA if run (isResultOk (vResultB)) (request, c) other vResultB} Any suggestions or any patterns that I can apply?

< / Div>

We will define successful results = results , and unsuccessful results = results Not right .

First, a , b , and c MyTrait . Therefore, they can be categorized into an array or a list of MyTrait .Vol objects = array (A, B, C) / * You list instead You can use if you want. * /

then the objects HE Government Array [Maytret] is .

Next, let us repeat this array.
Still, continue to call map on this array, even if the previous isResultOk () is false . array instead of stream .

See how the call can stop using stream map if any condition is satisfied.

  array (1, 2, 3, 4, 5) .map (i = & gt; {println (i) i + 100}) .Takues (_ and lieutenant; = 103) . The foreground code will be produced:  
  1 2 3 4 5 101 102 103  

Then, ends the map () , and then is taking () ends - take () calling Does not affect map () .
However, if we do the same thing on stream ,

, the  array (1, 2, 3, 4, 5) .stream.map (i = & Gt; {println (i) I + 100}). Take the (_ & lt; = 103) .foreach (println (_))  

output will be:

  1 101 2 102 3 103 4  

then calling will map () - & gt; Take while () - & gt; Foreign () - & gt; Map () - & gt; Take while () - & gt; ...
Finally, 4 has been printed, and 4 + 100 = 104> 103 will be moved to while Lieutenant () .
The following elements will not be

So, what do we have to use while taking it?

  take objects.toStream.map (run (request, _)), despite being unsuccessful (isResultOk (_)) due to the first unsuccessful results being required, get rid of these unsuccessful results Will find  
.)

How the opposite work dropWhile () / Code>?

  about objects.toStream.map (run (request, _)). (IsResultOk (_))  

This will get rid of all successful results, even if all results are successful.
(i.e., will cause problems if all these problems are fixed.)

Then, we will use the span () . c.span (p) = (c.takeWhile (p), C.dropwhile (p))
We will examine the results of whether the results are not correct.
If there is a result that is not good, then we will give back the first result like this. Otherwise, we will return the final result which is fine. Val (succ, fail) = objects.toStream.map (run (request), _)) span (isResultOk (_)) fail.headOption.getOrElse (succ.last)

fail.headOption back some (the first element of the failure) if failed is not empty, else None .

  Val objects = ARR (A, B, C) DRF (request: request): valid [result] = {val (succ, fail) = Objects .stream.map (run (request, _) span (isResultOk) (_)) Fail.headOption.getOrElse (succ.last)}  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -