Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP - discussion] - Alternate Method of Seq.map chaining #1528

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/fsharp/FSharp.Core/seq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,28 @@ namespace Microsoft.FSharp.Collections
member this.Reset() = noReset()
interface System.IDisposable with
member this.Dispose() = this.Dispose()

[<AbstractClass>]
type MapSingleAbstractEnumerator<'T> () =
inherit MapEnumerator<'T> ()
abstract member Map: ('T -> 'TResult) -> IEnumerator<'TResult>

let map f (e : IEnumerator<_>) : IEnumerator<_>=
upcast
{ new MapEnumerator<_>() with
member this.DoMoveNext (curr : byref<_>) =
if e.MoveNext() then
curr <- (f e.Current)
true
else
false
member this.Dispose() = e.Dispose()
}
type MapSingleEnumerator<'T0, 'T1> (e: IEnumerator<'T0>, f: 'T0 -> 'T1) =
inherit MapSingleAbstractEnumerator<'T1> ()
override this.DoMoveNext (curr : byref<_>) =
if e.MoveNext() then
curr <- f e.Current
true
else
false
override this.Dispose() = e.Dispose()
override this.Map (g: 'T1 -> 'T2) = new MapSingleEnumerator<'T0, 'T2>(e, f >> g) :> IEnumerator<'T2>

let map f (e : IEnumerator<_>) : IEnumerator<_> =
match e with
| :? MapSingleAbstractEnumerator<'TSource> as i ->
i.Map(f)
| _ -> new MapSingleEnumerator<'TSource, 'TResult>(e, f) :> IEnumerator<'TResult>

let mapi f (e : IEnumerator<_>) : IEnumerator<_> =
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
Expand Down