Thursday, August 25, 2011

Adding NuGet Support to F# Interactive

A little over a week ago, Rick Minerich and I had a brief conversation about how cool it would be to install NuGet packages via the F# Interactive (FSI). After some research, there seems to be 3 options for accomplishing this:

1. Write a script that is loaded when the FSI starts.
2. Modify the source of FSI.exe (which can be found at http://fsharppowerpack.codeplex.com/).
3. Create an alternate F# Interactive Window VSIX that starts the FSI.exe as a new process as well as provides new commands.

In this post, I'll explore the first of these options.

Building the Script:

There are 3 steps to accomplishing the desired goal with option 1.

1. Create a F# script that has the ability to retrieve packages from NuGet and interact with Visual Studio. The script that I used to accomplish this can be found here.

Note: NuGet must be installed on the machine. Additionally, you will need to change the path to the NuGet.Core.dll reference if running a 32-bit version of Windows. This will be evident if you receive an error like the following when starting the F# Interactive Window - "error FS0084: Assembly reference 'C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\NuGet.Core.dll' was not found or is invalid".

2. Update the FSI command line options in Visual Studio by going to Tools | Options | F# Tools | F# Interactive. Add "--use:<Path>\FsiExtension.fsx" to the "F# Interactive Options" value (see example below). Note: A full list of available FSI options can be found at http://msdn.microsoft.com/en-us/library/dd233172.aspx.


3. Open the F# Interactive Window (or reset the session if the window is already opened).

Taking it for a Test Drive:

Now that you are setup, it's time to try out the new functionality.

1. Create a new F# project and add a F# Script File (if one doesn't currently exist).

2.  Add some code (such as the following example from the Getting Started with F# PowerPack - Part 2 blog post) to the F# Script File:
open Microsoft.FSharp.Control  
  
let rec job = async {     
        for i in 1 .. 20 do    
            printfn "doing some work"  
            do! Async.Sleep 300  
            worker.ReportProgress i  
    }  
    and worker : AsyncWorker<_> = AsyncWorker(job)  
  
worker.ProgressChanged.Add(fun jobNumber -> printfn "job %d completed" jobNumber)   
worker.Error.Add(fun err -> printfn "Error: %A" err.Message)  
worker.Completed.Add(fun _ -> printfn "All jobs have completed")  
worker.Canceled.Add(fun _ -> printfn "Jobs have been canceled")  
  
worker.RunAsync() |> ignore  

3. In the F# Interactive Window, type InstallPackage "FSPowerPack.Core.Community";; and execute it. This will fire off the process to retrieve the FSPowerPack.Core.Community NuGet package from NuGet Gallery. The F# Interactive Window should look like the following once the process is complete:


This does two things:

1. It retrieves the specified NuGet package.
2. It adds any appropriate references to the fsx file. Your code will now look something like this:
#r @"C:\git\TestFsDte\packages\FSPowerPack.Core.Community.2.0.0.0\Lib\Net40\FSharp.PowerPack.dll"
open Microsoft.FSharp.Control  
  
let rec job = async {     
        for i in 1 .. 20 do    
            printfn "doing some work"  
            do! Async.Sleep 300  
            worker.ReportProgress i  
    }  
    and worker : AsyncWorker<_> = AsyncWorker(job)  
  
worker.ProgressChanged.Add(fun jobNumber -> printfn "job %d completed" jobNumber)   
worker.Error.Add(fun err -> printfn "Error: %A" err.Message)  
worker.Completed.Add(fun _ -> printfn "All jobs have completed")  
worker.Canceled.Add(fun _ -> printfn "Jobs have been canceled")  
  
worker.RunAsync() |> ignore  
4. You can also see what packages are installed by typing ShowInstalledPackages();;

Conclusion:

While this is hardly a perfect solution, hopefully it will at least spawn a few ideas. This implementation has several known issues (and probably several that are unknown). Let me know if you have ideas on better approaches or improvements.

Wednesday, August 24, 2011

Getting Started with the F# PowerPack - Part 3

In this series, I'm walking you through various features provided by the F# PowerPack.

Here are the links to the previous posts:

- Part 1
- Part 2

In this post, I'll discuss HashMultiMap and LazyList.

HashMultiMap:

http://cs.hubfs.net/forums/post/4371.aspx provides a nice description of the HashMultiMap type. Here's an excerpt from that link "HashSet and HashMultiMap are mutable structures, and both are implemented using hash comparison. When inserting a value, structural hash (or a user-supplied hash function) and (=) are used to find if a key already exists. Therefore, these are a good choice when the key is a complex or recursive data structure. HashSet contains keys only, and HashMultiMap contains keys and an associated value. Each key can only appear once in a HashSet or HashMultiMap, but multiple values can be bound to a single key in a HashMultiMap." Note: HashSet is now in System.Core. The version in the F# PowerPack has been deprecated.

Here is an example of HashMultiMap:
let hashSeq = 
    seq { for i in 1..100 do 
            yield ("key" + i.ToString()), ("value" + i.ToString())}
let hashMultiMap = HashMultiMap<string, string>(hashSeq, HashIdentity.Structural)
 
printfn "The value from the HashMultiMap for key:key5 is %s" (hashMultiMap.Item "key5")
LazyList:

Matthew Podwysocki has a nice post on Lazy Evaluation in F# that includes a section on LazyList. Here are a few excerpts from this post that explain the LazyList type. "The LazyList is a list which caches the results of the computed results on the items inside the collection." "The values aren't computed until the first is accessed, and then the results are cached."

The following contrived example shows the LazyList in use:
seq { for i in 1..10 do yield ("value" + i.ToString())}
|> LazyList.ofSeq
|> LazyList.iter(fun v -> printfn "The value from the LazyList is %s" v)
Wrapping Up:

That's it for today. You can find these examples (and several others) on my GitHub: https://github.com/dmohl/FSharpPowerPackExample.

Saturday, August 20, 2011

DevLink: Getting Started with F# Web Development

Thanks to all who attending the Getting Started with F# Web Development session at DevLink. The conference was excellent once again and I greatly enjoyed meeting each of you!

For those interested, the slides and code samples from this talk can be found at https://github.com/dmohl/GettingStartedWithWebDevInFSharpPresentation.

Thursday, August 18, 2011

Getting Started with the F# PowerPack - Part 2

In part 1 of this series, I provided a few links and examples of how to get started with the modules and types found in the math directory of the F# PowerPack. In this post, I'll do something similar with the command-line argument parser, AsyncOperations, AsyncStreamReader, and AsyncWorker.

ArgParser:

The F# PowerPack comes with a command-line parser that makes it easy to parse command-line options provided by a user.

The following links provide examples:

Example by Laurent Le Brun
Example by Robert Pickering
C# Port of Laurent Le Brun's Example by Steve Gilham

AsyncOperations:

Within the AsyncOperations.fsi file you'll find type extensions for StreamReader (AsyncReadToEnd) and File (AsyncOpenText, AsyncOpenRead, AsyncOpenWrite, AsyncAppendText, and AsyncOpen).

The following example is a modified version of one of the PowerPack unit tests, which shows AsyncOpenWrite and AsyncOpenRead in use.
open System.IO

async {
    async {
        let buffer = "F# is fun!"B
        use! is = File.AsyncOpenWrite "test.txt"
        do! is.AsyncWrite(buffer, 0, buffer.Length) 
        printfn "File written" } 
    |> Async.RunSynchronously

    async { 
        let buffer = Array.zeroCreate<byte>(7)
        use! is = File.AsyncOpenRead "test.txt"
        let! count = is.AsyncRead(buffer, 0, 7)
        printfn "File read"
        return count, buffer }
    |> Async.RunSynchronously |> ignore
} 
|> Async.Start

AsyncStreamReader:

The AsyncStreamReader type provides a simple approach to reading streams asynchronously. Here's an example:
open System
open System.IO
open System.Text

let text = [ new String([|for i in 1..1022 do yield 'x'|])
             new String([|for i in 1..1022 do yield 'y'|])
             new String([|for i in 1..1022 do yield 'z'|]) ].ToString()  

use stream = new MemoryStream()
stream.Write(Encoding.UTF8.GetBytes(text), 0, text.Length)
stream.Position <- 0L
let reader = new AsyncStreamReader(stream, Encoding.UTF8)          
async { let rec readAllChars () =
            async {
                let! eof = reader.EndOfStream
                if not eof then
                    let! character = reader.Read()
                    do printf "%s" (character.ToString())
                    return! readAllChars ()
                else 
                    printfn "%sRead complete" Environment.NewLine           
            }
        do! readAllChars ()
} 
|> Async.Start
Another example of AsyncStreamReader can be found here (see the answer provided by Brian McNamara).

AsyncWorker:

The AsyncWorker type provides a great way to define and launch a background worker. Don Syme shows how to create and use a similar type in his Async and Parallel Design Patterns in F#: Reporting Progress with Events (plus Twitter Sample) post.

Here's an example of the AsyncWorker type from the PowerPack.
open Microsoft.FSharp.Control

let rec job = async {   
        for i in 1 .. 20 do  
            printfn "doing some work"
            do! Async.Sleep 300
            worker.ReportProgress i
    }
    and worker : AsyncWorker<_> = AsyncWorker(job)

worker.ProgressChanged.Add(fun jobNumber -> printfn "job %d completed" jobNumber) 
worker.Error.Add(fun err -> printfn "Error: %A" err.Message)
worker.Completed.Add(fun _ -> printfn "All jobs have completed")
worker.Canceled.Add(fun _ -> printfn "Jobs have been canceled")

worker.RunAsync() |> ignore
Wrapping Up:

You can find working examples of each of these on my GitHub https://github.com/dmohl/FSharpPowerPackExample. Also, if you're interested in seeing more async examples, check out the series (C# Async Examples in F#) that Chris Marinos just started.

Sunday, August 14, 2011

Another CoffeeScript and jQuery UI Example

In a previous post, I showed a few examples of jQuery UI demos ported to CoffeeScript. While I prefer the approach that was shown in that post, there is another way that I could have done it. In this post, I'll show a different jQuery UI example (one of the the Slider demos) written using the CoffeeScript class structure.

You can find the full example (F# + ASP.NET MVC + CoffeeScript + jQuery UI) on my GitHub: https://github.com/dmohl/FsCoffeeScriptjQueryUIExample.
class Slider 
    constructor: ->
        $('#master').slider 
            value: 60 
            orientation: "horizontal" 
            range: "min" 
            animate: true

        $('#eq > span').each -> 
            value = parseInt $(this).text(), 10
            $(this).empty().slider
                value: value
                range: "min"
                animate: true
                orientation: "vertical" 

window.slider = new Slider()

Thursday, August 11, 2011

Getting Started with the F# PowerPack

I've been talking quite a bit about the F# PowerPack NuGet packages (see this post and this post for examples); however, I haven't written anything about the specific features that the F# PowerPack contains. To remedy this, I plan to do a series of posts to explore or review some of these features. The F# PowerPack has a wide variety of aspects, so if the features mentioned in this post are not of interest to you, don't let that stop you from reading future posts.

Note: The latest version of the F# PowerPack does not have official documentation. Documentation for version 1.9.6.16 can be found at http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/fsharp.powerpack/Microsoft.FSharp.Math.html.

I'll begin by looking at some of the modules and types found in the FSharp.PowerPack\math directory of the F# PowerPack source.

Associations:

The first module that we find in this directory is called GlobalAssociations. To quote from the comments in the F# PowerPack code, "Associations are a way of associating dictionaries of
operations with given types at runtime. Associations are global to a .NET application domain. Once specified an association may not be deleted or modified." The following links provide examples of this module in use:

F# INumerics Interface, and Matrix class by Yin
Example on StackOverflow by Tomas Petricek

Complex:

The module found in Complex.fs provides support for complex numbers and includes members that allow you to easily work with those numbers. A few of the members include retrieval of the real as well as imaginary parts of the complex number, addition/subtraction/multiplication of complex numbers, etc.

Here is a simple example from the F# PowerPack CodePlex home page:

let c = complex 0.0 1.0 * complex 0.0 1.0 // result is -1r+0i

Matrix:

There are several examples of using the Matrix type out on the web. The F# PowerPack CodePlex home page provides the following example:

let v  = vector [1.0;1.0;1.0] + vector [2.0;2.0;2.0] // result is (3.0; 3.0; 3.0)

Additionally, the previously mentioned post by Yin provides a few good examples. Also, Chapter 10 of the book Expert F# by Don Syme, Adam Granicz, and Antonio Cisternino has a section called Introducing Microsoft.FSharp.Math which contains a little more information and a few simple examples. A more involved example can be found here.

NativeArrayExtensionsForMatrix:

This module adds a few extension methods (i.e. of_vector, of_rowvec, and of_matrix) to the PinnedArray and PinnedArray2 types. An example of of_vector in use can be found here.

BigRational:

A simple example of the BigRational type in use, as documented on the F# PowerPack CodePlex home page, is shown below.

let r = (1N/2N) * (1N/3N) // result is 1/6

A more complex example can be found here.

Wrapping Up:

That's all for now. Everything described in this post (and the items described in the next few posts for this series) is provided in the FSharp.PowerPack.dll, which is available as part of an MSI on CodePlex as well as on NuGet Gallery as package ID FSPowerPack.Core.Community. In the next post for this series I will walk through and point out or provide examples for the command-line argument parser, AsyncOperations, AsyncStreamReader, and AsyncWorker.

Sunday, August 7, 2011

New F# PowerPack Packages on NuGet Gallery

There are several new F# PowerPack NuGet packages now available on NuGet Gallery. Additionally, the original F# PowerPack NuGet package has been modified slightly. A description of each PowerPack related package is provided below:

FSPowerPack.Community:


This is the original package. It includes all of the F# PowerPack libraries, but no longer includes the example .fs file. FSPowerPack.Community.Sample should be used when the example .fs file is desired.

FSPowerPack.Community.Sample:


This matches the contents of the original package (i.e. version 2.1.0.1 and below). It includes all F# PowerPack libraries as well as a simple .fs example file.

FSPowerPack.Core.Community:


This package contains only the FSharp.PowerPack.dll files for the various targeted frameworks.

FSPowerPack.Linq.Community:


This package contains the FSharp.PowerPack.Linq.dll files for the various targeted frameworks. It also takes a dependency on the FSPowerPack.Core.Community package.

FSPowerPack.Metadata.Community:


This package contains the FSharp.PowerPack.Metadata.dll files for the various targeted frameworks. It also takes a dependency on the FSPowerPack.Core.Community package.

FSPowerPack.Parallel.Seq.Community:


This package contains the FSharp.PowerPack.Parallel.Seq.dll files. It also takes a dependency on the FSPowerPack.Core.Community package.