3 ms·
You say it's used all the time -- so how about you give an example of a "lazy" solution to a problem in, say, C?
by theseoafs 13y ago
You say it's used all the time -- so how about you give an example of a "lazy" solution to a problem in, say, C?
- copergi 13y agofoo || bar
- ssprang 13y agoI implement laziness "manually" all the time in Objective-C. For example, imagine something happens which invalidates a property which must be calculated (say, a bounding box for a graphical object). Instead of immediately recomputing it, just set it to nil, and only recompute it the next time the property is actually accessed. This way you can harmlessly invalidate it multiple times without doing a potentially expensive calculation (which just gets thrown out by subsequent invalidations).
- theseoafs 13y agoThat's not laziness, that's just cache invalidation.
- swift 13y agoIt's fair to say that it's laziness, in my view. That sort of thing is just a manual implementation of what laziness would do in a particular situation. The closure that the thunk would hold is essentially diffused into the object that contains the lazy property.
- mavelikara 13y agoJava example: if (logger.isDebugEnabled()) logger.debug("..." + ...);
- cscheid 13y agoA fast sorting algorithm in Haskell degrades gracefully to a (faster) selection algorithm when you don't ask for the whole thing. In other words, "sort lst" gives you a sorted list, but "take 5 (sort lst)" gives you the first five elements, without sorting the rest of the list.
- geon 13y agoSomeObjec *getObject(){ if (!theObject) theObject = createObject(); return theObject; }