Inverse function in Scala

Is there a way to express the inverse of any function from scala?

For example, if I have a function f, such as

(x: Int) => x + 1

I would like to write the inverse function g as

(f (x): Int) => x // invalid scala syntax

or

(x: Int) => reverse (f (x)) // reverse will return (x => x -1)

Do you know a way to do this in scala?

NB = x => x + 1 just for example I’m looking for a general way to solve this kind of problem

Thanks!

+5
source share
5 answers

No, something like this is impossible. The problem is that not all mathematical functions have the inverse. From the Wikipedia entry in inverse functions :

Not all functions have the opposite. For this rule to apply, each element y ∈ Y must correspond to at most one x ∈ X; a function ƒ with this property is called one-to-one or preserving information or injection.

For example, the square root function ( sqrt ) is the inverse quadratic function ( x^2 ) only when x >= 0 , where the square root function is one-to-one. We can say that the negative square root function is the inverse quadratic function when x < 0 only because x^2 = (-x)^2 . But this is a special property of a square function and, of course, is not true at all.

+14
source

Depending on the usage scenario, you can do this by saving the map from (Function, Result) => Arguments , and then call a method, such as inverse(f, r) , which returns the arguments stored on the map. However, this only works 1) if the original function is called before the return value, and 2) if the original function is injective (as ddk already pointed out).

There is also quite a certain implementation overhead (for example, you probably need a dedicated card for Function1 - Function22 ), especially if you want to reduce the amount of template code superimposed on function executors. Aspect-oriented programming can help here; annotations similar to EJB3 Interceptors may also work.

It seems like the use case should be pretty special to justify all the hoops you have to jump with.

+2
source

You cannot express the inverse of a function, but you can express its result, and perhaps this will suit you.

This can be useful when you pass a function around.

For example, if you have a function f: Int => Boolean , which you use as a parameter in a higher-order function, you can wrap it in another function of the same type x => !f(x) , which simply returns the opposite result calculation result:

 def select(ls: List[String], p: String => Boolean): List[String] = ls.remove(x => !p(x)) // select: (ls: List[String], p: String => Boolean)List[String] val li = List("one", "two", "three") // li: List[java.lang.String] = List(one, two, three) /* using select with some conditions */ select(li, _.length() > 3) // equivalent to select(li, x => x.length() > 3) // res0: List[String] = List(three) select(li, _.length() <= 3) // equivalent to select(li, x => x.length() <= 3) // res1: List[String] = List(one, two) /* using remove with the same conditions */ li.remove(_.length() > 3) // equivalent to li.remove(x => x.length() > 3) // res2: List[java.lang.String] = List(one, two) li.remove(_.length() <= 3) // equivalent to li.remove(x => x.length() <= 3) // res3: List[java.lang.String] = List(three) 

NOTE: the remove method in the List class is deprecated and filterNot should be used instead, but I think that in this example, remove simply reads better.

+2
source

From a pragmatic view, the unapply method can be used to represent inverse functions:

 object f { def apply(x: Int) = x + 1 def unapply(x: Int) = Some(x - 1); } val x = 1 val f(y) = f(x) assert(x == y) 
+1
source

I am adding an answer from another question marked as a duplicate of this, and no one seems to have mentioned it yet: it can theoretically automatically calculate the “reverse” (in the sense that it performs an exhaustive search on the input and answers “no” if for a limited period of time there is no solution) of functions over infinite sequences of binary digits: see the article “Apparently impossible functional programs” (although it uses Haskell, not Scala).

The practical usefulness of this exhaustive search is, of course, a different matter.

0
source

All Articles