<?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[Luteceo — software chemistry - Medium]]></title>
        <description><![CDATA[We write about everything dear to our hearts like software architecture, functional programming, data science, domain-driven-design, front-end development, philosophy and life. - Medium]]></description>
        <link>https://medium.com/luteceo-software-chemistry?source=rss----8ff192d3f083---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Luteceo — software chemistry - Medium</title>
            <link>https://medium.com/luteceo-software-chemistry?source=rss----8ff192d3f083---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 20:43:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/luteceo-software-chemistry" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Parallel Scan Left in F#]]></title>
            <link>https://medium.com/luteceo-software-chemistry/parallel-scan-left-in-f-68fcce043f4d?source=rss----8ff192d3f083---4</link>
            <guid isPermaLink="false">https://medium.com/p/68fcce043f4d</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[fsharp]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[parallel-programming]]></category>
            <dc:creator><![CDATA[Tomasz Jaskuła]]></dc:creator>
            <pubDate>Sat, 15 Dec 2018 21:40:30 GMT</pubDate>
            <atom:updated>2018-12-15T21:40:30.307Z</atom:updated>
            <content:encoded><![CDATA[<p>Algorithms have always seemed to me easier to solve in an imperative mutable way. However, recently I had a look on how Scala implements some of the parallel collections and this gave me some ideas to try it out with F#.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zGmPAGgMPc5Muwnv" /><figcaption>Photo by <a href="https://unsplash.com/@natejgo?utm_source=medium&amp;utm_medium=referral">Nathan Gonthier</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Properties of sequential Scan Left</h3><p>Scala’s scanLeftis really an interesting function. It cumulate a collection of intermediate cumulative results using a start value.</p><pre>List(1, 5, 8).scanLeft(100)(_ + _) == List(100, 101, 106, 114)</pre><p>To calculate the next value we need to know the previous one. This is the sequential definition but can the scanLeft be made parallel? At first glance it seems impossible because:</p><ul><li>The last value in the sequence depends on all previous ones</li><li>All previous partial results needs to be computed before</li><li>This approach will always O(n) complexity</li></ul><p>The idea would be to find a way to reuse intermediate results. Otherwise we would need potentially to do more work but the parallelism should compensate for recalculation. It’s important to highlight that applied function should be <strong>associative </strong>or the the calculation wouldn’t be deterministic for parallel computation.</p><blockquote><strong>Associative operation<br></strong>Operation f is associative if for every x, y, z</blockquote><blockquote>f(x, f(y, z)) = f(f(x, y), z)</blockquote><blockquote>which can also be written with infix operator as<br>x ⊗ (y⊗ z) = (x ⊗ y)⊗ z</blockquote><h3>Thinking in terms of Tree</h3><p>I know that this is the Christmas time and when one talks about trees we could associate it with a Christmas tree. However for our task at hand we would need just a regular tree. It will helps us to save intermediate results of parallel computation.</p><p>Let’s define our input tree which would hold initial values in its leaves:</p><pre>type Tree&lt;&#39;T&gt; =<br>  | Leaf of &#39;T<br>  | Node of Tree&lt;&#39;T&gt; * Tree&lt;&#39;T&gt;</pre><p>We need also another tree to store intermediate results in its nodes and leaves:</p><pre>type TreeVal&lt;&#39;T&gt; =<br>  | LeafVal of &#39;T<br>  | NodeVal of TreeVal&lt;&#39;T&gt; * &#39;T * TreeVal&lt;&#39;T&gt;</pre><p>We can now write a reduceVal function which will transform Tree into TreeVal in order to preserver the computation tree.</p><p>For the input tree:</p><pre>let t = Node(Node(Leaf 1, Leaf 5), Node(Leaf 8, Leaf 48))<br>val t : Tree&lt;int&gt; = Node (Node (Leaf 1,Leaf 5),Node (Leaf 8,Leaf 48))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z7_gmSP6DpiQs2kyYfuOcg.png" /><figcaption>Input tree</figcaption></figure><p>We would like to obtain the following output with intermediate results:</p><pre>val it : TreeVal&lt;int&gt; =<br> NodeVal<br> (NodeVal (LeafVal 1,6,LeafVal 5),62,NodeVal (LeafVal 8,56,LeafVal 48))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PSLs-MmnC3Xy7IYdv7iEfw.png" /><figcaption>Tree with intermediate calculated results</figcaption></figure><p>Here we have the reduce function that preserves the computation tree:</p><pre>let rec reduceVal t f =<br>  let getValue = function<br>    | LeafVal v -&gt; v<br>    | NodeVal(_, v, _) -&gt; v<br>  <br>  match t with<br>  | Leaf v -&gt; LeafVal(v)<br>  | Node(l, r) -&gt;<br>    let leftVal, rightVal = (reduceVal l f, reduceVal r f)<br>    NodeVal(leftVal, f (getValue leftVal) (getValue rightVal), rightVal)</pre><p>To get the reduction result we simply call the function passing in the input tree and add operator:</p><pre>reduceVal t (+)</pre><p><strong>Can we design a parallel version of it?</strong></p><p>We can easily parallelize the reduction of the left and right tree with TPLTask. Let’s call it upsweep.</p><pre>let rec upsweep t f =<br>  match t with<br>  | Leaf v -&gt; LeafVal(v)<br>  | Node(l, r) -&gt;<br>    let leftT = Task.Run(fun _ -&gt; upsweep l f)<br>    let rightT = Task.Run(fun _ -&gt; upsweep r f)<br>    Task.WaitAll(leftT, rightT)<br>    let leftVal, rightVal = leftT.Result, rightT.Result<br>    NodeVal(leftVal, f (getValue leftVal) (getValue rightVal), rightVal)</pre><p>Then we need a function that would reduce the tree to create the final result:</p><pre>let rec downsweep t v0 f =<br>  match t with <br>  | LeafVal v -&gt; Leaf(f v0 v)<br>  | NodeVal (l, _, r) -&gt;<br>    let leftT = Task.Run(fun _ -&gt; downsweep l v0 f)<br>    let rightT = Task.Run(fun _ -&gt; downsweep r (f v0 (getValue l)) f)<br>    Task.WaitAll(leftT, rightT)<br>    let left, right = leftT.Result, rightT.Result<br>    Node(left, right)</pre><p>To get the result out of DU, I use this helper function:</p><pre>let getValue = function<br>  | LeafVal v -&gt; v<br>  | NodeVal(_, v, _) -&gt; v</pre><p>Now, we are ready to define our parallel scan left:</p><pre>let scanLeft t v0 f =<br>  let tVal = upsweep t f<br>  let scan = downsweep tVal v0 f<br>  prepend v0 scan</pre><p>where prepend function is:</p><pre>let rec prepend x = function <br>  | Leaf v -&gt; Node(Leaf x, Leaf v)<br>  | Node (l, r) -&gt; Node(prepend x l, r)</pre><p>Given our input tree</p><pre>Node (<br>  Node (<br>    Leaf 1,<br>    Leaf 5),<br>  Node (<br>    Leaf 8,<br>    Leaf 48)<br>) </pre><p>calling it with the following arguments scanLeft t 100 (+) produces the expected output:</p><pre>val it : Tree&lt;int&gt; =<br> Node (<br>  Node (<br>   Node (<br>    Leaf 100,<br>    Leaf 101),<br>  Leaf 106),<br>  Node (<br>   Leaf 114,<br>   Leaf 162))</pre><h3>Conclusion</h3><p>While most of the collections we are working with are not trees, it’s interesting to see that we could implement a parallel scan left operation that is O(log n) instead of O(n). This is just a simple example but we could also implement the same logic on regular arrays. The tree is a very good example to get the intuition of how the reduction could be implemented preserving the intermediate results. The next time I’ll try to benchmark what different implementations on large trees.</p><h4>References</h4><p>A very good resources and many ideas I’ve learnt following this Scala Parallel Programming course: <a href="https://www.coursera.org/learn/parprog1">https://www.coursera.org/learn/parprog1</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=68fcce043f4d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/luteceo-software-chemistry/parallel-scan-left-in-f-68fcce043f4d">Parallel Scan Left in F#</a> was originally published in <a href="https://medium.com/luteceo-software-chemistry">Luteceo — software chemistry</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lightweight starter kit for ASP.NET Core, React, Typescript and Webpack with HMR]]></title>
            <link>https://medium.com/luteceo-software-chemistry/lightweight-starter-kit-for-asp-net-core-react-typescript-and-webpack-with-hmr-7bc977b7712?source=rss----8ff192d3f083---4</link>
            <guid isPermaLink="false">https://medium.com/p/7bc977b7712</guid>
            <category><![CDATA[aspnetcore]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Tomasz Jaskuła]]></dc:creator>
            <pubDate>Sat, 14 Jul 2018 22:29:52 GMT</pubDate>
            <atom:updated>2018-07-14T22:29:52.288Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are looking for a starter kit based on NPM scripts, supporting ASP.NET Core 2.1, React, Typescript, Webpack with Hot Module Replacement and other features, this article is for you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*khmw62gsEnJ6dZEc" /><figcaption>“A displayed content of a toolkit” by <a href="https://unsplash.com/@toddquackenbush?utm_source=medium&amp;utm_medium=referral">Todd Quackenbush</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The most obvious choice today for bootstraping a new React application is to use Facebook’s <a href="https://github.com/facebookincubator/create-react-app">create-react-app</a> (CRA) template (or <a href="https://github.com/wmonk/create-react-app-typescript">create-react-app-typescript</a> with Typescript support). However if you want to leverage ASP.NET Core you can use <a href="https://docs.microsoft.com/fr-fr/aspnet/core/client-side/spa/react?tabs=visual-studio&amp;view=aspnetcore-2.1">Microsoft template</a> based on <em>create-react-app</em>.</p><p>For me <em>create-react-app</em> is really great until you need some more control over your dependencies and configuration, and you call npm run eject which is one way operation. Then you are on your own because there is no way back. In the end, for me, <em>create-react-app</em> has too many features that I don’t need.</p><p>Microsoft React template allows you to use React with ASP.NET Core but because it’s based on the original Facebook’s <em>create-react-app</em> you have to reconsider it twice before trying to go on your own and running the commandnpm run eject. Moreover, I don’t like the way how the template is calling into Node.js to run different npm scripts like npm install or npm run build. They are defined in the ASP.NET Core project properties contained in the *.csproj file. This is handy when you run dotnet CLI commands to compile, run and debug ASP.NET Core application but becomes really painfull when you have to define and run different npm scripts.</p><p>My requirements are really simple for a React starter kit. I want to:</p><ul><li>control my dependencies, the configuration and it should be really simple.</li><li>run it on ASP.NET Core 2.1 web server but everything should be controlled through npm scripts and not dotnet CLI.</li><li>leverage Typescript, Webpack, HMR…and many more</li></ul><p>That’s why I created a new template that fits my needs. ASP.NET Core Starter Kit 2</p><h3>ASP.NET Core Starter Kit 2</h3><p><a href="https://github.com/Luteceo/aspnet-starter-kit-2.0">ASP.NET Core Starter Kit 2</a> is a mix of the original <a href="https://github.com/kriasoft/aspnet-starter-kit">ASP.NET Core Starter Kit</a> (which seems to be not active anymore, thus “2” in the name) and Microsoft <a href="https://docs.microsoft.com/en-us/aspnet/core/spa/react?tabs=visual-studio">ASP.NET Core React/Redux template</a>. The detailed description can be found on the project repository <a href="https://github.com/Luteceo/aspnet-starter-kit-2.0">https://github.com/Luteceo/aspnet-starter-kit-2.0</a>.</p><h4>Main features are</h4><ul><li>Web component development with React 16.4</li><li>Application state managment with Redux 4.0</li><li>Bundling with Webpack 4.16</li><li>Static type checking with Typescript 2.9.2</li><li>Hot Module Replacement (HMR) with React Hot Loader</li><li>Lightweight build automation with plain JavaScript</li><li>Integration with ASP.NET Core 2.1</li></ul><h4>Lightweight build automation</h4><p>I think it is interesting to say some words about build automation. All the code is contained in run.js file adapted from the original <a href="https://github.com/kriasoft/aspnet-starter-kit">ASP.NET Core Starter Kit</a>. Here is an example of the start task which bundles client application using Webpack and starts ASP.NET Core web server:</p><pre>tasks.set(&#39;start&#39;, () =&gt; {<br>  global.HMR = !process.argv.includes(&#39;--no-hmr&#39;);<br>  <strong>return </strong>Promise.resolve()<br>    .then(() =&gt; run(&#39;clean&#39;))<br>    .then(() =&gt; run(&#39;appsettings&#39;))<br>    .then(() =&gt; run(&#39;build&#39;))<br>    .then(() =&gt; run(&#39;bundleVendor&#39;))<br>    .then(() =&gt; <strong>new </strong>Promise(resolve =&gt; {<br>      let count = 0;<br>      <strong>const </strong>webpackConfig = require(&#39;./webpack.config&#39;);<br>      <strong>const </strong>compiler = webpack(webpackConfig);<br><br>      // Node.js middleware that compiles application in watch mode with HMR support<br>      // http://webpack.github.io/docs/webpack-dev-middleware.html<br>      <strong>const </strong>webpackDevMiddleware = require(&#39;webpack-dev-middleware&#39;)(compiler, {<br>        publicPath: webpackConfig[0].output.publicPath,<br>        stats: webpackConfig[0].stats<br>      });<br>      compiler.hooks.done.tap(&#39;done&#39;, () =&gt; {<br>        // Launch ASP.NET Core server after the initial bundling is complete<br>        <strong>if </strong>(++count === 1) {<br>          <strong>const </strong>options = {<br>            cwd: path.resolve(__dirname, &#39;./server/&#39;),<br>            stdio: [&#39;ignore&#39;, &#39;pipe&#39;, &#39;inherit&#39;],<br>            env: Object.assign({}, process.env, {<br>              ASPNETCORE_ENVIRONMENT: &#39;Development&#39;,<br>              NODE_PATH: &#39;../node_modules/&#39;<br>            }),<br>          };<br>          cp.spawn(&#39;dotnet&#39;, [&#39;watch&#39;, &#39;run&#39;], options).stdout.on(&#39;data&#39;, data =&gt; {<br>            process.stdout.write(data);<br>          });<br>        }<br>      });<br>    }));<br>});</pre><p>This allows to bundle and compile the application in watch mode with HMR support and then launch ASP.NET Core server with the following line :</p><pre>cp.spawn(&#39;dotnet&#39;, [&#39;watch&#39;, &#39;run&#39;], options).stdout.on(&#39;data&#39;, data =&gt; {<br>            process.stdout.write(data);<br>          });</pre><p>In run.js file other tasks are defined to run test, build and publish the application. Of course it’s easy to add more tasks that fit you particular environment requirements.</p><h4>Running the application</h4><p>Once you’ve cloned the template from <a href="https://github.com/Luteceo/aspnet-starter-kit-2.0">https://github.com/Luteceo/aspnet-starter-kit-2.0</a> you run the application with yarn install and yarn start. The app should become available at <a href="http://localhost:5000/">http://localhost:5000/</a> (more details <a href="https://github.com/Luteceo/aspnet-starter-kit-2.0#getting-started">here</a>). HMR is by default enabled:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*n0HxJ4R_wC_JRX_jhRIhRQ.gif" /><figcaption>HMR integration with ASP.NET Core 2.1 web server. Changing the code in the reducer recompiles the appliction and reloads it in the browser.</figcaption></figure><h3>Conclusion</h3><p>The adaptation of two existing React starter kits, allowed me to get the best of two worlds and build a template that really fits my needs. Without to much configuration and ceremony I achieved what I really wanted; ASP.NET Core 2.1 integration with React/Redux, Typescript and HMR. I hope you’ll find this useful, <strong>please live any comments or suggestions below</strong>. I will also appreciate any PR’s and issues. You’ll find the code here: <a href="https://github.com/Luteceo/aspnet-starter-kit-2.0#getting-started">https://github.com/Luteceo/aspnet-starter-kit-2.0#getting-started</a>. Take a tour, see the code and enjoy!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7bc977b7712" width="1" height="1" alt=""><hr><p><a href="https://medium.com/luteceo-software-chemistry/lightweight-starter-kit-for-asp-net-core-react-typescript-and-webpack-with-hmr-7bc977b7712">Lightweight starter kit for ASP.NET Core, React, Typescript and Webpack with HMR</a> was originally published in <a href="https://medium.com/luteceo-software-chemistry">Luteceo — software chemistry</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Can programming be liberated from the von Neumann style?]]></title>
            <link>https://medium.com/luteceo-software-chemistry/can-programming-be-liberated-from-the-von-neumann-style-932ba107402b?source=rss----8ff192d3f083---4</link>
            <guid isPermaLink="false">https://medium.com/p/932ba107402b</guid>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[programming-paradigms]]></category>
            <dc:creator><![CDATA[Tomasz Jaskuła]]></dc:creator>
            <pubDate>Wed, 10 Jan 2018 22:43:03 GMT</pubDate>
            <atom:updated>2018-01-10T22:43:02.800Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HI3lsnUXtdVZ9NVdm5fuNQ.jpeg" /><figcaption>Universal machine: John von Neumann and his high-speed computer, circa 1952.</figcaption></figure><p>It’s been 40 years since <a href="https://en.wikipedia.org/wiki/John_Backus">John Backus</a>’ <a href="https://en.wikipedia.org/wiki/Turing_Award">Turning Award</a> lecture on 1977 “<a href="https://www.thocp.net/biographies/papers/backus_turingaward_lecture.pdf"><em>Can Programming Be Liberated from the von Neumann Style? A Functional Style and Its Algebra of Program</em>s</a>”. He described a functional-level programming language (not to be confused with functional programming) known as <a href="https://en.wikipedia.org/wiki/FP_(programming_language)">FP</a> that he was working on.</p><p>This was a visionary article well ahead of its time. The point is, that he pinpointed a fundamental problem of thought in programming that is still valid 40 years after. The argument is not about imperative vs functional programming but <em>it is more about how people are thinking and seeing programming as explicit statement control flow</em> without even realizing that this is not the only one approach of programming.</p><p>The John Backus’ paper is very controversial at the time but it was certainly a very good step in the right direction.</p><h3>Criticism of conventional programming languages</h3><p>The first part of the paper starts setting the tone for the following pages:</p><blockquote>Conventional programming languages are growing ever more enormous, but not stronger. Inherent defects at the most basic level cause them to be both fat and weak: their primitive word-at-a-time style of programming inherited from their common ancestor — the von Neumann computer, their close coupling of semantics to state transitions, their division of programming into a world of expressions and a world of statements, their inability to effectively use powerful combining forms for building new programs from existing ones, and their lack of useful mathematical properties for reasoning about programs.</blockquote><p>40 years has passed since this was written but do you think it’s not relevant anymore? I think that most of it still applies to your preferred mainstream programming language. I hear you saying that this is not true anymore because most of mainstream languages are not imperative but Object Oriented. Different paradigms!</p><p>But when I read trough Backus’ article, he is highlighting some of the problems that still apply to mainstream languages. They are getting more enormous but not stronger, they are fat and week, they have still primitive statements, close coupling of semantics and state transitions.</p><p>Sure, “word-at-a-time” is not relevant anymore but how many times we write code based on arrays not only to express a collection of elements but encoding different data structures like trees, binary search trees and graphs? In the mainstream languages most of the algorithms are using arrays as the basic data structure and your are left with implementing an error-prone logic and indexing arithmetics. This is like “word-at-a-time” processing even if you don’t call it so. Modifying mutable variables, using assignments and control structures such as if-then-else, loops, breaks, continue and return are the most common informal way to understand imperative programs as instruction sequences for a von Neumann computer. There is a strong correspondance between mutable variables and memory cells, variable dereferences and load instructions, variable assignments and store instructions, control structures and jumps. However how this is scaling up when we conceptualize programs using “word-at-a-time”?</p><blockquote>We are solving problems using abstractions and models but how can we express useful abstractions without using awkward way of encoding types in basic structures with flawed control flow semantics?</blockquote><p>So many questions but in the end you may say that imperatives languages belongs to the past so it’s not my problem anymore. More, most of the software industry is not interested in solving algorithmic problems at all. Mainstream languages are mostly used to encode business logic and construct models to solve real business problems with Object Oriented programming. Let’s model the real world, they say! Bear with me, most of the time people use objects (classes) as mutable data wrappers combined together with the same control structures used in the von Neumann programming style.</p><p>Backus is also highlighting:</p><blockquote>…their lack of useful mathematical properties for reasoning about programs.</blockquote><p>40 years later, Object Oriented languages has became the most popular choice for software development industry, but I don’t think we have any mathematical model to validate the correctness of OOP. We have Turing machines for imperative programming, lambda calculus for functional programming but nothing for OOP. The question is then, <em>how do we ensure the correctness of an Object Oriented program</em>? In C# for example, you could use “Code contracts” which are just an implementation of <a href="https://en.wikipedia.org/wiki/Bertrand_Meyer">Bertrand Mayer</a>’s “<a href="https://en.wikipedia.org/wiki/Design_by_contract">design by contract</a>” idea, but I never saw them implemented in the real field. This just doesn’t work! You can use invariants in very simple cases but when it becomes more complicated, when you have to depend on invariants from another class you end up with convulted logic just to implement code contracts. Also checking the pre and post conditions in many real world scenarios are so complicated that they forbid many use cases.</p><p>By the way even if we have mathematical model for functional programming (lambda calculus), people writing business software in FP don’t need to provide mathematical proofs that the program is correct. This is not the point (although I think that Haskell for example is compiled to intermediate lambda calculus which is then verified for correctness, that is why Haskellers say “If this compiles, it works”). Simple fact that your programming language is backed by mathematical model allows to think about business problems in different ways and provides to you construction blocks based on mathematical properties allowing you to write better software, with richer expressiveness and a way better abstractions and logic. He continues:</p><blockquote>The second world of conventional programming languages is the world of statements. The primary statement in that world is the assignment statement itself. All the other statements of the language exist in order to make it possible to perform a computation that must be based on this primitive construct: the assignment statement.</blockquote><p>This is one of the weakest elements of von Neumann programming style that Backus seems to be particulary heated. His vision for a better model is essentially without variables, with coherent algebra, precise definitions and applicative state transition properties.</p><p>In the second part of the article, Backus proposes a solution to the von Neumann programming problem.</p><h3>Functional progamming as solution</h3><p>Backus describes his vision of functional programming which seems rather modern, even if 40 years has passed. The syntax of the FP language he proposes is quite different from what we can imagine compared to the modern functional programming language but the ideas are still fresh. This was certainly a way to go at the time. It is based on function composition, higher order functions and the program is evaluated through reduction. He describes a short program than it compares it to von Neumann style highlighting the following properties that I’ll try to explain:</p><blockquote>a) It operates only on its arguments. There are no hidden states or complex transition rules…</blockquote><p>This is the core of functional programming. This is why it is so powerful and easy to work with.</p><blockquote>b) It is hierarchical, being built from three simpler functions (+, x, Trans)</blockquote><p>This is not 100% clear to me but I think today we talk about “composition” and not “hierarchy”.</p><blockquote>c) It is static and nonrepetitive, in the sense that its structure is helpful in understanding it without mentally executing it.</blockquote><p>This is also a core idea of functional programming. We care about what the program <em>means</em> and not <em>how it is implemented</em>. There is no cognitive load at mentaly executing a program step by step in order to understand what it does. How many times have you been mentaly executing steps of your program in your prefered popular language to understand what it does?</p><blockquote>d) It operates on whole conceptual units, not words; it has three steps; no step is repeated.</blockquote><p>Higher level abstractions.</p><blockquote>e) It incorporates no data; it is completely general; it works for any pair of conformable vectors.</blockquote><p>I’m not sure about that but it seems to me that he still talks about the simplicity of the language that can be abstracted to use any kind of data structures.</p><blockquote>f) It does not name its arguments; it can be applied to any pair of vectors without any procedure declaration or complex substitution rules.</blockquote><p>Seems like he is proposing a way to compose functions in a simple way.</p><blockquote>g) It employs housekeeping forms and functions that are generally useful in many other programs; in fact, only + and x are not concerned with housekeeping. These forms and functions can combine with others to create higher level housekeeping operators.</blockquote><p>For me what he is describing here as “housekeeping” are all the constructs that we use repeatedly in our mainstream languages like loops, if-else branching constructs, etc. How many times have you written loops with almost the same form and without even realizing how poor abstraction it is? It is rooted so deeply in our way of programming that we don’t even notice we’re still writing code like a von Neumann “word-at-a-time” style. In functional programming those were already captured as patterns so we don’t need to write them over and over. Instead of looping we have higher order functions, if-else can be replaced with powerful pattern matching decomposition, etc.</p><p>While Backus continues on describing his vison of functional programming system there are many details and mathematical proofs that I’m not able to explain correctly, but I hope I have captured the core idea of the paper. Definietly it is worth to read it.</p><p>However, you may ask. If the functional programming is so good why...</p><h3>…FP didn’t take over the world?</h3><p>Not yet! But it’s getting more and more popular for obvious reasons. The part of the response we find in the paper:</p><blockquote>Our fixation on yon Neumann languages has continued the primacy of the von Neumann computer, and our dependency on it has made non-von Neumann languages uneconomical and has limited their development. The absence of full scale, effective programming styles founded on non-von Neumann principles has deprived designers of an intellectual foundation for new computer architectures.</blockquote><p>and then:</p><blockquote>Moreover, most applicative systems employ the substitution operation of the lambda calculus as their basic operation. This operation is one of virtually unlimited power, but its complete and efficient realization presents great difficulties to the machine designer.</blockquote><p>It seems that the functional architectures has failed not because they were unworkable but because there were no support from the market rooted in the von Neumann style. There was no support from the industry or large academia either. Mainstream language have been steamrolled since then and then Object Oriented pattern forced itself as the only valid way of writing programs at large scale getting more and more popular. But the popularity is not related to quality. Not everything popular is bad but definietly not everything popular is good. In the end the popularity might be an evidence of minimum level of quality acceptable by the majority of people.</p><p>Object Oriented programming is good enough so it allows it to be widely used. People are happy to choose good enough solutions that seem easy not matter if there are better solution out there. OOP is relatively accessible to average programmer even if in the end it is more complex than functional programming but the root cause may be the way it is taught in universities. Often functional programming is associated with maths which indeed may be helpful, but bear with me, you don’t need to know category theory and lambda calculus to write composable functional software.</p><p>We’ve also touched upon the lack of verifying mathematical properties of Object Oriented languages and that it was formally impossible to prove the correctnes of Object Oriented programs but this didn’t stop the wide adoption and growing popularity. Indeed, OOP is still adopted mostly for business applications where ultimate correctness isn’t necessary. You don’t need 100% of correctness most of the time. From the business point of view you need a <em>good enough</em> software so you can earn more money than you can loose. Also very often a time to market is more important factor than ultimate correctness and stability of you program and you compose with available skills on the market. It’s how the business works! However in my opinion I think that you can write software in FP language quicker being more reliable at the same tume than what you can do in OOP. It depends on what paradigm the team is familiar with.</p><p>40 years later and I have a feeling that today we’re still fighting similar problems that Backus described in this paper. This is a visionary article ahead of its time. The biggest problem are not the mainstream languages but how most of the people are collectively thinking inside the boxes without even realizing that there are other available paradigms of thought. Many people are solving problems locking them up in mental structure of von Neumann style like if this was the only way. Even if, since some time functional programming is becoming more and more popular it will take some time before we would call it mainstream or another patter would emerge in the meanwhile (maybe quantum computer will change everything we know about programming). On the positive side, many concepts from functional programming are leaking into mainstream languages. For some people this could be good or bad, but what I’m seeing is the opportunity to learn to think and to solve problems differently.</p><p>And you, what do you think? Are we still trapped in von Neumann style?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=932ba107402b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/luteceo-software-chemistry/can-programming-be-liberated-from-the-von-neumann-style-932ba107402b">Can programming be liberated from the von Neumann style?</a> was originally published in <a href="https://medium.com/luteceo-software-chemistry">Luteceo — software chemistry</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Statistical analysis using F# and Jupyter notebooks]]></title>
            <link>https://medium.com/luteceo-software-chemistry/statistical-analysis-using-f-and-jupyter-notebooks-2e2f31ee4cc1?source=rss----8ff192d3f083---4</link>
            <guid isPermaLink="false">https://medium.com/p/2e2f31ee4cc1</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[jupyter-notebook]]></category>
            <category><![CDATA[plotly]]></category>
            <category><![CDATA[fsharp]]></category>
            <category><![CDATA[statistics]]></category>
            <dc:creator><![CDATA[Tomasz Jaskuła]]></dc:creator>
            <pubDate>Tue, 19 Dec 2017 13:43:06 GMT</pubDate>
            <atom:updated>2017-12-19T13:43:05.656Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*70VrbWG3jEPVVYVEmwmYNA.png" /><figcaption>Means histogram of exponential distribution</figcaption></figure><p>Some time ago I took <a href="https://fr.coursera.org/specializations/jhu-data-science">Coursera “Data Science” specialization</a>. Data science is not something I get paid for professionally. It’s just a hobby so bear with me if I do some mistakes or if I use the wrong vocabulary.</p><blockquote>This post is part of the <a href="https://sergeytihon.com/2017/10/22/f-advent-calendar-in-english-2017/">F# Advent Calendar 2017</a> initiative — make sure to go check out rest of posts as well.</blockquote><p>One of the homeworks I had to do for the specialization was to make the comparison of exponential distribution and Central Limit Theorem. At the time, I made the analysis with R language which was used throughout the specialization for obvious reason. R is very convinient for statistics and data analysis as it was designed for that purpose. Many built in statistical functions, great support for plotting with ggplot2 make the analysis focused on domain problem and not fighting with language, tools and libraries. This was exactly the problem I faced at the time when I tried to do the same analysis in F#. The language itself was great but the surrounding tooling and integration with external libraries bore no comparison with R at the time.</p><p>More than two years has passed. In the meantime F# tooling for data analysis has much improved. Jupyter Azure Notebooks supports now F# so I decided to give it another try and to reiterate my analysis. If your are interested in trying it yourself go to Azure Notebooks <a href="https://notebooks.azure.com/">https://notebooks.azure.com</a>. Let’s start the analysis.</p><h3>Comparison of exponential distribution and Central Limit Theorem with F#</h3><p>The goal of this article is to investigate in F# the exponential distribution using and to compare it with the Central Limit Theorem. The exponential distribution can be simulated in F# with <a href="https://numerics.mathdotnet.com/">MathNet.Numerics</a> Exponential.Samples(new Random(randomSeed), lambda) where lambda is the rate parameter. The mean of exponential distribution is 1/lambda and the standard deviation is also 1/lambda. I set lambda = 0.2 for all of the simulations. I will investigate the distribution of averages of 40 exponentials. Note that I will need to do a thousand simulations.</p><p>The result of this post will be to illustrate the properties of the distribution of the mean of 40 exponentials and:</p><ul><li>Show the sample mean and compare it to the theoretical mean of the distribution</li><li>Show how variable the sample is (via variance) and compare it to the theoretical variance of the distribution</li><li>Show that the distribution is approximately normal</li></ul><h4>Analysis</h4><p>Let’s explore several aspects of exponential distribution. First of all we need to load some librairies into the notebook.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YoYt2KfDNc1kyS2wU-j5mQ.png" /><figcaption>Loading F# dependencies into Azure Notebook with Paket support</figcaption></figure><p>What we need is the library for statistical analysis. The best out there for .NET platform is <a href="https://numerics.mathdotnet.com/">MathNet.Numerics</a>. We need also something for plotting. <a href="https://tahahachana.github.io/XPlot/">XPlot</a> is the most widely used F# data visualisation package. Azure Notebooks support also <a href="https://fsprojects.github.io/Paket/">Paket dependency manager</a> used for referencing and managing all the needed dependencies. You have to #load F# scripts for Paket and XPlot to make dependencies visible in the notebook but I didn’t dig deeper to check what it is doing under the hood.</p><h4>Sample mean vs theoretical mean</h4><p>The first step is to explore the sample mean and compare it to the theoretical mean. In order to have a significant data we would need to run 1000 simulations each containing 40 observations so we would be able to take means for each simulation. We can achieve it by running the method Exponential.Samples(new Random(randomSeed), lambda) |&gt; Seq.take (n * nbsim) |&gt; List.ofSeq and arranging it in the matrix of 100x40:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h3Ysly-FG7X7SM5_qefYVw.png" /><figcaption>Generating 1000 simulations of exponential distribution and arranging it in 100x40 Matrix</figcaption></figure><p>I’m using here MathNet.Numerics Exponential.Sample(...) method to generate 1000 samples containing 40 observations. MathNet.Numerics provide many additional classes and functions for statistical analysis. Here I’m using Matrix and Vector types to arrange my samples in 100x40 matrix. Note that I’m setting randomSeed=1111. It’s just a constant value which guarantees the reproducibility of the same samples, but you can change it to another value if you want. The key is that it should remain constant so other people can repeat your experience.</p><p>The next step is to check the mean of 1000 averages of exponential distribution:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SiawtDlLgRN-UBjRLkFscQ.png" /><figcaption>1000 averages of exponential distribution</figcaption></figure><p>Let’s compare it to the theoretical mean of exponential distribution:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CnQLbzdYdbGYJFEOqw4ehw.png" /><figcaption>Theoretical mean of exponential distribution</figcaption></figure><p>The simulated mean of exponential distribution 4.997430667 is very close to the therotetical mean 5.</p><p>Next step is to look at the distribution of the simulated means. I’m going to use <a href="https://tahahachana.github.io/XPlot/plotly.html">XPlot.Plotly</a> to draw a histogram of the distribution with a theoretical mean line. Here is the F# to draw the chart:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gZ8eykUNiwmwpMg9EdURPQ.png" /><figcaption>F# code for histogram generation</figcaption></figure><p>The code is quite straightforward and easy to understand. Note that XPlot will draw two overlapping charts; one for histogram and another one for mean line. This is very handy because each chart can be constructed independently and combined in the list just before calling the Chart.Plot function. All of the plots are documented on XPlot page with many examples. This is really a great resource to learn from.</p><p>Let’s look at our plotted histogram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KCYYoudSG_pTNWUZN1nMzQ.png" /><figcaption>Histogram of distribution if 1000 means of exponential distribution</figcaption></figure><p>What do you think? It’s rather cool. More, it’s interactive. You can hover with your mouse to show some interesting values:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/162/1*3KaWBkwE6coegdp7-cTTEA.png" /><figcaption>Interactive values on Histogram</figcaption></figure><h4>Sample variance vs theoretical variance</h4><p>The next step is to compare the variance present in the sample means of the 1000 simulations to the theoretical variance of the population.</p><p>The sample variance is :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VLqEpEm-OFQ0fmE1dTAzZw.png" /><figcaption>Sample means variance</figcaption></figure><p>And the theoretical variance is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4NKiUZn12m749DlvvZ3IbA.png" /><figcaption>Theoretical variance</figcaption></figure><p>The theoretical and sample variance are quite similar.</p><p>Now we need to calculate the distribution of means of the exponential distribution with mean equal to sample mean and theoretical mean. The standard deviation is equal to sqrt of sample variance and theoretical variance. We then can compare the similitude.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tiGXONlT7cLaJaEPhY5rEA.png" /><figcaption>Calculation of the distribution of means of the exponential distribution</figcaption></figure><p>Again for that I’m using Normal.PDF function from MathNet.Numerics package. Let’s plot those distributions along with the previous histogram so we can visually check that they are almost the same:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ObcrJwBHeMVzVS-5u2SkbA.png" /><figcaption>Distribution of means of exponential distribution</figcaption></figure><p>And here is our plot:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VKp0i8YAvXp-qgSvHAS7sQ.png" /><figcaption>Distribution of theoretical and simulated means of exponential distribution</figcaption></figure><h4>Conclusion of the analysis</h4><p>The analysis showed that averaging over 1000 simulations of 40 observations for exponential distribution is very close to the theoretical distribution mean. We can say the same about the variance which is very close. The distribution is also normal. I could have shown that on Q-Q plot but unfortunately XPlot doesn’t support it (or probably the Plotly API).</p><h3>Conclusion about F# and Azure Notebooks</h3><p>I was really nicely surprised about how good the integration of F# is inside the Azure Notebooks. While writing, you get the live type checking, intellisense, tooltips which is really what you would expect when working with F#. Plots are really looking good too and are very easy to generate. This was a simple analysis with just MathNet.Numerics and XPlot as a dependence but I’m sure it would also work in more complex scenarios. Azure Notebooks works very well, on the other hand I had hard time trying to make work F# Jupyter Notebooks on my Mac and my local Anaconda environment. I hope that with recent release of .NET Core 2.0 things will change in the long run and there will be no différence running .NET code on Mac and Windows. For now enjoy Azure Notebooks and F# and don’t hesitate to give it a try on your free time.</p><h3>References</h3><p>Here are some references you might find usefull:</p><ul><li>F# notebook for this analysis: <a href="https://notebooks.azure.com/tjaskula/libraries/simple">https://notebooks.azure.com/tjaskula/libraries/simple</a></li><li>The same analysis I’ve done before using R: <a href="https://github.com/tjaskula/Coursera/blob/master/Specializations/Data%20Science/Statistical%20Inference/CompareExpDistribWithClt.pdf">https://github.com/tjaskula/Coursera/blob/master/Specializations/Data%20Science/Statistical%20Inference/CompareExpDistribWithClt.pdf</a></li><li>Azure Notebooks: <a href="https://notebooks.azure.com/">https://notebooks.azure.com/</a></li><li>MathNet.Numerics: <a href="https://numerics.mathdotnet.com/">https://numerics.mathdotnet.com/</a></li><li>XPlot : <a href="http://tahahachana.github.io/XPlot/">http://tahahachana.github.io/XPlot/</a></li><li>F# Foundation: <a href="http://fsharp.org/">http://fsharp.org/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e2f31ee4cc1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/luteceo-software-chemistry/statistical-analysis-using-f-and-jupyter-notebooks-2e2f31ee4cc1">Statistical analysis using F# and Jupyter notebooks</a> was originally published in <a href="https://medium.com/luteceo-software-chemistry">Luteceo — software chemistry</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>