lazy-evaluation

Swift - Lazy loading a property that can be made nil later

落花浮王杯 提交于 2020-01-01 06:43:36
问题 I am looking for a way to lazy load my variable, but I want to be able to make it nil later and then recreate it on the get. For example in the instance that there is a memory warning i want to clear anything that isn't used and then recreate it when needed again later. Here is how I would do it in Objective-C and my current interpretation in swift. I am not sure that it preserves the variable for keeping current navigation. Obj-C Implementation @property (strong, nonatomic, readwrite)

nest yields to return IEnumerable<IEnumerable<T>> with lazy evaluation

好久不见. 提交于 2020-01-01 04:17:12
问题 I wrote a LINQ extension method SplitBetween analogous to String.Split . > new List<int>(){3,4,2,21,3,2,17,16,1} > .SplitBetween(x=>x>=10) [3,4,2], [3,2], [], [1] Source: // partition sequence into sequence of contiguous subsequences // behaves like String.Split public static IEnumerable<IEnumerable<T>> SplitBetween<T>(this IEnumerable<T> source, Func<T, bool> separatorSelector, bool includeSeparator = false) { var l = new List<T>(); foreach (var x in source) { if (separatorSelector(x)) { if

Is there a lazy `String.Split` in C#

不打扰是莪最后的温柔 提交于 2020-01-01 04:12:08
问题 All string.Split methods seems to return an array of strings ( string[] ). I'm wondering if there is a lazy variant that returns an IEnumerable<string> such that one for large strings (or an infinite length IEnumerable<char> ), when one is only interested in a first subsequences, one saves computational effort as well as memory. It could also be useful if the string is constructed by a device/program (network, terminal, pipes) and the entire strings is thus not necessary immediately fully

Least-strict (*)

风流意气都作罢 提交于 2019-12-31 12:12:08
问题 Is it possible to implement (*) with least-strict semantics in Haskell (standardized Haskell preferred, but extensions are OK. Using compiler internals is cheating)? For example, such a definition should result in the following being true: 0 * ⊥ = 0 ⊥ * 0 = 0 and only: ⊥ * ⊥ = ⊥ I can build pattern matches that satisfy one of the above cases, but not both, because the zero check forces the value. 回答1: Yes, but only using constrained impurity. laziestMult :: Num a => a -> a -> a laziestMult a

Lazy, overloaded C++ && operator?

你说的曾经没有我的故事 提交于 2019-12-30 17:45:11
问题 I'm trying to implement my own boolean class, but cannot replicate native semantics for &&. The following contrived code demonstrates the issue: #include <iostream>> class MyBool { public: bool theValue; MyBool() {} MyBool(bool aBool) {theValue = aBool;} MyBool operator&& (MyBool aBool) {return theValue && aBool.theValue;} }; bool f1() {std::cout << " First\n"; return false;} bool f2() {std::cout << " Second\n"; return false;} int main(int argc, char** argv) { std::cout << "Native &&\n"; f1()

Does SQLite optimize a query with multiple AND conditions in the WHERE clause?

老子叫甜甜 提交于 2019-12-30 10:25:08
问题 In SQL databases (I use Python+Sqlite), how to make sure that, if we have 1 million rows, the query SELECT * FROM mytable WHERE myfunction(description) < 500 AND column2 < 1000 [-----------------------------] [--------------] high-CPU cost condition easy-to-test requiring 100 µs per test condition is optimized so that the 1st condition (CPU-expensive) is only tested if the easy-to-test second condition is already True? (since it's a logical AND , is it a lazy AND ?) Example: if the 1st

Can you more clearly explain lazy evaluation in R function operators?

流过昼夜 提交于 2019-12-30 05:55:11
问题 If I create a function as follows: what_is_love <- function(f) { function(...) { cat('f is', f, '\n') } } And call it with lapply : funs <- lapply(c('love', 'cherry'), what_is_love) I get unexpected output: > funs[[1]]() f is cherry > funs[[2]]() f is cherry But note that this is not the case when you do not use lapply : > f1 <- what_is_love('love') > f2 <- what_is_love('cherry') > f1() f is love > f2() f is cherry What gives? I know that funs <- lapply(c('love', 'cherry'), what_is_love) can

How to force Spark to evaluate DataFrame operations inline

只愿长相守 提交于 2019-12-29 08:36:07
问题 According to the Spark RDD docs: All transformations in Spark are lazy, in that they do not compute their results right away...This design enables Spark to run more efficiently. There are times when I need to do certain operations on my dataframes right then and now . But because dataframe ops are " lazily evaluated " (per above), when I write these operations in the code, there's very little guarantee that Spark will actually execute those operations inline with the rest of the code. For

NonSQL: initialization of DB -object in browser console? DB -logic in the user-space?

放肆的年华 提交于 2019-12-29 08:28:57
问题 I am trying to move the logic to the very high-level from the db. Technically, I am looking for extremely lazy-evaluation of things. I feel some non-sql -db is for this purpose because it looks like the computation is expensive in non-sql db and hence tend to be moved to the userspace. So "by which NonSQL db I can initialize the DB -object in the user-space probably in things such as JavaScript/jQuery?" TRIALS Trial 0: MongoDB, not possible according to this here. Trial 1: CouchDB, open.

Haskell Fibonacci Explanation

寵の児 提交于 2019-12-29 07:33:26
问题 I am quite new to Haskell and I'm trying to wrap my head around how the lazy expression of Fibonacci sequences work. I know this has been asked before, but none of the answers have addressed an issue I'm having with visualising the result. The code is the canonical one using zipWith fibs = 0 : 1 : zipWith (+) fibs (tail fibs) I understand the following: zipWith literally zips two lists together tail grabs all but the first element of a list Haskell references 'to-be' computed data as thunks .