Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I intercept the F# sequence generator?

Tags:

sequence

f#

trying to work around a problem in outside library - is there a way to try-catch the generator itself item by item (probably not, but just to be sure...)?

let myTest() =
    let mySeq = seq { for i in -3 .. 3 -> 1 / i }
    // how to keep the line above intact, but modify the code below to try-catch-ignore the bad one?
    mySeq |> Seq.iter (fun i -> printfn "%d" i)
    ()
like image 348
Andres Avatar asked May 21 '15 22:05

Andres


People also ask

What is the vertical intercept for f?

Given a linear function f(x) = mx + b, The vertical intercept (y-intercept) is found by evaluating the function when the input variable, x, is 0 and is always the same as the constant b. It can be thought of as the original value of the function.

How do you find the intercept of a function?

Intercepts are those points on a Cartesian coordinate system where the graph of an equation or function crosses the x- and y-axes. To find the x-intercept algebraically, simply set y to 0 and solve for x. Conversely, to find the y-intercept, set the value of x to 0 and solve for y.

How do you find the y-intercept of f?

To find the y-intercept of a function, we need to find the point on the graph where x = 0. Given a function, f(x), the y-intercept occurs at f(0). Thus, the y-intercept is at (0, -4).

Is f x the same as y-intercept?

The y-intercept formula says that the y-intercept of a function y = f(x) is obtained by substituting x = 0 in it. Using this, the y-intercept of a graph is the point on the graph whose x-coordinate is 0. i.e., just look for the point where the graph intersects the y-axis and it is the y-intercept.


1 Answers

You can't.
Once the exception happens, the state of the source enumerator is screwed up. If you can't get into the source enumerator to "fix" its state, you can't make it keep producing values.

You can, however, make the whole process "stop" after the exception, but you'll have to go a level below and work with IEnumerator<T>:

let takeUntilError (sq: seq<_>) = seq {
    use enm = sq.GetEnumerator()
    let next () = try enm.MoveNext() with _ -> false
    let cur () = try Some enm.Current with _ -> None

    while next() do
      match cur() with
      | Some c -> yield c
      | None -> ()
  }

mySeq |> takeUntilError |> Seq.iter (printf "%d")
like image 54
Fyodor Soikin Avatar answered Oct 11 '22 10:10

Fyodor Soikin