Hide this comment

Don't you want

let seq = seq {

while enu.MoveNext() do

yield enu.Current }

or you will start by repeating the last value. If you are peeking into the enumerable to see where to continue from then to avoid the ref var how about.

let seq = seq {
yield enu.Current
while enu.MoveNext() do
yield enu.Current }

By on 6/16/2009 10:13 AM ()Reply
Hide this comment

I think I was trying too hard to use recursion - there's always this kind of solution:

let convertEnumeratorToSequence (x: IEnumerator<'t>) =
seq {
let continueProcessing = ref true
while !continueProcessing do
yield x.Current
continueProcessing := x.MoveNext()
}

By on 6/15/2009 1:09 PM ()Reply
Hide this comment

would this have 'unexpected' side effect ?

The standard F# sequence is sort of 'immutable' in the sense that it can be passed around and consumed multiple times.

this seems to be limited to 'once off' consumption ?

By on 6/15/2009 1:29 PM ()Reply
Hide this comment

Yes; you can make a LazyList out of that and then return the LazyList (upcast back to a seq) to fix it so that the original enumerator is traversed at most once, but you can create any number of 'seq's out of it.

By on 6/15/2009 2:15 PM ()Reply
Hide this comment

Yes; you can make a LazyList out of that and then return the LazyList (upcast back to a seq) to fix it so that the original enumerator is traversed at most once, but you can create any number of 'seq's out of it.

Something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  let convertEnumeratorToSequence (x: IEnumerator<'t>) =

    let result = seq {

      let continueProcessing = ref true

      while continueProcessing.Value do

        yield x.Current

        continueProcessing := x.MoveNext()

    }

    (LazyList.of_seq result :> seq<'t>)
By on 6/16/2009 9:38 AM ()Reply
Hide this comment

Also you can try this:

type EnumerableEnumerator<'a>(enumerator)=

interface IEnumerable<'a> with

[<OverloadID("generic")>]

member this.GetEnumerator(): IEnumerator<'a> = enumerator

[<OverloadID("non-generic")>]

member this.GetEnumerator(): IEnumerator = enumerator :> IEnumerator

This can be used multiple times, but not at the same time.

BTW, how to format F# code here?

By on 6/16/2009 12:56 AM ()Reply
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper

Logging in...