<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Michael De Wildt &#187; F#</title>
	<atom:link href="http://www.mikeyd.com.au/category/f/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikeyd.com.au</link>
	<description>An uber nerd rambling about open source, PHP, Python and whatever I find interesting</description>
	<lastBuildDate>Wed, 04 Jan 2012 22:36:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>F# string to object model</title>
		<link>http://www.mikeyd.com.au/2009/11/06/f-string-to-object-model/</link>
		<comments>http://www.mikeyd.com.au/2009/11/06/f-string-to-object-model/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 04:14:11 +0000</pubDate>
		<dc:creator>Mikey</dc:creator>
				<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">http://www.mikeyd.com.au/?p=76</guid>
		<description><![CDATA[A while back I wrote about a nice little piece of code that I wrote which converted an object into a string, and vice versa, in order to be sent and retrieved from a transactional main frame. Check it out here &#8211; C# string to object model using method attributes, reflection, anonymous types and LINQ. [...]]]></description>
			<content:encoded><![CDATA[<p>A while back I wrote about a nice little piece of code that I wrote which converted an object into a string, and vice versa, in order to be sent and retrieved from a transactional main frame. Check it out here &#8211; <a href="http://www.mikeyd.com.au/?p=15">C# string to object model using method attributes, reflection, anonymous types and LINQ.</a></p>
<p>A while back I also dabbled in a bit of F# &#8211; The new functional programming language from Microsoft. F# is a fantastic language! It took me a while to get used to not having curly braces and the layout of F#, but once I got around that I was away.</p>
<p>My first venture into F# was to convert my string to object model code, mentioned above, into F#!</p>
<p>Here is my F# version of my C# code:</p>
<pre class="brush:csharp">//The Validation Attribute
type public InputRegexAttribute public (format : string) as this =
    inherit Attribute()
    member self.Format with get() = format

//The class definition
type public Foo public (firstName, familyName) as this =
    [<InputRegex("^[a-zA-Z\s]+$")>]
    member self.FirstName with get() = firstName 

    [<InputRegex("^[a-zA-Z\s]+$")>]
    member self.FamilyName with get() = familyName 

module ObjectExtensions =
    type System.Object with
        member this.BuildString template =
            let mutable updatedTemplate : string  = template
            for prop in this.GetType().GetProperties() do
                for attribute in prop.GetCustomAttributes(typeof<InputRegexAttribute>,true)
                                           .Cast<InputRegexAttribute>() do
                    let regex = new Regex(attribute.Format)
                    let value = prop.GetValue(this, null).ToString()
                    if regex.IsMatch(value) then
                        updatedTemplate <- updatedTemplate.Replace("{" + prop.Name + "}", value)
                    else
                        raise (new Exception "Regex Failed")
            updatedTemplate

open ObjectExtensions
try
    let foo = new Foo("Jane", "Doe")
    let out = foo.BuildInputString("Hello {FirstName} {FamilyName}! How Are you?")
    printf "%s" out
with | e -> printf "%s" e.Message</pre>
<p>The methods are not purely functional as I have used a mutable string, but as you can see there is a lot less code to do, effectively, the same thing!</p>
<p>Maybe if I get some time I will investigate a way of making the code purely functional without any mutable objects.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikeyd.com.au/2009/11/06/f-string-to-object-model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

