Programmer bleg
I realized that two of my classes were going to use a function identical in all respects except that one of them would test for amount x being greater than amount y, while the other would test for x being less than y. Right now, I have coded the function to accept a boolean parameter I call "gt" which controls an if statement as to which test the function does. But what I really wanted to do was to pass in the operator to use itself.
However, generally speaking, programming languages do not accept operators as parameters to functions.
Is there a way to do this without the if statement?
However, generally speaking, programming languages do not accept operators as parameters to functions.
Is there a way to do this without the if statement?
You want to use the parameter as the operator, rather than a value operated on. It is possible in C to pass a pointer to a function, receive the pointer as a parameter, and have the receiving code execute the function.
ReplyDeleteSo if you create your own set of operators as functions (or methods) I think you could achieve the objective you have in mind. Might be more trouble than it is worth.
Yes, in Python also I can pass a function in: but that is more code then I have now.
DeleteProgramming languages are evolving to more commonly support this kind of thing, called "first-class functions": http://en.wikipedia.org/wiki/First-class_function#Language_support
ReplyDeleteIt's usually at least possible to do it, but whether or not you can without resorting to silly gymnastics really depends on what language you're working in.
If you're working in Python, you could use the 'operator' package: my_function(x, y, operator.lt) and my_function(x, y, operator.gt) for less than and greater than, respectively.
Matt, I wrote a lisp interpreter a number of years ago: so I know about first class functions.
DeleteBut your Python code looks very interesting, and that is, indeed, the language I am working in.
It must suck to be a Lisp programmer with a lisp.
Deletereturn gt ? (x > y ? true : false) : (x < y ? true : false)
ReplyDeleteRiffing: return x > y == gt
DeleteBut in practice I usually do the if statement so I don't have to scratch my head later!
Oy Matt, very clever!
DeleteI tend prefer prettiness over optimization, so I agree with you.
DeleteNice coding, Samson!
ReplyDeleteThank you. I just so happened to have used Python to make sure I got the syntax right and learned its ternary operator is "expr1 if condition else expr2" instead of "condition ? expr1 : expr2". So, what I gave you would have to be converted over into that.
DeleteIf that's too much of an eyesore or a hassle, you can also modify the function take a string instead of a boolean parameter and pass it to "eval(str(x)+operator+str(y))".
def Comp(x, op, y):
ReplyDeletereturn eval(str(x)+op+str(y))
Tada!
Here's the one using the ternary operator.
ReplyDeletedef Comp(x, op, y):
return (True if x>y else False) if op else (True if x<y else False)
You won't like this answer, but from what you say the design sounds probably flawed. If you have functions ( methods, whatever) identical except in that one respect then passing in a comparator, while better than passing a flag, is probably wrong. Refactor. You will probably end up with cleaner code. If not, all C descended languages and all OO languages allow passing comparators.
ReplyDelete"If you have functions ( methods, whatever) identical except in that one respect then passing in a comparator, while better than passing a flag, is probably wrong."
DeleteHuh? Why?
"If not, all C descended languages and all OO languages allow passing comparators."
A function? But that is no simpler than the 'if'.
You did get the point that I DON'T have "functions," I have A function, right?
Delete