1

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

13
  • As far as I know, you can't do that without changing the outer function! Commented Sep 28, 2013 at 10:25
  • Also, should it not be outer(10,inner) // TRUE!! ? Commented Sep 28, 2013 at 10:26
  • 2
    First, this looks like homework from the Coursera course, so giving you a full solution would not be appropriate. That said, as a hint on writing exists (which if I understand the question correctly is ultimately what you are stuck on): don't reuse forall - 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. Commented Sep 28, 2013 at 11:08
  • 1
    Further hint: say you are looking for even numbers - if there are none in the set, then what must be true for all elements of the set? You should find that the simplest solution for exists is actually a one-liner. Commented Sep 28, 2013 at 12:39
  • 1
    Also, it's ok to use vars inside of functions. You just don't want to leak them to the outside, of the function or class. You just need to make sure you understand if there are leakages to the rest of your app. Use mutable only locally and if needed. Commented Sep 28, 2013 at 17:56

3 Answers 3

1

In Scala, the last expression is returned unless you use the return keyword. In your case, the function outer always returns false.

Since you just wrap the inner function you could remove the false:

def outer(x: Int, inner: Int => Boolean): Boolean = {
  inner(x)
}

def inner(x: Int): Boolean = {
  if (x == 10) true else false
}

Or, even shorter:

def inner(x: Int): Boolean = {
  x == 10
}

This would return the returned expression of the inner function, namely true if x == 10, otherwise false.

Sign up to request clarification or add additional context in comments.

3 Comments

I know, but I can't chanhe the outer function
If the other function really looks like the one you posted, then it will always return false because the last expression (false) is returned. There's nothing you can do about it.
can I avoid using var, but use val instead?
1

If you can define your wrapper, you probably can avoid using var

def outerWrapper(x: Int, f: Int => Boolean): Boolean = {
    if (f(x)) true
    else outer(x, f)
}

Then you can pass inner method to outerWrapper method

outerWrapper(10,inner)

Comments

0

My method is here:

import scala.util.control.Breaks
def outer(x: Int, inner: Int => Boolean): Boolean = {
  Breaks.breakable {
    inner(x)
    return false
  }
  true
}

def inner(x: Int): Boolean = {
  if (x == 10) Breaks.break()
  else false
}

println(outer(10, inner)) // TRUE!!

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.