Skip to content

Commit

Permalink
Matrix and Vector implement interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty committed Nov 26, 2023
1 parent b1ed24b commit 4b38cbf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
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)

0 comments on commit 4b38cbf

Please sign in to comment.