Skip to content

Commit b2f3c8c

Browse files
committed
+ Functions for IList and IReadOnlyList
1 parent 466d3e1 commit b2f3c8c

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/FSharpPlus/Extensions/IList.fs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace FSharpPlus
22

3-
#if !FABLE_COMPILER
43

54
/// Additional operations IList<'T>
65
[<RequireQualifiedAccess>]
@@ -9,9 +8,17 @@ module IList =
98
open System.Collections.ObjectModel
109
open System.Collections.Generic
1110

11+
#if !FABLE_COMPILER
1212
/// <summary>Converts an IList to an IReadOnlyList (from System.Collections.Generic).</summary>
1313
/// <param name="source">The System.Collections.Generic.IList</param>
1414
/// <returns>The list converted to a System.Collections.Generic.IReadOnlyList</returns>
1515
let toIReadOnlyList (source: IList<_>) = ReadOnlyCollection source :> IReadOnlyList<_>
1616

1717
#endif
18+
19+
let ofArray (source: 'T[] ) = source :> IList<'T>
20+
let ofList (source: 'T list) = source |> Array.ofList :> IList<'T>
21+
let ofSeq (source: seq<'T>) = source |> Array.ofSeq :> IList<'T>
22+
let map mapping (source: IList<'T>) = Seq.map mapping source |> Seq.toArray :> IList<'U>
23+
let iter mapping (source: IList<'T>) = Seq.iter mapping source
24+

src/FSharpPlus/Extensions/IReadOnlyList.fs

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ module IReadOnlyList =
88
#if !FABLE_COMPILER
99

1010
let ofArray (source: 'T array) = IList.toIReadOnlyList source
11+
let ofList (source: 'T list) = source |> Array.ofList |> IList.toIReadOnlyList
12+
let ofSeq (source: seq<'T>) = source |> Array.ofSeq |> IList.toIReadOnlyList
13+
1114
#endif
15+
1216
let toArray (source: IReadOnlyList<'T>) = Array.ofSeq source
1317

1418
#if !FABLE_COMPILER
@@ -19,8 +23,13 @@ module IReadOnlyList =
1923
if 0 <= i && i < source.Count then
2024
source |> Array.ofSeq |> setNth i value |> ofArray |> Some
2125
else None
26+
27+
let map mapping (source: IReadOnlyList<'T>) : IReadOnlyList<'U> = Seq.map mapping source |> Seq.toArray |> IList.toIReadOnlyList
28+
2229
#endif
2330

2431
let tryItem i (source: IReadOnlyList<_>) =
2532
if 0 <= i && i < source.Count then Some source.[i]
26-
else None
33+
else None
34+
35+
let iter mapping (source: IReadOnlyList<'T>) = Seq.iter mapping source

0 commit comments

Comments
 (0)