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 9b34ece
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/FSharpPlus/Control/Alternative.fs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
namespace FSharpPlus.Control

/// <namespacedoc>
/// <summary>
/// Generally internal and not useful directly - contains generic function overloaded implementations.
/// </summary>
/// </namespacedoc>
namespace FSharpPlus.Control

#if (!FABLE_COMPILER || FABLE_COMPILER_3) && !FABLE_COMPILER_4

Expand Down
8 changes: 5 additions & 3 deletions src/FSharpPlus/Extensions/IList.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace FSharpPlus

#if !FABLE_COMPILER

/// Additional operations IList<'T>
[<RequireQualifiedAccess>]
module IList =
Expand All @@ -14,4 +12,8 @@ module IList =
/// <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
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
let iter mapping (source: IReadOnlyList<'T>) = Seq.iter mapping source

0 comments on commit 9b34ece

Please sign in to comment.