Skip to content

Commit 384579c

Browse files
committed
renaming cleanup. Adding ListEnumerable. Adding Choose
1 parent 15aa412 commit 384579c

File tree

1 file changed

+104
-45
lines changed

1 file changed

+104
-45
lines changed

src/fsharp/FSharp.Core/seq.fs

Lines changed: 104 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -833,21 +833,21 @@ namespace Microsoft.FSharp.Collections
833833

834834
abstract Composer : SeqComponent<'U,'V> -> SeqComponent<'T,'V>
835835

836-
abstract ComposeMap<'S> : Map<'S,'T> -> SeqComponent<'S,'U>
837-
abstract ComposeFilter : Filter<'T> -> SeqComponent<'T,'U>
838-
abstract ComposeFilterMap<'S> : FilterMap<'S,'T> -> SeqComponent<'S,'U>
839-
abstract ComposeMapFilter<'S> : MapFilter<'S,'T> -> SeqComponent<'S,'U>
840-
abstract ComposeSkip : Skip<'T> -> SeqComponent<'T,'U>
836+
abstract ComposeWithMap<'S> : Map<'S,'T> -> SeqComponent<'S,'U>
837+
abstract ComposeWithFilter : Filter<'T> -> SeqComponent<'T,'U>
838+
abstract ComposeWithFilterThenMap<'S> : FilterThenMap<'S,'T> -> SeqComponent<'S,'U>
839+
abstract ComposeWithMapThenFilter<'S> : MapThenFilter<'S,'T> -> SeqComponent<'S,'U>
840+
abstract ComposeWithSkip : Skip<'T> -> SeqComponent<'T,'U>
841841

842842
default __.OnComplete () = ()
843843

844-
default first.Composer (second:SeqComponent<'U,'V>) : SeqComponent<'T,'V> = upcast Composed (first, second)
844+
default first.Composer (second:SeqComponent<'U,'V>) : SeqComponent<'T,'V> = upcast Composed (first, second)
845845

846-
default second.ComposeMap<'S> (first:Map<'S,'T>) : SeqComponent<'S,'U> = upcast Composed (first, second)
847-
default second.ComposeFilter (first:Filter<'T>) : SeqComponent<'T,'U> = upcast Composed (first, second)
848-
default second.ComposeFilterMap<'S> (first:FilterMap<'S,'T>) : SeqComponent<'S,'U> = upcast Composed (first, second)
849-
default second.ComposeMapFilter<'S> (first:MapFilter<'S,'T>) : SeqComponent<'S,'U> = upcast Composed (first, second)
850-
default second.ComposeSkip (first:Skip<'T>) : SeqComponent<'T,'U> = upcast Composed (first, second)
846+
default second.ComposeWithMap<'S> (first:Map<'S,'T>) : SeqComponent<'S,'U> = upcast Composed (first, second)
847+
default second.ComposeWithFilter (first:Filter<'T>) : SeqComponent<'T,'U> = upcast Composed (first, second)
848+
default second.ComposeWithFilterThenMap<'S> (first:FilterThenMap<'S,'T>) : SeqComponent<'S,'U> = upcast Composed (first, second)
849+
default second.ComposeWithMapThenFilter<'S> (first:MapThenFilter<'S,'T>) : SeqComponent<'S,'U> = upcast Composed (first, second)
850+
default second.ComposeWithSkip (first:Skip<'T>) : SeqComponent<'T,'U> = upcast Composed (first, second)
851851

852852
and Composed<'T,'U,'V> (first:SeqComponent<'T,'U>, second:SeqComponent<'U,'V>) =
853853
inherit SeqComponent<'T,'V>()
@@ -870,16 +870,16 @@ namespace Microsoft.FSharp.Collections
870870
inherit SeqComponent<'T,'U>()
871871

872872
override first.Composer (second:SeqComponent<'U,'V>) : SeqComponent<'T,'V> =
873-
second.ComposeMap first
873+
second.ComposeWithMap first
874874

875-
override second.ComposeMap<'S> (first:Map<'S,'T>) : SeqComponent<'S,'U> =
875+
override second.ComposeWithMap<'S> (first:Map<'S,'T>) : SeqComponent<'S,'U> =
876876
upcast Map (first.Map >> second.Map)
877877

878-
override second.ComposeFilter (first:Filter<'T>) : SeqComponent<'T,'U> =
879-
upcast FilterMap (first.Filter, second.Map)
878+
override second.ComposeWithFilter (first:Filter<'T>) : SeqComponent<'T,'U> =
879+
upcast FilterThenMap (first.Filter, second.Map)
880880

881-
override second.ComposeFilterMap<'S> (first:FilterMap<'S,'T>) : SeqComponent<'S,'U> =
882-
upcast FilterMap (first.Filter, first.Map >> second.Map)
881+
override second.ComposeWithFilterThenMap<'S> (first:FilterThenMap<'S,'T>) : SeqComponent<'S,'U> =
882+
upcast FilterThenMap (first.Filter, first.Map >> second.Map)
883883

884884
override __.ProcessNext (input:'T, halted:byref<bool>, output:byref<'U>) : bool =
885885
output <- map input
@@ -902,17 +902,17 @@ namespace Microsoft.FSharp.Collections
902902
and Filter<'T> (filter:'T->bool) =
903903
inherit SeqComponent<'T,'T>()
904904

905-
override this.Composer (next:SeqComponent<'T,'V>) : SeqComponent<'T,'V> =
906-
next.ComposeFilter this
905+
override first.Composer (second:SeqComponent<'T,'V>) : SeqComponent<'T,'V> =
906+
second.ComposeWithFilter first
907907

908-
override second.ComposeMap<'S> (first:Map<'S,'T>) : SeqComponent<'S,'T> =
909-
upcast MapFilter (first.Map, second.Filter)
908+
override second.ComposeWithMap<'S> (first:Map<'S,'T>) : SeqComponent<'S,'T> =
909+
upcast MapThenFilter (first.Map, second.Filter)
910910

911-
override second.ComposeFilter (first:Filter<'T>) : SeqComponent<'T,'T> =
911+
override second.ComposeWithFilter (first:Filter<'T>) : SeqComponent<'T,'T> =
912912
upcast Filter (Helpers.ComposeFilter first.Filter second.Filter)
913913

914-
override second.ComposeMapFilter<'S> (first:MapFilter<'S,'T>) : SeqComponent<'S,'T> =
915-
upcast MapFilter (first.Map, Helpers.ComposeFilter first.Filter second.Filter)
914+
override second.ComposeWithMapThenFilter<'S> (first:MapThenFilter<'S,'T>) : SeqComponent<'S,'T> =
915+
upcast MapThenFilter (first.Map, Helpers.ComposeFilter first.Filter second.Filter)
916916

917917
override __.ProcessNext (input:'T, halted:byref<bool>, output:byref<'T>) : bool =
918918
if filter input then
@@ -923,20 +923,20 @@ namespace Microsoft.FSharp.Collections
923923

924924
member __.Filter :'T->bool = filter
925925

926-
and MapFilter<'T,'U> (map:'T->'U, filter:'U->bool) =
926+
and MapThenFilter<'T,'U> (map:'T->'U, filter:'U->bool) =
927927
inherit SeqComponent<'T,'U>()
928928

929929
override __.ProcessNext (input:'T, halted:byref<bool>, output:byref<'U>) :bool =
930930
output <- map input
931931
Helpers.avoidTailCall (filter output)
932932

933933
override first.Composer (second:SeqComponent<'U,'V>):SeqComponent<'T,'V> =
934-
second.ComposeMapFilter first
934+
second.ComposeWithMapThenFilter first
935935

936936
member __.Map : 'T->'U = map
937937
member __.Filter : 'U->bool = filter
938938

939-
and FilterMap<'T,'U> (filter:'T->bool, map:'T->'U) =
939+
and FilterThenMap<'T,'U> (filter:'T->bool, map:'T->'U) =
940940
inherit SeqComponent<'T,'U>()
941941

942942
override __.ProcessNext (input:'T, halted:byref<bool>, output:byref<'U>) : bool =
@@ -946,7 +946,7 @@ namespace Microsoft.FSharp.Collections
946946
else
947947
false
948948

949-
override this.Composer (next:SeqComponent<'U,'V>) : SeqComponent<'T,'V> = next.ComposeFilterMap this
949+
override first.Composer (second:SeqComponent<'U,'V>) : SeqComponent<'T,'V> = second.ComposeWithFilterThenMap first
950950

951951
member __.Filter : 'T->bool = filter
952952
member __.Map : 'T->'U = map
@@ -973,10 +973,10 @@ namespace Microsoft.FSharp.Collections
973973
let mutable count = 0
974974

975975
override first.Composer (second:SeqComponent<'T,'V>) : SeqComponent<'T,'V> =
976-
second.ComposeSkip first
976+
second.ComposeWithSkip first
977977

978-
override second.ComposeSkip (first:Skip<'T>) : SeqComponent<'T,'T> =
979-
if System.Int32.MaxValue - second.SkipCount > first.SkipCount then
978+
override second.ComposeWithSkip (first:Skip<'T>) : SeqComponent<'T,'T> =
979+
if Int32.MaxValue - first.SkipCount - second.SkipCount > 0 then
980980
upcast Skip (first.SkipCount + second.SkipCount)
981981
else
982982
upcast Composed (first, second)
@@ -1002,9 +1002,9 @@ namespace Microsoft.FSharp.Collections
10021002

10031003
let mutable count = 0
10041004

1005-
override second.ComposeSkip (first:Skip<'T>) : SeqComponent<'T,'T> =
1006-
if System.Int32.MaxValue - second.TakeCount > first.SkipCount then
1007-
upcast SkipTake (first.SkipCount, first.SkipCount+second.TakeCount)
1005+
override second.ComposeWithSkip (first:Skip<'T>) : SeqComponent<'T,'T> =
1006+
if Int32.MaxValue - first.SkipCount - second.TakeCount > 0 then
1007+
upcast SkipThenTake (first.SkipCount, first.SkipCount+second.TakeCount)
10081008
else
10091009
upcast Composed (first, second)
10101010

@@ -1026,7 +1026,7 @@ namespace Microsoft.FSharp.Collections
10261026

10271027
member __.TakeCount = takeCount
10281028

1029-
and SkipTake<'T> (startIdx:int, endIdx:int) =
1029+
and SkipThenTake<'T> (startIdx:int, endIdx:int) =
10301030
inherit SeqComponent<'T,'T>()
10311031

10321032
let mutable count = 0
@@ -1053,13 +1053,22 @@ namespace Microsoft.FSharp.Collections
10531053
let x = endIdx - count
10541054
invalidOpFmt "tried to take {0} {1} past the end of the seq"
10551055
[|SR.GetString SR.notEnoughElements; x; (if x=1 then "element" else "elements")|]
1056+
1057+
and Choose<'T, 'U> (choose:'T->'U option) =
1058+
inherit SeqComponent<'T,'U>()
1059+
1060+
override __.ProcessNext (input:'T, halted:byref<bool>, output:byref<'U>) : bool =
1061+
match choose input with
1062+
| Some value -> output <- value
1063+
true
1064+
| None -> false
10561065

10571066
type SeqProcessNextStates =
10581067
| NotStarted = 1
10591068
| Finished = 2
10601069
| InProcess = 3
10611070

1062-
type SeqEnumeratorEnumerator<'T,'U>(enumerator:IEnumerator<'T>, t2u:SeqComponent<'T,'U>) =
1071+
type SeqComposedEnumerator<'T,'U>(enumerator:IEnumerator<'T>, t2u:SeqComponent<'T,'U>) =
10631072
let mutable state = SeqProcessNextStates.NotStarted
10641073
let mutable current = Unchecked.defaultof<'U>
10651074
let mutable halted = false
@@ -1078,7 +1087,7 @@ namespace Microsoft.FSharp.Collections
10781087
false
10791088

10801089
interface IDisposable with
1081-
member x.Dispose():unit =
1090+
member __.Dispose():unit =
10821091
match source with
10831092
| null -> ()
10841093
| _ -> source.Dispose (); source <- Unchecked.defaultof<_>
@@ -1091,13 +1100,13 @@ namespace Microsoft.FSharp.Collections
10911100
member __.Reset () : unit = noReset ()
10921101

10931102
interface IEnumerator<'U> with
1094-
member x.Current =
1103+
member __.Current =
10951104
match state with
10961105
| SeqProcessNextStates.NotStarted -> notStarted()
10971106
| SeqProcessNextStates.Finished -> alreadyFinished()
10981107
| _ -> current
10991108

1100-
type SeqArrayEnumerator<'T,'U>(array:array<'T>, t2u:SeqComponent<'T,'U>) =
1109+
type ArrayComposedEnumerator<'T,'U>(array:array<'T>, t2u:SeqComponent<'T,'U>) =
11011110
let mutable state = SeqProcessNextStates.NotStarted
11021111
let mutable current = Unchecked.defaultof<'U>
11031112
let mutable halted = false
@@ -1117,7 +1126,7 @@ namespace Microsoft.FSharp.Collections
11171126
false
11181127

11191128
interface IDisposable with
1120-
member x.Dispose() : unit = ()
1129+
member __.Dispose() : unit = ()
11211130

11221131
interface IEnumerator with
11231132
member this.Current : obj = box (this:>IEnumerator<'U>).Current
@@ -1127,7 +1136,43 @@ namespace Microsoft.FSharp.Collections
11271136
member __.Reset () : unit = noReset ()
11281137

11291138
interface IEnumerator<'U> with
1130-
member x.Current =
1139+
member __.Current =
1140+
match state with
1141+
| SeqProcessNextStates.NotStarted -> notStarted()
1142+
| SeqProcessNextStates.Finished -> alreadyFinished()
1143+
| _ -> current
1144+
1145+
type ListComposedEnumerator<'T,'U>(alist:list<'T>, t2u:SeqComponent<'T,'U>) =
1146+
let mutable state = SeqProcessNextStates.NotStarted
1147+
let mutable current = Unchecked.defaultof<'U>
1148+
let mutable halted = false
1149+
1150+
let mutable list = alist
1151+
1152+
let rec moveNext () =
1153+
match halted, list with
1154+
| false, head::tail ->
1155+
list <- tail
1156+
if t2u.ProcessNext (head, &halted, &current) then
1157+
true
1158+
else
1159+
moveNext ()
1160+
| _ -> state <- SeqProcessNextStates.Finished
1161+
t2u.OnComplete ()
1162+
false
1163+
1164+
interface IDisposable with
1165+
member __.Dispose() : unit = ()
1166+
1167+
interface IEnumerator with
1168+
member this.Current : obj = box (this:>IEnumerator<'U>).Current
1169+
member __.MoveNext () =
1170+
state <- SeqProcessNextStates.InProcess
1171+
moveNext ()
1172+
member __.Reset () : unit = noReset ()
1173+
1174+
interface IEnumerator<'U> with
1175+
member __.Current =
11311176
match state with
11321177
| SeqProcessNextStates.NotStarted -> notStarted()
11331178
| SeqProcessNextStates.Finished -> alreadyFinished()
@@ -1141,7 +1186,7 @@ namespace Microsoft.FSharp.Collections
11411186
inherit ComposableEnumerable<'U>()
11421187

11431188
let getEnumerator () : IEnumerator<'U> =
1144-
upcast (new SeqEnumeratorEnumerator<'T,'U>(enumerable.GetEnumerator(), current ()))
1189+
upcast (new SeqComposedEnumerator<'T,'U>(enumerable.GetEnumerator(), current ()))
11451190

11461191
interface IEnumerable with
11471192
member this.GetEnumerator () : IEnumerator = upcast (getEnumerator ())
@@ -1172,7 +1217,7 @@ namespace Microsoft.FSharp.Collections
11721217
inherit ComposableEnumerable<'U>()
11731218

11741219
let getEnumerator () : IEnumerator<'U> =
1175-
upcast (new SeqArrayEnumerator<'T,'U>(array, current ()))
1220+
upcast (new ArrayComposedEnumerator<'T,'U>(array, current ()))
11761221

11771222
interface IEnumerable with
11781223
member this.GetEnumerator () : IEnumerator = upcast (getEnumerator ())
@@ -1200,6 +1245,20 @@ namespace Microsoft.FSharp.Collections
12001245
//
12011246
// state
12021247

1248+
type SeqListEnumerable<'T,'U>(alist:list<'T>, current:unit->SeqComponent<'T,'U>) =
1249+
inherit ComposableEnumerable<'U>()
1250+
1251+
let getEnumerator () : IEnumerator<'U> =
1252+
upcast (new ListComposedEnumerator<'T,'U>(alist, current ()))
1253+
1254+
interface IEnumerable with
1255+
member this.GetEnumerator () : IEnumerator = upcast (getEnumerator ())
1256+
1257+
interface IEnumerable<'U> with
1258+
member this.GetEnumerator () : IEnumerator<'U> = getEnumerator ()
1259+
1260+
override __.Compose (next:unit->SeqComponent<'U,'V>) : IEnumerable<'V> =
1261+
upcast new SeqListEnumerable<'T,'V>(alist, fun () -> (current ()).Composer (next ()))
12031262

12041263

12051264
#if FX_NO_ICLONEABLE
@@ -1326,6 +1385,7 @@ namespace Microsoft.FSharp.Collections
13261385
match source with
13271386
| :? SeqComposer.ComposableEnumerable<'T> as s -> s.Compose createSeqComponent
13281387
| :? array<'T> as a -> upcast (new SeqComposer.SeqArrayEnumerable<_,_>(a, createSeqComponent))
1388+
| :? list<'T> as a -> upcast (new SeqComposer.SeqListEnumerable<_,_>(a, createSeqComponent))
13291389
| _ -> upcast (new SeqComposer.SeqEnumerable<_,_>(source, createSeqComponent))
13301390

13311391
let private seqFactoryForImmutable seqComponent (source:seq<'T>) =
@@ -1367,8 +1427,7 @@ namespace Microsoft.FSharp.Collections
13671427

13681428
[<CompiledName("Choose")>]
13691429
let choose f source =
1370-
checkNonNull "source" source
1371-
revamp (IEnumerator.choose f) source
1430+
source |> seqFactoryForImmutable (SeqComposer.Choose f)
13721431

13731432
[<CompiledName("Indexed")>]
13741433
let indexed source =

0 commit comments

Comments
 (0)