0

I have a check(a, b, c) method that checks if a is in range between b and c (a >= b && a <= c). For a given lists, for example, v = List(1,2) and r = List((3,4),(5,6)); I'd like to check if all values r is in ranges from r using the check method: check(1, 3, 4) && check (2, 5, 6).

I have high level solution as follows, but I have some missing parts.

val x = v zip r // (Int, (Int, Int)) 
val y = ???     // (Int, (Int, Int)) => (Int, Int, Int)
(y map check).forall {_ == true} // error 

How can I get the solution?

1 Answer 1

5

How about just directly calling forall?

(v zip r).forall{case (a,(b,c)) => check(a,b,c)}

One way to make your approach work (I would not recommend it though).

val x = v zip r
val y = x map {case (a,(b,c)) => (a,b,c)}
val tupledCheck = (check _).tupled

//Some alternatives for the result
(y map tupledCheck).forall(_ == true)
(y map tupledCheck).forall(identity)
y forall tupledCheck
Sign up to request clarification or add additional context in comments.

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.