lazy-evaluation

Would you ever write seq x x?

筅森魡賤 提交于 2019-12-23 07:30:31
问题 I'm not entirely clear on how seq works in Haskell. It seems like it there are lots of cases where it would be useful to write seq x x and maybe even define a function: strict x = seq x x but such a function doesn't already exist so I'm guessing this approach is somehow wrongheaded. Could someone tell me if this is meaningful or useful? 回答1: seq a b returns the value of b , but makes that value depend on the evaluation of a . Thus, seq a a is exactly the same thing as a . I think the

Spring Integration. Unknown host and tcp-connection-factory

假装没事ソ 提交于 2019-12-23 04:52:08
问题 I'm implementing the TCP client using the Spring Integration. The requirements are: 1. Through the UDP connection (from somewhere) receive the ip or host address of the TCP server. 2. Open TCP connection to the server, to the destination host from previous step and send some business data to this server. I use the Spring Integration framework, version "2.2.0.RELEASE", and the problem is that in the default configuration of the tcp-connection-factory the host attribute should be "hardcoded" in

Infinite random sequence loops with randomIO but not with getRandom

时光毁灭记忆、已成空白 提交于 2019-12-23 02:38:33
问题 I'm having difficulty trying to figure out a way to reason about why the following two, seemingly equivalent definitions of an infinite random number sequence ( inf and inf' ) are evaluated completely differently: import Control.Monad.Random (Rand, evalRandIO, getRandom) import System.Random (Random, RandomGen, randomIO) inf :: (RandomGen g, Random a) => Rand g [a] inf = sequence (repeat getRandom) inf' :: (Random a) => IO [a] inf' = sequence (repeat randomIO) -- OK main = do i <- evalRandIO

map not quite lazy?

守給你的承諾、 提交于 2019-12-22 12:26:34
问题 map doesn't seem quite as lazy as I would like, in this example map calls the function one time as I would expect: (first (map #(do (println "x: " %) %) '(0 1))) but in these two examples it calls the function two times: (first (map #(do (println "x: " %) %) '[0 1])) (first (map #(do (println "x: " %) %) (doall (range 2)))) What is the underlying principle for making the choice to be lazy or not? Is there a way to guarantee total laziness? Thanks for your time. 回答1: Map (and similar HOFs that

A syntax for custom lazy-evaluation/short-circuiting of function parameters

北慕城南 提交于 2019-12-22 10:44:58
问题 Oracle defines several structures that make use of what looks like lazy evaluation but what's actually short-circuiting. For example: x := case when 1 = 2 then count_all_prime_numbers_below(100000000) else 2*2 end; The function count_all(...) will never be called. However, what I'm more interested in is the syntax that looks like regular function call: x := coalesce(null, 42, hundreth_digit_of_pi()); Hundreth_digit_of_pi() will not be called since coalesce is not a regular function, but a

Differences in whether realization of a lazy sequence inside of a lazy sequence occurs

霸气de小男生 提交于 2019-12-22 09:48:23
问题 I wondered: What happens when you embed an expression that forces realization of a lazy sequence inside of an outer lazy sequence that's not realized? Answer: It seems to depend on how you create the outer lazy sequence. If the outer sequence comes from map , the inner sequence is realized, and if the outer sequence comes for iterate , it's not. Well, I'm pretty sure that that is not the right way to describe what happens below--I'm pretty sure that I'm not understanding something. Can

How to lazily read big Excel 2007 files with Apache POI

末鹿安然 提交于 2019-12-22 09:18:22
问题 I'd like to read a big Excel 2007 file with Apache POI. Quick start guide states that a File should be used to conserve memory. When opening a workbook, either a .xls HSSFWorkbook, or a .xlsx XSSFWorkbook, the Workbook can be loaded from either a File or an InputStream. Using a File object allows for lower memory consumption, while an InputStream requires more memory as it has to buffer the whole file. I therefore wrote approximately this: opcPackage = OPCPackage.open(file); XSSFWorkbook

Transpose on infinite stream loops forever

假装没事ソ 提交于 2019-12-22 08:26:44
问题 Consider the following: Stream.continually(List(1,2,3)).transpose You would expect to get something equivalent to: Stream( Stream.continually(1), Stream.continually(2), Stream.continually(3) ) However, the upper construct loops infinitely. Is there a workaround for this? Is this to be considered a bug? Question came up discussing this answer. EDIT I know that a function like this writable, see the following example. But is there something in the library? def myTranspose[A,B](in: Stream[A])

Playing with infinity - Lazy arithmetics

旧巷老猫 提交于 2019-12-22 06:10:37
问题 Many modern programming languages allow us to handle potentially infinite lists and to perform certain operations on them. Example [Python]: EvenSquareNumbers = ( x * x for x in naturals() if x mod 2 == 0 ) Such lists can exist because only elements that are actually required are computed. (Lazy evaluation) I just wondered out of interest whether it's possible (or even practised in certain languages) to extend the mechanism of lazy evaluation to arithmetics. Example: Given the infinite list

Playing with infinity - Lazy arithmetics

本秂侑毒 提交于 2019-12-22 06:10:02
问题 Many modern programming languages allow us to handle potentially infinite lists and to perform certain operations on them. Example [Python]: EvenSquareNumbers = ( x * x for x in naturals() if x mod 2 == 0 ) Such lists can exist because only elements that are actually required are computed. (Lazy evaluation) I just wondered out of interest whether it's possible (or even practised in certain languages) to extend the mechanism of lazy evaluation to arithmetics. Example: Given the infinite list