2 people like it.

A failed attempt at evaluating sequence items in terms of a try-with block

A broken code example demonstrating how it's you can't catch a single throwing enumeration and continue with F#'s IEnumerable.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
open System
open System.Linq

module Seq =
    let catchExceptions handler (sequence: _ seq) =
        let e = sequence.GetEnumerator()
        let evaluateNext () = 
            try Some (e.Current)
            with ex -> handler ex 
        seq { while e.MoveNext() do
                  match evaluateNext() with
                  | Some (item) -> yield item 
                  | None -> () }
namespace System
namespace System.Linq
Multiple items
module Seq

from Script

--------------------
module Seq

from Microsoft.FSharp.Collections
val catchExceptions : handler:(exn -> 'a option) -> sequence:seq<'a> -> seq<'a>

Full name: Script.Seq.catchExceptions
val handler : (exn -> 'a option)
val sequence : seq<'a>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val e : Collections.Generic.IEnumerator<'a>
Collections.Generic.IEnumerable.GetEnumerator() : Collections.Generic.IEnumerator<'a>
val evaluateNext : (unit -> 'a option)
union case Option.Some: Value: 'T -> Option<'T>
property Collections.Generic.IEnumerator.Current: 'a
val ex : exn
Collections.IEnumerator.MoveNext() : bool
val item : 'a
union case Option.None: Option<'T>

More information

Link:http://fssnip.net/4p
Posted:13 years ago
Author:Rick Minerich
Tags: seq , sequences , collections , error handling