4 ms·
If there was a way to mark functions and their inverses consistently in language, then functions with inverses could be used in (semanticaly) pure functional co
by ajuc 15y ago
If there was a way to mark functions and their inverses consistently in language, then functions with inverses could be used in (semanticaly) pure functional code, even if they were destructive.
For (stupid) example:
def reverse(x) as inversible with(reverse): #special case - reverse is itself inverse function
x = x.reversed() #I'm too lazy to code this by hand
def lastFive(x) as pure: #let's say language checks if pure function only uses pure and inversable functions
return x[:5] under reverse
Here lastFive is pure function even thought reverse is destructive.
This assumes everybody sticks to one definition of reversing something, in this case functions f and g are inverse of each other, when after invoking f(x), doing only pure or reversable things to x, and invoking g(x), x is unchanged.
And I don't know if this will be useful for anything - the only point seems to be avoiding making copies and still making pure functions. But resulting code, while semanticaly pure, is not threadsafe. So I don't know if it's worth anything.