lazy-evaluation

@transient lazy val field serialization

南笙酒味 提交于 2019-12-03 02:41:59
I have a problem on Scala. I serialize an instance of class with @transient lazy val field. And then I deserialize it, the field is assigned null . I expect the lazy evaluation after deserialization. What should I do? Following is a sample code. object Test { def main(args: Array[String]){ //---------------- // ClassA - with @transient //---------------- val objA1 = ClassA("world"); println(objA1); // This works as expected as follows: // "Good morning." // "Hello, world" saveObject("testA.dat", objA1); val objA2 = loadObject("testA.dat").asInstanceOf[ClassA]; println(objA2); // I expect this

Lazy evaluation in Ruby

亡梦爱人 提交于 2019-12-03 02:02:20
问题 I have a situation for Ruby, where an object is possibly necessary to be created, but it is not sure. And as the creation of the object might be costly I am not too eager creating it. I think this is a clear case for lazy loading. How can I define an object which is not created only when someone sends a message to it? The object would be created in a block. Is there a way for simple lazy loading/initialisation in Ruby? Are these things supported by some gems, which provide different solutions

How to not fall into R's 'lazy evaluation trap'

≯℡__Kan透↙ 提交于 2019-12-03 01:57:02
"R passes promises , not values. The promise is forced when it is first evaluated, not when it is passed.", see this answer by G. Grothendieck. Also see this question referring to Hadley's book. In simple examples such as > funs <- lapply(1:10, function(i) function() print(i)) > funs[[1]]() [1] 10 > funs[[2]]() [1] 10 it is possible to take such unintuitive behaviour into account. However, I find myself frequently falling into this trap during daily development. I follow a rather functional programming style, which means that I often have a function A returning a function B, where B is in some

What are Haskell's strictness points?

会有一股神秘感。 提交于 2019-12-03 01:47:20
问题 We all know (or should know) that Haskell is lazy by default. Nothing is evaluated until it must be evaluated. So when must something be evaluated? There are points where Haskell must be strict. I call these "strictness points", although this particular term isn't as widespread as I had thought. According to me: Reduction (or evaluation) in Haskell only occurs at strictness points. So the question is: what, precisely , are Haskell's strictness points? My intuition says that main , seq / bang

How is debugging achieved in a lazy functional programming language?

≯℡__Kan透↙ 提交于 2019-12-03 01:43:08
问题 I'd like to know how debugging is achieved in a lazy functional language. Can you use breakpoints, print statements and traditional techniques? Is this even a good idea? It is my understanding that pure functional programming does not allow side-effects, with the exception of monads. Order of execution is also not guaranteed. Would you have to program a monad for every section of code you want to test? I'd like some insight into this question from someone more experienced in this area. 回答1:

Force pre-computation of a constant

女生的网名这么多〃 提交于 2019-12-02 23:19:39
I have a constant declaration in Haskell -- can I force this to be evaluated ahead of time? I'm seeing some code that looks roughly like, myList = [(a, b), (c, d)] ... map (f . fst) myList take time in the fst call when I profile it (it does have 168M calls). The binary representation of myList is quite small, and could be, for example, copied into global memory [if this were a C program]. I'm compiling with -O3 -optc-O3 of course. Thanks very much! Generating Lift instances for a custom type Any expression given to the lift call in sclv's answer must be an instance of Lift. There's a library

Least-strict (*)

不问归期 提交于 2019-12-02 22:26:09
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. Philip JF Yes, but only using constrained impurity. laziestMult :: Num a => a -> a -> a laziestMult a b = (a * b) `unamb` (b * a) here unamb is Conal Elliott's "pure" variant of amb . Operationally,

Truly declarative language?

假如想象 提交于 2019-12-02 22:21:16
Does anyone know of a truly declarative language? The behaviour I'm looking for is kind of what Excel does, where I can define variables and formulas, and have the formula's result change when the input changes (without having set the answer again myself) The behaviour I'm looking for is best shown with this pseudo code: X = 10 // define and assign two variables Y = 20; Z = X + Y // declare a formula that uses these two variables X = 50 // change one of the input variables ?Z // asking for Z should now give 70 (50 + 20) I've tried this in a lot of languages like F#, python, matlab etc, but

Is everything in Haskell stored in thunks, even simple values?

烈酒焚心 提交于 2019-12-02 21:41:35
What do the thunks for the following value/expression/function look like in the Haskell heap? val = 5 -- is `val` a pointer to a box containing 5? add x y = x + y result = add 2 val main = print $ result Would be nice to have a picture of how these are represented in Haskell, given its lazy evaluation mode. Official answer It's none of your business. Strictly implementation detail of your compiler. Short answer Yes. Longer answer To the Haskell program itself, the answer is always yes, but the compiler can and will do things differently if it finds out that it can get away with it, for

lazy function definitions in scala

大憨熊 提交于 2019-12-02 20:51:40
I've been learning scala and I gotta say that it's a really cool language. I especially like its pattern matching capabilities and function literals but I come from a javascript, ruby background and one of my favorite patterns in those languages is the lazy function and method definition pattern. An example in javascript is var foo = function() { var t = new Date(); foo = function() { return t; }; return foo(); }; The same code with minor tweaks works in ruby where you just use the singleton object to redefine the method after the computation is performed. This kind of thing comes in really