Tuesday, October 4, 2011

BCL Team Blog provides more information on code reuse (Portable Libraries part 4)

Today the BCL Team Blog addressed the issue of code reuse: Porting existing .NET code to Metro style apps

"One of the things we know people want to do is reuse existing .NET code but due to the constraints on Metro style apps it is not possible to directly reuse existing .NET class libraries nor is it always possible to simply recompile existing code. We realize this is going to be a pain point for many current .NET developers so we are working on a guide at the .NET for Metro style apps overview page which will assist developers in translating their existing .NET skill set into the Metro style apps world.”

Bringing existing managed code into Metro style apps" by Daniel Plaisted is a recommended resource.

Daniel ports a windows phone app to a Metro app. Most of presentation is mainly about porting the UI.

Some slides from the presentation:

move_code

differenes

Getting the pictures from a local folder.

folders

There is no database access.

Wednesday, September 28, 2011

.NET portability is not an afterthought (adding another layer of indirection - Portable Libraries part 3)

Before //build/ I was not aware of the Portable Class Libraries Project. The origin of the project is the notion that the number of platform that provide .NET support has increased from one to four and that this number will increase in the future. Making .NET available on more platforms is the future of .NET.

The Portable Class Libraries Project was the first attempt of solving the portability issue but it ran into some constraints. One of them was the increasing complexity: “Portability is an afterthought”.

After watching some of the presentation at //build/ and experimenting with Visual Studio Express for Windows Developer Preview I thought that the whole purpose of creating a portable library was to bridge the gap between .NET and the WinRT.

ClassLibrary

This is not the case. Yesterday I watched .NET 4.5: Portable Libraries by David Kean and Mircea Trofin at Channel 9. It all made more sense to me.

 

The show:

  • The first 15 minutes explains portability and the first version of the Portable Class Libraries Project.
  • 15:00 whiteboard time.
  • Explaining the first way to solve portability problem:
  • wb1
  • wb2
  • Explaining the problems with System.NET
  • wb3
  • 22:00 the new solution: one portable library by design.
  • Portability is not an afterthought
  • 28:00
    • You program against a contract
    • It is up to the platform to implement the contract
    • Type forwarding:
      wb5
    • You program against the exposed api.
    • wb4
    • And the GAC will forward to dll that implements the actual code.
    • 33:00 Portability is a first class citizen
  • 35:00 It is about granularity a method is too small a dll is too big.
  • 36:00
    • Q: Are there two gacs in the Windows 8 world?
    • A: there is one gac. The metro mode is under “us”.
  • 37:00 breaking changes:
    • Type
    • wb6
    • wb7
  • 45:00 Close and Dispose. Close is disposed, no more closing just disposing.
  • They reference: A .NET developer's view of Windows 8 app development by Krzysztof Cwalina
  • 50:00 .NET has a great future with portability in mind.

So it makes sense to fix and recompile your legacy libraries. You can port them to all platforms that will have .NET available.

Thursday, September 22, 2011

Reuse of legacy software by WinRT and Metro (Windows 8) (Portable Libraries part 2)

 

This is a follow up to my previous post Create a Metro App in F# (NOT).

Things become quit complex if you want to reuse your windows legacy software. In this case legacy has a positive connotation. Legacy software is an asset of you and/or your company.

Suppose one want to reuse it and make it available for a Metro app on Windows 8 what are your options? After watching some of the build presentations:

 

I came up with the following flow chart:

 

LegacyWinRT_4

So:

  • You can reuse html, css and JavaScript.
  • I am not an expert on C++, COM, etc. so I do not know.
  • If you have only a dll you cannot use it.
  • If you have source code that references a language dll (like F#) or have no options today. This will change when the final product becomes available.
  • If you want to create a Metro Style project in C++ or JavaScript you can reuse your components code by fixing issues and recompile it to a WinMD file (WinRT component). Some issues can be exposing:
    • non WinRT types
    • non sealed classes
    • non system provided generic types
  • If you want to create a Metro Style project and this is one of the .NET languages you can reuse your components code by fixing issues and recompile it to a dll. Some issues can be:
    • Streams

 

Disclaimer: This is my current understanding, based on the presentations I viewed. If you have other information available please leave a note in the comments.

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.
FSharpMetro
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.
requirements
profile
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.
WinRt
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. WinMD
This are the restrictions:
Restrictions
Update(September 21, 2011):
Jason_Olson

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.

Code Snippet
  1. #r "FSharp.Data.TypeProviders.dll"
  2. #r "System.Data.Services.Client.dll"
  3.  
  4. open Microsoft.FSharp.Data.TypeProviders
  5.  
  6. [<Generate>]
  7. type NuGet = ODataService<"http://packages.nuget.org/v1/FeedService.svc/">
  8.  
  9. let dbNuGet = NuGet.GetDataContext()
  10. let packages = dbNuGet.Packages
  11.  
  12. let nuGetQuery =
  13.     query { for package in packages do
  14.             where (package.Tags.Contains("fsharp") && package.IsLatestVersion)
  15.             select package }
  16.  
  17. let printPackage (package:NuGet.ServiceTypes.PublishedPackage) =
  18.     printfn "- - - - - - - - - - - - - - - - - - - -"
  19.     printfn "Title: %s" package.Title
  20.     printfn "Authors: %s" package.Authors
  21.     printfn "Description: %s" package.Description
  22.  
  23.     
  24. nuGetQuery |> Seq.iter printPackage

This is the result:

result

One of the nice features of type providers is intellisense for the available types.

intellisense

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:

Web:

F# resources

Computation Expressions are addressed in most of the available F# books (Professional F# 2.0 does not): F# and functional programming

Web:

Presentations

Some theory

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).

tegelspreukmaker.nl

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:


SelectMany


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:
ToUpper

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:

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:


AddInAction


When we change the xml into:

var el2 = XElement.Parse("<c><b>3</b><b>4</b></c>");

The result is:


AddInActionException

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.

Thursday, July 14, 2011

What is the USP of the monad?

I am preparing an introductory article on monads for a Dutch software magazine. It is intended for .NET developers. I have to restrict myself so I am looking for the Unique Selling Point (USP) of the monad.

This is the list of selling points I could find. I left out selling points of specific monads and used the formulation of Wikipedia:

  1. Monads are a kind of abstract data type constructor that encapsulate program logic instead of data in the domain model.
  2. Control structure/inspection.
  3. Hiding complexity with syntactic sugar.
  4. Composition : monads chain actions together to build a pipeline.
  5. To express input/output (I/O) operations and changes in state without using language features that introduce side effects.

I put them in the order of most favorite to least favorite.

My remarks:

1. Monads are a kind of abstract data type constructor that encapsulate program logic instead of data in the domain model.

This point can be split into two aspects of monads:

  1. The monadic type is a wrapper of a data type (If M is the name of the monad and t is a data type, then "M t" is the corresponding type in the monad).
  2. The functions: read (unit) and bind.

I think that the ability to have both access to the functionality of the wrapper and the original data type and a way to transform solution between the two is the most valuable selling point.

2. Control structure/inspection.

The monad is an excellent way to encapsulate inspection logic. Most monad tutorials start with the maybe monad because it is a simple example. It also shows the value of the monadic approach by hiding inspection plumbing.

3. Hiding complexity with syntactic sugar.

I think that the popularity of LINQ in C# and VB.NET is a proof that hiding complexity with syntactic sugar has its value.

4. Composition: monads chain actions together to build a pipeline.

In F# computation expressions or monads are also described as workflows. So this could be the USP. Sequence expression are a proof of the value of composition.

5. To express input/output (I/O) operations and changes in state without using language features that introduce side effects.

This is the least relevant point for a .NET developer. I do understand that this the most relevant one for a developer in a pure language or one that want to reduce the number of side effects.

Please feel free to add a command in case I missed a selling point or you have a better way to order the them.

Tuesday, June 28, 2011

WTF is a Monad by Robert C. Martin in F#

I have read some material describing Computation Expressions and knew computation expressions are the F# implementation of monads, but I did know why one should use a monad.

A few days ago saw the video of presentation by Robert C. Martin (Uncle Bob) at the Norwegian Developers Conference called “WTF is a Monad” and now it makes sense to me.

It is a proven way of solving problems. If you:

  • have a problem in one domain
  • you can transform it to another domain
  • solve the problem in the other domain
  • and can transform it back

you have a solution.

I have translated some of the code of the presentation to F#. I tried to stay as close to the original code as possible and did not do much refactoring. I skipped the last two examples (distributions an state).

Update: I did not implement the examples that required lift. I have not figured out how to do that. I have created the lift myself (details: http://bugsquash.blogspot.com/2010/12/notes-on-haskell-functors-and-f.html)

I like to thank Robert for the permission to share the code. Feel free to add improvements in the comments.

//Non-Monadic dots

let dotsToN (d:string) =
d.Length

let result1 = dotsToN "....."

let nToDots (n:int) =
new string('.', n)

let result2 = nToDots 5

let addDots da db =
let a = dotsToN da
let b = dotsToN db
nToDots (a+b)

let result3 = addDots "..." "..."

//Monadic dots

let dotResult = nToDots

let dotBind d (f:int -> string) =
d |> dotsToN |> f

let addDots' da db =
dotBind da (fun a ->
dotBind db (fun b ->
dotResult (a + b)))

let result4= addDots' "..." "..."

//dot-Monad
type dotBuilder()=
member x.Bind(d,(f:int -> string)) = d |> dotsToN |> f
member x.Return(n) = new string('.', n)

let dot = new dotBuilder()

let addDots'' da db =
dot { let! x = da
let! y = db
return x + y
}

let result5= addDots'' "..." "..."

let multiplyDots da db =
dot { let! x = da
let! y = db
return x*y
}

let result6= multiplyDots "..." "..."

let dcd dt du =
dot { let! tens = dt
let! units = du
return (10 * tens + units)
}

let result7= dcd "..." "....."

// In Clojure the monad gets the lift for free
// In F# we have to create it ourselves
// More info: http://bugsquash.blogspot.com/2010/12/notes-on-haskell-functors-and-f.html --
//

let liftDots_1 f a1 =
dot {
let! x1 = a1
return f x1
}

let liftDots_2 f a1 a2 =
dot {
let! x1 = a1
let! x2 = a2
return f x1 x2
}

let liftDots_4 f a1 a2 a3 a4 =
dot {
let! x1 = a1
let! x2 = a2
let! x3 = a3
let! x4 = a4
return f x1 x2 x3 x4
}

let substractDots = liftDots_2 (-)

let result7a= substractDots "....." ".."

let mean4 a b c d = (a+b+c+d)/4

let dmean4 = liftDots_4 mean4

let result7b= dmean4 "..." ".." "..." "...."

//
//complex number (inspection)
//

open System.Numerics

let c_0 = Complex.Zero
let c_1 = Complex.One
let c_i = Complex.ImaginaryOne

type complexBuilder()=
member b.Return(x:float) = new Complex(x,0.0)
member b.Bind((c:Complex),(f:float -> Complex)) =
if c.Imaginary = 0.0 then
f c.Real
else
failwithf "Imaginary"

let complex = new complexBuilder()

let addComplex c1 c2 =
complex { let! x = c1
let! y = c2
return x + y
}

let result8= addComplex c_1 c_1;;
let result9= addComplex c_1 c_i;; //System.Exception: Imaginary
let result10= addComplex c_i c_i;; //System.Exception: Imaginary

//null

type noNullBuilder()=
member b.Return(mv) = mv
member b.Bind(mv,f) = if (mv = null) then null else f mv

let nuNull = new noNullBuilder()

// int can not be null in .Net

let fragile (a:string) (b:string) (c:string) =
if (a= null||b=null||c=null)
then
"CRASH"
else
a + b + c

let result11 = fragile "a" "b" "c"
let result12 = fragile "a" null "c" //"CRASH"

let safeFragile (a:string) (b:string) (c:string) =
nuNull { let! x = a
let! y = b
let! z = c
return x + y + z
}

let result13 = safeFragile "a" "b" "c"
let result14 = safeFragile "a" null "c" //val result14 : string = null

//List
let l_a = [1;2;3]
let l_b = [4;5;6]

let addList = seq {for a in l_a do
for
b in l_b do
yield
a+b
}|> Seq.toList

let multList = seq {for a in l_a do
for
b in l_b do
yield
a*b
}|> Seq.toList

//helper for the demo
let flatten l =
let rec flatten' l result =
match l with
|[] -> result
|h::t -> flatten' t (h@result)
flatten' (l|>List.rev) []

let result15 = flatten [[1;2;3];[7;8;9]]

type listBuilder()=
member b.Return(v) = [v]
member b.Bind(mv,f) = mv |> List.map f |> flatten

let list = new listBuilder()

let addList' a b = list {
let! x = a
let! y = b
return x + y
}

let result16 = addList' l_a l_b

let multList' a b = list {
let! x = a
let! y = b
return x * y
}

let result17 = multList' l_a l_b

//distribution example see Expert F# of Don Syme page 239..244
//state http://fsharpcode.blogspot.com/2008/12/f-state-monad-type-state-state-state-of.html

Wednesday, June 22, 2011

LINQ to XML, when lazy is too lazy in F# (and C# )

 

Today a F# program did not work as expected. This is a small example I created:

let xml ="<a><b>1</b><b>2</b></a>"
let doc = XDocument.Parse(xml)

let getXElements_b (doc:XDocument) =
let els = doc.Root.Elements(XName.Get("b"))
els
//test
let result1 = doc |> getXElements_b |> Seq.toList
//val result1 : XElement list = [<b>1</b>; <b>2</b>]
//the expected result 

let proces (el:XElement) =
XElement(XName.Get("c")) |> el.ReplaceWith

doc |> getXElements_b |> Seq.iter proces

let result2 =  doc.ToString();;
//val result2 : string = "<a>
//  <c />
//  <b>2</b>
//</a>"
This is not the expected result. I expected:

val result2 : string = "<a>
<c />
<c />
</a>"

I can fix it by changing the getXElements_b function:


let getXElements_b (doc:XDocument) =
let els = doc.Root.Elements(XName.Get("b")) |> Seq.toList
els

With this adjustment everything works fine. It seems that I have to force the the computation.


This is a problem in C# too.

private void GetXml()
{
var xml = "<a><b>1</b><b>2</b></a>";
var doc = XDocument.Parse(xml);
var els = doc.Root.Elements(XName.Get("b"));

foreach (var el in els)
{
el.ReplaceWith(new XElement(XName.Get("c")));
}

txtResult.Text = doc.ToString();
}
produces:

<a>
  <c />
  <b>2</b>
</a>


When I step with the debuger through the code the foreach is only one time triggered.


With support of Google I found the following document:


http://msdn.microsoft.com/en-us/library/bb308960.aspx


lazyXML


You can learn something new everyday.


Interesting links:

The "Halloween Problem" for XML APIs:

http://blogs.msdn.com/b/mikechampion/archive/2006/07/20/672208.aspx

Query Composition using Functional Programming Techniques in C# 3.0:

http://blogs.msdn.com/b/ericwhite/archive/2006/10/04/fp-tutorial.aspx

Monday, June 20, 2011

Update an open XML word document in F#

 
I have created a small function in F# to update the content of a word document.
//reference to the package 
#r "WindowsBase"
//reference to the Xml and Linq 
#r "System.Xml"
#r "System.Xml.Linq"

open System
open System.IO
open System.IO.Packaging
open System.Xml
open System.Xml.Linq

//helper to create XName with a namespace
let xname str =
XName.Get(str, "http://schemas.openxmlformats.org/wordprocessingml/2006/main")

// the main function 
// changeDoc copies a word .docx document and updates the content by changing the xml that represents the content
let changeDoc oldPath newPath (updateDocument:XDocument -> XDocument) =
//update the package part
let updatePart (part:PackagePart) =
using (part.GetStream(FileMode.Open, FileAccess.Read)) (fun stream -> 
let xDocumentOut = stream |> XDocument.Load |> updateDocument
xDocumentOut.Save(part.GetStream(FileMode.Create, FileAccess.Write))
)

File.Copy(oldPath, newPath, true)
using (Package.Open(newPath, FileMode.Open, FileAccess.ReadWrite)) (fun package ->
let uri = new Uri("/word/document.xml", UriKind.Relative )

uri |> package.GetPart|> updatePart
)

// test function to update the document
// adds "_test" to every text  in the document.
let updateText (xDoc:XDocument) =
"t" |> xname  |> xDoc.Descendants |> Seq.iter (fun x -> x.Value <- x.Value + "_test")
xDoc

//test
changeDoc @"D:\Tmp\test.docx" @"D:\Tmp\test_copy.docx" updateText;;
Before "D:\Tmp\test.docx":
before
 
After "D:\Tmp\test_copy.docx":
after

Wednesday, June 8, 2011

Creation of a very simple F# model of a spreadsheet

Today I posted  a F# snipped at http://fssnip.net/5F.

It creates an Open Xml spreadsheet and does not depend on the Open Xml SDK.

To add data to the sheet you need SheetData . I have created a small model op a spreadsheet and some functions to generate the SheetData.

First the model:
//model of spreadsheet
// column * value
type Cell =
    | Text of string * string
    | Number of string * int
    | Formula of string * string

// row number * cells in the row
type Row =
    |Cells of int * Cell list

//list of rows
type Sheet =
    | Rows of Row list

Notice that the model reflects the structure of the Open Xml specifications. Sheets contain Rows and Rows contain Cells.  I have modeled three types of cells inline text, numbers and formulas. There are more. From the specifications (17.18.11 ST_CellType (Cell Type)):
Enumeration Value
Description
b (Boolean)
Cell containing a boolean.
d (Date)
Cell contains a date in the ISO 8601 format.
e (Error)
Cell containing an error.
inlineStr (Inline String)
Cell containing an (inline) rich string, i.e., one not in the shared string table. If this cell type is used, then the cell value is in the is element rather than the v element in the cell (c element).
n (Number)
Cell containing a number.
s (Shared String)
Cell containing a shared string.
str (String)
Cell containing a formula string.

These are the tests:
//test
let cell1 = Text("A","test1")
let cell2 = Number("B", 42)
let cell3 = Text("A","test2")
let cell4 = Number("B", 43)
let cell5 = Formula("B""SUM(B1:B2)")
 
let row1 = Cells(1, [cell1; cell2])
let row2 = Cells(2, [cell3; cell4])
let row3 = Cells(3, [cell5])
 
let sheet = Rows([row1; row2; row3])

This are the required functions to generate the SheetData:
let createTextCell column rowNumber (value:string) =
    let cell = new XElement(xname "c",
                                new XAttribute(xnameEmpty "r", column + rowNumber.ToString()),
                                new XAttribute(xnameEmpty "t""inlineStr"),
                                    new XElement(xname "is",
                                        new XElement(xname "t", value)))
    cell
 
//test
let result1 = createTextCell "A" 1 "abc"
 
let createNumberCell column rowNumber (value:int) =
    let cell =new XElement(xname "c",
                                new XAttribute(xnameEmpty "r", column + rowNumber.ToString()),
                                new XAttribute(xnameEmpty "t""n"),
                                    new XElement(xname "v", value
                                        ))
    cell
 
let createFormulaCell column rowNumber (formula:string) =
    let cell =new XElement(xname "c",
                                new XAttribute(xnameEmpty "r", column + rowNumber.ToString()),
                                new XAttribute(xnameEmpty "t""n"),
                                    new XElement(xname "f", formula
                                        ))
    cell
 
//test
let result2 = createNumberCell "B" 1 4
 
let createCell rowNumber = function
    | Text (culumn, value) -> createTextCell culumn rowNumber value
    | Number (culumn, value) -> createNumberCell culumn rowNumber value
    | Formula (culumn, formula) -> createFormulaCell culumn rowNumber formula
 
//test
let result3 =createCell 1 cell1
let result4 =createCell 1 cell2
 
let createRow = function
    |Cells (rowNumber, cells) -> 
        let rowElement = new XElement(xname "row"new XAttribute(xnameEmpty "r", rowNumber))
        cells |> List.map (createCell rowNumber) |> rowElement.Add
        rowElement
 
let result5 = createRow row2
 
let createSheetData (sheet:Sheet) =
    let createRows =  function
        | Rows rows -> rows|> List.map createRow
    let sheetData = new XElement(xname "sheetData")
    sheet|> createRows |> sheetData.Add
    sheetData
 
let result6 = createSheetData sheet

This is the result:



Because the sheet contains a formula in cell B3 Excel will calculates the result. 

Total Pageviews