F# Tip of the Week (26 August 2013)

It’s possible to use function composition and modify the type of value returned.  Consider the following equation:

1/4x2

I might encode the function to calculate this like so:


let square x = x * x

//Want this to be a float because if this is an int division

// it will always round to 0 if x is greater than 1.

let recip x = 1.0/x

let myFunc =  fun x -> 4* (square x) >> recip

This function composition will not work because I need to pass a float to recip. But I can make it work by doing this:


let square x = x * x

let recip x = 1.0/x

let myFunc = fun x -> 4* (square x) >> float >> recip

 

There’s a great deal more information on conversion functions in F# here.

First Detroit F# Meetup

A few of us from Quicken had attended LambdaJam in Chicago.  By the way, it was a great conference–big kudos to Alex Miller. Anyway, at that conference we met Mathias Brandewinder, a Microsoft F# MVP from San Francisco.  Some of the guys started talking to Mathias about visiting us here in Motown to spread the F# goodness.

One thing lead to another and we were able to get him to come here.  John Fair, myself, and a few others interested in F# were talking about what we could do to make his visit most effective for teaching folks around here about F#.  I had been running an F# user group here till around 2011 or so.  I’ve run a few user groups in my time and I can tell you that they often end up being a major chore and by 2011 I was just kind of tired of being in charge of things.  But this time John and the others volunteered to lead the effort so it was hard to argue with.

At any rate, Mathias lead a machine learning coding dojo and it just blew us all away–how much fun it was.  To tell you the truth I was sort of skeptical–I mean how much can one learn about machine learning and its algorithms and techniques in one short session.  But there was a lot there and we had a great time.

Kudos to Mathias for leading an awesome session and to John and those other F# folks who got him to join us and set up the session.

Here are some pics I took at the meetup:

 

Some of the fellows at the first F# meetup

Some of the fellows at the first F# meetup

Some of the fellows standing around chatting

Some of the fellows standing around chatting

F# Tip Of The Week (Week of August 19, 2013)

Even though tuples, records and discriminated unions are reference types, they all have the built-in equality properties you would expect in a value type.  For example:


> let a = (1,'a');;

val a : int * char = (1, 'a')

> let b = (1,'a');;

val b : int * char = (1, 'a')

> a = b;;

val it : bool = true

That is, you get the equality behavior you would expect rather than the usual equality behavior associated with reference types.  Here’s an example of records and the built-in equality checking:


> type rec1 = {

a:int

b:string

};;

type rec1 =

{a: int;

b: string;}

> let a = {a=1;b="one"};;

val a : rec1 = {a = 1;

b = "one";}

> let b = {a=1;b="one"};;

val b : rec1 = {a = 1;

b = "one";}

> a = b;;

val it : bool = true

F# Tip Of The Week (Week of August 12, 2013)

The F# developer can use pattern matching syntax pretty much anywhere in F#.  For example, consider the following list:


let l = [1..25]

If I want to get the last element of the list, I can do this with a little trivial pattern match:


let lastElem::_ = l |> List.rev;;

stdin(7,5): warning FS0025: Incomplete pattern matches on this expression. For example, the value '[]' may indicate a case not covered by the pattern(s).

val lastElem : int = 25

In this case, you can safely ignore the warning.

You can also pull one of the elements from a tuple very easily with this pattern match:


let name,_ = (“Onorio”,”11W”);;

val name : string = "Onorio"

The underscore character in both of these pattern matches functions as a wildcard.  In both cases it signifies a value that we don’t care about.

Getting The Band Back Together

So back in 2011 I was trying to get an F# user group going here in Detroit.  As is so often the case with user groups, most of the responsibility for setting things up and arranging things was falling on one person–me.  In fairness, the folks at Epitec did their best to help; they provided us a place to meet and they provided food.  Still I was the one always doing the presentations and, as anyone who’s ever run a user group knows, that burns a person out in short order.

Still I really wanted people to know the F# story; I think it’s very compelling.  Fast forward to 2012 when I joined John Fair at Quicken Loans.  John is another F#anatic (to coin an unnecessary neologism). John and I have both been running a weekly F# learning session for quite some time.  So yesterday I felt it’s time to apply some of what I’ve learned since 2011 and give another try to running an F# user group here in Detroit.  John has graciously agreed to help and I know that means I won’t be stuck doing all the heavy lifting.

We’re meeting on August 22; location is still to be determined as is the topic of the first meeting.  But either way, I am excited to once again be able to start sharing the F# goodness.  And there is a lot of goodness there.

http://www.meetup.com/Detroit-F-Meetup/

Utility Git Bash Shortcuts

Big hat tip to sartorial exemplar John Fair for putting me on to this idea and sharing his set of default aliases.

If you use git bash for your typical interaction with git (and probably you should if you don’t because it behaves the same on all git platforms), adding a few shortcuts can dramatically cut down on the typing you need to do.

Open a git bash prompt.  From the prompt, open ~/.bashrc in your favorite editor.  Then paste these aliases into that file.

alias st2='/c/progra~1/sublim~1/sublime_text.exe'
alias fsi='/c/progra~2/mid3a6~1/v4.0/fsi.exe'
alias s='start '

#Change to base directory
alias ctb='cd /c/mysandbox'

#Git status--use this one all the time
alias gs='git status'

#Git checkout -- another one I use all the time
alias gche='git checkout'

alias ls='ls -alp '
alias ga='git add '
alias gb='git branch '
alias gche='git checkout '
alias gcom='git commit '
alias gcam='git commit -am '
alias gl='git log '
alias gpl='git pull '
alias gm='git merge '
alias gd='git diff '

You may notice that I have a shortcut for my favorite editor (Sublime Text 2) as well as an alias to bring up a F# Repl too.  I also have a utility alias to change to the root directory of my current development work.