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.
69
questions
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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?
...
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 ...
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 ...
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:
...
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 ...
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: ...
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 "...
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 ...
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 ...
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 ...
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 ...
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:
...
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 ...
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 ...
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 ...
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 ...
-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:
...
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 ...
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 ...
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 ...
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 ...
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 ...
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....
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.
...
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 ...
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 ...
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:
...
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 ...
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, ...
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 ...
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. ...
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 ...
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.
...
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* ...
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 ...
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 ...
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)...
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 ...
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 ...
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.
...
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 ...
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 ...
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 ...
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.
...