What I have
I have a function below (I can't change outer function)
def outer(x: Int, inner: Int => Boolean): Boolean = {
inner(x)
false
}
What I want
Define inner function in such way that: if (x == 10) outer function return true
def inner(x: Int): Boolean = {
if (x == 10) OUTER_FUNCTION_SHOULD_RETURN_TRUE!!!
else false
}
outer(10, inner) // TRUE!!
Question
How can I do it?
Edit:
I use the next trick:
// If inner return true at least once, then outerWraper return true
def outerWrapper(x: Int, inner: Int => Boolean): Boolean = {
var flag = false
def inner2(e: Int): Boolean = {
if (!flag) flag = inner(e)
inner(e)
}
outer(x, p2)
flag
}
Can I avoid using var flag, but use val insted? As I understand var is a bad style in Scala
outerfunction!outer(10,inner) // TRUE!!?exists(which if I understand the question correctly is ultimately what you are stuck on): don't reuseforall- you only need to check entries in the set until you find a match, so write a tail-recursive inner method that will work that way.existsis actually a one-liner.