lazy-evaluation

How to implement lazy iterate when IO is involved in Haskell

十年热恋 提交于 2020-05-13 21:17:09
问题 I'm using IO to encapsulate randomness. I am trying to write a method which iterates a next function n times, but the next function produces a result wrapped in IO because of the randomness. Basically, my next function has this signature: next :: IO Frame -> IO Frame and I want to start with an initial Frame , then use the same pattern as iterate to get a list [Frame] with length n . Essentially, I'd like to be able to write the following: runSimulation :: {- parameters -} -> IO [Frame]

How to implement lazy iterate when IO is involved in Haskell

和自甴很熟 提交于 2020-05-13 21:16:12
问题 I'm using IO to encapsulate randomness. I am trying to write a method which iterates a next function n times, but the next function produces a result wrapped in IO because of the randomness. Basically, my next function has this signature: next :: IO Frame -> IO Frame and I want to start with an initial Frame , then use the same pattern as iterate to get a list [Frame] with length n . Essentially, I'd like to be able to write the following: runSimulation :: {- parameters -} -> IO [Frame]

Understanding lazy evaluation in LINQ in C#

别说谁变了你拦得住时间么 提交于 2020-05-11 01:55:16
问题 I was reading this article about LINQ and can't understand how the query is executed in terms of lazy evaluation. So, I simplified the example from the article to this code: void Main() { var data = from f in GetFirstSequence().LogQuery("GetFirstSequence") from s in GetSecondSequence().LogQuery("GetSecondSequence", f) select $"{f} {s}"; data.Dump(); // I use LINQPAD to output the data } static IEnumerable<string> GetFirstSequence() { yield return "a"; yield return "b"; yield return "c"; }

Understanding lazy evaluation in LINQ in C#

心已入冬 提交于 2020-05-11 01:49:44
问题 I was reading this article about LINQ and can't understand how the query is executed in terms of lazy evaluation. So, I simplified the example from the article to this code: void Main() { var data = from f in GetFirstSequence().LogQuery("GetFirstSequence") from s in GetSecondSequence().LogQuery("GetSecondSequence", f) select $"{f} {s}"; data.Dump(); // I use LINQPAD to output the data } static IEnumerable<string> GetFirstSequence() { yield return "a"; yield return "b"; yield return "c"; }

Understanding lazy evaluation in LINQ in C#

≯℡__Kan透↙ 提交于 2020-05-11 01:49:30
问题 I was reading this article about LINQ and can't understand how the query is executed in terms of lazy evaluation. So, I simplified the example from the article to this code: void Main() { var data = from f in GetFirstSequence().LogQuery("GetFirstSequence") from s in GetSecondSequence().LogQuery("GetSecondSequence", f) select $"{f} {s}"; data.Dump(); // I use LINQPAD to output the data } static IEnumerable<string> GetFirstSequence() { yield return "a"; yield return "b"; yield return "c"; }

Haskell: how to detect “lazy memory leaks”

霸气de小男生 提交于 2020-05-10 03:26:27
问题 After few hours of debugging, I realized that a very simple toy example was not efficient due to a missing ! in an expression return $ 1 + x (thanks duplode!... but how come ghc does not optimize that??). I also realized it because I was comparing it with a Python code that was quicker, but I won't always write Python code to benchmark my code... So here is my question: is there a way to automatically detect these "lazy memory leaks", that slow down a program for no real reason? I'm still

Kotlin: lateinit to val, or, alternatively, a var that can set once

♀尐吖头ヾ 提交于 2020-05-09 19:04:41
问题 Just curious: In Kotlin, I would love to get some val that can be initialized by lazy, but with a parameter. That's because I need something that's created very late in order to initialize it. Specifically, I wish I had: private lateinit val controlObj:SomeView or: private val controlObj:SomeView by lazy { view:View->view.findViewById(...)} and then: override fun onCreateView(....) { val view = inflate(....) controlObj = view.findViewById(...) or in the 2nd case controlObj.initWith(view) or

R - non linear regression (nls) and polynomial interactions (poly)

旧时模样 提交于 2020-04-16 04:21:29
问题 I can run a nls regression at R if I explicitly define the parameters ("a" and "b" in the example below). However, how could I code the nls with a generic number of variables/higher degress in the poly function? df <- data.frame(var1 = rnorm(100), var2 = rnorm(100)) p <- as.data.frame(poly(df$var2, degree = 2)) names(p) <- paste0("poly", names(p)) df <- cbind(df, p) nls(var1 ~ a*poly1 + b*poly2, data = df, start = list(a = 1, b = 2)) Trying code, as is done with the lm function, is not

Trouble understanding / visualising SICP streams Hamming numbers program

て烟熏妆下的殇ゞ 提交于 2020-02-03 20:54:27
问题 I'm basically stuck at excercise 3.56 in SICP. The problem goes like this: Exercise 3.56. A famous problem, first raised by R. Hamming, is to enumerate, in ascending order with no repetitions, all positive integers with no prime factors other than 2, 3, or 5. One obvious way to do this is to simply test each integer in turn to see whether it has any factors other than 2, 3, and 5. But this is very inefficient, since, as the integers get larger, fewer and fewer of them fit the requirement. As

Lazy class property decorator

蹲街弑〆低调 提交于 2020-01-30 06:38:45
问题 I have one django model which needs to do some processing referring the custom user model. I can't work with the class of this model at class loading time because the loading order of the classes is unknown. So I need to add some class attributes at runtime, at the moment I'm adding them in the __init__ or __new__ like: def __new__(cls, *args, **kwargs): # hack to avoid INSTALLED_APPS initialization conflicts. # get_user_model() can't be called from this module at class loading time, # so