Match.jl icon indicating copy to clipboard operation
Match.jl copied to clipboard

Inverse functions

Open mschauer opened this issue 10 years ago • 3 comments

I am thinking since a while, that it would be nice and very julian to match general functional expressions if the inverse of the function is provided, for example

@match (a,b) g(x,(y,z))

would translate into

x, _yz = @inversefun(g, (typeof(a,b))(a,b)
y,z = _yz

where @inversefun can be extended.

mschauer avatar Jan 21 '15 16:01 mschauer

@mschauer, thank you for noting this. I was pretty busy when you contributed this comment, and it fell off my radar--sorry for my slow response!

I think that the suggestion sounds reasonable. I'm not sure if or when I'll have a chance to look at this, but if you have time and want to explore contributing something like this to Match.jl, I'd be happy to consider it.

kmsquire avatar Apr 05 '15 06:04 kmsquire

Macros cannot be extended, but inversefun can easily be made a function instead of a macro. One potential problem is that functions can't be dispatched on. Here's a very inelegant possible solution:

immutable Inverse end

f(x) = x^2
f(::Inverse, x) = sqrt(x)

that is, using the inverse function as a method of the original. This probably has ambiguity problems, and it doesn't strike me as very Julian.

TotalVerb avatar Jan 03 '16 02:01 TotalVerb

This is very similar to https://github.com/JuliaServices/Match.jl/issues/92.

nystrom avatar Sep 27 '23 07:09 nystrom