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.

CodeStock 2012

First of all, kudos to Michael Neel and everyone that put on CodeStock.  This was my first time attending CodeStock and to see such a well-run conference is always a pleasure. I know from experience that there’s a lot of work that goes in to making things run that smoothly.

I got to attend a few very interesting sessions.  Mike Falanga’s session on using F# and MongoDB for analysis of large datasets was very interesting.  Daniel Mohl’s session on F# and Web Development was also good—and it was really nice to meet Daniel in person and get a chance to gab with him a bit.  There were a few folks from the Nashville area at the conference and they must have some major-league brain power out that way.

Also Rob Gillen’s talk on how buffer overflow attacks work was both fascinating and a bit scary. I can just imagine how many unpatched vulnerabilities there are running around in all the publicly deployed sites I regularly depend on.

Finally, here’s the slide deck for my F# Shell Scripting Talk.  I’ll edit this post later to share sample code.  I’ve posted the slide deck as a PDF file to maximize the number of people that can see it.

Note: Thanks to Dennis for pointing out to me that I got the name of the presenter for the buffer overflow attack talk incorrect. 

Configuring Sublime Text 2 To Work With FSharp

I like working with the Sublime Text 2 editor and I surely like to work with FSharp so I’ve been trying to find ways to make life easier for myself.  A few tips to pass along:

  1. Get the F# Textmate Bundle. Once you get it, unzip it into the ~\Application Data\Sublime Text 2\Packages directory.  In my case I created a F# directory under the Packages directory.
  2. Add the following settings via the File Settings-User menu
    	"tab_size": 4,
    	"translate_tabs_to_spaces": true
    
  3. To make it easier to compile and build your app, create a couple of environment variables: MSBuildBin and FSBin. In my case I set the MSBuildBin to the directory where the MSBuild.exe is located and I set FSBin to the directory where Fsc.exe is located.

Under the ~\Application Data\Sublime Text 2\Packages\User directory, create a file called msbuild.sublime-build and put the following in it:

{
    "path": "%FSBin%;%MSBuildBin%",
    "cmd": ["msbuild"]
}

Once you do this, on the Tools->Build System menu, you should have an msbuild option.  If you select the msbuild option from the build system you should be able to build your FSharp app with an msbuild *.proj file.  You can grab the lines from the output via a Perl RegEx if you want but I’ve not done anything so complex up to this point.

I’m working on adding the ability to shell out to the fsi.exe utility.  If I make any progress with that, I’ll post on it here.

UPDATE: See Leaf’s comment below for information on adding a REPL to Sublime Text 2. Great many thanks Leaf.

Also, here is a Gist of a small, modest F# shell script to kick off Sublime Text 2 adding git into the path for the ST2 git plug-in.

UPDATE 2:  The eminent F# developer, PaulMichael Blasucci has created a new Sublime Text 2 plug-in for F# developers.

Technorati Tags: ,,