lazy-evaluation

How to get notified before static variables are finalized

笑着哭i 提交于 2020-01-28 09:19:25
问题 When can i cleanup objects stored in static variables in C#? I have a static variable that is lazily initialized: public class Sqm { private static Lazy<Sqm> _default = new Lazy<Sqm>(); public static Sqm Default { get { return _default.Value; } } } Note : That i've just changed Foo to be a static class. It doesn't change the question in any way if Foo is static or not. But some people are convinced that there is no way that an instance of Sqm could be constructed without first constructing an

Execute dplyr operation only if column exists

陌路散爱 提交于 2020-01-22 13:59:44
问题 Drawing on the discussion on conditional dplyr evaluation I would like conditionally execute a step in pipeline depending on whether the reference column exists in the passed data frame. Example The results generated by 1) and 2) should be identical. Existing column # 1) mtcars %>% filter(am == 1) %>% filter(cyl == 4) # 2) mtcars %>% filter(am == 1) %>% { if("cyl" %in% names(.)) filter(cyl == 4) else . } Unavailable column # 1) mtcars %>% filter(am == 1) # 2) mtcars %>% filter(am == 1) %>% {

R: How make dump.frames() include all variables for later post-mortem debugging with debugger()

眉间皱痕 提交于 2020-01-14 12:44:08
问题 I have the following code which provokes an error and writes a dump of all frames using dump.frames() as proposed e. g. by Hadley Wickham: a <- -1 b <- "Hello world!" bad.function <- function(value) { log(value) # the log function may cause an error or warning depending on the value } tryCatch( { a.local.value <- 42 bad.function(a) bad.function(b) }, error = function(e) { dump.frames(to.file = TRUE) }) When I restart the R session and load the dump to debug the problem via load(file = "last

prefix(_ maxLength:) is type-erased when used with a struct that conforms to LazySequenceProtocol

纵然是瞬间 提交于 2020-01-13 15:04:31
问题 prefix(_ maxLength:) returns a type-erased Sequence in the following code EXAMPLE : public struct CycleIterator <Base: Sequence>: IteratorProtocol { public typealias Element = Base.Element private var sequence: Base private var iterator: Base.Iterator internal init (_ sequence: Base) { self.sequence = sequence self.iterator = self.sequence.makeIterator() } public mutating func next () -> Element? { var next = self.iterator.next() if next == nil { self.iterator = self.sequence.makeIterator()

prefix(_ maxLength:) is type-erased when used with a struct that conforms to LazySequenceProtocol

空扰寡人 提交于 2020-01-13 15:03:32
问题 prefix(_ maxLength:) returns a type-erased Sequence in the following code EXAMPLE : public struct CycleIterator <Base: Sequence>: IteratorProtocol { public typealias Element = Base.Element private var sequence: Base private var iterator: Base.Iterator internal init (_ sequence: Base) { self.sequence = sequence self.iterator = self.sequence.makeIterator() } public mutating func next () -> Element? { var next = self.iterator.next() if next == nil { self.iterator = self.sequence.makeIterator()

Improving clojure lazy-seq usage for iterative text parsing

喜欢而已 提交于 2020-01-13 12:15:10
问题 I'm writing a Clojure implementation of this coding challenge, attempting to find the average length of sequence records in Fasta format: >1 GATCGA GTC >2 GCA >3 AAAAA For more background see this related StackOverflow post about an Erlang solution. My beginner Clojure attempt uses lazy-seq to attempt to read in the file one record at a time so it will scale to large files. However it is fairly memory hungry and slow, so I suspect that it's not implemented optimally. Here is a solution using

Is MySQL logic evaluation lazy/short-circuiting in JOIN clause?

南楼画角 提交于 2020-01-13 08:37:06
问题 Take the following expression: FALSE AND (expression) Will MySQL evaluate the expression or just move on as soon as it sees FALSE ? Some background context-- I wanted to speed up a query by doing: JOIN... ON (indexed_column1=indexed_column2 AND non_indexed_column_a=non_indexed_column_b) For background on why I'm doing this query see this answer If it's going to always evaluate non_indexed_column_a=non_indexed_column_b then no time is saved with that. 回答1: The MySQL query optimizer uses

Always guaranteed evaluation order of `seq` (with strange behavior of `pseq` in addition)

余生颓废 提交于 2020-01-12 06:30:23
问题 The documentation of seq function says the following: A note on evaluation order: the expression seq a b does not guarantee that a will be evaluated before b . The only guarantee given by seq is that the both a and b will be evaluated before seq returns a value. In particular, this means that b may be evaluated before a . If you need to guarantee a specific order of evaluation, you must use the function pseq from the "parallel" package. So I have a lazy version of sum function with

When is unsafeInterleaveIO unsafe?

我是研究僧i 提交于 2020-01-11 15:28:33
问题 Unlike other unsafe* operations, the documentation for unsafeInterleaveIO is not very clear about its possible pitfalls. So exactly when is it unsafe? I would like to know the condition for both parallel/concurrent and the single threaded usage. More specifically, are the two functions in the following code semantically equivalent? If not, when and how? joinIO :: IO a -> (a -> IO b) -> IO b joinIO a f = do !x <- a !x' <- f x return x' joinIO':: IO a -> (a -> IO b) -> IO b joinIO' a f = do !x

Pass parameters to constructor, when initializing a lazy instance

ⅰ亾dé卋堺 提交于 2020-01-10 09:34:51
问题 public class myClass { public myClass(String InstanceName) { Name = InstanceName; } public String Name { get; set; } } // Now using myClass lazily I have: Lazy<myClass> myLazy; Console.WriteLine(myLazy.Value.Name); My question is how to pass InstanceName to myClass constructor when we are using a lazy instance ? 回答1: Try this: Lazy<myClass> myLazy = new Lazy<myClass>(() => new myClass(InstanceName)); Remember that the expression is evaluated lazily, so if you change the value of the variable