0

It's probably obvious, but it's making me crazy and I think my code is being bloated as a result (missing the obvious, not the making me crazy part).

I have a method as follows:

def fun(f: Int => Boolean, y: Int): Int 

This method is going to be called like this fun(*some anon func*, y) and should increase and return y + 1 when some anon function (applied to some param) is greater than 0 and y - 1 otherwise. How to define fun? I'm trying something like

def fun(f: Int => Boolean, y: Int): Int = 
  if (f) y - 1
  else y + 1

Obviously, that doesn't work. I should put f(tmp), where tmp:Int but I'm now sure how to express that.

All examples I could find online always apply f on y, but this is just funny.

Update: here's a similar problem I have already solved, but am not happy with how did I do it:

For a given function forall(s: Set, p: Int => Boolean): Boolean, which returns true iff all the set elements x in S (def S: int => Boolean, indicator function for set S) satisfy p(x), create a function that will implement exists quantifier (exists function). In set theory, one can express this quantifier as (in terms of the problem described above) not all elements (not forall) of a set satisfy NOT p.

I did something like this:

def exists(s: Set, p: Int => Boolean): Boolean = {
  def negP(x: Int): Boolean = !p(x)
  !forall(s, negP)
}

My question is: how to do this without defining negP? I cannot state !p because Scala gives error for ! operator. I cannot use !p(x) because there is no x. Hence the problem above and my question.

TIA

11
  • Regarding anon function is greater than 0 -- f: Int => Boolean cannot be compared to 0: Int, so probably you should clarify your goals. Commented Nov 25, 2017 at 23:12
  • Your function f : what is the int parameter you want to call it with? If you already provide the value then it should not take a parameter Commented Nov 25, 2017 at 23:14
  • @dveim modified the question a bit. essentially, if I pass (x: Int) => x * x, then y would always go up, no matter what. However, when I pass (x: Int) => x - 2, it will get different results for x <= 2 and for x > 2. Commented Nov 25, 2017 at 23:24
  • 2
    It feels like you don't know what you want to do rather than not knowing how to do it Commented Nov 25, 2017 at 23:48
  • 1
    @hummingBird I updated my answer, hope it works. :) Commented Nov 28, 2017 at 23:22

3 Answers 3

1

You could make it return a function that does that, like so:

def fun(f: Int => Int, y: Int): Int => Int = 
input => if (f(input) > 0) y - 1 else y + 1


  val test1 = fun(x => x*2, 1)
  println(test1(-1))  // "2"
  val test2 = fun(x => x - 100, 5)
  println(test2(101)) // "4"
  val test3 = fun(x => x, -10)
  println(test3(0))  // "-9"

EDIT: Note that I changed the signature of the input function, as I think it probably reflects the requirements better, changing it back to boolean should be trivial(see other answers). :)

EDIT(2): Seeing is you updated your question, I thought best to update my answer: You can always just inline a function, in your example, maybe you could do it like this:

def exists(s: Set, p: Int => Boolean): Boolean = !forall(s, x => !p(x))
Sign up to request clarification or add additional context in comments.

6 Comments

Although the OP didn't make what they wanted very clear, it seems like it could be the right answer
@Dici, you made it clear - you hate when people can't explain very well. What I did was giving an example and did my best to explain. Learning scala for the first time and also functional programming, certain concepts are giving me a headache since I don't know how to approach the problem. And to help - No, this doesn't resolve my answer. Thx for trying
@raudbjorn, unfortunately, this doesn't help because function signature has changed. Thx and sorry for issues in explaining. If it helps, I have Dici complaining about my issues in explaining to share some pain around :)
@hummingBird hum ? Hope it didn't come as aggressive, I wasn't particularly criticizing your question, just saying I was not sure what the exact requirements were although this answer seemed to meet them. Don't take things personally dude :p
@Dici i didn't in your first comment. i actually supported... just kinda minded when u started answering around to other ppl :P
|
1

Your original problem can't be solved because you can't test the output of f() without invoking f(), and you can't invoke f() without an input argument to pass. I think the best you can hope for is something like this.

def fun(f: Int => Boolean, y: Int): Int => Int = (arg: Int) =>
  if (f(arg)) y - 1
  else        y + 1

Your "similar problem" looks to me like it should be a different question.

Your proposed solution can be solved via recursion, but using forall() it can be expressed more concisely.

def exists(s: Set[Int], p: Int => Boolean): Boolean =
  !forall(s, (x:Int) => !p(x))

2 Comments

Thx for you reply. It doesn't compile because I didn't expose foreach code. Essentially, that code uses recursion to iterate through all possible options and it was my goal - and task's goal to use foreach and some rules of first order logic to achieve the same goal. Just imagine that foreach is defined in a library and that it goes through all of the set elements and say if all of them satisfy the function p: Int => Boolean. Then exists can simply be resolved as NOT (FOREACH (P(X) == FALSE)) - not all of the elements give p(x) false
I agree ... I put it wrong and your updated answer does contain the solution I was looking for. I already accepted the answer though .. although both solutions give the same answer pretty much.
0

It sounds like you don't actually want to pass a parameter to your anonymous function? Then try this:

def fun(f: () => Boolean, y: Int): Int = 
  if (f()) y - 1
  else y + 1

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.