Skip to content

Commit

Permalink
+ Functions for IList and IReadOnlyList
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty committed Oct 15, 2023
1 parent 466d3e1 commit c10bd73
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/FSharpPlus/Extensions/IList.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ module IList =
/// <returns>The list converted to a System.Collections.Generic.IReadOnlyList</returns>
let toIReadOnlyList (source: IList<_>) = ReadOnlyCollection source :> IReadOnlyList<_>

let ofArray (source: 'T[] ) = source :> IList<'T>
let ofList (source: 'T list) = source |> Array.ofList :> IList<'T>
let ofSeq (source: seq<'T>) = source |> Array.ofSeq :> IList<'T>
let map mapping (source: IList<'T>) = Seq.map mapping source |> Seq.toArray :> IList<'U>
let iter mapping (source: IList<'T>) = Seq.iter mapping source

#endif
9 changes: 8 additions & 1 deletion src/FSharpPlus/Extensions/IReadOnlyList.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ module IReadOnlyList =
if 0 <= i && i < source.Count then
source |> Array.ofSeq |> setNth i value |> ofArray |> Some
else None

let ofList (source: 'T list) = source |> Array.ofList |> IList.toIReadOnlyList
let ofSeq (source: seq<'T>) = source |> Array.ofSeq |> IList.toIReadOnlyList

#endif

let tryItem i (source: IReadOnlyList<_>) =
if 0 <= i && i < source.Count then Some source.[i]
else None
else None

let map mapping (source: IReadOnlyList<'T>) : IReadOnlyList<'U> = Seq.map mapping source |> Seq.toArray |> IList.toIReadOnlyList

Check failure on line 32 in src/FSharpPlus/Extensions/IReadOnlyList.fs

View workflow job for this annotation

GitHub Actions / testFable3SubsetOnCore

The type 'IList<_>' does not define the field, constructor or member 'toIReadOnlyList'.
let iter mapping (source: IReadOnlyList<'T>) = Seq.iter mapping source

0 comments on commit c10bd73

Please sign in to comment.