Skip to content

Matrix and Vector implement interfaces #572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/FSharpPlus.TypeLevel/Data/Matrix.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Matrix< 'Item, 'Row, 'Column > = private { Items: 'Item[,] } with
[<MethodImpl(MethodImplOptions.AggressiveInlining)>]
static member UnsafeCreate (_row: 'm, _column: 'n, items: _[,]) : Matrix<_, 'm, 'n> =
{ Items = items }
(*

interface System.Collections.Generic.IReadOnlyCollection<'Item> with
member this.Count = this.Items.Length
member this.GetEnumerator() = this.Items.GetEnumerator()
Expand All @@ -27,7 +27,7 @@ type Matrix< 'Item, 'Row, 'Column > = private { Items: 'Item[,] } with
for j = 0 to (items |> Array2D.length2) - 1 do
yield items.[i,j]
}).GetEnumerator()
*)


[<Struct; StructuredFormatDisplayAttribute("{Items}")>]
type Vector<'Item, 'Length> = private { Items: 'Item[] } with
Expand All @@ -36,13 +36,13 @@ type Vector<'Item, 'Length> = private { Items: 'Item[] } with
[<MethodImpl(MethodImplOptions.AggressiveInlining)>]
static member UnsafeCreate (_length: 'n, items: _[]) : Vector<_, 'n> =
{ Items = items }
(*

interface System.Collections.Generic.IReadOnlyList<'Item> with
member this.Count = this.Items.Length
member this.Item with get i = this.Items.[i]
member this.GetEnumerator() = this.Items.GetEnumerator()
member this.GetEnumerator() = (this.Items :> seq<_>).GetEnumerator()
*)


module Vector =
[<MethodImpl(MethodImplOptions.AggressiveInlining)>]
Expand Down
20 changes: 20 additions & 0 deletions tests/FSharpPlus.Tests/TypeLevel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,23 @@ module TypeProviderTests =
Assert (Matrix.colLength row1 =^ (Z |> S |> S |> S))
areEqual 5 (Matrix.get Z (S Z) row1)
areEqual [3; 6; 9] (Vector.toList col2)

module TestFunctors1 =
[<Test>]
let applicativeOperatorWorks() =
let v = vector ((fun i -> i + 1), (fun i -> i * 2))
let u = vector (2, 3)
let vu = v <*> u
NUnit.Framework.Assert.IsInstanceOf<Option<Vector<int,S<S<Z>>>>> (Some vu)
CollectionAssert.AreEqual ([|3; 6|], Vector.toArray vu)

module TestFunctors2 =
open FSharpPlus

[<Test>]
let applicativeWorksWithoutSubsumption() =
let v = vector ((fun i -> i + 1), (fun i -> i * 2))
let u = vector (2, 3)
let vu = v <*> u
NUnit.Framework.Assert.IsInstanceOf<Option<Vector<int,S<S<Z>>>>> (Some vu)
CollectionAssert.AreEqual ([|3; 6|], Vector.toArray vu)