From 92afa42a8658347b89f0727e8337182d52b707b4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 16 May 2026 08:24:13 +0000 Subject: [PATCH] Add @@ infix operator for LazyList.consLazy with tests Agent-Logs-Url: https://github.com/fsprojects/FSharpx.Collections/sessions/6aa75bb1-5b3c-4098-ba66-f9885c271d76 Co-authored-by: gdziadkiewicz <8547855+gdziadkiewicz@users.noreply.github.com> --- .../AssemblyInfo.fs | 33 +++++-------------- src/FSharpx.Collections/AssemblyInfo.fs | 33 +++++-------------- .../FSharpx.Collections.fsproj | 1 + src/FSharpx.Collections/LazyListOperators.fs | 10 ++++++ .../LazyListTests.fs | 31 +++++++++++++++++ .../LazyList.test.fs | 15 +++++++++ 6 files changed, 73 insertions(+), 50 deletions(-) create mode 100644 src/FSharpx.Collections/LazyListOperators.fs diff --git a/src/FSharpx.Collections.Experimental/AssemblyInfo.fs b/src/FSharpx.Collections.Experimental/AssemblyInfo.fs index 329acac8..aeed0d4d 100644 --- a/src/FSharpx.Collections.Experimental/AssemblyInfo.fs +++ b/src/FSharpx.Collections.Experimental/AssemblyInfo.fs @@ -1,6 +1,5 @@ // Auto-Generated by FAKE; do not edit namespace System - open System.Reflection open System.Runtime.CompilerServices @@ -15,27 +14,11 @@ open System.Runtime.CompilerServices do () module internal AssemblyVersionInformation = - [] - let AssemblyTitle = "FSharpx.Collections.Experimental" - - [] - let AssemblyProduct = "FSharpx.Collections" - - [] - let AssemblyDescription = - "FSharpx.Collections is a collection of datastructures for use with F# and C#." - - [] - let InternalsVisibleTo = "FSharpx.Collections.Tests" - - [] - let InternalsVisibleTo_1 = "FSharpx.Collections.Experimental.Tests" - - [] - let AssemblyVersion = "3.1.0" - - [] - let AssemblyFileVersion = "3.1.0" - - [] - let AssemblyConfiguration = "Release" + let [] AssemblyTitle = "FSharpx.Collections.Experimental" + let [] AssemblyProduct = "FSharpx.Collections" + let [] AssemblyDescription = "FSharpx.Collections is a collection of datastructures for use with F# and C#." + let [] InternalsVisibleTo = "FSharpx.Collections.Tests" + let [] InternalsVisibleTo_1 = "FSharpx.Collections.Experimental.Tests" + let [] AssemblyVersion = "3.1.0" + let [] AssemblyFileVersion = "3.1.0" + let [] AssemblyConfiguration = "Release" diff --git a/src/FSharpx.Collections/AssemblyInfo.fs b/src/FSharpx.Collections/AssemblyInfo.fs index b156bd37..bd6e200f 100644 --- a/src/FSharpx.Collections/AssemblyInfo.fs +++ b/src/FSharpx.Collections/AssemblyInfo.fs @@ -1,6 +1,5 @@ // Auto-Generated by FAKE; do not edit namespace System - open System.Reflection open System.Runtime.CompilerServices @@ -15,27 +14,11 @@ open System.Runtime.CompilerServices do () module internal AssemblyVersionInformation = - [] - let AssemblyTitle = "FSharpx.Collections" - - [] - let AssemblyProduct = "FSharpx.Collections" - - [] - let AssemblyDescription = - "FSharpx.Collections is a collection of datastructures for use with F# and C#." - - [] - let InternalsVisibleTo = "FSharpx.Collections.Tests" - - [] - let InternalsVisibleTo_1 = "FSharpx.Collections.Experimental.Tests" - - [] - let AssemblyVersion = "3.1.0" - - [] - let AssemblyFileVersion = "3.1.0" - - [] - let AssemblyConfiguration = "Release" + let [] AssemblyTitle = "FSharpx.Collections" + let [] AssemblyProduct = "FSharpx.Collections" + let [] AssemblyDescription = "FSharpx.Collections is a collection of datastructures for use with F# and C#." + let [] InternalsVisibleTo = "FSharpx.Collections.Tests" + let [] InternalsVisibleTo_1 = "FSharpx.Collections.Experimental.Tests" + let [] AssemblyVersion = "3.1.0" + let [] AssemblyFileVersion = "3.1.0" + let [] AssemblyConfiguration = "Release" diff --git a/src/FSharpx.Collections/FSharpx.Collections.fsproj b/src/FSharpx.Collections/FSharpx.Collections.fsproj index e36674e2..e42e1c0e 100644 --- a/src/FSharpx.Collections/FSharpx.Collections.fsproj +++ b/src/FSharpx.Collections/FSharpx.Collections.fsproj @@ -25,6 +25,7 @@ + diff --git a/src/FSharpx.Collections/LazyListOperators.fs b/src/FSharpx.Collections/LazyListOperators.fs new file mode 100644 index 00000000..0d2fb1a5 --- /dev/null +++ b/src/FSharpx.Collections/LazyListOperators.fs @@ -0,0 +1,10 @@ +/// Operators for working with LazyList. +/// This module is opened automatically when FSharpx.Collections is opened. +[] +module FSharpx.Collections.LazyListOperators + +/// O(1). Returns a new LazyList with the given head and a lazy tail. +/// Infix operator alias for LazyList.consLazy. +/// Example: let rec ones = 1 @@ lazy ones +let inline (@@) (head: 'T) (tail: Lazy>) : LazyList<'T> = + LazyList.consLazy head tail diff --git a/tests/FSharpx.Collections.Tests/LazyListTests.fs b/tests/FSharpx.Collections.Tests/LazyListTests.fs index 0f726bfb..5b9dd7aa 100644 --- a/tests/FSharpx.Collections.Tests/LazyListTests.fs +++ b/tests/FSharpx.Collections.Tests/LazyListTests.fs @@ -348,6 +348,37 @@ module LazyList = Expect.equal "consLazy infinite" [ 1; 1; 1; 1; 1 ] (LazyList.take 5 ones |> LazyList.toList) } + test "@@ head" { + let tail = lazy (LazyList.ofList [ 2; 3 ]) + Expect.equal "@@ head" 1 (LazyList.head(1 @@ tail)) + } + + test "@@ toList" { + let tail = lazy (LazyList.ofList [ 2; 3 ]) + Expect.equal "@@ toList" [ 1; 2; 3 ] (LazyList.toList(1 @@ tail)) + } + + test "@@ lazy divergence" { + // tail is not evaluated unless the tail is consumed + let mutable tailForced = false + + let tail = + lazy + (tailForced <- true + LazyList.ofList [ 2; 3 ]) + + let ll = 1 @@ tail + Expect.isFalse "@@ divergence: construction should not force the tail" tailForced + let _ = LazyList.head ll + Expect.isFalse "@@ divergence: head should not force the tail" tailForced + } + + test "@@ infinite" { + // build ones = 1 :: 1 :: ... using @@ operator + let rec ones: LazyList = 1 @@ lazy ones + Expect.equal "@@ infinite" [ 1; 1; 1; 1; 1 ] (LazyList.take 5 ones |> LazyList.toList) + } + test "takedrop" { Expect.equal "takedrop" [ 4; 5; 6 ] <| LazyList.toList(LazyList.take 3 (LazyList.skip 4 nats)) diff --git a/tests/fable/FSharpx.Collections.Tests/LazyList.test.fs b/tests/fable/FSharpx.Collections.Tests/LazyList.test.fs index 3732a8f5..e9b4d123 100644 --- a/tests/fable/FSharpx.Collections.Tests/LazyList.test.fs +++ b/tests/fable/FSharpx.Collections.Tests/LazyList.test.fs @@ -19,4 +19,19 @@ let tests = let xs = xs |> LazyList.tail |> LazyList.tail Expect.equal (LazyList.tryHead xs) (Some 3) "should reach third element" + } + + test "@@ head" { + let tail = lazy (LazyList.ofList [ 2; 3 ]) + Expect.equal (LazyList.head(1 @@ tail)) 1 "@@ head" + } + + test "@@ toList" { + let tail = lazy (LazyList.ofList [ 2; 3 ]) + Expect.equal (LazyList.toList(1 @@ tail)) [ 1; 2; 3 ] "@@ toList" + } + + test "@@ infinite" { + let rec ones: LazyList = 1 @@ lazy ones + Expect.equal (LazyList.take 5 ones |> LazyList.toList) [ 1; 1; 1; 1; 1 ] "@@ infinite" } ]