C# style continuations in F# - yet another Fibonacci implementation

F# does not get support C# style continuations yet; I am reliably informed that it will do at some point.

 

However if you can not wait that long it is already fair easy to get C# style continuations by implementing System.Collections.Generic.IEmunerable your self. What’s more this can be implemented as a library function then implementing your enumerator becomes just a couple of lines of code.

 

Here is my implementation:

 

module Strangelights.Continuations

 

open System

open System.Collections

open System.Collections.Generic

 

// This function makes an IEnumerable interface from

// a mutable value "n" and a function "f" that takes unit and

// returns a boolean. The idea is that the function mutates

// the value return true while there are more values left

let make_enumberable n f =

 

    // this function creates a class that implments the

    // IEnumerator interface

    let make_enumerator() =

        { new IEnumerator<_> with // implment IEnumerator itself

            get_Current() = ! n  // return value held in n

        interface IEnumerator with // implment the non generic IEnumerator

            Reset() = failwith "not implmented"

            and MoveNext() =

                f() // call the funcition we were given

            and get_Current() = ! n :> obj // expose n with downcast to obj

        interface  IDisposable with // implment IDisposable

            Dispose() = () } in

           

    // now we need to implment IEnumerable to make IEnumerator

    // more friendly to our calling clients

    { new IEnumerable<_> with // implment IEnumerable itself

        GetEnumerator() = make_enumerator()

    interface IEnumerable with // implment the non generic IEnumerable

        GetEnumerator() = make_enumerator() :> IEnumerator }

       

let make_fib_enumberable() =

    let n  = ref 0I in      // the Fibonacci number itself

    let nPlus1 = ref 1I in  // the next number in the sequence

 

    // function that calculates the next Fibonacci number

    // by changing the values n and nPlus1

    let get_next() =       

        let temp = ! nPlus1 in

        nPlus1 := ! n  + ! nPlus1;

        n :=  temp;

        true in

 

    // make the enumberator

    make_enumberable n get_next

 

// take the fist 40 numbers from our infinite list

let fibs = IEnumerable.take 40 (make_fib_enumberable())

 

// display the list

do

    fibs |> List.iter (fun i -> print_string (i.ToString()) ; print_newline());

    read_line() |> ignore

Bookmark
dotnetkicks+, digg+, reddit+, del.icio.us+, dzone+, facebook+

Print | posted @ Thursday, June 15, 2006 9:50 PM

Comments on this entry:

No comments posted yet.

Your comment:

(Note: all comments are moderated so it may take sometime to appear)

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 6 and 8 and type the answer here:
 

Links

 Subscribe in a reader
Twitter Follow me on Twitter
FaceBook View my Facebook
LinkedIn View my LinkedIn Profile Viadeo Viadeo Profile (Français)

Badges


Disclaimer

The views expressed on this weblog are mine and do not necessarily reflect the views of my employer.

All postings are provided "AS IS" with no warranties, and confer no rights.


follow robertpi at http://twitter.com