Skip to content

Commit a4819fc

Browse files
committed
Compact code
1 parent f17a734 commit a4819fc

File tree

1 file changed

+27
-80
lines changed

1 file changed

+27
-80
lines changed

src/FSharpPlus/Data/Seq.fs

Lines changed: 27 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,7 @@ module SeqT_V2 =
214214
let mutable state = SeqState.NotStarted source
215215
let mutable current = Option<'T>.None
216216
{ new IEnumeratorM<'``Monad<bool>``, 'T> with
217-
member _.Current =
218-
match current with
219-
| Some c -> c
220-
| None -> invalidOp enumNotStarted
217+
member _.Current = Option.defaultWith (fun () -> invalidOp enumNotStarted) current
221218
member x.MoveNext () = monad' {
222219
match state with
223220
| SeqState.NotStarted inp ->
@@ -250,10 +247,7 @@ module SeqT_V2 =
250247
let mutable state = SeqState.NotStarted source
251248
let mutable current = Option<'T>.None
252249
{ new IEnumeratorM<'``Monad<bool>``, 'T> with
253-
member _.Current =
254-
match current with
255-
| Some c -> c
256-
| None -> invalidOp enumNotStarted
250+
member _.Current = Option.defaultWith (fun () -> invalidOp enumNotStarted) current
257251
member x.MoveNext () = monad' {
258252
match state with
259253
| SeqState.NotStarted inp ->
@@ -391,10 +385,7 @@ module SeqT_V2 =
391385
match started with
392386
| Some _ -> result false
393387
| None -> source |> (if opaqueId false then liftM else map) (fun v -> started <- Some v; true)
394-
member _.Current =
395-
match started with
396-
| Some v -> v
397-
| None -> invalidOp enumNotStarted
388+
member _.Current = Option.defaultWith (fun () -> invalidOp enumNotStarted) started
398389
member _.Dispose () = () } }
399390

400391
[<RequireQualifiedAccess; EditorBrowsable(EditorBrowsableState.Never)>]
@@ -411,10 +402,7 @@ module SeqT_V2 =
411402
let mutable state = CollectState.NotStarted source
412403
let mutable current = Option<'U>.None
413404
{ new IEnumeratorM<'``Monad<bool>``, 'U> with
414-
member _.Current =
415-
match current with
416-
| Some c -> c
417-
| None -> invalidOp enumNotStarted
405+
member _.Current = Option.defaultWith (fun () -> invalidOp enumNotStarted) current
418406
member x.MoveNext () = monad' {
419407
match state with
420408
| CollectState.NotStarted inp ->
@@ -458,10 +446,7 @@ module SeqT_V2 =
458446
let mutable state = CollectState.NotStarted f
459447
let mutable current = Option<'U>.None
460448
{ new IEnumeratorM<'``Monad<bool>``, 'U> with
461-
member _.Current =
462-
match current with
463-
| Some c -> c
464-
| None -> invalidOp enumNotStarted
449+
member _.Current = Option.defaultWith (fun () -> invalidOp enumNotStarted) current
465450
member x.MoveNext () = monad' {
466451
match state with
467452
| CollectState.NotStarted f ->
@@ -505,10 +490,7 @@ module SeqT_V2 =
505490
let mutable state = CollectState.NotStarted x1
506491
let mutable current = Option<'U>.None
507492
{ new IEnumeratorM<'``Monad<bool>``, 'U> with
508-
member _.Current =
509-
match current with
510-
| Some c -> c
511-
| None -> invalidOp enumNotStarted
493+
member _.Current = Option.defaultWith (fun () -> invalidOp enumNotStarted) current
512494
member x.MoveNext () = monad' {
513495
match state with
514496
| CollectState.NotStarted x1 ->
@@ -560,10 +542,7 @@ module SeqT_V2 =
560542
let mutable state = AppendState.NotStarted1 (source1, source2)
561543
let mutable current = Option<'T>.None
562544
{ new IEnumeratorM<'``Monad<bool>``, 'T> with
563-
member _.Current =
564-
match current with
565-
| Some c -> c
566-
| None -> invalidOp enumNotStarted
545+
member _.Current = Option.defaultWith (fun () -> invalidOp enumNotStarted) current
567546
member x.MoveNext () = innerMonad {
568547
match state with
569548
| AppendState.NotStarted1 (inp1, inp2) ->
@@ -621,10 +600,7 @@ module SeqT_V2 =
621600
let mutable state = MapState.NotStarted source
622601
let mutable current = Option<'U>.None
623602
{ new IEnumeratorM<'``Monad<bool>``, 'U> with
624-
member _.Current =
625-
match current with
626-
| Some c -> c
627-
| None -> invalidOp enumNotStarted
603+
member _.Current = Option.defaultWith (fun () -> invalidOp enumNotStarted) current
628604
member x.MoveNext () = innerMonad {
629605
match state with
630606
| MapState.NotStarted inp ->
@@ -661,10 +637,7 @@ module SeqT_V2 =
661637
let mutable state = Map2State.NotStarted (source1, source2)
662638
let mutable current = Option<'U>.None
663639
{ new IEnumeratorM<'``Monad<bool>``, 'U> with
664-
member _.Current =
665-
match current with
666-
| Some c -> c
667-
| None -> invalidOp enumNotStarted
640+
member _.Current = Option.defaultWith (fun () -> invalidOp enumNotStarted) current
668641
member x.MoveNext () = innerMonad {
669642
match state with
670643
| Map2State.NotStarted (s1, s2) -> return! (
@@ -697,10 +670,7 @@ module SeqT_V2 =
697670
let mutable state = Map2State.NotStarted (source1, source2)
698671
let mutable current = Option<'U>.None
699672
{ new IEnumeratorM<'``Monad<bool>``, 'U> with
700-
member _.Current =
701-
match current with
702-
| Some c -> c
703-
| None -> invalidOp enumNotStarted
673+
member _.Current = Option.defaultWith (fun () -> invalidOp enumNotStarted) current
704674
member x.MoveNext () = innerMonad {
705675
match state with
706676
| Map2State.NotStarted (s1, s2) ->
@@ -750,10 +720,7 @@ module SeqT_V2 =
750720
let mutable state = CollectState.NotStarted source
751721
let mutable current = Option<'T>.None
752722
{ new IEnumeratorM<'``Monad<bool>``, 'T> with
753-
member _.Current =
754-
match current with
755-
| Some c -> c
756-
| None -> invalidOp enumNotStarted
723+
member _.Current = Option.defaultWith (fun () -> invalidOp enumNotStarted) current
757724
member x.MoveNext () = innerMonad {
758725
match state with
759726
| CollectState.NotStarted inp ->
@@ -799,7 +766,7 @@ module SeqT_V2 =
799766

800767
let inline internal tryPickMAndMap<'T, 'U, .. > (f: 'T -> '``Monad<'U option>``) (source: SeqT<'``Monad<bool>``, 'T>) (postMap: 'U option -> 'V) : '``Monad<'V>`` = innerMonad2<_, '``Monad<unit>``> () {
801768
use ie = (source :> IEnumerableM<'``Monad<bool>``, 'T>).GetEnumerator ()
802-
let! (move: bool) = ie.MoveNext ()
769+
let! move = ie.MoveNext ()
803770
let mutable b = move
804771
let mutable res = None
805772
while b && res.IsNone do
@@ -813,7 +780,7 @@ module SeqT_V2 =
813780

814781
let inline internal tryPickAndMap<'T, 'U, .. > (f: 'T -> 'U option) (source: SeqT<'``Monad<bool>``, 'T>) (postMap: 'U option -> 'V) : '``Monad<'V>`` = innerMonad2<_, '``Monad<unit>``> () {
815782
use ie = (source :> IEnumerableM<'``Monad<bool>``, 'T>).GetEnumerator ()
816-
let! (move: bool) = ie.MoveNext ()
783+
let! move = ie.MoveNext ()
817784
let mutable b = move
818785
let mutable res = None
819786
while b && res.IsNone do
@@ -829,13 +796,13 @@ module SeqT_V2 =
829796
tryPickMAndMap<_, 'U, _, '``Monad<unit>``, _, _, _> f source id
830797

831798
let inline pickM<'T, 'U, .. > (f: 'T -> '``Monad<'U option>``) (source: SeqT<'``Monad<bool>``, 'T>) : '``Monad<'U>`` =
832-
tryPickMAndMap<_, 'U, '``Monad<'U option>``, '``Monad<unit>``, _, _, _> f source (function Some v -> (v: 'U) | _ -> raise (KeyNotFoundException ()))
799+
tryPickMAndMap<_, 'U, '``Monad<'U option>``, '``Monad<unit>``, _, _, _> f source (function Some v -> v | _ -> raise (KeyNotFoundException ()))
833800

834801
let inline tryPick<'T, 'U, .. > (f: 'T -> 'U option) (source: SeqT<'``Monad<bool>``, 'T>) : '``Monad<'U option>`` =
835802
tryPickAndMap<_, _, _, _, '``Monad<unit>``, _> f source id
836803

837804
let inline pick (f: 'T -> 'U option) (source: SeqT<'``Monad<bool>``, 'T>) : '``Monad<'U>`` =
838-
tryPickAndMap<_, _, _, _, '``Monad<unit>``, _> f source (function Some v -> (v: 'U) | _ -> raise (KeyNotFoundException ()))
805+
tryPickAndMap<_, 'U, _, _, '``Monad<unit>``, _> f source (function Some v -> v | _ -> raise (KeyNotFoundException ()))
839806

840807
let inline contains value (source: SeqT<'``Monad<bool>``, 'T>) : '``Monad<bool>`` =
841808
tryPickAndMap<_, _, _, _, '``Monad<unit>``, _> (fun v -> if v = value then Some () else None) source Option.isSome
@@ -844,7 +811,7 @@ module SeqT_V2 =
844811
tryPickAndMap<_, _, _, _, '``Monad<unit>``, _> (fun v -> if f v then Some v else None) source id
845812

846813
let inline find f (source: SeqT<'``Monad<bool>``, 'T>) : '``Monad<'T>`` =
847-
tryPickAndMap<_, _, _, _, '``Monad<unit>``, _> (fun v -> if f v then Some v else None) source (function Some v -> (v: 'T) | _ -> raise (KeyNotFoundException ()))
814+
tryPickAndMap<'T, _, _, _, '``Monad<unit>``, _> (fun v -> if f v then Some v else None) source (function Some v -> v | _ -> raise (KeyNotFoundException ()))
848815

849816
let inline exists f (source: SeqT<'``Monad<bool>``, 'T>) : '``Monad<bool>`` =
850817
tryPickAndMap<_, _, _, _, '``Monad<unit>``, _> (fun v -> if f v then Some v else None) source Option.isSome
@@ -867,10 +834,7 @@ module SeqT_V2 =
867834
let mutable state = TryWithState.NotStarted source
868835
let mutable current = Option<'T>.None
869836
{ new IEnumeratorM<'``Monad<bool>``, 'T> with
870-
member _.Current =
871-
match current with
872-
| Some c -> c
873-
| None -> invalidOp enumNotStarted
837+
member _.Current = Option.defaultWith (fun () -> invalidOp enumNotStarted) current
874838
member x.MoveNext () = innerMonad2<_, '``Monad<unit>``> () {
875839
match state with
876840
| TryWithState.NotStarted inp ->
@@ -939,10 +903,7 @@ module SeqT_V2 =
939903
let mutable state = TryFinallyState.NotStarted source
940904
let mutable current = Option<'T>.None
941905
{ new IEnumeratorM<'``Monad<bool>``, 'T> with
942-
member _.Current =
943-
match current with
944-
| Some c -> c
945-
| None -> invalidOp enumNotStarted
906+
member _.Current = Option.defaultWith (fun () -> invalidOp enumNotStarted) current
946907
member x.MoveNext () = innerMonad {
947908
match state with
948909
| TryFinallyState.NotStarted inp ->
@@ -1003,7 +964,7 @@ module SeqT_V2 =
1003964
///
1004965
/// <exception cref="T:System.ArgumentNullException">Thrown when count is negative.</exception>
1005966
let inline truncate count (source: SeqT<'``Monad<bool>``, 'T>) : SeqT<'``Monad<bool>``, 'T> =
1006-
if (count < 0) then invalidArg "count" "must be non-negative"
967+
if count < 0 then invalidArg "count" "must be non-negative"
1007968
SeqT
1008969
{ new IEnumerableM<'``Monad<bool>``, 'T> with
1009970
member _.GetEnumerator () =
@@ -1035,7 +996,7 @@ module SeqT_V2 =
1035996
/// <exception cref="T:System.InvalidOperationException">Thrown when count exceeds the number of elements.
1036997
/// in the sequence.</exception>
1037998
let inline take count (source: SeqT<'``Monad<bool>``, 'T>) : SeqT<'``Monad<bool>``, 'T> =
1038-
if (count < 0) then invalidArg "count" "must be non-negative"
999+
if count < 0 then invalidArg "count" "must be non-negative"
10391000
SeqT
10401001
{ new IEnumerableM<'``Monad<bool>``, 'T> with
10411002
member _.GetEnumerator () =
@@ -1047,20 +1008,15 @@ module SeqT_V2 =
10471008
if i > 0 then
10481009
i <- i - 1
10491010
e.MoveNext () |> monomorphicBind (fun res ->
1050-
if not res then invalidOp (
1051-
sprintf
1052-
"The input sequence has an insufficient number of elements: tried to take %i %s past the end of the sequence. Use SeqT.truncate to get %i or less elements."
1053-
(i + 1)
1054-
(if i = 0 then "element" else "elements")
1055-
count)
1011+
if not res then invalidOp $"The input sequence has an insufficient number of elements: tried to take {i + 1} element{if i = 0 then String.Empty else string 's'} past the end of the sequence. Use SeqT.truncate to get {count} or less elements."
10561012
result res)
10571013
else
10581014
x.Dispose ()
10591015
result false
10601016
member _.Dispose () = dispose e } }
10611017

10621018
let inline internal skipImpl throw count (source: SeqT<'``Monad<bool>``, 'T>) : SeqT<'``Monad<bool>``, 'T> =
1063-
if (count < 0) then invalidArg "count" "must be non-negative"
1019+
if count < 0 then invalidArg "count" "must be non-negative"
10641020
SeqT
10651021
{ new IEnumerableM<'``Monad<bool>``, 'T> with
10661022
member _.GetEnumerator () =
@@ -1072,23 +1028,14 @@ module SeqT_V2 =
10721028
if i > 0 then
10731029
i <- i - 1
10741030
e.MoveNext () |> monomorphicBind (fun res ->
1075-
if res then
1076-
x.MoveNext ()
1031+
if res then x.MoveNext ()
1032+
elif throw then invalidOp $"Tried to skip {i + 1} element{if i = 0 then String.Empty else string 's'} past the end of the seq. Use SeqT.drop to skip {count} or less elements."
10771033
else
1078-
if throw then
1079-
invalidOp (
1080-
sprintf
1081-
"tried to skip %i %s past the end of the seq. Use SeqT.drop to skip %i or less elements."
1082-
(i + 1)
1083-
(if i = 0 then "element" else "elements")
1084-
count)
1085-
else
1086-
x.Dispose ()
1087-
result false)
1034+
x.Dispose ()
1035+
result false)
10881036
else
10891037
e.MoveNext () |> monomorphicBind (fun res ->
1090-
if not res then
1091-
x.Dispose ()
1038+
if not res then x.Dispose ()
10921039
result res)
10931040
member _.Dispose () = dispose e } }
10941041

0 commit comments

Comments
 (0)