<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[F#ing About - Medium]]></title>
        <description><![CDATA[On the subject of functional programming in .NET - Medium]]></description>
        <link>https://medium.com/f-ing-about?source=rss----9c134363b49a---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>F#ing About - Medium</title>
            <link>https://medium.com/f-ing-about?source=rss----9c134363b49a---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 10:10:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/f-ing-about" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[F# Tree Diff Algorithm]]></title>
            <link>https://medium.com/f-ing-about/f-tree-diff-algorithm-5b7f9c85beac?source=rss----9c134363b49a---4</link>
            <guid isPermaLink="false">https://medium.com/p/5b7f9c85beac</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[fsharp]]></category>
            <category><![CDATA[trees]]></category>
            <dc:creator><![CDATA[Luke Burgess-Yeo]]></dc:creator>
            <pubDate>Wed, 01 Jan 2020 16:01:01 GMT</pubDate>
            <atom:updated>2020-01-01T16:01:01.255Z</atom:updated>
            <content:encoded><![CDATA[<h4>Implementing an algorithm to find the difference between two trees</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yNeGaUP37PXRlYsphtCxjg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@niko_photos?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">niko photos</a> on <a href="https://unsplash.com/s/photos/tree?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Suppose you have two tree data structures and you want to find the difference between them. What follows is a description and implementation of a simple algorithm with takes two trees — one original, one modified — as inputs and outputs a single tree with nodes labelled to indicate whether they have been inserted, deleted or had their values updated when compared with the original tree.</p><p>This algorithm is inspired by the diff algorithms behind Virtual DOMs in JavaScript frameworks like React. This implementation in particular is based on a post by <a href="https://medium.com/@deathmood/how-to-write-your-own-virtual-dom-ee74acc13060">deathmood</a>.</p><p>An F# script file with the full implementation code and simple test case can be found here: <a href="https://github.com/LukeBurgessYeo/tree-diff">https://github.com/LukeBurgessYeo/tree-diff</a>.</p><h3>Introduction To Trees</h3><p>Let’s begin with a quick introduction to tree data structures. The idea is to define a hierarchical structure containing nodes where each node has a value and a (potentially empty) collection of child nodes. We will be concerning ourselves with labeled, unordered trees.</p><h4>Defining a tree</h4><p>For the purposes of our algorithm we will give each node an Id — so we can match nodes between different trees — as well as a label specifying whether the node has been changed (modeled here as a discriminated union):</p><pre>type Change =<br>    | Unchanged<br>    | Inserted<br>    | Deleted    <br>    | Updated of string</pre><pre>type Node = {    <br>    Id : int    <br>    Value : string  <br>    Modified : Change   <br>    Children : Node list<br>}</pre><p>(For simplicity I am setting the type of Value to string, but there is no reason why it could not be any other type, or in fact a generic type — as in the repo linked above.)</p><p>Instances of trees can then be constructed easily. For example, this tree with a “Root” node that has two children — “A” and “B”:</p><pre>let tree0 = {    <br>  Id = 0    <br>  Value = &quot;Root&quot;    <br>  Modified = Unchanged    <br>  Children = [        <br>    { Id = 1; Value = &quot;A&quot;; Modified = Unchanged; Children = [] }<br>    { Id = 2; Value = &quot;B&quot;; Modified = Unchanged; Children = [] }<br>  ] <br>}</pre><h4>Working with trees</h4><p>Using this data structure is slightly more difficult than built in types such as Lists. In order to read data from our tree we will have to traverse the nodes using a tree traversal algorithm. The simplest to demonstrate is <a href="https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/">Depth First Search</a>.</p><p>Depth First Search attempts to traverse the tree by visit leaf nodes (nodes with no children) first — i.e. going as deep into the tree as possible first — before going back up the tree and down the next branch. Here is an implementation in F# which we can use to print out our trees in a more human readable form:</p><pre>let printTree tree =<br>  let rec traverse node level =<br>    let spacing = <br>      List.init (level * 4) (fun _ -&gt; &quot; &quot;) <br>      |&gt; String.concat &quot;&quot;        <br>    let nodeString = <br>      sprintf &quot;(%i) %s - %A&quot; node.Id node.Value node.Modified<br>    printfn &quot;%s%s&quot; spacing nodeString<br>    node.Children<br>    |&gt; List.iter (fun child -&gt; traverse child (level + 1))<br>  traverse tree 0</pre><p>The above function uses a recursive inner function to traverse each node of the tree — printing out the node in a readable format — before visiting the children of that node. The traverse function also takes a state parameter “level” which keeps track of the depth of the tree so the appropriate indentation can be added to each line, making the output easier to read.</p><p>Here as the output from fsi when printTree is called on tree0:</p><pre>&gt; printTree tree0;;<br>(0) Root - Unchanged<br>    (1) A - Unchanged<br>    (2) B - Unchanged<br>val it : unit = ()</pre><h3>The Tree Diff Algorithm</h3><h4>Overview of the algorithm</h4><p>The algorithm itself will have to deal with a number of different cases when comparing nodes in two trees:</p><ol><li>A new node has been inserted.</li><li>An old node has been deleted.</li><li>The node exists in both trees.</li></ol><p>If the node exists in both trees then we need to check to see if the value of the node needs to be modified. We also need to compare the children of the old node and the new node.</p><h4>Implementation — Part 1: Helper functions</h4><p>Firstly, let’s define some helper functions to mark a node as inserted, deleted, or updated. For inserted and deleted nodes, we also need to mark the rest of the sub-tree based at these nodes as inserted or deleted. Hence it is useful to define the Depth First Search implementation:</p><pre>let rec markNode change node =<br>    { node with<br>        Modified = change<br>        Children = <br>            node.Children <br>            |&gt; List.map (markNode change) }</pre><p>This allows us to define the insert and delete function succinctly:</p><pre>let insert node = markNode Inserted node</pre><pre>let delete node = markNode Deleted node</pre><p>(The use of the explicit “node” parameter is required if using this code with fsi and a generic type tree.)</p><p>As the value of child nodes are not necessarily updated when a node has changed value, the update function can be written simply as a record update:</p><pre>let update node value =<br>    { node with<br>        Modified = Updated value }</pre><p>Where “value” indicates the new value, while the node itself will retain it’s original “Value”, so we can track the exact change.</p><h4>Implementation — Part 2: Main diff function</h4><p>Now we are ready to compare two nodes. As neither node is guaranteed to exist, we shall use the Option type to write a function which takes two nodes which may, or may not, exist. The type of this function will therefore be:</p><pre>Node option * Node option -&gt; Node</pre><p>Using a tuple rather than currying will turn out to be useful later, but also this is not a function where partial application makes sense. Note that the function will also need to be recursive so we can call it on the children of the input nodes.</p><p>We will match on the cases of our two option inputs. The first three cases are straightforward:</p><ol><li>Delete the old node if there is no new node.</li><li>Insert the new node if there is no old node.</li><li>Do nothing if the two nodes are identical.</li></ol><pre>let rec diff (oldNodeOpt, newNodeOpt) =<br>  match oldNodeOpt, newNodeOpt with<br>    | Some oldNode, None -&gt; delete oldNode<br>    | None, Some newNode -&gt; insert newNode<br>    | Some oldNode, Some newNode when oldNode = newNode -&gt; oldNode</pre><p>Next we should do a simple update the value of a node has changed, but everything else is identical. To do this we can define an infix operator to do this comparison:</p><pre>let (==) node1 node2 =<br>  node1.Id = node2.Id<br>  &amp;&amp; node1.Children = node2.Children<br>  &amp;&amp; node1.Value &lt;&gt; node2.Value</pre><p>and add the following line to our diff function: (note the use of “==”)</p><pre>| Some oldNode, Some newNode when oldNode == newNode -&gt; <br>  update oldNode newNode.Value</pre><p>The final (non-degenerative) case is when we have two nodes with different children. To handle this case, we need to pass the children of the two nodes into our diff function to be compared. This means we have to construct a list of Node option tuples and iterate over the list, passing each pair back into our diff function.</p><p>The first item in each pair will contain the child of the old node (if it exists), and the second item will contain the child of the new node (if it exists). The determine whether a given node should exist, we must iterate first over the children of the old node, checking to see if that node exists in the list of children of the new node. Then do similarly for the new node, checking the list of children of the old node. The following function does exactly that:</p><pre>let combine node1 node2 =<br>  let first = <br>    node1.Children <br>    |&gt; List.map (fun x -&gt;<br>      let maybeNode =<br>        node2.Children <br>        |&gt; List.tryFind (fun y -&gt; y.Id = x.Id)<br>      (Some x, maybeNode))</pre><pre>  let second = <br>    let maybeNode =<br>      node2.Children <br>      |&gt; List.map (fun x -&gt; <br>        (node1.Children <br>        |&gt; List.tryFind (fun y -&gt; y.Id = x.Id)<br>    (maybeNode, Some x))</pre><pre>  first @ second |&gt; List.distinct</pre><p>Note the use of List.distinct at the end to remove any double counted nodes.</p><p>We are now in a position to complete our diff function by using List.map to pass each child node combination back into our diff function to get the list of compared child nodes. This is the reason why we chose to pass a tuple to diff. Here is the completed function, with the newly added case in <strong>bold</strong>:</p><pre>let rec diff (oldNodeOpt, newNodeOpt) =<br>  match oldNodeOpt, newNodeOpt with<br>  | Some oldNode, None -&gt; delete oldNode<br>  | None, Some newNode -&gt; insert newNode<br>  | Some oldNode, Some newNode when oldNode = newNode -&gt; oldNode<br>  | Some oldNode, Some newNode when oldNode == newNode -&gt; <br>    update oldNode newNode.Value<br>  <strong>| Some oldNode, Some newNode -&gt;<br>    { oldNode with<br>        Modified = <br>          if oldNode.Value = newNode.Value then <br>            Unchanged <br>          else Updated newNode.Value<br>        Children = <br>          combine oldNode newNode<br>          |&gt; List.map diff }</strong><br>  | None, None -&gt; <br>    failwith &quot;Must have at least one node to compare.&quot;</pre><p>The final match case is “None, None” which does not make sense as an input to the function and so we raise an exception. This function should never receive two None’s as inputs. Even if it did, we would not have a node to return and creating an empty node to continue the execution of the program could cause problems later on.</p><p>Finally we can wrap our diff function in a more intuitive API for external use:</p><pre>let compare oldTree newTree = diff (Some oldTree, Some newTree)</pre><h3>Final Comments</h3><p>This algorithm can likely be further optimized and improved upon — for example respecting the order of nodes as well as their Id’s.</p><p>The careful reader may have noticed that the match case which deals with children of the given nodes also deals with the previous two cases (when the nodes are equal, and when they are equal in all but value). The inclusion of these two cases improves the performance of the algorithm for large trees, and trees with very few changes. We can quickly eliminate identical sub-trees by comparing nodes by value (as F# does by default), and the case where only the value has changed gives a practical example of defining a custom infix operator.</p><p>Of course, if the user is comparing extremely large trees then other optimizations may be necessary, and F# may not even be the most suitable tool for the task. Other algorithms exist which try to find the “minimum update script” — a list of edits to transform one tree into another as efficiently as possible (where you can decide how to measure efficiency).</p><p>Finally, please feel free to contribute any bug fixes or improvements to the code at <a href="https://github.com/LukeBurgessYeo/tree-diff">https://github.com/LukeBurgessYeo/tree-diff</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b7f9c85beac" width="1" height="1" alt=""><hr><p><a href="https://medium.com/f-ing-about/f-tree-diff-algorithm-5b7f9c85beac">F# Tree Diff Algorithm</a> was originally published in <a href="https://medium.com/f-ing-about">F#ing About</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Functional Reactive Programming]]></title>
            <link>https://medium.com/f-ing-about/functional-reactive-programming-bac706d922e2?source=rss----9c134363b49a---4</link>
            <guid isPermaLink="false">https://medium.com/p/bac706d922e2</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[fsharp]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Luke Burgess-Yeo]]></dc:creator>
            <pubDate>Tue, 24 Dec 2019 12:10:30 GMT</pubDate>
            <atom:updated>2019-12-24T12:10:30.259Z</atom:updated>
            <content:encoded><![CDATA[<h4>Using Observable’s and Events</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Dj_PV5H9kDimPbtIpyQmfQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@oskaryil?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Oskar Yildiz</a> on <a href="https://unsplash.com/s/photos/programming?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Functional Reactive Programming is a powerful technique which allows for easy handling of events and complex control-flow situations. However, this style of programming can be initially confusing without a solid understanding of the different types and functions which are involved.</p><p>The basic idea is to raise events in one part of the system, and write listeners in another part of the system to handle the events. This decoupling of producers and consumers allows for a cleaner — more modular — architecture, as well as making applications which rely on network communication or parallel execution much easier to build.</p><p>For more examples and explanation of this topic. See Scott Wlaschin’s blog: <a href="https://fsharpforfunandprofit.com/posts/concurrency-reactive/">https://fsharpforfunandprofit.com/posts/concurrency-reactive/</a>.</p><h3>What Are Events?</h3><p>Events are instances of the <a href="https://msdn.microsoft.com/visualfsharpdocs/conceptual/control.event%5b%27t%5d-class-%5bfsharp%5d">built in F# Event type</a>. Events are raised automatically by certain user inputs (in WinForms apps for example) or custom events can be created, published and triggered. Below is an example of creating, publishing and triggering a custom event:</p><pre>// create the event<br>let customEvent = Event&lt;string&gt;()</pre><pre>// publish the event so it can be listened to<br>let eventStream = customEvent.Publish</pre><pre>// trigger the event<br>customEvent.Trigger(&quot;Hello world!&quot;)</pre><p>Events can take any type you wish. Events containing record types representing the state, or union types representing one of several options a user may have chosen are particularly useful.</p><h3>Listening To Events</h3><p>Now that we have a published event, we need to listen out for the event being triggered. There are several ways to do this, however the below method (using the <a href="https://msdn.microsoft.com/visualfsharpdocs/conceptual/control.observable-module-%5bfsharp%5d">Observable module</a> methods) is preferred:</p><pre>// string -&gt; unit<br>let handler message = printfn &quot;%s&quot; message</pre><pre>eventStream<br>|&gt; Observable.subscribe handler</pre><p>Observable.subscribe takes a handler function (which in this case must be have type “string -&gt; unit” because our eventStream was created from a Event&lt;string&gt; event) and an IEvent (which we can by publishing our custom event) and calls the handler function every time the corresponding event is triggered.</p><p>Now every time we trigger our customEvent, the message which is passed as a parameter to Trigger will be logged to the console. Here it is in fsi:</p><pre>&gt; customEvent.Trigger(&quot;Hello world!&quot;);;<br>Hello world!</pre><h3>Other Functions In The Observable Module</h3><p>With the basics covered, what else can we do with events? Well much like the List module (and others), the Observable module comes with several built in functions which allow us to manipulate events in order to make complex control flow tasks with events easy. For example, we might want to alter the data in our events when we receive them. This can be done using the <a href="https://msdn.microsoft.com/visualfsharpdocs/conceptual/observable.map%5b%27t%2c%27u%5d-function-%5bfsharp%5d">map function</a>:</p><pre>eventStream<br>|&gt; Observable.map (fun x -&gt; sprintf &quot;Message: %s&quot; x)<br>|&gt; Observable.subscribe(fun x -&gt; printfn &quot;%s&quot; x)</pre><p>Or we might want to <a href="https://msdn.microsoft.com/visualfsharpdocs/conceptual/observable.filter%5b%27t%5d-function-%5bfsharp%5d">filter</a> out messages over a certain length:</p><pre>eventStream<br>|&gt; Observable.filter (fun x -&gt; x.Length &lt; 10)<br>|&gt; Observable.subscribe(fun x -&gt; printfn &quot;%s&quot; x)</pre><p>Or we might want to accumulate some state for each event. This can be done using the <a href="https://msdn.microsoft.com/visualfsharpdocs/conceptual/observable.scan%5b%27u%2c%27t%5d-function-%5bfsharp%5d">scan function</a>:</p><pre>eventStream<br>|&gt; Observable.scan (fun acc cur -&gt; sprintf &quot;%s %s&quot; acc cur) &quot;&quot;<br>|&gt; Observable.subscribe(fun x -&gt; printfn &quot;%s&quot; x)</pre><p>This will add each new message to the state for every event that is triggered. Here is the output from fsi (with the printed messages in <strong>bold</strong>):</p><pre>&gt; customEvent.Trigger(&quot;hello&quot;);;<br> <strong>hello</strong><br>val it : unit = ()</pre><pre>&gt; customEvent.Trigger(&quot;world&quot;);;<br> <strong>hello world</strong><br>val it : unit = ()</pre><pre>&gt; customEvent.Trigger(&quot;this&quot;);;<br> <strong>hello world this</strong><br>val it : unit = ()</pre><pre>&gt; customEvent.Trigger(&quot;is&quot;);;<br> <strong>hello world this is</strong><br>val it : unit = ()</pre><pre>&gt; customEvent.Trigger(&quot;F#&quot;);;<br> <strong>hello world this is F#</strong><br>val it : unit = ()</pre><p>This can be incredibly useful for keeping track of your applications state whilst listening to events.</p><p>There are several other functions in the Observable module, as well as others in the <a href="https://docs.microsoft.com/en-us/previous-versions/dotnet/reactive-extensions/hh242985(v=vs.103)">Reactive Extensions</a> module to give you complete control over your event handling.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bac706d922e2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/f-ing-about/functional-reactive-programming-bac706d922e2">Functional Reactive Programming</a> was originally published in <a href="https://medium.com/f-ing-about">F#ing About</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using F# Scripts To Explore Your .NET Codebase]]></title>
            <link>https://medium.com/f-ing-about/using-f-scripts-to-explore-your-net-codebase-9f8cd982727c?source=rss----9c134363b49a---4</link>
            <guid isPermaLink="false">https://medium.com/p/9f8cd982727c</guid>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[fsharp]]></category>
            <dc:creator><![CDATA[Luke Burgess-Yeo]]></dc:creator>
            <pubDate>Sat, 15 Jun 2019 16:10:03 GMT</pubDate>
            <atom:updated>2019-06-15T16:10:03.507Z</atom:updated>
            <content:encoded><![CDATA[<h3>Using F# Scripts To Explore Your .NET Codebase</h3><h4>Some powerful techniques to quickly prototype new features</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xAFAiAxqZVrOVLBTo9tf6w.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@emilep?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Émile Perron</a> on <a href="https://unsplash.com/search/photos/programming?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Having to make a change inside a large codebase which you are unfamilar with can be a daunting task. How do you know that you aren’t breaking consciously designed logic? How do you know that the changes you are making won’t have any side effects or break something somewhere else in the code? Hopefully the codebase is well designed and well structured with high unit test coverage and documentation to mitigate against these issues, but that just isn’t how things usually are in the real world. So what can we do to make our lives easier in these situations? Specifically, how can F# help us explore and get a better understanding of our .NET codebase?</p><h3>F# Scripts and F# Interactive</h3><p>Firstly, a brief introduction to F# scripts and F# interactive (fsi). FSI is a super powerful tool which can (and should) be used to interactively test code as it’s being written. But it can do a lot more than simply verify that the chain of list module functions that you’ve just put together actual does what you want it to do — as useful as that is.</p><p>Writing code inside a .fsx file and sending it to fsi is just the first step on a journey to more productive programming experience. For more on this see Mathias Brandewinder’s (slightly dated but still extremely useful) blog post on <a href="https://brandewinder.com/2016/02/06/10-fsharp-scripting-tips/">10 Tips for Productive F# Scripting</a>. I will be expanding on some of the points in this post…</p><h3>Importing The Things You Need</h3><p>The first step to leveling up your scripting experience is learning how to get the various libraries and other binaries you need into your script. You can also load other scripts, or other F# files, into your script, allowing you to abstract away the common elements of your scripts — so you can follow all the best software development principles (single responsibility, don’t repeat yourself, etc.) with your scripts too!</p><p>Loading in F# files or scripts is done via the <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/tutorials/fsharp-interactive/"><em>#load</em> directive</a>, followed by the relative path to your script:</p><pre>#load &quot;MyScript.fsx&quot;</pre><pre>open MyScript</pre><p>Loading external libraries or built code can be done using <em>#r</em> (and here is a more explicit use of relative paths, using @ to avoid needing to escape characters in the path). Then you can open any modules inside the built dll:</p><pre>#r @&quot;../src/myproject/bin/debug/myproject.dll&quot;</pre><pre>open MyModule</pre><p>However, you may well want to load several dll’s from the same location. In this case, it may be better to make the entire directory available to be loaded from using <em>#I</em>:</p><pre>#I @&quot;../src/myproject/bin/debug&quot;<br>#r &quot;myproject.dll&quot;<br>#r &quot;somelibrary.dll&quot;</pre><pre>open MyModule<br>open LibraryModule</pre><p>An even faster way of doing this (if your scripts are in the same location as your built dll’s) is to use the <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/source-line-file-path-identifiers">__SOURCE_DIRECTORY__</a> built-in value:</p><pre>#I __SOURCE_DIRECTORY__<br>#r &quot;myproject.dll&quot;<br>#r &quot;somelibrary.dll&quot;</pre><pre>open MyModule<br>open LibraryModule</pre><p>This allows you to access any code from your codebase directly in your scripts, ready to be sent to fsi. It is worth remembering, however, that after loading these dll’s into fsi, fsi will “lock” the files themselves. This means that any attempt to rebuild them (i.e. copy or overwrite them) will fail. So make sure you’ve quit your fsi session before trying to rebuild the projects you’ve referenced.</p><h3>Exploring Your Codebase</h3><p>Now that you can load in literally any code you need from your codebase, you can use F# scripts to interatively explore the code. Here are some possible use cases for these kinds of scripts.</p><h4>Exploring Data</h4><p>Let’s suppose you are working on an application which loads a large amount of data into memory before the application, or at least part of it, becomes usable.</p><p>Restarting the application and waiting for the data to load every time you make a code change could become tedious. However, with fsi, this may not be necessary. Simply import the code which does the data loading into a script and and run it once to load the data into the memory of the fsi process. Now we can interactively run any code we like against this data without having to reload it.</p><p>This could be extremely useful for developing a new feature which requires an intamte knowledge of that data — or at least how it’s structured — or just to save time while you use trial-and-error to figure out the details of the logic you are implementing.</p><h4>Targetted Debugging</h4><p>It is possible to attach the Visual Studio (or Visual Studio Code with some extra work) debugger to the fsi.exe process, and then set breakpoints and debug your code as you would if you were running the application.</p><p>There are situations where this is much more effecient or desirable than traditional debugging methods. For example, if you wanted to test what happens when a function recives an input that is unusual or difficult to replicate through the UI (to test that a new feature where that situation may occur won’t break the code). Or if you needed to answer a question like “what happens when…”, simply write a script that creates that situation and attach the debugger to see what happens.</p><h4>Sanity Checks and Ad-Hoc Integration Testing</h4><p>Suppose your application has a long list of items which the user can click on to perform some task or calculation. How do you know that a data change won’t break that task for every possible item that the user might click?</p><p>Write a script which calls the code that would run if the user did click every item and find out for yourself! This trick could be extended to run ad-hoc integration tests before the code gets to an integration testing environment.</p><p>In general F# scripts are extremely powerful and a practicle way to increase your productivity, even if your main codebase is written in another .NET language.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f8cd982727c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/f-ing-about/using-f-scripts-to-explore-your-net-codebase-9f8cd982727c">Using F# Scripts To Explore Your .NET Codebase</a> was originally published in <a href="https://medium.com/f-ing-about">F#ing About</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning F#]]></title>
            <link>https://medium.com/f-ing-about/learning-f-3135d495d883?source=rss----9c134363b49a---4</link>
            <guid isPermaLink="false">https://medium.com/p/3135d495d883</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[fsharp]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Luke Burgess-Yeo]]></dc:creator>
            <pubDate>Mon, 10 Jun 2019 21:20:55 GMT</pubDate>
            <atom:updated>2019-06-15T16:13:08.779Z</atom:updated>
            <content:encoded><![CDATA[<h4>Changing the way you think about code</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4hMD3YXsF8jGgxnhsn28Tg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/Agx5_TLsIf4?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Fatos Bytyqi</a> on <a href="https://unsplash.com/search/photos/programming?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>So you’ve decided you want to learn F#, what are the next steps to become proficient? This post will hopefully point you in the right direction for the sorts of things that you should — and more importantly <em>should not — </em>be doing whilst learning F#.</p><h3>First Steps</h3><p>Forget about imperative programming. By that I mean forget about variables, loops, if-else statements, and pretty much all of the little things that you might rely on when writing imperative code.</p><p>Embrace immutability. Embrace pattern matching. Embrance partial application and currying, and embrace the F# type system.</p><p>Write lots of small functions and glue them together, making use of composition and partial application.</p><p>Speaking of the type system. This is probably the most important aspect of F# to spend your time learning properly. Forget about classes, interfaces and other object oriented notions and instead use native F# types.</p><h3>The Type System</h3><p>F# allows you to consisely define your domain model in such a way that invalid application state is impossible. Doing this in traditional imperative code is extremely difficult and requires many lines of error handling code.</p><p>F# gives us discriminated unions, records and tuples which can be used to define state which cannot be invalid, much more consisely and acurately than in imperative code.</p><p>Pattern matching then allows you to confidently implement the logic that works with this state with the compiler automatically checking that you’ve covered every possible branch.</p><h3>Enumeral Types and Modules</h3><p>It is also well worth spending some time understanding the differences between lists (singlely linked and immutable), arrays (the traditional mutable array we all know and love) and seqs (lazily loaded lists) and the various core module functions for working with those data types.</p><p>As I said above, you should forget about loops and embrace the enumerable module functions. Get used to map, filter, reduce/fold and all the other functions in these modules. These functions allow you to abstract away the looping itself and focus purely on the logic that matters in manipulating these data structures.</p><h3>Conclusion</h3><p>Try and avoid using mutable variables, loops, if-else statements and any object oriented features (classes, etc.). Instead use the native F# types, use the list module functions, use pattern matching. Write lots of little functions and compose them together to build larger modules.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3135d495d883" width="1" height="1" alt=""><hr><p><a href="https://medium.com/f-ing-about/learning-f-3135d495d883">Learning F#</a> was originally published in <a href="https://medium.com/f-ing-about">F#ing About</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[First Look At F#]]></title>
            <link>https://medium.com/f-ing-about/first-look-at-f-542c6d500217?source=rss----9c134363b49a---4</link>
            <guid isPermaLink="false">https://medium.com/p/542c6d500217</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[fsharp]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Luke Burgess-Yeo]]></dc:creator>
            <pubDate>Thu, 04 Apr 2019 17:20:28 GMT</pubDate>
            <atom:updated>2019-06-15T16:13:54.547Z</atom:updated>
            <content:encoded><![CDATA[<h4>What makes F# unique and worth using?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d0I7athFoo0jgOpGMLPCKg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/Hl1stIQkVRw?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">John Salzarulo</a> on <a href="https://unsplash.com/search/photos/coding?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>I recently started a new job in which I had to learn F#. I’ve been a fan of the language for a long time but this was the first time I had the chance to learn the language properly and in detail. In this post I will list a number of features that make F# unique — compared to C# at least —and worth learning. F# is not just lightweight syntax for things you can already do, and here’s why…</p><h3>The Five C’s of F#</h3><p>Taken from Scott Wlaschin’s <a href="https://fsharpforfunandprofit.com/why-use-fsharp/">excellent blog</a>, F# is good for its Conciseness, Convenience, Correctness, Concurrency and Completeness. Read the blog post for more details but these are the headline ideas.</p><p>In practice these things make F# a joy to write. The syntax is clean, F# interactive allows you to rapidly test your code as you’re writing it, and the type system ensures that you write far fewer bugs than in conventional imperative languages.</p><p>That being said, the minimalist syntax and strict type system do take some getting used to — however the payoff is worth it. F# is not a language that has been designed for ease of use in the first few hours of programming, but rather for the seasoned enterprise developer to work with over many years.</p><h3>A Shift In Mindset</h3><p>As well as getting used to the syntax and type system, programming in F# requires a shift in mindset from the imperative programming approach. F# encourages you to think “functionally” rather than in terms of classes and Object Oriented principles. Again, this may seem daunting when we are so used to OOP, but I would argue that thinking in terms of functions rather than classes is more natural. Classes, objects, interfaces, inheritance, polymorphism, etc. are all concepts which we have to learn — and eventually become natural — but once upon a time they were strange ideas. Functions on the other hand are taught to primary school children.</p><h3>Yet More Blog Posts</h3><p>A common criticism of F# is that C# is good enough for basically everything the enterprise programmer could ever want to do. This is true, but that doesn’t mean that C# is the best tool for the job. For some more reasons as to why, read <a href="https://fsharpforfunandprofit.com/posts/is-your-language-unreasonable/">this post</a> on how reasoning about F# code is far easier than OO code. Furthermore, <a href="https://fsharpforfunandprofit.com/posts/fsharp-is-the-best-enterprise-language/">here is a detailed argument</a> for why F# is the best enterprise programming language on the market right now.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=542c6d500217" width="1" height="1" alt=""><hr><p><a href="https://medium.com/f-ing-about/first-look-at-f-542c6d500217">First Look At F#</a> was originally published in <a href="https://medium.com/f-ing-about">F#ing About</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>