Wednesday, September 21, 2011
Create a Metro App in F# (NOT) (Portable Libraries part 1)
update January 24, 2013: you are now able to create “Metro apps”: Windows Store Apps with F# (part 1) : Make things as simple as possible, but not simpler.
Today you can follow the all-F# approach (Build MVVM Applications in F#). This is not the easiest way to build a solution, it is not the preferred way, but it is an option.
Don Syme presented last Friday in his build talk the following slide.
When he explained this it was not completely clear to me what this meant.
Does this tell us that we cannot create a F# Metro front-end today but we will when F# 3.0 is ready? Or does this mean that you cannot create a F# 3.0 project and reference it from a C# or a VB Metro front-end app in one solution?
Today I watched some other presentations of day 4 of build:
Windows Runtime internals: understanding "Hello World" by Matt Merry
Being pragmatic by leveraging existing code in Metro style apps by Jason Olson
A .NET developer's view of Windows 8 app development by Krzysztof Cwalina
Ten Tips When Writing a Hybrid Language Metro style Application by Brent Rector
Using the Windows Runtime from C# and Visual Basic by Harry Pierson and Jesse Kaplan
and experimented with Visual Studio 11 (Developer Preview and Express for Windows Developer Preview).
It became clear to me that you cannot create a Metro style solution that references a project that references .NET dll's that are not part of the .NET Profile for Metro style apps.
The F# dll is one of them today. So creating a Metro app in C#/VB/C++/JavaScript that references a F# project is not possible.
This should be possible when F# 3.0 becomes available. Will it be possible to create an all-F# Metro app in the F# 3.0 time frame? For me, this is still an unanswered question.
Another consequence of this approach: when you intend to reuse your .NET dll's you have to recompile your code as WinMD files. So you need to own the source code.
This are the restrictions:
Update(September 21, 2011):
Thursday, September 15, 2011
F# type providers: oData from NuGet
Yesterday F# 3.0 Developer Preview became available at the MSDN Site. It will be general available on Friday.
One of the new features is the type provider.
This little snippet shows how easy it is to query a site that provides oData. NuGet is an example of a site that provides oData.
- #r "FSharp.Data.TypeProviders.dll"
- #r "System.Data.Services.Client.dll"
- open Microsoft.FSharp.Data.TypeProviders
- [<Generate>]
- type NuGet = ODataService<"http://packages.nuget.org/v1/FeedService.svc/">
- let dbNuGet = NuGet.GetDataContext()
- let packages = dbNuGet.Packages
- let nuGetQuery =
- query { for package in packages do
- where (package.Tags.Contains("fsharp") && package.IsLatestVersion)
- select package }
- let printPackage (package:NuGet.ServiceTypes.PublishedPackage) =
- printfn "- - - - - - - - - - - - - - - - - - - -"
- printfn "Title: %s" package.Title
- printfn "Authors: %s" package.Authors
- printfn "Description: %s" package.Description
- nuGetQuery |> Seq.iter printPackage
This is the result:
One of the nice features of type providers is intellisense for the available types.
It is possible to create your own type provider.
More information will be available on Friday. Don Syme will present all the details at build: http://channel9.msdn.com/events/BUILD/BUILD2011/SAC-904T
update: Writing F# Type Providers with the F# 3.0 Developer Preview - An Introductory Guide and Samples
Thursday, September 1, 2011
Monads for .NET developers
I have written an article for the Dutch developer magazine of SDN (Magazine 111). It is titled: “Monads voor de .NET-ontwikkelaar”.
In this post I will list some of the available resources on monads for .NET developers:
C# resources
Books:
- Tomas Petricek & Jon Skeet: Real World Functional Programming: With Examples in F# and C#, website
- Oliver Sturm: Functional Programming in C#: Classic Programming Techniques for Modern Projects, website
Web:
- Wes Dyer: The marvels of monads
- Mike Hadlow: Monads in C#–1. Introduction.
- Derick Bailey: Monads in C#: Which Part Is The Monad?
- Matthew Podwysocki: Functional .NET - LINQ or Language Integrated Monads?
- Tomas Petricek: Beyond the Monad fashion (I.): Writing idioms in LINQ
- Mauricio Scheffer: Refactoring to monadic C#
- Erik Meijer: The World According to LINQ
F# resources
Computation Expressions are addressed in most of the available F# books (Professional F# 2.0 does not): F# and functional programming
Web:
- Wiki: Computation Expressions
- Don Syme: Some Details on F# Computation Expressions
- Francois Vanderseypen : Categories, monads and F# for dummies
- Steffen Forkmann: Using monads in F# – Part I: The State Monad
- Brian McNamara : catamorphisms part seven
- Ivan Towlson : F# computation expressions for beginners, part 1: what’s the problem?
- Chance Coble: Why use Computation Workflows (aka Monads) in F#?
- Ryan Riley: A monad library for F# projects
Presentations
- Mike Hadlow: on Monads @ E-VAN 29 March 2011
- Robert C. Martin (Uncle Bob) at the Norwegian Developers Conference: “WTF is a Monad” (F# code)
- Erik Meijer: Fundamentalist Functional Programming & Chapter 8 - Functional Parsers
- Bart De Smet: LINQ, Take Two: Realizing the LINQ to Everything Dream
- Rick Minerich: Getting the MVVM Kicked Out of Your F#’n Monads
Some theory
- Wiki: monad
- Monad tutorials timeline
- Eugenio Moggi: Notions of Computation and Monads
- Real World Haskell: Chapter 14. Monads
- Learn You a Haskell for Great Good!: A Fistful of Monads
- Ertugrul Söylemez: Understanding Haskell Monads
Code:
The source code of the article: MonadsVoorDeDotNetOntwikkelaar and MonadsVoorDeDotNetOntwikkelaar_english
Monday, August 22, 2011
The newbie feeling
I read a blog post:http://blog.recursivity.com/post/9204470867/the-problem-with-the-scala-community that triggered me to write this post.
I do not know the Scala community so I can not agree or disagree with the conclusion:
“I love the Scala community and the eco-system around the language, but quite frankly, to foster the continued growth of the community, we need to call out the small minority of bullies on their bullshit whenever it occurs: being able to confuse and intellectually intimidate newbies to the language and community is not proving your supposed “intellectual superiority”, it is merely a reflection of an insecure ego and social incompetence.”
I do think that as a person we can not know everything of everything and even if we could one moment in time the world will change and we have to learn the new thing.
So we have two option: stick with what we know or accept the newbie feeling.
When you are a newbie you will make mistakes. My grant parents had the following text on a tile:”The only man who never makes mistakes is the man who never does anything.” I just found out it is a quote from Theodore Roosevelt (thanks Google).
So if we want to progress we have to accept the newbie feeling, accept the insecurity and make mistakes.
When we want to progress we have several options, one option is to learn it all by yourself, another is to accept the support of the community.
The community can support you in two ways:
- one is provide knowledge
- the other to provide mentorship
I think a programming language community is able to provide knowledge, that is nature of the community. To provide mentorship is something else and it is very hard to provide it on line.
So as a newbie look for knowledge on line and find mentorship somewhere else.
Sunday, August 7, 2011
The difference between SelectMany and Select in LINQ
This is follow up on my previous blog post:Adding xml strings in C# using LINQ.
In the previous post I showed that LINQ can be used to to manipulate XElements and strings. LINQ managed the transformation between the classes.
XElement result =
from x1 in el1
from x2 in el2
select x1.Substring(0, x1.LastIndexOf("<")) + x2.Substring(x2.IndexOf(">") + 1);
el1, el2 and the result are XElements and x1, x2 and the object before select are strings.
In the example there are two XElements and the compiler requires that we implement SelectMany. If delete SelectMany we get the following error:
In case we have one XElement we expect that the following will compile:
private static XElement ToUpper(XElement el)
{
XElement result =
from x in el
select x.ToUpper();
return result;
}
It does not: “'System.Xml.Linq.XElement' does not contain a definition for 'Select'”
So we implement Select:
public static XElement Select(this XElement el, Func<string, XElement> f)
{
return SelectMany(el, f, (x, y) => y);
}
Again it does not compile: “Cannot implicitly convert type 'string' to 'System.Xml.Linq.XElement'”
So we add an extra conversion:
private static XElement ToUpper(XElement el)
{
XElement result =
from x in el
select XElement.Parse(x.ToUpper());
return result;
}
This time it works:
var el = XElement.Parse("<a><b>1</b><b>2</b></a>");
Console.WriteLine(string.Format("ToUpper: {0}", ToUpper(el)));
Results in:
as expected.
Another way to solve the issue is to add a dummy line and use SelectMany:
private static XElement ToUpper(XElement el)
{
XElement result =
from x in el
from dummy in XElement.Parse("<dummy></dummy>")
select x.ToUpper();
return result;
}
with the same result.
Update: more information at: http://msdn.microsoft.com/en-us/library/bb546168.aspx
Adding xml strings in C# using LINQ
This is follow up on my previous blog post:Adding xml strings in F# using computation expressions. The text is nearly identical, the language is C# instead of F#.
In some cases classes are enhanced wrappers of simple classes. For instance in .NET:
- DateTime is a point in time and provides, among others things, extra functionality related to representing date and time formats.
- XElement is an xml string and provides, among others things, checks to determine the validity of the xml string.
- Option can contain a value and can tell you if there is value available.
Sometimes you have to deal with underling class and reuse some of the functionality of the wrapper. In this case you could consider using LINQ. We first define the Return, Bind and SelectMany functions to create the required functions.
For details more details see:
- Functional Programming in C#: Classic Programming Techniques for Modern Projects (Wrox Programmer to Programmer)
- Real World Functional Programming: With Examples in F# and C#
- Monads in C#–1. Introduction
All excellent resources.
public static classXmlMonad
{
//monad
public staticXElement Return(this stringtext)
{
try
{
returnXElement.Parse(text);
}
catch(XmlException exc)
{
returnXElement.Parse(String.Format("<XmlException>{0}</XmlException>", exc.Message));
}
}
public staticXElement Bind(thisXElement el, Func<string, XElement> f)
{
try
{
returnf(el.ToString());
}
catch(XmlException exc)
{
returnXElement.Parse(String.Format("<XmlException>{0}</XmlException>", exc.Message));
}
}
public staticXElement SelectMany(thisXElement el, Func<string, XElement> f, Func<string, string, string> select)
{
returnel.Bind(text => f(text).Bind(x => select(text, x).Return()));
}
}
We can now define a add function that will xml elements by using a string manipulation:
private static XElement Add(XElement el1, XElement el2)
{
XElement result =
from x1 in el1
from x2 in el2
select x1.Substring(0, x1.LastIndexOf("<")) + x2.Substring(x2.IndexOf(">") + 1);
return result;
}
Add in action:
var el1 = XElement.Parse("<a><b>1</b><b>2</b></a>");
var el2 = XElement.Parse("<a><b>3</b><b>4</b></a>");
Console.WriteLine(string.Format("Add: {0}", Add(el1,el2)));
Results in:
When we change the xml into:
var el2 = XElement.Parse("<c><b>3</b><b>4</b></c>");
The result is:
Thursday, August 4, 2011
Adding xml strings in F# using computation expressions
In some cases types are enhanced wrappers of simple types. For instance in .NET:
- DateTime is a point in time and provides, among others things, extra functionality related to representing date and time formats.
- XElement is an xml string and provides, among others things, checks to determine the validity of the xml string.
- Option can contain a value and can tell you if there is value available.
Sometimes you have to deal with underling type and reuse some of the functionality of the wrapper. In this case you could consider using computation expressions.
This is a simple xml example. We create a xmlBuilder and reuse the validation logic of XElement:
open System
open System.Xml
open System.Xml.Linq
type xmlBuilder()=
member x.Bind(el:XElement,(f:string -> XElement)) =
try
f(el.ToString())
with
| :? XmlException as exc -> XElement.Parse(String.Format("<XmlException>{0}</XmlException>", exc.Message))
member x.Return(text) =
try
XElement.Parse(text)
with
| :? XmlException as exc -> XElement.Parse(String.Format("<XmlException>{0}</XmlException>", exc.Message))
The string “123” creates an error:
let xml = new xmlBuilder()
let error =
xml {
return "123"
}
val error : XElement = <XmlException>Data at the root level is invalid. Line 1, position 1.</XmlException>
The expression can now be used to reuse sting functions:
let toUpper el =
xml {
let! x = el
return x.ToUpper()
}
let el = XElement.Parse("<a><b>1</b><b>2</b></a>")
let result1 = el |> toUpper
val result1 : XElement = <A>
<B>1</B>
<B>2</B>
</A>
In the same way we can define a simple add function for xml:
let (+) (el1:XElement) (el2:XElement) =
xml {
let! x1 = el1
let! x2 = el2
return x1.Substring(0, x1.LastIndexOf("<")) + x2.Substring(x2.IndexOf(">") + 1)
}
let el1 = XElement.Parse("<a><b>1</b><b>2</b></a>")
let el2 = XElement.Parse("<a><b>3</b><b>4</b></a>")
let result2 = el1 + el2
val result2 : XElement =
<a>
<b>1</b>
<b>2</b>
<b>3</b>
<b>4</b>
</a>
let el3 = XElement.Parse("<c><b>3</b><b>4</b></c>")
let result3 = el1 + el3
val result3 : XElement = <XmlException>The 'a' start tag on line 1 position 2 does not match the end tag of 'c'. Line 7, position 3.</XmlException>
This may not be in line with the concept of information hiding, but it can help you in case you have to deal with the simple type to get the work done.