Skip to content

[Repo Assist] feat: add LazyList.consLazy for constructing from Lazy(LazyList<'T)>#239

Draft
github-actions[bot] wants to merge 2 commits intomasterfrom
repo-assist/improve-lazylist-conslazy-23c4939b68642ec5
Draft

[Repo Assist] feat: add LazyList.consLazy for constructing from Lazy(LazyList<'T)>#239
github-actions[bot] wants to merge 2 commits intomasterfrom
repo-assist/improve-lazylist-conslazy-23c4939b68642ec5

Conversation

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented Mar 9, 2026

🤖 This PR was created by Repo Assist, an automated AI assistant.

Implements the enhancement requested in #134.

Summary

LazyList.consDelayed accepts unit -> LazyList<'T> (a thunk). When a Lazy(LazyList<'T)> value is already available, wrapping it in fun () -> tail.Value works but is unnecessarily wasteful — it allocates an extra closure and forgoes .NET's built-in Lazy caching.

consLazy provides the direct construction:

val consLazy : 'T -> Lazy(LazyList<'T)> -> LazyList<'T>

Usage example (infinite list of 1s, as shown in #134):

let rec ones: LazyList(int) = LazyList.consLazy 1 (lazy ones)

Implementation

let consLazy x (l: Lazy(LazyList<'T)>) =
    lzy(fun () -> consc x l.Value)

The outer lzy keeps the head cell lazy until the list is consumed. l.Value is the standard .NET Lazy(T) evaluation, which caches the result and is evaluated at most once.

Changes

  • src/FSharpx.Collections/LazyList.fs — add consLazy
  • src/FSharpx.Collections/LazyList.fsi — add signature + doc comment
  • tests/FSharpx.Collections.Tests/LazyListTests.fs — 4 new tests: head access, toList, lazy divergence check (tail not evaluated on construction), and infinite list

Test Status

✅ All 710 tests pass (Passed: 710, Skipped: 6, Failed: 0). The 6 skipped tests are pre-existing skips unrelated to this change.

Related to #134

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@346204513ecfa08b81566450d7d599556807389f

Implements the enhancement requested in #134. consLazy takes a head
value and a Lazy<LazyList<'T>> tail, providing a more efficient
alternative to consDelayed when a Lazy value is already available.

Example use:
  let rec ones: LazyList<int> = LazyList.consLazy 1 (lazy ones)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants