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 b2f3c8c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/FSharpPlus/Extensions/IList.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace FSharpPlus

#if !FABLE_COMPILER

/// Additional operations IList<'T>
[<RequireQualifiedAccess>]
Expand All @@ -9,9 +8,17 @@ module IList =
open System.Collections.ObjectModel
open System.Collections.Generic

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

#endif

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

11 changes: 10 additions & 1 deletion src/FSharpPlus/Extensions/IReadOnlyList.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ module IReadOnlyList =
#if !FABLE_COMPILER

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

#endif

let toArray (source: IReadOnlyList<'T>) = Array.ofSeq source

#if !FABLE_COMPILER
Expand All @@ -19,8 +23,13 @@ module IReadOnlyList =
if 0 <= i && i < source.Count then
source |> Array.ofSeq |> setNth i value |> ofArray |> Some
else None

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

#endif

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

let iter mapping (source: IReadOnlyList<'T>) = Seq.iter mapping source

0 comments on commit b2f3c8c

Please sign in to comment.