SyntaxHighlighter
Thursday, September 9, 2010
Introducing MATLAB -> ( F# & WPF ) Series
My latest purchase a few days ago was at Borders book store. I went in the store looking for a specific WPF charting book which covers visualizing mathematical formulas. No such luck. However, after turning my attention to the core computer science section, I found the following book on MATLAB (short for" matrix laboratory"). I was never really strong in math and this book seems to be a nice self guided introduction which works up to some advanced data analysis/visualizations. Since I'm constantly looking for core computer science type approaches from a functional perspective I figured this is a good choice. My plan is to obviously teach myself MATLAB but also translate the MATLAB functions over to F# and move the graphical stuff over to WPF. For the last 8 years I've been doing hardcore imperative programming using mainstream languages. More and more every day I wish I where doing functional programming using (F# of course) or something like ML,OCaml, or dare I say Haskell. My long term plan is to teach myself core functional programming so that I can get a job which safely transitions into the functional programming world. My dream job would be to write core functional programs while having the freedom to visualize them using a great presentation framework like WPF. As the title says, I will be going through the chapters & exercises and presenting them here. I will take as much time as I can to produce good quality posts on this topic. You can expect the format to be something like the following: MATLAB source and screen shots( if any), followed by F# source, followed by WPF screen shots. Any way, I'm looking forward to learning MATLAB and sharing my discoveries here on my blog.
-Stay tuned and keep on dev'n
Saturday, July 31, 2010
My F# awareness is improving
So today I read an article that linked to a site called language references. It's basically a site like code keep, it has some snippets of code for common programming tasks or problems. I noticed that there is a section on F# so I decided to check it out. I started scrolling through the list and came to a snippet of code for reversing a string. This coding example is near and dear to me because it's one of those interview questions programmers tend to get... much like fizbuzz. However, after reviewing the implementation which was simple Array.Reverse on a character array I decided to create an account so that I could contribute my version of reversing a string in F#. If you go to the following url: http://langref.org/fsharp/strings/reversing-a-string/reverse-characters you'll see my implementation which is also below.
Here's the problem: http://langref.org/fsharp/xml/parsing/xml-parse
So I came up with the following implementation: (which you will also see on the website)
F# is awesome!!
let word = "reverse me" //reverse the word let reversedword = word.ToCharArray() |> Array.fold(fun acc x -> List.Cons(x,acc)) []I'm basically using a simple fold with an empty list as the accumulator or the element that's being threaded through. I picked up that tip from a functional programming video I saw on videos.google.com. The topic was Map Reduce. I then went on to the section of the site where they have a list of problems that have not been solved yet. So I decided to tackle one of those. I came across the following and immediately thought it would be rather simple to implement and I could use a simple fold again. :-)
Here's the problem: http://langref.org/fsharp/xml/parsing/xml-parse
So I came up with the following implementation: (which you will also see on the website)
#r @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll" open System open System.Xml.Linq //XElement Helper let xname sname = XName.Get sname let xmlsnippet = let snippet = new XElement(xname "shopping") //create bread let bread = new XElement(xname "item") bread.SetAttributeValue(xname "name","bread") bread.SetAttributeValue(xname "quantity",3) bread.SetAttributeValue(xname "price",2.50) //add bread to snippet snippet.Add(bread) //create milk let milk = new XElement(xname "item") milk.SetAttributeValue(xname "name","milk") milk.SetAttributeValue(xname "quantity",2) milk.SetAttributeValue(xname "price",3.50) //add milk to snippet snippet.Add(milk) snippet let totalprice (xe: XElement) = xe.Descendants(xname "item") |> Seq.map(fun i -> Double.Parse(i.Attribute(xname "price").Value)) |> Seq.fold(fun acc x -> acc + x) 0.0
F# is awesome!!
Wednesday, May 26, 2010
Another late night learning more about F#
Just thought I'd share a few minor things I'm working on in my spare time. The first little project is a visualization project that I'm doing for my wife. She's collecting some information from a large group of schools in the district and compiling it all in an excel file. So I thought I'd write some F# to scrape the data from the excel file and perform some analysis on it and then visualize it in Silverlight.
The silverlight part hasn't started quite yet, but I should get to that tonight or tomorrow. Below is a screen shot of the excel document in the early stages of compiling the data manually.
The data is basically about competed teams at a state competition over the past 5 years broken out
by category. My job is to pull all this information and display by teams size (which is the red, green, and yellow on the right hand side of the document). Below is some of the F# I've come up with so far.
open Microsoft.Office.Interop.Excel
open System
open System.Runtime.InteropServices
let TeamSizeByCategory (category: int) (startColumn: string) (endColumn: string) = // Pull the data from each row and yield it into a list of obj[Dance,Military,Hip Hop,Kick,Pom,Show]
let row = (worksheet.Range(String.Format("{0}{1}",startColumn,i) , String.Format(" {0}{1}",endColumn,i)).Value2 :?> obj[,])
yield [row.GetValue(1,1)
row.GetValue(1,2)
row.GetValue(1,3)
row.GetValue(1,4)
row.GetValue(1,5)
row.GetValue(1,6)]]
let CategoryFolks =
data
|> List.filter( fun i -> i.[category] <> null)
let CategoryFolksSmall =
CategoryFolks
|> List.filter( fun item -> (item.[category] :?> double) <= 15.0)
let CategoryFolksMedium =
CategoryFolks
|> List.filter( fun item -> (item.[category] :?> double) >= 16.0)
|> List.filter( fun item -> (item.[category] :?> double) <= 29.0)
let CategoryFolksLarge =
CategoryFolks
|> List.filter( fun item -> (item.[category] :?> double) >= 30.0)
seq {
if CategoryFolks.Length > 0 && CategoryFolksSmall.Length > 0 then
yield "Small", Math.Abs(float CategoryFolksSmall.Length / float CategoryFolks.Length) * 100.0
if CategoryFolks.Length > 0 && CategoryFolksMedium.Length > 0 then
yield "Medium", Math.Abs(float CategoryFolksMedium.Length / float CategoryFolks.Length) * 100.0
if CategoryFolks.Length > 0 && CategoryFolksLarge.Length > 0 then
yield "Large", Math.Abs(float CategoryFolksLarge.Length / float CategoryFolks.Length) * 100.0
}
//Simple type to encapsulate a single year of data for all categories
type AnnualStatistic(year,data) =
member x.Year = year
member x.Data = data
//I really want the ability to persist the data from the excel file into a Db40 object database, which is something I've blogged about before.
let dbpath = "C:\Users\cfrederick\Documents\Visual Studio 2010\Projects\ResearchVisualization\db"
let persist (year: string) startColumn endColumn =
let data = [ for i=5 to 80 do
let row = (worksheet.Range(String.Format("{0}{1}",startColumn,i) , String.Format(" {0}{1}",endColumn,i)).Value2 :?> obj[,])
yield [row.GetValue(1,1)
row.GetValue(1,2)
row.GetValue(1,3)
row.GetValue(1,4)
row.GetValue(1,5)
row.GetValue(1,6)]]
/// Db40 Implementation to persists historical data from excel
//the use keyword is synonymous to using in c#
use container = Db4oFactory.OpenFile(Path.Combine(dbpath, "Researchdb.yap"));
container.Store(new AnnualStatistic(year,data))
container.Close
//Finally a helper function to retrieve a given years worth of data for all categories from the Db4o database.
let AnnualStatistic year =
use container = Db4oFactory.OpenFile(Path.Combine(dbpath, "Researchdb.yap"));
let results =
seq {
let r = container.Cast().Select(fun i -> i)
for i in r do
yield i
}
|> Seq.filter(fun item -> item.Year = year)
|> Seq.head
results
The more I use F# the more I love it. :-)
-Develop with Passion
The silverlight part hasn't started quite yet, but I should get to that tonight or tomorrow. Below is a screen shot of the excel document in the early stages of compiling the data manually.
The data is basically about competed teams at a state competition over the past 5 years broken out
by category. My job is to pull all this information and display by teams size (which is the red, green, and yellow on the right hand side of the document). Below is some of the F# I've come up with so far.
#r @"C:\Windows\assembly\GAC\Microsoft.Office.Interop.Excel\12.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll"
#r @"C:\Windows\assembly\GAC\Office\12.0.0.0__71e9bce111e9429c\Office.dll"
open Microsoft.Office.Interop.Excel
open System
open System.Runtime.InteropServices
// Create new Excel.Application
let app = new ApplicationClass(Visible = true) //Create the workbook path
let workbookPath = @"C:\Research.xlsx"// Open the workbook from the workbook path
let workbook = app.Workbooks.Open(workbookPath)// Get the worksheets collection
let sheets = workbook.Worksheets // Grab the worksheet we need to pull data from
let worksheet = (sheets.[box 1] :?> _Worksheet) let TeamSizeByCategory (category: int) (startColumn: string) (endColumn: string) = // Pull the data from each row and yield it into a list of obj[Dance,Military,Hip Hop,Kick,Pom,Show]
// I-N (2006) P-U (2007) W-AB (2008) AD-AI (2009) AK-AP (2010)
let data = [ for i=5 to 80 dolet row = (worksheet.Range(String.Format("{0}{1}",startColumn,i) , String.Format(" {0}{1}",endColumn,i)).Value2 :?> obj[,])
yield [row.GetValue(1,1)
row.GetValue(1,2)
row.GetValue(1,3)
row.GetValue(1,4)
row.GetValue(1,5)
row.GetValue(1,6)]]
let CategoryFolks =
data
|> List.filter( fun i -> i.[category] <> null)
let CategoryFolksSmall =
CategoryFolks
|> List.filter( fun item -> (item.[category] :?> double) <= 15.0)
let CategoryFolksMedium =
CategoryFolks
|> List.filter( fun item -> (item.[category] :?> double) >= 16.0)
|> List.filter( fun item -> (item.[category] :?> double) <= 29.0)
let CategoryFolksLarge =
CategoryFolks
|> List.filter( fun item -> (item.[category] :?> double) >= 30.0)
seq {
if CategoryFolks.Length > 0 && CategoryFolksSmall.Length > 0 then
yield "Small", Math.Abs(float CategoryFolksSmall.Length / float CategoryFolks.Length) * 100.0
if CategoryFolks.Length > 0 && CategoryFolksMedium.Length > 0 then
yield "Medium", Math.Abs(float CategoryFolksMedium.Length / float CategoryFolks.Length) * 100.0
if CategoryFolks.Length > 0 && CategoryFolksLarge.Length > 0 then
yield "Large", Math.Abs(float CategoryFolksLarge.Length / float CategoryFolks.Length) * 100.0
}
//Simple type to encapsulate a single year of data for all categories
type AnnualStatistic(year,data) =
member x.Year = year
member x.Data = data
//I really want the ability to persist the data from the excel file into a Db40 object database, which is something I've blogged about before.
let dbpath = "C:\Users\cfrederick\Documents\Visual Studio 2010\Projects\ResearchVisualization\db"
let persist (year: string) startColumn endColumn =
let data = [ for i=5 to 80 do
let row = (worksheet.Range(String.Format("{0}{1}",startColumn,i) , String.Format(" {0}{1}",endColumn,i)).Value2 :?> obj[,])
yield [row.GetValue(1,1)
row.GetValue(1,2)
row.GetValue(1,3)
row.GetValue(1,4)
row.GetValue(1,5)
row.GetValue(1,6)]]
/// Db40 Implementation to persists historical data from excel
//the use keyword is synonymous to using in c#
use container = Db4oFactory.OpenFile(Path.Combine(dbpath, "Researchdb.yap"));
container.Store(new AnnualStatistic(year,data))
container.Close
//Finally a helper function to retrieve a given years worth of data for all categories from the Db4o database.
let AnnualStatistic year =
use container = Db4oFactory.OpenFile(Path.Combine(dbpath, "Researchdb.yap"));
let results =
seq {
let r = container.Cast
for i in r do
yield i
}
|> Seq.filter(fun item -> item.Year = year)
|> Seq.head
results
The more I use F# the more I love it. :-)
-Develop with Passion
Sunday, May 2, 2010
New path for possible career opportunity
Lately, functional programming is all I think about. Sometimes I get a little down about writing C#. :-(
Gasp! I never thought in a million years that I'd ever think that let alone write it. Anyway, at the present time I find myself continuing to make a difference for my current employer. Introducing them to modern solutions and technology to help them have a leg up on the competition. However, I'm constantly reminded that I'm not in an environment where I'm learning from my co-workers. So I have to seek external conferences, blogs, camps, user groups, etc to make up for it. For the last five years I've always kept an eye on Google. Lately, I've seen more opportunities pop up... even some with .NET development. I'm thinking that over next two years I'm going to ramp up my skills on F#, Python, and Ruby. From what I've seen and heard so far those languages are core prerequisites. There will be a lot to learn including Google App framework, but the payoff could be totally worth it. We'll see what happens, but that's the thought for now. :-)
-Develop with Passion
Gasp! I never thought in a million years that I'd ever think that let alone write it. Anyway, at the present time I find myself continuing to make a difference for my current employer. Introducing them to modern solutions and technology to help them have a leg up on the competition. However, I'm constantly reminded that I'm not in an environment where I'm learning from my co-workers. So I have to seek external conferences, blogs, camps, user groups, etc to make up for it. For the last five years I've always kept an eye on Google. Lately, I've seen more opportunities pop up... even some with .NET development. I'm thinking that over next two years I'm going to ramp up my skills on F#, Python, and Ruby. From what I've seen and heard so far those languages are core prerequisites. There will be a lot to learn including Google App framework, but the payoff could be totally worth it. We'll see what happens, but that's the thought for now. :-)
-Develop with Passion
Saturday, April 3, 2010
The road to F#
Yesterday I got called to a meeting to talk about a prototype which I can't speak of.
However, I can talk about one requirement which is to read information from a ms project file. Things like tasks and resources. I've never parsed a ms project file before but I figured it would be similar to any other office object model using yucky COM underneath. So I fired up VS2010 and did a file -> new project on the F# node and started down the normal path of adding a reference to the ms project library blah blah blah.
Then I had to launch ms project 2007 and load a project file, and then I could go back to FSI and start to write some code. After about 2 min of hacking, here's what I got
#r @"C:\Windows\assembly\GAC\Microsoft.Office.Interop.MSProject\12.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.MSProject.dll"
open Microsoft.Office.Interop.MSProject
let projectapp = new ApplicationClass()
let project = projectapp.ActiveProject
for t in project.Tasks do
printfn "%A" t.Name
Not to shabby. However, I did have to open project and load a .mpp file (which is the project file format).
This is not a big deal since the app requires it, but I thought to myself... what if it did not require it?
So I started searching around for a library or just information the file format itself to see if I could build something to parse it directly. After about 10 min I found mpxj, this library is originally written in Java and then they wrote a .NET library on top of it. So .NET interops with this library. Best part is... you can work with many versions of MS project and you don't need to have it installed. You can point to the file load it up and pull out almost any data you wish. I thought it might sound to good to be true, so I downloaded it and tried to use it in F#. After 20 min of hacking and reading the documentation... I came up with this.
#r @"local path to this file ->\mpxj.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Core.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Util.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Beans.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Charsets.dll"
#r @"local path to this file ->\IKVM.OpenJDK.jdbc.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Misc.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Security.dll"
#r @"local path to this file ->\IKVM.OpenJDK.SwingAWT.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Text.dll"
#r @"local path to this file ->\IKVM.OpenJDK.XML.API.dll"
#r @"local path to this file ->\IKVM.OpenJDK.XML.Bind.dll"
#r @"local path to this file ->\IKVM.OpenJDK.XML.Parse.dll"
#r @"local path to this file ->\IKVM.Runtime.dll"
#r @"local path to this file ->\poi-3.6-20091214.dll"
open System
open net.sf.mpxj
open net.sf.mpxj.reader
open net.sf.mpxj.writer
let filepath = "b4ubuild_sample_07.mpp"
let preader = ProjectReaderUtility.getProjectReader(filepath)
let pfile = preader.read(filepath)
let ta = pfile.getAllTasks()
let resources = pfile.getAllResources()
for i = 0 to ta.size() do
let t = (ta.get(i) :?> Task) //down casting in F# (taking an obj and casting it to typeof(Task))
let taskName = t.getName()
Console.WriteLine(taskName)
for i = 0 to resources.size() do
let r = (resources.get(i) :?> Resource)
let rname = r.getName()
Console.WriteLine(rname)
Now that's more like it. I've officially decided to invest in F#. The last time I made a big investment like this was around 2005 when I first started playing with WPF. :-)
However, I can talk about one requirement which is to read information from a ms project file. Things like tasks and resources. I've never parsed a ms project file before but I figured it would be similar to any other office object model using yucky COM underneath. So I fired up VS2010 and did a file -> new project on the F# node and started down the normal path of adding a reference to the ms project library blah blah blah.
Then I had to launch ms project 2007 and load a project file, and then I could go back to FSI and start to write some code. After about 2 min of hacking, here's what I got
#r @"C:\Windows\assembly\GAC\Microsoft.Office.Interop.MSProject\12.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.MSProject.dll"
open Microsoft.Office.Interop.MSProject
let projectapp = new ApplicationClass()
let project = projectapp.ActiveProject
for t in project.Tasks do
printfn "%A" t.Name
Not to shabby. However, I did have to open project and load a .mpp file (which is the project file format).
This is not a big deal since the app requires it, but I thought to myself... what if it did not require it?
So I started searching around for a library or just information the file format itself to see if I could build something to parse it directly. After about 10 min I found mpxj, this library is originally written in Java and then they wrote a .NET library on top of it. So .NET interops with this library. Best part is... you can work with many versions of MS project and you don't need to have it installed. You can point to the file load it up and pull out almost any data you wish. I thought it might sound to good to be true, so I downloaded it and tried to use it in F#. After 20 min of hacking and reading the documentation... I came up with this.
#r @"local path to this file ->\mpxj.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Core.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Util.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Beans.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Charsets.dll"
#r @"local path to this file ->\IKVM.OpenJDK.jdbc.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Misc.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Security.dll"
#r @"local path to this file ->\IKVM.OpenJDK.SwingAWT.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Text.dll"
#r @"local path to this file ->\IKVM.OpenJDK.XML.API.dll"
#r @"local path to this file ->\IKVM.OpenJDK.XML.Bind.dll"
#r @"local path to this file ->\IKVM.OpenJDK.XML.Parse.dll"
#r @"local path to this file ->\IKVM.Runtime.dll"
#r @"local path to this file ->\poi-3.6-20091214.dll"
open System
open net.sf.mpxj
open net.sf.mpxj.reader
open net.sf.mpxj.writer
let filepath = "b4ubuild_sample_07.mpp"
let preader = ProjectReaderUtility.getProjectReader(filepath)
let pfile = preader.read(filepath)
let ta = pfile.getAllTasks()
let resources = pfile.getAllResources()
for i = 0 to ta.size() do
let t = (ta.get(i) :?> Task) //down casting in F# (taking an obj and casting it to typeof(Task))
let taskName = t.getName()
Console.WriteLine(taskName)
for i = 0 to resources.size() do
let r = (resources.get(i) :?> Resource)
let rname = r.getName()
Console.WriteLine(rname)
Now that's more like it. I've officially decided to invest in F#. The last time I made a big investment like this was around 2005 when I first started playing with WPF. :-)
Friday, March 19, 2010
The functional programming journey
For the last year and half I've been playing around with F#. Accomplishing little task like simple File I/O, Winform UI Development, standard calculations, etc. Doing all of this because I have a F# book and a few web resources. So I figured that I would finally break down and do what the experts say I should do... which is to learn another functional programming language. A few weeks ago I went looking on I Tunes-U for some functional programming lectures. To my surprise I found quite a bit of lectures. UC Berkley & Stanford lectures stood out the most so I tried the Stanford lectures. Programming Paradigms by Jerry Cain is a great series of lectures covering C/C++/Java/LISP/Python/networking and more. I chose to jump into the LISP section of the series. After reviewing the lectures it gave me a fresh perspective on F#, and suddenly F# made a lot more sense in my head. Now I'm able to do thing like interface with WPF from F#, I wrote my own F# library and leveraged it in a C# project I'm working on, and the more I learn about core functional fundamentals the more I learn about F#. I guess I just needed a deeper understanding in order to wrap my mind around F# and functional programming in general. And I must say, the more I realize what the F# compiler is doing behind the scenes the more I appreciate it. Just yesterday I was writing some c# code in a project and thought to
myself "I wish I was in F# right now, because I'd be done with this task if I was". :-)
What does that mean exactly... should I make the transition to functional programming? Sometimes I think I could. I'd just need to beef up on my math skills, and I'd be all set. I'll save that topic for another day. :-)
myself "I wish I was in F# right now, because I'd be done with this task if I was". :-)
What does that mean exactly... should I make the transition to functional programming? Sometimes I think I could. I'd just need to beef up on my math skills, and I'd be all set. I'll save that topic for another day. :-)
Tuesday, April 14, 2009
Brilliant!!!!
(Udi Dahan) Make Roles Explicit!!!I happen to catch this talk on my lunch break today. And it's simply brilliant. For some it may seem obvious, but for me... the light bulb definitely went off. Lately, I've been going through the design pattern books again... even took out Refactoring by Martin Fowler for review. Even though they make sense I've always had trouble implementing it in a brown field project. I'm currently faced with a few projects at work right now. One is a major project that I'm maintaining, and the other is actually a green field project that I'm also working on. It's exciting because it's a WPF application that hosts a 3D Model viewer made by the folks over at Autodesk.
It's COM but it's still cool. My job is to parse the 3D model for data and surface it in WPF.
Their are a lot of moving parts... such as... XLinq,Xceed Datagrid, LINQ2SQL, SQL, custom DWF parser (to parse DWF files), 3rd party COM API (Autodesk), Log4net, IOC, MVP, etc.
So it certainly can get quite confusing quite quickly. Having an over arching strategy to help manage complexity is key. After watching the video, it became clear and now I'm continuing the green field project in this specific manner. Make Roles Explicit!!!!
Thanks Udi,
-Develop with passion
Subscribe to:
Posts (Atom)

