Phillip Trelford's Array

POKE 36879,255

F# Summer of Code 2014

In the northern hemisphere summer is nearly open us, and to celebrate the F#unctional Londoners are putting on a series of 3 free hands on coding sessions.

The sessions are open to all levels of experience (and operating systems), and I believe will be a great opportunity to pick up new skills or extend existing ones. So bring your friends, your partners, your kids and your laptops for some coding fun.

 

F#unctional Londoners

 

Fractal Forest Dojo – Thursday June 26th


In this hands on session, we'll have some fun, dig into some recursion and fractals, and create beautiful tree drawings, along the lines of this one:

tall-tree

This session was the brainchild of Mathias Brandewinder, first showing in San Francisco at the beginning of the year. Since then it’s spread to groups in Minsk, Washington DC and now London.


Build a 2048 bot Thursday July 10th


2048 is a fun (and very addictive) game that can be played on the Web:

Canopy is an awesome F# UI web testing framework, built on top of Selenium:

Let's put them together, and build a simple bot that plays 2048, using Canopy!

This is another session from Mathias, check out his video above.


F#ore! A pleasant round of Code Golf – Thursday July 24th


In this hands-on session with Grant Crofton, we'll be having a game of Code Golf, where the objective is to complete your program in as few (key)strokes as possible.

Throw caution and good programming practice to the wind in your quest for an ever-decreasing character count!  Well-named variables?  Not today my friend.  Sensibly modularised code structure?  Hell no!  Comments?  You must be kidding..

Although the main aim is to have fun, it's also a great way to improve your F# language knowledge.  Do you *really* need those parentheses? Is pattern matching more concise than an If? Isn't there an operator that does this?! 

 

GameCraft

 

This summer sees the return of the GameCraft London at Skills Matter on Saturday June 7th! Last year’s event was huge, expect this year to be even bigger.

Although not specifically an F# event, expect to see a number of teams using F#.

Check out the great GameCraft resources page to get you started, and for F# game development I’d recommend also looking at:

With F# and Mono you can target iOS, Android, Mac, PC and Linux.

 

fsharpWorks |> Paris

 

fsharpWorks/Paris '14 on Friday June 27th is a one-day conference for developers who want to take the fast track to F#. The program will feature a morning of talks by world-class experts, demonstrating F# in action on a wide range of practical applications, and an afternoon of hands-on workshops, to learn first-hand how to use it productively.

Why go?

F# is an open-source, cross-platform, functional first programming language.

F# makes it easy to model complex problems simply. It offers great benefits in terms of reliability and safety. Its lightweight syntax make it a fantastic language for scripting, rapid prototyping, and interactive data exploration, with all the performance benefits of a statically typed language. And it integrates smoothly with existing .NET code bases. And... it makes coding fun again!

F# has seen an explosive growth in 2013, and is not slowing down. So if you want to take your development skills to the next level, come and join us for a fast track to F#!

Speakers include Tomas Petricek, Scott Wlaschin, Steffen Forkmann, Mathias Brandewinder, Robert Pickering and Jeremie Chassaing.

Date Types

When you want to set a date literal in F# (or C#) you create a new DateTime instance:

let today = DateTime(2014,26,4)

If you’re rocking VB.Net you get date literals which are checked at design & compile time:

Dim d = #2014/04/26#

I thought it’d be nice to add design and compile time checked date literals to F#, & throw in code completion for good measure. Enter F# Type Providers. No need to open millions of lines of compiler code, instead simply implement a class to provide the types & voilà:

let today = Date.``2014``.``04``.``26``

As you type the date, you can only enter valid values:

today

Or you can for example use it to easily find the last Saturday of the month:

saturday

The code is up on BitBucket & there’s a Nuget package, it’s pretty simple, less than a 100 lines of code, and no compilers were harmed in the process. Being what it is, this is really just scratching the surface of the power of F# Type Providers, which can give you typed access to data from web services or databases all the way through to other languages.

Build your own Type Provider

Creating your own type providers is easier than you might think, Michael Newton has a great article to get you started: Type Providers From the Ground Up.

F#unctional LondonersCoincidentally the F#unctional Londoners will be hosting a Creating Type Providers Hands On Session with Michael next Thursday at Skills Matter.

Pop along, have fun, and see the kind of things you can build.

Case for VB.Net vNext

Following up on the last Roslyn preview way back in 2012, this week saw the availability of a new preview with a more complete compiler along with a few new language features for C# and VB. A lot of inspiration for these features seems to have come from the F# language.

The C# interactive shell from 2012 appears to be missing, perhaps ScriptCS is expected to fill this space, or you could just use F# interactive which already exists in Visual Studio.

On the language features side, C# 6 gets primary constructors for classes, heavily inspired by F#, and using static which brings parity with Java and VB.Net.

For me VB.Net gets the most interesting new feature in the form of Select Case TypeOf. which provides the first steps towards pattern matching.

Shapes

Taking a hierarchy of shapes as an example:

Public MustInherit Class Shape
End Class

Public Class Rectangle
    Inherits Shape
    Public Property Width As Integer
    Public Property Height As Integer
End Class

Public Class Circle
    Inherits Shape
    Public Property Radius As Integer
End Class

Sub Main()
    Dim shape As Shape = New Rectangle With {.Width = 10, .Height = 10}
    Select Case shape
        Case r As Rectangle When r.Width = r.Height
            Console.WriteLine("Square of {0}", r.Width)
        Case r As Rectangle
            Console.WriteLine("Rectangle of {0},{1}", r.Width, r.Height)
        Case c As Circle
            Console.WriteLine("Circle of {0}", c.Radius)
    End Select
End Sub

The functionality is still quite limited and quite verbose in comparison to say F# or Scala, but I feel it’s definitely an interesting development for VB.Net.

For comparison here’s an equivalent F# version using discriminated unions:

type Shape =
    | Rectangle of width:int * height:int
    | Circle of radius:int

let shape = Rectangle(10,10)
match shape with
| Rectangle(w,h) when w=h -> printfn "Square %d" w
| Rectangle(w,h) -> printfn "Rectangle %d, %d" w h
| Circle(r) -> printfn "Circle %d" r

Eval

Pattern matching can be really useful when writing compilers, here’s a simple expression tree evaluator in F#:

type Expression =
   | Factor of value:int
   | Add of lhs:Expression * rhs:Expression

let rec eval e =
   match e with
   | Factor(x) -> x
   | Add(l,r) -> eval l + eval r

let onePlusOne = Add(Factor(1),Factor(1))

VB.Net vNext can approximate this, albeit in a rather more verbose way:

Public MustInherit Class Expression
End Class

Public Class Factor
    Inherits Expression
    Public Property Value As Integer
    Sub New(x As Integer)
        Value = x
    End Sub
End Class

Public Class Op
    Inherits Expression
    Public Property Lhs As Expression
    Public Property Rhs As Expression
End Class

Public Class Add
    Inherits Op
End Class

Function Eval(e As Expression) As Integer
    Select Case e
        Case x As Factor
            Return x.Value
        Case op As Add
            Return Eval(op.Lhs) + Eval(op.Rhs)
        Case Else
            Throw New InvalidOperationException
    End Select
End Function

Sub Main()
    Dim onePlusOne As Expression =
        New Add With {.Lhs = New Factor(1), .Rhs = New Factor(1)}
    Console.WriteLine(Eval(onePlusOne))
End Sub

Summary

It will be interesting to see how VB.Net vNext develops. I think first-class support for tuples could be an interesting next step for the language.