Seattle F# user group Feb 2012 Programming Contest question
http://www.meetup.com/FSharpSeattle/messages/boards/thread/20700462
Tuesday, February 21, 2012
Saturday, February 18, 2012
F# ≥ C# (Options)
Today is one of my most rewarding days. I’ve exchanged some ideas with Brian and Vlad on our team and get some good understanding of null value in F#.
Let us first tell me the discussion result. I am converted from an option hater to a lover, at least not that hate. The reason I hate option is the fact that I have to type “Some” every time and it is, to me, a redundant. I can use C# NULL to check if a variable is being set or not. Today I found out I was wrong, the option is a better way to handle this situation. It forces me to pay attention to check the value. Do you still remember the last time you encounter null exception? I guess not long time ago, right? Let us start with a C# sample with a string type variable.
string str = read_string(); //a function return a string type value
if (str.Length < 10) then Console.WriteLine(“less than 10”) else “long string… “
Seems everything is perfect until you realize the str can be null! L So you have to check str != null first and then perform the real work. If you forget at one place, a monster will be staring at you in the dark.
Let us look at the F# version:
let str = read_string() // return a string option to indicate the return value can be an uninitialized value
match str with
| Some(data) -> printfn “good, we got value and we can process…”
| None -> printfn “what happened, call IT right now!”
We still need to write match and make decision about if the value is None or not, but at least this let you pay attention. Comparing to the C#'s implicit NULL approach, I start to like this way more. It is true that some people still prefer the C# version which puts the check everywhere, but if you write a SDK and expose it to end user, do you expect that guy’s code has a snickering monster? J
Also the option avoid the reference type's NULL value, this NULL value is not a valid value in the problem space. For example, if we use the string to represent first name, what is NULL mean for a people's name? This implicit value give us tons of trouble and increase the complexity of the program because you have to check the NULL everywhere. Why not using an option and make your problem space clean?
Wednesday, February 15, 2012
Define property in F# interface
If you do the Azure programming with F#, you will bump into a problem where you have to define a property in an interface. Something like:
interface IMyInterface
{
int MyProperty { get; set; }
}
The way to define this in F# is:
type IMyInterface= interface
abstract MyProperty : int with get, set
end
The interesting thing about F# interface implementation is F# use explicit interface implementation. So if the interface definition does not have "set", then your class implementation will have to change if the class still need both "get" and "set".
The interesting thing about F# interface implementation is F# use explicit interface implementation. So if the interface definition does not have "set", then your class implementation will have to change if the class still need both "get" and "set".
type IMyInterface= interface
abstract MyProperty : int with get
end
class implementation:
type MyClass() =
interface IMyInterface with
member this.MyProperty with get() = ...
member this.MyProperty with set(..) = ...
class implementation:
type MyClass() =
interface IMyInterface with
member this.MyProperty with get() = ...
member this.MyProperty with set(..) = ...
Tuesday, February 7, 2012
Azure Service Bus Sample code (F# version)
C# has already has the sample code for Azure service bus's "relayed messaging". After I apply for a Azure account, I decided to do a F# version just for fun. The code needs Azure 1.6 and Visual Studio 2010.
The code won't work because I hide my account key. Here is where to find your key in Azure management portal. In the UI, it called default key. I have to say the UI design is not bad, but finding something for a new user is not that easy.
If you want to start from scratch by referring to Service Bus Relayed Messaging Tutorial, the following are something you might want to pay attention to:
The code won't work because I hide my account key. Here is where to find your key in Azure management portal. In the UI, it called default key. I have to say the UI design is not bad, but finding something for a new user is not that easy.
Where to find the account key |
If you want to start from scratch by referring to Service Bus Relayed Messaging Tutorial, the following are something you might want to pay attention to:
- the project should be targeting to .NET 4. (not client profile).
- define the interface method with parameter name.
[ < ServiceContract(Name = "IEchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/") > ]
type IEchoContract = interface
[ < OperationContract > ]
abstract member Echo : msg:string -> string
end
- the tutorial is based on Azure SDK 1.5, so the app.config it provides has version = 1.5, you need to fix that to 1.6 as the latest download is 1.6.
- the ServiceBus dll is located under C:\Program Files\Windows Azure SDK\v1.6\ServiceBus\ref
if you ever upgrade to 1.7 version, remember to update the app.config file and might have to use the key value in the "Service identities" under "access control service".
This sample can also be found at F# 3.0 sample pack.
This sample can also be found at F# 3.0 sample pack.
Labels:
Azure,
Cloud,
F#,
F# Programming,
Functional Programming
Monday, February 6, 2012
Seattle F# user group meeting Feb 21, 2012
As the previous meetup, we have two sessions + one tiny program contest:
Ryan Riley (F# MVP)
Title: Web Apps and APIs with F# Abstract: Most .NET web applications today
use ASP.NET WebForms or MVC. However, several F# libraries offer new ways to build web APIs and applications help reduce code and offer better abstractions, especially for single-page applications (SPA). We'll start with an existing web application written in C# using both WebForms and MVC and transition first to a F# application that follows the same patterns, then transition again to using several tools that expose the additional power offered by F#.
F# team member Jack Hu (MSFT)
Title: F#, American Dream
Since F# became a first-class programming language in Visual Studio 2010, it has been gaining popularity among the financial and scientific communities. In this talk, we will showcase several F# applications in the context of financial investments. We will highlight F#'s value propositions, through the themes of simplicity, powerfulness and programmer satisfaction. We'll also take a look at current industry adoptions, who uses F# and what they gain from it. The majority of the talk will be slightly technical and look at some of innovative aspects of F# 3.0 that help to simplify programming and achieve great results ' F# 3.0 typeProvider ' F# 3.0 Query ' FsharpChart ' .Net Integration ' Async programming ' Units of measure ' TryFsharp.net
F# programming contest
question will be disclosed at end of the meeting and winner will be announced at the next meeting.
Special thanks to Chris Brockett from MSR for reserving a nice place for this meeting!
MSR (Microsoft building 99, room 1919-C)
Saturday, February 4, 2012
F# ≥ C# (warning 9)
Today one customer ask me why the there is a warning when use StructLayout on a type.
Uses of this construct may result in the generation of unverifiable .NET IL code. This warning can be disabled using '--nowarn:9' or '#nowarn "9"'.The warning is because this can lead to some "interesting" results, make a managed code like C code. All the F# attribute is inserted an extra space so browser can display it correctly.
open System.Runtime.InteropServices
[ < StructLayout(LayoutKind.Sequential) > ]
type A =
val mutable X : int
val mutable Y : int
new(x,y) = { X = x; Y = y }
[ < StructLayout(LayoutKind.Sequential) > ]
type B =
val mutable Z : int64
new(x) = { Z = x }
[ < StructLayout(LayoutKind.Explicit) > ]
type C =
[ < FieldOffset 0 > ] val mutable X : A
[ < FieldOffset 0 > ] val mutable Y : B
new (a, b) = { X = a; Y = b }
[ < StructLayout(LayoutKind.Explicit) > ]
type D =
[ < FieldOffset 0 > ] val mutable X : int
[ < FieldOffset 0 > ] val mutable Y : int
new(a,b) = { X = a; Y = b }
let a = A(0,0)
let b = B(1L)
let c = C(a,b) //debugger will show two b's.
let d = D(1, 2) // two fields are both 2
The managed code should not have this side-effect even a few people might think this is a good feature. I like the F#'s way to give you a warning than C# does not give you anything.
Friday, February 3, 2012
F# PowerPack with Visual Studio 11 Preview
Everyday I learn something new.. :-)
If you use PowerPack with DEV11 preview version and somehow cannot reference to some types in the powerpack. Adding the following line into the App.config fiile
< bindingredirect newversion="4.3.0.0" oldversion="2.0.0.0" >< / bindingredirect >
is the way to fix it. Detailed info about bindingRedirect can be found here.
if it is for FSI.exe, then you need to fix in C:\Program Files\Microsoft SDKs\F#\3.0\Framework\v4.0\Fsi.exe.config.
Not only the PowerPack, any DEV10 project you find cannot compile after upgrade, you can always add the following section in the app.config file. The following is the full list the redirection.
If you use PowerPack with DEV11 preview version and somehow cannot reference to some types in the powerpack. Adding the following line into the App.config fiile
< bindingredirect newversion="4.3.0.0" oldversion="2.0.0.0" >< / bindingredirect >
is the way to fix it. Detailed info about bindingRedirect can be found here.
if it is for FSI.exe, then you need to fix in C:\Program Files\Microsoft SDKs\F#\3.0\Framework\v4.0\Fsi.exe.config.
Not only the PowerPack, any DEV10 project you find cannot compile after upgrade, you can always add the following section in the app.config file. The following is the full list the redirection.
< dependentAssembly >
< assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
< bindingRedirect oldVersion="4.0.0.0" newVersion="4.3.0.0" />
< bindingRedirect oldVersion="2.3.5.0" newVersion="4.3.0.0" />
< bindingRedirect oldVersion="2.0.0.0" newVersion="4.3.0.0" />
Subscribe to:
Posts (Atom)