lazy-evaluation

Ruby Challenge - Method chaining and Lazy Evaluation

喜你入骨 提交于 2019-12-06 01:35:26
问题 After reading the article http://jeffkreeftmeijer.com/2011/method-chaining-and-lazy-evaluation-in-ruby/, I started looking for a better solution for method chaining and lazy evaluation. I think I've encapsulated the core problem with the five specs below; can anyone get them all passing? Anything goes: subclassing, delegation, meta-programming, but discouraged for the latter. It would be favourable to keep dependencies to a minimum: require 'rspec' class Foo # Epic code here end describe Foo

Lazily concatenate an enumerable of lists

喜夏-厌秋 提交于 2019-12-06 00:13:07
问题 I would like to write a function akin to List.concat/1 that takes an enumerable of lists and emits the concatenated lists as a continuous stream. It would work like this: iex> 1..3 |> Stream.map(&([&1])) |> Enum.to_list [[1], [2], [3]] iex> 1..3 |> Stream.map(&([&1])) |> MyStream.concat |> Enum.to_list [1, 2, 3] What I have come up with so far is this: defmodule MyStream do def concat(lists) do Enumerable.reduce(lists, [], fn(x, acc) -> acc ++ x end) end end This produces the correct result

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

◇◆丶佛笑我妖孽 提交于 2019-12-05 23:51:31
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 syntax sugar that looks like one - for regular ones parameters get evaluated when the function is called.

Laziness in C++11

非 Y 不嫁゛ 提交于 2019-12-05 23:02:30
Do you know how to perform a lazy evaluation of string, like in this D snippet: void log(lazy string msg) { static if (fooBarCondition) writefln(…) /* something with msg */ } Actually, the problem might not need laziness at all since the static if. Maybe it’s possible to discard char const* strings when not used? Like, in C++: void log(char const *msg) { #ifdef DEBUG cout << … << endl; /* something with msg */ #else /* nothing at all */ #endif } Any idea? Thank you. #ifdef DEBUG #define log(msg) do { cout << … << endl; } while(0) #else #define log(msg) do { } while(0) #endif There are two ways

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

丶灬走出姿态 提交于 2019-12-05 20:52:48
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 someone explain? (There is one quirk, which is that while map returns a LazySeq , iterate returns a Cons

R functions that pass on unevaluated arguments to other functions

孤者浪人 提交于 2019-12-05 19:21:06
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? 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 substituting the value of x with the ellipsis. But substitute(...) essentially substitutes in the value sent to

Listing all the contents of a directory by breadth-first order results in low efficiency

↘锁芯ラ 提交于 2019-12-05 18:54:28
I writed a Haskell module to list all the contents of a directory by breadth-first order. The below is the source code. module DirElements (dirElem) where import System.Directory (getDirectoryContents, doesDirectoryExist) import System.FilePath ((</>)) dirElem :: FilePath -> IO [[FilePath]] dirElem dirPath = iterateM (not.null) (concatMapM getDirectoryContents') [dirPath] >>= return.tail getDirectoryContents' :: FilePath -> IO [FilePath] getDirectoryContents' dirPath = do isDir <- do doesDirectoryExist dirPath if isDir then dirContent else return [] where dirContent = do contents <-

Learning Haskell: Seemingly Circular Program - Please help explain

为君一笑 提交于 2019-12-05 18:23:39
问题 I'm currently going through the book "The Haskell Road to Logic, Math, and Programming" by Doets and Van Eijck. I've never been exposed to any functional programming language until this book, so keep that in mind. Still early in the book, it gives the following code for a primality test: ldp :: Integer -> Integer ldp n = ldpf primes1 n ldpf :: [Integer] -> Integer -> Integer ldpf (p:ps) n | rem n p == 0 = p | p^2 > n = n | otherwise = ldpf ps n primes1 :: [Integer] primes1 = 2 : filter prime

Lazy transform in C++

廉价感情. 提交于 2019-12-05 18:02:28
问题 I have the following Python snippet that I would like to reproduce using C++: from itertools import count, imap source = count(1) pipe1 = imap(lambda x: 2 * x, source) pipe2 = imap(lambda x: x + 1, pipe1) sink = imap(lambda x: 3 * x, pipe2) for i in sink: print i I've heard of Boost Phoenix, but I couldn't find an example of a lazy transform behaving in the same way as Python's imap . Edit: to clarify my question, the idea is not only to apply functions in sequence using a for , but rather to

Improving clojure lazy-seq usage for iterative text parsing

非 Y 不嫁゛ 提交于 2019-12-05 17:17:32
I'm writing a Clojure implementation of this coding challenge , attempting to find the average length of sequence records in Fasta format: >1 GATCGA GTC >2 GCA >3 AAAAA For more background see this related StackOverflow post about an Erlang solution. My beginner Clojure attempt uses lazy-seq to attempt to read in the file one record at a time so it will scale to large files. However it is fairly memory hungry and slow, so I suspect that it's not implemented optimally. Here is a solution using the BioJava library to abstract out the parsing of the records: (import '(org.biojava.bio.seq.io