Skip to content

Instantly share code, notes, and snippets.

@nshibano
Last active July 18, 2017 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nshibano/a9ab56ba75d662ca581d5f063a038167 to your computer and use it in GitHub Desktop.
Save nshibano/a9ab56ba75d662ca581d5f063a038167 to your computer and use it in GitHub Desktop.
IReadOnlyList extensions
open System
open System.Collections.Generic
[<AutoOpen>]
module Extensions =
type IReadOnlyList<'T> with
static member Append(a : IReadOnlyList<'T>, b : IReadOnlyList<'T>) =
{ new IReadOnlyList<'T> with
member this.Count = a.Count + b.Count
member this.Item with get i = if i < a.Count then a.[i] else b.[i - a.Count]
member this.GetEnumerator () = (seq { for i = 0 to this.Count - 1 do yield this.[i]}).GetEnumerator()
member this.GetEnumerator () = this.GetEnumerator() :> System.Collections.IEnumerator }
static member Init(count : int, func : int -> 'T) =
{ new IReadOnlyList<'T> with
member this.Count = count
member this.Item with get i = if 0 <= i && i < count then func i else raise (IndexOutOfRangeException())
member this.GetEnumerator () = (seq { for i = 0 to this.Count - 1 do yield this.[i]}).GetEnumerator()
member this.GetEnumerator () = this.GetEnumerator() :> System.Collections.IEnumerator }
member this.ToArray() =
let ary = Array.zeroCreate<'T> this.Count
for i = 0 to this.Count - 1 do
ary.[i] <- this.[i]
ary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment