When if is just a function

https://news.ycombinator.com/rss Hits: 15
Summary

HELLO In Python, when you write if x > 5: print("big"), you’re using special syntax baked into the language. You can’t change how if works. You can’t compose it, pipe it, or partially apply it. You can’t pass if as an argument to another function. But what if you could? What if if, for, while and even fn and var were just regular functions? In languages like REBOL, Red and Rye they are. Three Reasons This Matters# Consistency. Most languages treat control structures as special forms—exceptions to the normal rules. In languages like Rye and REBOL, they’re ordinary functions that follow the same patterns as everything else. Flexibility. Functions can be composed, passed around, and combined. When control structures are functions, you inherit all those capabilities. Extensibility. If if and for are just functions, you can create your own specialized versions for specific purposes. No part of the language remains off-limits. Let’s see what this looks like in practice. Looking at If# Here’s a conditional in Python: temperature = 36 # We can evaluate and print an expression print(temperature > 30) # prints: True # But we can't print a block of code without executing it # (well, we could stringify it, but that's not the same) # Standard conditional - special syntax if temperature > 30: print("It's hot!") # prints: It's hot! Now compare this to Rye: temperature: 36 ; Evaluates expression and prints the result print temperature > 30 ; prints: true ; In Rye, blocks are data - we can print them print { print "It's hot!" } ; prints: print "It's hot!" ; The 'do' function evaluates a block do { print "It's hot!" } ; prints: It's hot! ; And here's the conditional - also just a function if temperature > 30 { print "It's hot!" } ; prints: It's hot! Look at that last if statement. It’s not special syntax it’s a function call. While functions print and do take one argument, if function takes two arguments: A condition that evaluates to true or false A block of code to run if the condi...

First seen: 2025-10-17 23:55

Last seen: 2025-10-18 15:57