6 ms·
Quick poll: who prefers laziness by default. Who prefers strictness by default?
by throweway 10y ago
Quick poll: who prefers laziness by default. Who prefers strictness by default?
- hackaflocka 10y agoI prefer the laziness of PHP and Python. JavaScript threw me off with its eagerness. Took a while getting used to.
- deleted 10y ago[deleted]
- spicyj 10y agoIn what ways are PHP and Python more lazy than JS?
- RussianCow 10y agoI can't speak for PHP, but in Python (version 3 at least), a lot of the built-in functions that return iterators, like `map` and `range`, are lazy.
- daveguy 10y agoAlso in python 2.7 (and I assume py3k stuff too) conditionals also have lazy evaluation if you have the statement: if False and (fibonacci(1000)/fibonacci(1000)): print "Nope" It will immediately evaluate to False and pring "Nope" rather than checking the (fibonacci(1000)/fibonacci(1000)) portion of the conditional. EDIT: Side note, is there a shortcut for displaying code snippets within HN posts? Extra returns (what I usually use for clarification by formatting) do not display very well.
- lfowles 10y agoThat's not necessarily "lazy" in the normal programming context, it's specifically short-circuit evaluation in Python. Although as Wikipedia points out: "In languages that use lazy evaluation by default (like Haskell), all functions are effectively "short-circuit", and special short-circuit operators are unnecessary."
- mastazi 10y agoPHP too. (if isset($foo) && $foo == 14) If $foo is not set the second boolean expression is not evaluated (short circuit evaluation). In fact, checking if isset followed by another boolean expression is very idiomatic in PHP.
- throwanem 10y agoJavascript will short-circuit in && and the ternary operator, too. But that's not really the same as lazy evaluation, which both is more general and happens in a different context; ES6 generators, for example, are lazy, which means that any sequence function in which one is used is lazy as well. Indent your code snippets with a minimum of four spaces and they will be rendered as preformatted, monospaced text. Vertically adjacent lines thus indented will be rendered as a single block.
- Buge 10y agoI think there is a bug in that code. It will not print Nope.
- daveguy 10y agoYou are right. I meant immediately evaluate False and not print Nope.
- tikhonj 10y agoI'm a big fan of taking advantage of laziness and think it's drastically underrated. It's incredibly expressive and it's what really makes Haskell more than just an ML variant. I gave a slightly rough talk about "Thinking with Laziness"[1] which tries to capture how expressive it can be. I'm a big fan and sad that so many people are willing to categorically dismiss it as a wart in Haskell. [1]: https://begriffs.com/posts/2015-06-17-thinking-with-laziness.html https://begriffs.com/posts/2015-06-17-thinking-with-laziness...
- lmm 10y agoI prefer strict by default, despite liking fancy typed languages. It's easy to build lazy on top of strict (as long as functions are values), but impossible to build strict on top of lazy. Consistency and reasonability are important, and the runtime performance of Haskell doesn't have either. I currently use Scala and have high hopes for Idris.