lazy-evaluation

How to create an infinite enumerable of Times?

房东的猫 提交于 2019-12-01 00:34:02
I want to be able to have an object extend Enumerable in Ruby to be an infinite list of Mondays (for example). So it would yield: March 29, April 5, April 12...... etc How can I implement this in Ruby? In 1.9 (and probably previous versions using backports ), you can easily create enumerator: require 'date' def ndays_from(from, step=7) Enumerator.new {|y| loop { y.yield from from += step } } end e = ndays_from(Date.today) p e.take(5) #=> [#<Date: 2010-03-25 (4910561/2,0,2299161)>, #<Date: 2010-04-01 (4910575/2,0,2299161)>, #<Date: 2010-04-08 (4910589/2,0,2299161)>, #<Date: 2010-04-15 (4910603

Generate powerset lazily

十年热恋 提交于 2019-11-30 22:59:42
I want to calculate powerset of a set. Because I don't need the whole powerset at a time, it's better to generate it lazily. For example: powerset (set ["a"; "b"; "c"]) = seq { set []; set ["a"]; set ["b"]; set ["c"]; set ["a"; "b"]; set ["a"; "c"]; set ["b"; "c"]; set ["a";"b"; "c"]; } Since the result is a sequence, I prefer it in the above order. How can I do it in an idomatic way in F#? EDIT: This is what I'm going to use (based on BLUEPIXY's answer): let powerset s = let rec loop n l = seq { match n, l with | 0, _ -> yield [] | _, [] -> () | n, x::xs -> yield! Seq.map (fun l -> x::l)

R: passing expression to an inner function

╄→尐↘猪︶ㄣ 提交于 2019-11-30 22:28:40
问题 Further delving into the mysteries of R evaluation...This is closely related to my previous question ( How to write an R function that evaluates an expression within a data-frame ). Let's say I want to write a function topfn that takes a data-frame and an expression involving column-names of that data-frame. I want to pass both these arguments on to another function fn that actually evaluates the expression within the "environment" of the data-frame. And I want both fn and topfn to work

What's the best way to return an Enumerator::Lazy when your class doesn't define #each?

此生再无相见时 提交于 2019-11-30 21:45:37
Enumerable#lazy relies on your enumerable providing an #each method. If your enumerable doesn't have an #each method you can't use #lazy . Now Kernel#enum_for and #to_enum provide the flexibility to specify an enumeration method other than #each : Kernel#enum_for(method = :each, *args) But #enum_for and friends always construct plain (non-lazy) enumerators, never Enumerator::Lazy . I see that Enumerator in Ruby 1.9.3 offers this similar form of #new: Enumerator#new(obj, method = :each, *args) Unfortunately that constructor has been completely removed in Ruby 2.0. Also I don't think it was ever

Defer expression evaluation without using `quote`

北战南征 提交于 2019-11-30 21:45:17
I have created the following function/example as a generic way to display variable labels in tables and so forth: #' Function to prettify the output of another function using a `var.labels` attribute #' This is particularly useful in combination with read.dta et al. #' @param dat A data.frame with attr `var.labels` giving descriptions of variables #' @param expr An expression to evaluate with pretty var.labels #' @return The result of the expression, with variable names replaced with their labels #' @examples #' testDF <- data.frame( a=seq(10),b=runif(10),c=rnorm(10) ) #' attr(testDF,"var

In Swift, why does assigning to a static variable also invoke its getter

早过忘川 提交于 2019-11-30 20:46:52
I understand that in Swift, static vars are implicitly lazy: https://stackoverflow.com/a/34667272/1672161 But I'm not clear on why this happens: protocol HatType {} class Hat: HatType { init() { print("real hat") } } class MockHat: HatType { init() { print("mock hat") } } struct HatInjector { static var hat: HatType = Hat() } HatInjector.hat = MockHat() // Output: // real hat // mock hat What I'm seeing is that the assignment to the static var, is also invoking the getter in a sense. This isn't intuitive to me. What is happening here? Why doesn't the assignment only happen? This is because

Why is django's settings object a LazyObject?

老子叫甜甜 提交于 2019-11-30 20:27:17
Looking in django.conf I noticed that settings are implemented like this: class LazySettings(LazyObject): ... What is the rationale behind making settings objects lazy? Michael Mior Check out this section of the Django coding style. The reason is explained in there (quoted below). In addition to performance, third-party modules can modify settings when they are imported. Accessing settings should be delayed to ensure this configuration happens first. Modules should not in general use settings stored in django.conf.settings at the top level (i.e. evaluated when the module is imported). The

Python: how to do lazy debug logging

Deadly 提交于 2019-11-30 20:11:40
I have some python like this: def foo(): logger = logging.getLogger() # do something here logger.debug('blah blah {}'.format(expensive_func())) foo() where expensive_func() is a function that returns string and that it is expensive to execute. When developping, the log level is set to DEBUG, and expensive_func() get executed, the message get logged, everything is fine. The problem is that when I set the log level strictly greater than DEBUG, say WARNING, in production env, obviously the return value of expensive_func() won't get logged, but the expensive function itself will still be executed.

Lazily evaluated indexed sequence type

放肆的年华 提交于 2019-11-30 18:43:56
I need to build a sequence of objects that are loaded from an external resource. This loading being an expensive operation needs to be delayed until the time the objects are needed. After the collection is built, I need an indexed access to the contained objects. Does Scala standard library provide a collection suited to this use case? If not, what will be the best way to implement it? Edit: The indexed lookup should preferably be an O(1) operation. Strange, Miles recently tweeted about this . A reply then points out to Need at the end of Name.scala in scalaz and another one points to specs'

How to create an infinite enumerable of Times?

天大地大妈咪最大 提交于 2019-11-30 18:10:41
问题 I want to be able to have an object extend Enumerable in Ruby to be an infinite list of Mondays (for example). So it would yield: March 29, April 5, April 12...... etc How can I implement this in Ruby? 回答1: In 1.9 (and probably previous versions using backports ), you can easily create enumerator: require 'date' def ndays_from(from, step=7) Enumerator.new {|y| loop { y.yield from from += step } } end e = ndays_from(Date.today) p e.take(5) #=> [#<Date: 2010-03-25 (4910561/2,0,2299161)>, #<Date