Extending existing module is just same syntax to creating new module. For example, add slice function like phosphorescence: The way to add both indexer and slice to F# sequence with extending both Seq and List modules.
> module Seq = - let slice (lower : int option, upper : int option) aSeq = - match lower, upper with - | Some(lower), Some(upper) -> aSeq |> Seq.skip lower |> Seq.take (upper - lower + 1) - | Some(lower), None -> aSeq |> Seq.skip lower - | None, Some(upper) -> aSeq |> Seq.take (upper + 1) - | None, None -> aSeq;; module Seq = begin val slice : lower:int option * upper:int option -> aSeq:seq<'a> -> seq<'a> end > let seq1To5 = seq {1..5};; val seq1To5 : seq<int> > Seq.slice (Some(1), Some(3)) seq1To5;; val it : seq<int> = seq [2; 3; 4] > module List = - let slice (lower : int option, upper : int option) aList = - match lower, upper with - | Some(lower), Some(upper) -> aList |> Seq.skip lower |> Seq.take (upper - lower + 1) |> Seq.toList - | Some(lower), None -> aList |> Seq.skip lower |> Seq.toList - | None, Some(upper) -> aList |> Seq.take (upper + 1) |> Seq.toList - | None, None -> aList;; module List = begin val slice : lower:int option * upper:int option -> aList:'a list -> 'a list end > let list1To5 = [1..5];; val list1To5 : int list = [1; 2; 3; 4; 5] > List.slice (Some(1), Some(3)) list1To5;; val it : int list = [2; 3; 4]
No comments:
Post a Comment