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?

16 comments:

  1. 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.

    So 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.

    ReplyDelete
    Replies
    1. Yes, in Python also I can pass a function in: but that is more code then I have now.

      Delete
  2. Programming 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

    It'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.

    ReplyDelete
    Replies
    1. Matt, I wrote a lisp interpreter a number of years ago: so I know about first class functions.

      But your Python code looks very interesting, and that is, indeed, the language I am working in.

      Delete
    2. It must suck to be a Lisp programmer with a lisp.

      Delete
  3. return gt ? (x > y ? true : false) : (x < y ? true : false)

    ReplyDelete
    Replies
    1. Riffing: return x > y == gt

      But in practice I usually do the if statement so I don't have to scratch my head later!

      Delete
    2. Oy Matt, very clever!

      Delete
    3. I tend prefer prettiness over optimization, so I agree with you.

      Delete
  4. Replies
    1. Thank 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.

      If 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))".

      Delete
  5. def Comp(x, op, y):
    return eval(str(x)+op+str(y))

    Tada!

    ReplyDelete
  6. Here's the one using the ternary operator.

    def Comp(x, op, y):
    return (True if x>y else False) if op else (True if x<y else False)

    ReplyDelete
  7. 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
    Replies
    1. "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."

      Huh? Why?

      "If not, all C descended languages and all OO languages allow passing comparators."

      A function? But that is no simpler than the 'if'.

      Delete
    2. You did get the point that I DON'T have "functions," I have A function, right?

      Delete