Questions tagged [lazy]

Lazy evaluation refers to a variety of concepts that seek to avoid evaluation of an expression unless its value is needed, and to share the results of evaluation of an expression among all uses of its, so that no expression need be evaluated more than once.

Filter by
Sorted by
Tagged with
3 votes
1 answer
288 views

Implementation of java.util.stream.Stream (and friends) that reads lines from the internet without requiring you to manage the resources

This streams lines of information over the internet using BufferedReader::lines. However, what makes this special (and thus, extraordinarily complicated imo) is ...
davidalayachew's user avatar
5 votes
1 answer
2k views

Writing a lazy-evaluation dataclass [closed]

I have a dataclass which is frequently used but is slow because as it processes multiple large datasets. I have tried to speed it up by making it lazily-evaluate, where the data is only read when ...
tgpz's user avatar
  • 69
4 votes
1 answer
213 views

Task manager object for lazy initialization

The Problem I am developing a Visual Studio extension for importing code from a server. Because of the way Visual Studio works, multiple threads could try to perform the same operation at the same ...
trinalbadger587's user avatar
5 votes
0 answers
195 views

@lazylist - Converts a Python Generator to a List without evaluating every element

Inspired by a blog I read, I made a LazyList class that can wrap an iterable (list, generator etc.) and turn it into a ...
Greedo's user avatar
  • 2,345
2 votes
2 answers
181 views

How can I make a shorter binary rule in haskell?

Here is a code I experimented with recently: let br = [0]:[n:(concat $ take n br) | n <- [1..]] in concat br This code produces the binary rule ...
rambi's user avatar
  • 131
5 votes
1 answer
377 views

lazy_map implementation in C++ | Similar to unordered_map but O(1) copyable

I implemented lazy_map - https://github.com/tinytrashbin/lazy_map. Can someone help with reviews ? lazy_map is an implementation of unordered_map that has O(1) cost of copying irrespective of the ...
Mohit Saini's user avatar
4 votes
1 answer
1k views

C++ shared pointer wrapper for lazy initialization

I have written a very simple wrapper around std::shared_ptr that supports lazy initialization. Do you see any problems? ...
Martin Perry's user avatar
8 votes
4 answers
544 views

Lazy generator of strings in lexicographical order

I needed to write a Python generator that lazily generates, in lexicographical order, strings composed of a certain alphabet (e.g. lowercase English letters). My first thought was to make an infinite ...
Anakhand's user avatar
  • 645
6 votes
1 answer
365 views

Taking n lines from a file which contains m lines, repeating the file lazily if necessary

The desire for such a function came out of the necessity of generating a "source" of Lorem ipsum by repeating over and over the same 50 or something paragraphs of the text. Actually I am ...
Enlico's user avatar
  • 547
2 votes
1 answer
222 views

Efficient Implementation of functional and Lazy evaluation in C++

I am creating a c++ library implementing Java Functional Programming alike interface. In short, the code will look like this: ...
Harper's user avatar
  • 121
3 votes
0 answers
48 views

React.js component to lazy-load image

I'm trying to design a better solution for img srcset with lazy load. Below is the code from the component responsible for lazy load img, maybe someone has a better way to achieve this effect. Full ...
Jacob's user avatar
  • 31
3 votes
1 answer
2k views

Swift protocol with lazy property requirement

First question here on CodeReview. I was directed here from a question I asked on SO: Swift protocol with lazy property - Cannot use mutating getter on immutable value: '$0' is immutable. Goal: ...
koen's user avatar
  • 179
7 votes
1 answer
1k views

Lazy Resource Loading class in C++

Recently I was studying about the benefits and uses of std::optional in C++17 (Bartek's Blog) and there I saw a line that said "...
Cosmo Ferrito's user avatar
3 votes
1 answer
1k views

Cancelable AsyncLazy that is thread-safe

I've been thinking about the AsyncLazy<T> class that Stephen Toub and Stephen Cleary present in various resources such as this. I am trying to find a way to ...
rory.ap's user avatar
  • 267
2 votes
0 answers
2k views

JavaFX lazy loading of images in TableView

This code is based off https://stackoverflow.com/questions/25711103/javafx-combobox-lazy-loading-images but modified to work with a TableCell Firstly, the model object should have an observable ...
geometrikal's user avatar
9 votes
1 answer
456 views

Lazy, Functional Parser Combinators

Parser Combinators are the amazingly elegant way to write parsers that has evolved over on the functional programming side of the world, but have been less available or accessible for imperative ...
luser droog's user avatar
  • 2,018
1 vote
1 answer
1k views

Lazy evaluation function decorator

Suppose I have some expensive function expensive_computation which takes a long time to compute but it always returns the same value. I want to avoid paying the ...
cglacet's user avatar
  • 233
4 votes
1 answer
210 views

Lazy Loading a Bioinformatic SAM record

I'm currently writing an API to work with Bioinformatic SAM records. Here's an example of one: ...
Sam's user avatar
  • 332
1 vote
1 answer
1k views

Singleton class to contain a DocumentClient instance, that can be overridden for testing

The DocumentClient is an object that accesses CosmosDb, but Microsoft recommends its kept as a ...
Slothario's user avatar
  • 334
3 votes
2 answers
859 views

Lazy-loading iframes as they scroll into view

I'm working in a Tableau driven system, with large numbers of charts, I needed a way to reduce the strain of the initial load of our app. It's fairly simple, but wanted to ensure I accounted for every ...
darcher's user avatar
  • 724
27 votes
11 answers
7k views

Own implementation of Lazy<T> object

The problem with the original Lazy in C# is that you have to put the initialization in the constructor if you want to refer to this. For me that is 95% of the ...
Dirk Boer's user avatar
  • 650
1 vote
1 answer
318 views

Lazy prime counting function [closed]

I have written a simple snippet of code that implements a function that returns a list of primes up to a certain integer n. Assuming that this function could be ...
Anakhand's user avatar
  • 645
-1 votes
1 answer
58 views

Shorthand private field caching of public property [closed]

I'm caching the value of a configuration object in a private field: ...
crenshaw-dev's user avatar
19 votes
8 answers
3k views

Counting elements meeting a predicate in an enumerable without iterating the entire sequence

I was reading this question, and the answer that mentioned streams, and a thought had occurred to me. In many cases (at least in my usage) I want to determine if a sequence (or ...
Der Kommissar's user avatar
6 votes
1 answer
217 views

Lazy lists with transformations, using generators & iterators

I built this very-basic lazy list (I'll add more methods as I need them). You provide it an array, a generator or any iterator. It creates a lazy list, which lets you run a pipeline of transformations ...
Joseph Silber's user avatar
9 votes
2 answers
3k views

Lazy split and semi-lazy split

Sometimes I need to be able to split data into chunks, and so something like str.split would be helpful. This comes with two downsides: Input has to be strings You ...
Peilonrayz's user avatar
  • 43.1k
3 votes
1 answer
72 views

Python JIT container type

This is a follow on from this review. Where I am attempting to improve the performance of my rest client. I have created an container type (as suggested in my previous post) that lazily instantiates ...
James Schinner's user avatar
15 votes
1 answer
6k views

ResettableLazy<T>: a resettable version of .NET Lazy<T>

I implemented a resettable version of .NET Lazy (context: mostly to use it in a MemoryCache for auto refresh purposes on item expiration) This is a very naive implementation and I am pretty sure ...
Natalie Perret's user avatar
3 votes
1 answer
262 views

Efficiently determine every factor of a given natural number

It's not hard to find all of the factors of a given natural number. Below is a pretty fast python function that can do just that: (Modified version of Steinar Lima's function, https://stackoverflow....
Will Da Silva's user avatar
4 votes
1 answer
417 views

Selection algorithm using merge sort and IEnumerable

For educational purposes, I wrote a selection algorithm based on a Merge sort. I would like to improve performance. ...
tigrou's user avatar
  • 421
3 votes
1 answer
612 views

Lazy, functional streams library for Node.js

I’ve written a small library that implements lazy streams in (ES2016) JavaScript for use in Node.js. The library could work in the browser as well with some minor modifications, but that isn’t ...
Alexis King's user avatar
  • 3,172
5 votes
1 answer
1k views

Singletons with eager and lazy instantiation

I've found more than one implementation of the Singleton Pattern in Java (more like 5-6). I want to know if both of the following snippets of code (written by me) are valid implementations of ...
ovidiu-miu's user avatar
1 vote
1 answer
77 views

Class to help dealing with files, with lazy properties

I have the following class to help me deal with files: File class: ...
user avatar
3 votes
1 answer
4k views

Using Concurrent Dictionary and Lazy<T> to cache expensive query results and only run query once in a threadsafe manner

Ok, so I'm querying a webservice. This webservice is slow with multiple o's. I want to cache the results of the query, because I only want to query on a given set of parameters once during my ...
Sidney's user avatar
  • 215
5 votes
2 answers
230 views

Snoozy: A lazy reevaluation upon get

I would appreciate critique and comments in regards of thread-safety on a basic type I'm trying to create. In C#, the Lazy<T> type is initialized only once, ...
Reyhn's user avatar
  • 153
3 votes
1 answer
701 views

Optionally Lazy Parameters

Inspired by Swift's @autoclosure feature, I tried writing a brief C++-14 header that permits "optionally" lazy parameters (by "lazy" I mean ...
Kyle Strand's user avatar
5 votes
2 answers
2k views

Python lazy dict with iterator in constructor

This question began as an off-topic answer to this question, but the code here serves a different goal. I wrote the following class for the purpose of populating a dict on demand from an iterator. ...
Eirik Fuller's user avatar
1 vote
1 answer
3k views

C++ lazy constructor

I'm implementing a lazy constructor in C++. The goal is that for a type T, lazy<T>(args...) returns a callable object ...
Zizheng Tai's user avatar
5 votes
1 answer
607 views

Lazy String splitter in C++

I created a string splitter in C++. It splits a string in a lazy fashion and makes use of forward iterators to sequentially supply the next token from the string until it runs out of tokens. ...
smac89's user avatar
  • 1,467
6 votes
1 answer
1k views

Lazy prime number generator

There are multiple Project Euler problems (e.g. 7, 10) that deal with prime numbers. I decided make a reusable solution that could be used for most of them. I wrote a method to generate an infinite* ...
Gediminas Masaitis's user avatar
4 votes
2 answers
12k views

Finding a Cartesian product of multiple lists

I came up with an extension method to find a Cartesian product of multiple IEnumerable sets. I was able to achieve lazy enumeration via ...
Gediminas Masaitis's user avatar
3 votes
0 answers
423 views

Lazy WebAPI object

I am working to design an object which would abstract away the use of a WebAPI. I want the developer to be able to use these objects as if they are using a traditional API. My core problem is how to ...
David Price's user avatar
8 votes
1 answer
402 views

Caching data by using the result of first running operation

My code handles concurrent requests by waiting for the result of an already running operation. Requests for data may come in simultaneously with same/different credentials (including empty credentials)...
ipetr's user avatar
  • 1
11 votes
3 answers
1k views

Poor man's lazy evaluation in Java 8

I have an class that cheaply imitates a lazy evaluation scheme. The class is for representing a file and additionally providing meta data on the file. Some of the meta-data can be expensive to ...
cheezsteak's user avatar
  • 2,401
3 votes
3 answers
5k views

Unity Lazy Resolve Implementation

I have been using Microsoft.Unity as my container and have decided that the approach for a lazy implementation causes too much rework. Each time you decide to swap ...
sQuir3l's user avatar
  • 209
7 votes
2 answers
516 views

Reading lines from a file in random order

I originally wrote this as an answer to a question on Stack Overflow, but it turned out so nicely that I decided to post it here to see if I can make it even better. ...
Sam Estep's user avatar
  • 1,767
4 votes
1 answer
264 views

Clojure word count

I'm fairly new to Clojure and looking to improve my use of the proper idioms, and make my code more readable. Here's the problem: Read from standard input and produce the count of each word to ...
Ray Toal's user avatar
  • 765
4 votes
1 answer
2k views

Early-termination foldLeft for Scala Streams

The foldRight and foldLeft methods for Scala Streams cannot terminate early, so here is my ...
qed's user avatar
  • 1,419
2 votes
2 answers
2k views

Date formatters, lazily instantiated once

I would like to create two date formatters only on first call. In Objective-C I would use dispatch_once() but now working in Swift. Am I doing it correctly? My code ...
Szu's user avatar
  • 203
3 votes
3 answers
1k views

Loading visible images

I am using this code to get images to load only if they are visible. However, it seems to be slow with thousands of images, even if they are not rendered. ...
Ciprian's user avatar
  • 133