lazy-evaluation

Non-pointer-operand error when dereferencing an iterator into a temporary range

我们两清 提交于 2019-12-10 16:29:41
问题 Using auto empty_line = [](auto& str){ return str.size() == 0; }; we can do this: auto line_range_with_first_non_empty = ranges::view::drop_while(ranges::getlines(std::cin),empty_line); auto input1 = std::stoi(*line_range_with_first_non_empty.begin()); and we can also do this: auto line_range2 = ranges::getlines(std::cin); auto iter2 = ranges::find_if_not(line_range2,empty_line); auto input2 = std::stoi(*iter2); Unfortunately, when I try to shorten version above into: auto iter3 = ranges:

Keeping IO lazy under append

穿精又带淫゛_ 提交于 2019-12-10 15:27:39
问题 I may have been under the false impression that Haskell is lazier than it is, but I wonder if there's a way to get the best of both worlds... Data.Monoid and Data.Semigroup define two variations of First . The monoidal version models the leftmost, non-empty value, whereas the semigroup version simply models the leftmost value. This works fine for pure value values, but consider impure values: x = putStrLn "x" >> return 42 y = putStrLn "y" >> return 1337 Both of these values have the type Num

Python lazy evaluation numpy ndarray

拈花ヽ惹草 提交于 2019-12-10 15:26:58
问题 I have a large 2D array that I would like to declare once, and change occasionnaly only some values depending on a parameter, without traversing the whole array. To build this array, I have subclassed the numpy ndarray class with dtype=object and assign to the elements I want to change a function e.g. : def f(parameter): return parameter**2 for i in range(np.shape(A)[0]): A[i,i]=f for j in range(np.shape(A)[0]): A[i,j]=1. I have then overridden the __getitem__ method so that it returns the

Evaluation of higher-order functions

这一生的挚爱 提交于 2019-12-10 15:19:24
问题 I've written a function getSamplesFromFile that takes a file and returns its contents as a Vector of Floats . The functions reads the contents of the file into a Data.ByteString using Data.ByteString.hGet , it then converts this Data.ByteString to a Vector of Floats using: import qualified Data.Vector.Unboxed as V import qualified Data.ByteString as BS import Data.Word import System.Environment import GHC.Int toVector :: BS.ByteString -> V.Vector Float toVector bs = vgenerate (fromIntegral

F# lazy eval from stream reader?

只谈情不闲聊 提交于 2019-12-10 15:18:45
问题 I'm running into a bug in my code that makes me think that I don't really understand some of the details about F# and lazy evaluation. I know that F# evaluates eagerly and therefore am somewhat perplexed by the following function: // Open a file, then read from it. Close the file. return the data. let getStringFromFile = File.OpenRead("c:\\eo\\raw.txt") |> fun s -> let r = new StreamReader(s) let data = r.ReadToEnd r.Close() s.Close() data When I call this in FSI: > let d = getStringFromFile(

Dealing with lazy computation in C++ classes

为君一笑 提交于 2019-12-10 15:18:10
问题 Let's say I have a class: class NumberCollection { public: typedef std::set<int> SetType; typedef SetType::iterator iterator; void insert(int n); iterator begin(); iterator end(); size_t size() const; iterator difficultBegin(); iterator difficultEnd(); size_t difficultSize() const; private: SetType easySet_, difficultSet_; } Where insert() adds an element to easySet_ . difficultSet_ 's members change depending on the members of easySet_ . The problem I am having is that, multiple insertions

Least expensive way to construct cyclic list in Haskell

落花浮王杯 提交于 2019-12-10 13:46:24
问题 So if I want to construct a circular list of n 0's and 1 1, which of the following ways is better/cheaper? And is there an even better/cheaper way? Taking into account that n is an Integer and may be large (though realistically its not going to exceed 2^32). aZerosAndOnes :: Integer -> [Int] aZerosAndOnes n | n >= 0 = cycle (genericReplicate n 0 ++ [1]) | otherwise = [] versus bZerosAndOnes :: Integer -> [Int] bZerosAndOnes n | n >= 0 = tail (cycle (1 : genericReplicate n 0)) | otherwise = []

Different LINQ Answer in VS 2010 and VS 2012

穿精又带淫゛_ 提交于 2019-12-10 13:18:13
问题 The following gives answer as 1 in VS 2010 and 2 in VS 2012. I personally think it should be 2. I am not sure what's going on here. using System.Linq; using System.Text; using System.Threading.Tasks; using System; namespace _335ExamPreparation { public class Doubts { int[] nums = { 10, 11, 12, 13, 14, 15, 16 }; int[] divisors = { 7, 10 }; static void Main(string[] args) { Doubts d = new Doubts(); d.func(); } public void func() { var m = Enumerable.Empty<int>(); foreach (int d in divisors) { m

R functions that pass on unevaluated arguments to other functions

北战南征 提交于 2019-12-10 09:43:45
问题 I'm still trying to understand lazy evaluation in R. Here's something that confuses me: f=function(x) as.character(substitute(x)) g=function(...) f(...) h=function(z) f(z) f(y) # [1] "y" g(y) # [1] "y" h(y) # [1] "z" Why do g and h not behave the same way? 回答1: The Lazy Evaluation aspect of R is that it does not evaluate an object until it is needed. However, what you are seeing has little to do with lazy evaluation. Rather it is related to the ellipsis argument: When calling g(y) , f is

Spring + hibernate lazy fetching

落爺英雄遲暮 提交于 2019-12-10 08:49:01
问题 I have problem with org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role. How to implement lazy fetching with gwt + spring + hibernate? Here's my appContext: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www