Category Archives: F#

Fun with FunScript

FunScript is a compiler or better a transpiler, which converts F# code into JavaScript code.
I have not tried it much since the last couple of days and I’m really amazed how well it works.

I had really good expierences creating D3 charts but I wasn’t shure how well it works with AngularJS, with all his dependency mechanisms and “magic” behind the scenes.
So I thought I give it a try!

I came up with this DSL like syntax:

The DSL code looks like this (mainly it wraps the kinda ugly generated code):

It is still a bit clunky to use (mainly to get angular’s dependency injection to work) and it is far from completed, but at least a simple controller works!

The downsides of using FunScript to generate JavaScript code is the additional code generation step, it generates more verbose code than writing similar code in JavaScript and the debugging is chunky (but that’s a problem nearly every transpiler has). I don’t know of a source map file for the generated code, but maybe I’m missing something.

On the other side you can use this nice functional and composable code in places where I never thought it is possible to get it to work!

The sample project can be found here.

Advertisements
Tagged , , , ,

Import NuGet Packages to F# Interactive Scripts

Yes, it’s been a long time (exactly one year, but that’s a coincidence) since my last blog post. The main reason was that in the last year I learnt F# and slowly I have the feeling that I have understood the language and especially the paradigm shift that comes with F#.

I jump over the usual homage of why F# is a wonderful language – that’s material for a plethora of blog posts – and come straight to the motivation of this blog post.

If you are like me and you learn by trying things out you probably love the interactive and iterative nature of F#. Just create a new fsx (F# Interactive) file and start hacking until you have a working prototype. You don’t even have to save the file!
I actually have a folder full of F# scripts with my experimentations in them.

One pain point though is that F# interactive does not have support for NuGet package management. You can reference the library files manually but that’s very cumbersome.

There exists an older blog post from Daniel Mohl where he suggests how to add NuGet support to F# Interactive, but his solution requires the VS Object Model and doesn’t support loose fsx files (without the creation of a F# project).

So I wrote my own “PackageManagement.fsx” file which can be initialized with F# Interactive:

And here is the script code: https://gist.github.com/toburger/9786275

It depends on the NuGet.Core.dll which you can get from here and you must define your target NuGet packages directory, where the packages are downloaded and from where they are referenced (you can define the “NugetLibDir” environment variable so you can use the same script on multiple machines). I ignore the versioning of the packages (I always download the latest stable release), but you can enable it by setting the boolean parameter “useSideBySidePaths” on line 11 to true.

So how to work with this script?

The main command is “ipr” which stands for “install package and references”, which downloads the package with all its dependencies and adds the references to the clipboard, so the only thing you have to do is to paste the references on top of your script file.

Just enter ipr “package name”;; in the f# Interactive REPL (don’t forget the two semicolons) and it will try it’s best to download and reference the packages.

There are also commands to update and uninstall packages or to simply copy the references without trying to download the latest package.

Limitations:

I try my best to find the best matching references for the framework version in which F# interactive runs, but this matching is not bullet proof and unfortunately the NuGet lib doesn’t provide a way to get the best matching references automatically (at least I didn’t found any practical way).

The script should also work without VS and should work on Mono but I don’t have tested it so please feel free to contact me or edit the file if you have any problems.


Actually there is now a suggestion on fslang uservoice with a propose to natively support NuGet packages: http://fslang.uservoice.com/forums/245727-f-language/suggestions/5670137–package-directive-to-import-nuget-packages-in-f

Tagged , ,