<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Igal Tabachnik</title>
    <description>The latest articles on Forem by Igal Tabachnik (@hmemcpy).</description>
    <link>https://forem.com/hmemcpy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F30476%2F4e789d7d-ba4e-442a-b345-f4951fdb4bc1.jpg</url>
      <title>Forem: Igal Tabachnik</title>
      <link>https://forem.com/hmemcpy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hmemcpy"/>
    <language>en</language>
    <item>
      <title>Your C# is already functional, but only if you let it</title>
      <dc:creator>Igal Tabachnik</dc:creator>
      <pubDate>Mon, 09 Mar 2020 06:19:08 +0000</pubDate>
      <link>https://forem.com/hmemcpy/your-c-is-already-functional-but-only-if-you-let-it-3afn</link>
      <guid>https://forem.com/hmemcpy/your-c-is-already-functional-but-only-if-you-let-it-3afn</guid>
      <description>&lt;p&gt;A few days ago I &lt;a href="https://twitter.com/hmemcpy/status/1235287026364276737"&gt;tweeted&lt;/a&gt; a C# code snippet, showing a &lt;a href="https://en.wikipedia.org/wiki/Fizz_buzz"&gt;FizzBuzz&lt;/a&gt; implementation using some of the &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8"&gt;new features in C# 8.0&lt;/a&gt;. The tweet "went viral", as the kids say, with several people admiring the terse and &lt;em&gt;functional&lt;/em&gt; aspect of it, while others asked me why I wasn't writing it in F# in the first place?&lt;/p&gt;


&lt;blockquote class="twitter-tweet"&gt;
&lt;p&gt;First time writing C# in 4 years :P &lt;a href="https://t.co/m7kM9j9skN"&gt;pic.twitter.com/m7kM9j9skN&lt;/a&gt;&lt;/p&gt;— Igal Tabachnik (&lt;a class="comment-mentioned-user" href="https://dev.to/hmemcpy"&gt;@hmemcpy&lt;/a&gt;
) &lt;a href="https://twitter.com/hmemcpy/status/1235287026364276737?ref_src=twsrc%5Etfw"&gt;March 4, 2020&lt;/a&gt;
&lt;/blockquote&gt; 

&lt;p&gt;It has been over 4 years since I last wrote C#, and being exposed to functional programming clearly affected how I write code today. The snippet I wrote seems very neat and natural, however, some people expressed concerns that it doesn't &lt;em&gt;feel&lt;/em&gt; like C# code --- "&lt;em&gt;It looks too functional.&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You keep using that word...&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Depending on who you ask, the word &lt;em&gt;functional&lt;/em&gt; means different things to different people. But rather than debating semantics, I'd like to offer an explanation of &lt;em&gt;why&lt;/em&gt; this tiny FizzBuzz snippet &lt;em&gt;feels&lt;/em&gt; functional.&lt;/p&gt;

&lt;p&gt;But first, let's unpack the snippet (with few tweaks to the above image):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;FizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;                &lt;span class="c1"&gt;// Local function, defined as an expression-bodied method&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;    &lt;span class="c1"&gt;// Tuple definition  &lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"FizzBuzz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;// Pattern-matching on the tuple values&lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Fizz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;// Discard (_) is used to omit&lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Buzz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;// the values we don't care about&lt;/span&gt;
            &lt;span class="n"&gt;_&lt;/span&gt;            &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;     
        &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="n"&gt;Enumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                 &lt;span class="c1"&gt;// Make a range of numbers from 1 to 100&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FizzBuzz&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;ToList&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;           &lt;span class="c1"&gt;// Map each number to a corresponding FizzBuzz value&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;         &lt;span class="c1"&gt;// Print the result to the console  &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The novelty here lies in using a tuple (pair) to capture the result of both equations together (&lt;code&gt;x % 3 == 0&lt;/code&gt; and &lt;code&gt;x % 5 == 0&lt;/code&gt;). This allows using pattern matching to deconstruct the tuple and examine both values together. If none of the cases match, a default (&lt;code&gt;_&lt;/code&gt;) case will always match, returning the string value of the number.&lt;/p&gt;

&lt;p&gt;However, none of the many "functional" features used in the snippet (including LINQ-style &lt;code&gt;foreach&lt;/code&gt; loops) is what makes this approach in itself &lt;em&gt;functional&lt;/em&gt;. What makes it functional is the fact that except for the final printing to the console, all of the methods used in this program are &lt;strong&gt;&lt;em&gt;expressions&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Expression-oriented programming&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Simply put, an &lt;em&gt;expression&lt;/em&gt; is a question that always has an answer. In programming terms, an expression is a combination of constants, variables, operators, and functions, evaluated by the runtime to compute ("return") a value. To illustrate the difference with &lt;em&gt;statements&lt;/em&gt;, let's write a more common C# solution to FizzBuzz:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Enumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;FizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;FizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"FizzBuzz"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Fizz"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Buzz"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Obviously, this snippet could be tweaked to remove duplication, but I doubt anyone would argue this does not look like C#. And yet, upon closer inspection of the &lt;code&gt;FizzBuzz&lt;/code&gt; method, it reveals several design issues (even in a program as simple as FizzBuzz).&lt;/p&gt;

&lt;p&gt;First of all, this program violates the Single Responsibility principle. It mixes both "business logic" of calculating the output value based on the number with the act of printing this value to the console. As a consequence, it violates the Dependency Inversion principle by tightly-coupling to the console output. Finally, we cannot reuse and test this program in isolation without introducing a few indirections. For such a trivial program like FizzBuzz we would never do this, but taken to the extreme, it's easy to end up with &lt;a href="https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition"&gt;Enterprise FizzBuzz&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;All of the problems stated above can be solved by separating the act of producing a FizzBuzz value and printing it to the console. Even without using fancy language features, the simplest act of &lt;em&gt;returning a value to the caller&lt;/em&gt; frees us from the responsibility of doing something with that value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;FizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"FizzBuzz"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Fizz"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Buzz"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This might not look like a dramatic change (and it could be implemented in various ways, here using the most naive way for illustration), but few things happened here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;FizzBuzz&lt;/code&gt; method is now an &lt;em&gt;expression&lt;/em&gt;, given some numeric input produces a string output&lt;/li&gt;
&lt;li&gt;It has no other responsibilities or side-effects, making it a &lt;em&gt;pure function&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;It can be tested and reused on its own, without any additional dependencies or setup&lt;/li&gt;
&lt;li&gt;The caller of this function is free to do whatever it wants with the result -- not our responsibility&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And here lies the &lt;em&gt;essence&lt;/em&gt; of functional programming - all functional code is made up of expressions, producing some value and returning it to their callers. These expressions are usually stand-alone, completely specified by their input parameters. At the very top, the entry point (or, sometimes known as "the end of the world") these values are gathered and interacted upon with the rest of the world. In object-oriented jargon, this is sometimes called the "Onion architecture" (or "Ports and Adapters") - a pure core comprised of business logic and the imperative outer shell that is responsible for interactions with the external world.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x9QvhnW---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://hmemcpy.com/2020/03/your-csharp-is-already-functional/onion.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x9QvhnW---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://hmemcpy.com/2020/03/your-csharp-is-already-functional/onion.png" width="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C# (and Java and Python and ...) can be functional&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using expressions instead of statements in the small is at the core of nearly all programming languages today. C# evolved over time to introduce features to make it easier to work with expressions: LINQ, expression-bodied methods, pattern matching and more. These features are oftentimes called "functional" because they are --- in languages such as F# these features are used &lt;em&gt;all the time&lt;/em&gt;, allowing the data to flow from the inside-out. Other functional languages, such as Haskell, make it nearly impossible to have anything other than expressions.&lt;/p&gt;

&lt;p&gt;In fact, this style of programming is now encouraged by the C# team. In a recent talk given at NDC London, Bill Wagner urges you to change your (imperative) habits and embrace &lt;em&gt;modern&lt;/em&gt; techniques:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/aUbXGs7YTGo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;C# (and other imperative languages, like Java) can be used functionally, but it requires a high level of diligence. These languages make the functional style the exception, not the norm. I urge you to explore other languages, where expression-oriented programming is the default, making it a first-class citizen.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This was originally posted on &lt;a href="https://hmemcpy.com/2020/03/your-csharp-is-already-functional/"&gt;my blog&lt;/a&gt;.&lt;/em&gt; &lt;/p&gt;

</description>
      <category>csharp</category>
      <category>fsharp</category>
      <category>dotnet</category>
      <category>functional</category>
    </item>
    <item>
      <title>Becoming Foolish</title>
      <dc:creator>Igal Tabachnik</dc:creator>
      <pubDate>Sat, 21 Oct 2017 06:15:30 +0000</pubDate>
      <link>https://forem.com/hmemcpy/becoming-foolish-8l6</link>
      <guid>https://forem.com/hmemcpy/becoming-foolish-8l6</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally published &lt;a href="https://hmemcpy.com/2017/10/becoming-foolish/"&gt;on my blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The book &lt;a href="https://pragprog.com/book/tpp/the-pragmatic-programmer"&gt;&lt;em&gt;The Pragmatic Programmer: From Journeyman to Master&lt;/em&gt;&lt;/a&gt; by Andy Hunt and Dave Thomas suggests that as developers, we should "learn at least one new language every year." (pg. 14)&lt;/p&gt;

&lt;p&gt;When I recently asked a roomful of developers, if there's anyone who had learned a new language this year, only very few hands went up. A year ago today, that would have been me in the audience, keeping my hands down.&lt;/p&gt;

&lt;p&gt;I had been a happy C# developer for over a decade, starting professionally sometime before .NET 2.0 was released. During my career I had the privilege of working in a very specialized field - building tools for other developers, mainly in form of extensions to Visual Studio itself. My job often required from me to understand the code better than the compiler, in fact, I often had to take a peek under the hood to understand what the compiler, MSIL, JIT, and sometimes native code were doing, all long before tools like &lt;a href="https://github.com/dotnet/roslyn"&gt;Roslyn&lt;/a&gt; even existed. I knew implementation details of the runtime that were not documented anywhere. I knew most of the corner cases and language gotchas. But overall, my language, C#, was a very powerful hammer, capable of handling any kind of nail.&lt;/p&gt;

&lt;p&gt;Which caused me to pretty much ignore any other languages out there, which weren't relevant to my day job. Whenever people from the F# camp were boasting their lack of nulls, pattern matching, or not needing semicolons or braces, I would typically shrug it away. I have spent years of my career learning to use productivity tools, such as ReSharper and IntelliJ IDEA, to the point where every bit of boilerplate was one keystroke away. I simply didn't care! Having a few nice features was never a good enough reason for me to switch a programming language -- why would I even bother, when my language that I'm an expert in, could kinda-sorta do that too? Why would a different &lt;em&gt;syntax&lt;/em&gt; make a difference in how I write software? After all, doesn't it all compile down to the same thing?&lt;/p&gt;

&lt;p&gt;Because when you really get down to it, (almost) all programming languages are, essentially, this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In most programming languages that we've ever experienced, the world as we know it &lt;em&gt;lives and dies&lt;/em&gt; in the confines of the opening and closing brace of the &lt;code&gt;main&lt;/code&gt; function, which is the entry point to our application. Everything that happens in our program, the universe our program interacts with, happens between those two braces. By its very definition, our program returns an &lt;em&gt;exit code&lt;/em&gt;, specifying whether it succeeded or not, and everything else happens as a side-effect.&lt;/p&gt;

&lt;p&gt;The implication, that all the work is done between the two braces of the &lt;code&gt;main&lt;/code&gt; method, is what drives &lt;strong&gt;everything&lt;/strong&gt; we know about this model - every design pattern, every practice, every discipline, every tool, library, or framework - were created to let us manage this model of our world that exists between those two braces. We have decades of knowledge in how to do this properly in almost any language.&lt;/p&gt;

&lt;p&gt;For me, this was the core, the very foundation on which all the software is built! It was the one thing that was the ultimate truth, regardless of the programming language.&lt;/p&gt;

&lt;p&gt;And it was like this, until &lt;strong&gt;Haskell ruined it for me&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;I will not go into details - I inadvertently did that in my &lt;a href="https://dev.to/2017/05/from-net-to-scala-and-beyond-a-journey-to-functional-programming/"&gt;previous post&lt;/a&gt;. But during watching a talk on seemingly unrelated topic, the speaker managed to sneak some Haskell in there! Until that moment, I haven't really seen any Haskell code before - I've always dismissed it as irrelevant - and there it was,  staring me in the face.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;IO&lt;/span&gt; &lt;span class="nb"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;-- reads as 'main, having the return type of IO ()'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What I saw being explained is that Haskell doesn't actually do any work inside of its &lt;code&gt;main&lt;/code&gt; function. Instead, the program "describes" in a declarative way what is going to have happen when your program executes, and this "description" will be returned into the Haskell runtime, and exactly what was described will happen - with no possible &lt;em&gt;side-effects&lt;/em&gt; or surprises. It flips the entire idea of execution on its head - the work, the actual execution of effects is not being done &lt;em&gt;inside&lt;/em&gt; of &lt;code&gt;main&lt;/code&gt; - it's done on the &lt;em&gt;outside&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;Seeing this for the first time, trying to wrap my head around it, had created a &lt;em&gt;bug&lt;/em&gt; inside my head. It had conflicted with what I knew - that all languages worked the same, and the difference was merely syntax! But here, I was seeing something that just worked differently, and did not fit my world view! I struggled with trying to understand it, and it took me a couple of more re-watches, until one day, it finally clicked!&lt;/p&gt;

&lt;p&gt;In one single moment, I suddenly understood the &lt;em&gt;idea&lt;/em&gt; of Haskell, of why it works the way it does. Everything just clicked into place and I finally "understood monads"! To me, it felt like Neo finally coming to terms with the fact that &lt;a href="https://www.youtube.com/watch?v=uAXtO5dMqEI"&gt;there is no spoon&lt;/a&gt;, and only then he's able to bend it. &lt;/p&gt;

&lt;p&gt;I am thankful for &lt;a href="https://twitter.com/silverSpoon"&gt;Andrea Magnorsky&lt;/a&gt;'s excellent keynote talk, &lt;a href="https://www.youtube.com/watch?v=WBu43Tj0zOY"&gt;Inviting Everyone to the Party&lt;/a&gt;, which helped me realize that not only what I had experienced had a name, it was, in fact, a textbook definition of the phenomenon: what I had experienced is called a &lt;strong&gt;paradigm shift&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The term "paradigm shift", coined by &lt;a href="https://en.wikipedia.org/wiki/Thomas_Kuhn"&gt;Thomas Kuhn&lt;/a&gt; in his 1962 book &lt;a href="https://en.wikipedia.org/wiki/The_Structure_of_Scientific_Revolutions"&gt;The Structure of Scientific Revolutions&lt;/a&gt;, describes a process where if an established model no longer fits the world view, it is rejected and simultaneously replaced by another. Kuhn says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The decision to reject one paradigm is always simultaneously the decision to accept another, and the judgment leading to that decision involves the comparison of both paradigms with nature and with each other.&lt;br&gt;
&lt;strong&gt;Thomas Kuhn&lt;/strong&gt;, The Structure of Scientific Revolutions: 50th Anniversary Edition (pg. 78)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I had two conflicting ideas, and when finally that other idea started making sense, my mind accepted it, and immediately switched to that new idea - the switch was instantaneous! I was able to see things from a different perspective, understanding a whole new world model, which is precisely the definition of Kuhn's "paradigm shift".&lt;/p&gt;

&lt;p&gt;But something else happened -- I realized just how little I knew about software, there was an entire world out there that was unknown to me! What I knew until that moment, all my expertise in a narrow field was just a tip of the iceberg. And this realization removed a barrier I had that prevented me from trying to learn new languages and experimenting with new things. Where previously I would dismiss these things as irrelevant, I suddenly gained interest in things that I never had interest in before. In the past year alone I've used Haskell, Clojure, and Rust. I've looked at Erlang, Elm and PureScript. I am working my way through &lt;a href="https://mitpress.mit.edu/sicp/full-text/book/book.html"&gt;SICP&lt;/a&gt; and learning &lt;a href="https://github.com/hmemcpy/milewski-ctfp-pdf"&gt;Category Theory&lt;/a&gt;. I've done all that because for the first time in a long time, I am genuinely curious about what these languages/paradigms can &lt;em&gt;teach&lt;/em&gt; me! I really don't remember the last time I was this excited about learning. Genuinely proud of being able to say - &lt;em&gt;"I don't know everything!"&lt;/em&gt; - is incredibly liberating!&lt;/p&gt;

&lt;p&gt;This exact sentiment is also echoed in Bret Victor's fantastic, and highly entertaining keynote talk, &lt;a href="https://vimeo.com/71278954"&gt;The Future of Programming&lt;/a&gt;, where he pretended he was a software developer in 1973, and speculated about the next 40 years of programming. The last 5 minutes of that talk has some of the best advice I ever heard, and I wish I understood it sooner. He said,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;...you have to say to yourself - "I don't know what I'm doing. We, as a field, don't know what we're doing. We don't know what programming is, we don't know  what computing is, we don't even know what a computer is." And once you truly understand that, once you truly believe that -- then you're free, and you can think anything.&lt;br&gt;
&lt;strong&gt;Bret Victor&lt;/strong&gt; - &lt;a href="https://vimeo.com/71278954"&gt;The Future of Programming&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Stay hungry. Stay foolish.&lt;/em&gt; Be free!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>learning</category>
      <category>haskell</category>
    </item>
  </channel>
</rss>
