André Thieme wrote: > [EMAIL PROTECTED] schrieb: > William James schrieb: > >>> How would you solve this in Python? > >>> You could embed it inside a lambda and must somehow make the > >>> variable "it" visible in it, because in the context of aif this > >>> "it" gets bound to the result.In Ruby: > >> def aif val > >> yield val if val > >> end > >> > >> def complex_calc n > >> if n % 2 == 1 > >> n**n > >> else > >> nil > >> end > >> end > >> > >> aif( complex_calc( 9 ) ) {|x| puts "hello, #{x}"} > >> aif( complex_calc( 8 ) ) {|x| puts "hello, #{x}"} > >> > >> --- output ----- > >> hello, 387420489 > >> > > Okay, so it's certainly _doable_. I think the question is more if it's > > usable. Regular if:s don't like that in Ruby/Python/similar, and adding > > such a construct would mean extending the syntax. > > > > In Lisp, all (well, almost) functions are equal, so an (if ...) would > > look the same like the (aif ...). I think that's the point here. > > Exactly that is the point. > *Of course* it is doable. > And what William shows us is maybe one of the best solutions > in Ruby. He used advanced abstractions that made it possible. > The thing with most (if not all) programming languages other than Lisp > is: these abstractions are leaky. > They have a low level knowledge leak. Users of aif need to know how to > pass in arguments. As you see he had do embed the code that needs execution > into a block (anon function) and then use this special syntax for x. > > > André > --
I may have tried too hard. The simpler way is usually better. No aif() is needed: if x = complex_calc( 9 ) puts "hello, #{x}" end -- http://mail.python.org/mailman/listinfo/python-list