Last active
July 18, 2017 09:22
-
-
Save nshibano/a9ab56ba75d662ca581d5f063a038167 to your computer and use it in GitHub Desktop.
IReadOnlyList extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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