3 ms·
Replace all conditionals with polymorphism: class TrueClass: def if(then, else): then.call() class FalseClass: def if(then,else):
by jules 17y ago
Replace all conditionals with polymorphism:
class TrueClass:
def if(then, else):
then.call()
class FalseClass:
def if(then,else):
else.call()
condition = true
condition.if({print "true"}, {print "false"})
- beza1e1 17y agoYou can't betray the processor, though. At least of x86 you would need to write condition = x < y and a compiler would create a conditional jump.
- jules 17y agoNot if you implement your own integers ;)
- UnFleshedOne 17y agoHere let me add some syntax sugar: if(condition) { print "true" } else { print "false" }
- silentbicycle 17y agoIf the call-able values are looked up dynamically, both branches will always run up to call().
- jules 17y agoI don't understand what you mean. Can you elaborate?
- silentbicycle 17y agoWell, I'm guessing that in your Python-ish pseudocode any code wrapped in curly braces is a lambda. I missed that when I commented. What I meant was that if you had code that ran to look up / generate those blocks, rather than just a literal block, it would always process both (even though only one is needed).
- wlievens 17y agoThe way Smalltalk does things, IIRC.
- jules 17y agoSyntactically yes. To make it fast the compiler recognizes it as a special case. I think that there is no special case for this in Self: the self compiler can optimize this away (even if you write your own True and False). But maybe I didn't remember that correctly.