Hacker News new | ask | show | jobs
by pelme 823 days ago
Python's inline if's still read backwards to me and it would be nice to switch the order. But we are kind of stuck with it. I also don't want to introduce new control structures in this lib. Just want to use whatever Python has to offer in terms of control structures/syntax to make it more approachable.

htpy supports generators/callables as children (https://htpy.dev/streaming/). As long as you are careful to wrap things in generators/lambdas everything is lazy. Implementing if_ and for_ is straightforward and anyone can build constructs like that to use. But will not be lazy unless all arguments are wrapped in lambdas:

  from htpy import div, li, ul

  def if_(cond, a, b):
      return a() if cond() else b()

  def for_(items, func):
      return (func(item) for item in items())

  print(div[if_(lambda: True, lambda: "True!", lambda: "False!")])
  print(div[if_(lambda: False, lambda: "True!", lambda: "False!")])
  print(ul[for_(lambda: ["a", "b", "c"], lambda x: li[x])])
output:

  <div>True!</div>
  <div>False!</div>
  <ul><li>a</li><li>b</li><li>c</li></ul>