<?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[AnyJunk - Medium]]></title>
        <description><![CDATA[AnyJunk Tech Stories - Medium]]></description>
        <link>https://medium.com/anyjunk?source=rss----a165f627a6fc---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>AnyJunk - Medium</title>
            <link>https://medium.com/anyjunk?source=rss----a165f627a6fc---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 04:58:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/anyjunk" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Data driven system design]]></title>
            <link>https://medium.com/anyjunk/data-driven-system-design-b300a08df17?source=rss----a165f627a6fc---4</link>
            <guid isPermaLink="false">https://medium.com/p/b300a08df17</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[scala]]></category>
            <dc:creator><![CDATA[Adam Coales]]></dc:creator>
            <pubDate>Mon, 13 Dec 2021 09:21:11 GMT</pubDate>
            <atom:updated>2021-12-13T09:21:11.146Z</atom:updated>
            <content:encoded><![CDATA[<h4>Designing a flexible data driven skip delivery/collection system</h4><h3>The Problem:</h3><p>To build a system for managing the booking/charging of skip delivery/exchange/collection. In the future this system should be able to handle many different services with different pricing rules for different sorts of events.</p><p>Our goal was to build a system unaware it was working with skips. A scalable design that could handle skips today and kitchen installations tomorrow without input from a developer. Giving the users visibility over the rules of the system by making them configurable and viewable through an admin portal.</p><h3>Approach:</h3><p>Embrace SQL. We isolated the key concepts likely to vary as the system expanded and created a set of “type structure” tables in the database which defined these and the relationships between the types. Users can then insert into these type tables and link tables to setup the system to their needs.</p><h3>Small Example Problem:</h3><p>Create a system to calculate journey time for a person to walk a route based on distance. We know in the future that we’d like to include other information such as elevation gain and may include calculations for travel methods besides walking.</p><h4>Simple approach:</h4><pre>case class Walker(id: Walker.Id, pace: Int)<br>case class Route(id: Route.Id, distance: Int)</pre><p>This is the quickest solution to the problem, in order to calculate journey time you take a Walker and a Route and multiply distance * pace. However, this system will need developer intervention to expand to accommodate adding on additional time for elevation gained. Something like below.</p><pre>case class Walker(id: Walker.Id, pace: Int, climbingSpeed: Int)<br>case class Route(id: Route.Id, distance: Int, elevationGain: Int)</pre><p>This is still relatively simple but we also know there will be other travel methods in the future. If we add in electric scooters which don’t care about elevation again a developer is required to come in and add something like the following.</p><pre>case class Scooter(id: Scooter.id, pace: Int)</pre><p>This approach works well for a small system or for elements of a system that won’t vary much during its lifecycle. However we know the future of this system is to have many methods of travel which could each have different metrics for journey time. We don’t want to pollute our system design with repetitive business logic that causes compounding costs in developer time, code maintainability and testing as more expected features are requested.</p><p>We wanted something more flexible (and fun for a developer to build).</p><h4>Type approach:</h4><p>First we’ll define the basic concepts of the system. A mode of transport (be it walking or scooter) and a journey timer that can be associated with a transport type.</p><pre>case class TransportType(id: TransportType.Id, name: String)<br>case class JourneyTimer(id: JourneyTimer.Id, transportTypeId: TransportType.Id)</pre><p>Then we need a field containing an amount (be it distance or elevation) in order to add time against.</p><pre>case class JourneyMetricType(id: JourneyMetric.Id, name: String)</pre><p>Next a user will need to be able to inform the system which transport types care about which metrics and also how a timer should use those fields.</p><pre>case class TransportJourneyMetricType(id: TransportJourneyMetricType, transportTypeId: TransportType.Id, journeyMetricTypeId: JourneyMetricType.Id)</pre><pre>case class JourneyTimerMetric(id: JourneyTimerMetric.Id, journeyTimerId: JourneyTimer.Id, journeyMetricTypeId: JourneyMetricType.Id, amount: Int)</pre><p>Lastly a user should be able to create instances of routes and relevant metrics. This can then be used in combination with the journey timers to give the users the functionality they need.</p><pre>case class Route(id: Route.Id)</pre><pre>case class RouteMetric(id: RouteMetric.Id, routeId: Route.Id, journeyMetricTypeId: JourneyMetricType.Id, amount: Int)</pre><p>This removes the concept of walking / scooters from the system and replaces them with the generic concepts of a TransportType, JourneyTimer and Route. The Scala code is now purely working with generic rules and implementation details are left to the users.</p><p>The final system looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3_bzkmxy19YmFsU2zCed-Q.png" /><figcaption>Example Problem Diagram</figcaption></figure><h3>Scala</h3><h4>Type safe IDs</h4><p>With the type structure approach the resulting system has a heavy reliance on foreign keys and thus IDs in order to function. This can become a challenge during development and a source for subtle bugs if ever the wrong type of ID is used.</p><p>By using the concept of a KeyedEntity andKeyedEntityCompanion our database model case classes can define IDs as their own types allowing the compiler to enforce correct ID usage throughout the Scala codebase.</p><pre>case class TransportType(<br>  id: TransportType.Id,<br>  ...<br>) extends KeyedEntity[TransportType.Id] {<br>  def key = id<br>}<br><br>object TransportType extends KeyedEntityCompanion[Int]</pre><p>We occasionally found bugs still sneaking in through the API layer as encodings / decodings couldn’t enforce this type correctness. To resolve this we created the concept of PrefixedKeyedEntityCompanion which also provides encodings / decodings with a given prefix to allow the API to reject invalid IDs.</p><pre>object TransportType extends PrefixedKeyedEntityCompanion[Int, KeyPrefix.TT.type]</pre><h4>DataIntegrity</h4><p>Alongside the IDs being of the correct type / structure we also needed a convenient way to ensure IDs exist and were active where required. Whilst it’s impossible to have the compiler check if an ID will exist in the database we can force developers to consider this through the concept of Evidence and Having.</p><pre>type Having[+T, +E &lt;: EvidenceConstraint] = T with E</pre><pre>case class ThingWithForeignKey(id: Thing.Id, fkId: FK.Id Having DataIntegrity)</pre><p>Now ThingWithForeignKey requires the type of the foreign key to be combined withDataIntegrity before it will compile. This forces the developer to wrap the foreign key in the required Evidence and thus we have greater confidence it is correct and won’t upset the database when we attempt to insert / update.</p><h4>Repositories</h4><p>In order to make the lives of developers easier whilst checking ID validity our Repository classes have an ActiveKeyedRepository trait which provides helpful utility functions to validate single / multiple IDs wrapping them with DataIntegrity if all exist and are active. Below is a standard usage pattern we’d have within a Service which accepts unvalidated IDs from the API but is required to validate them before passing to create / update DB queries.</p><pre>class TransportTypesService (<br>  repository: TransportTypesRepository,<br>  journeyMetricTypesRepository: JourneyMetricTypesRepository,<br>) {</pre><pre>def create(<br>  name: String,<br>  journeyMetricTypeIds: List[JourneyMetricType.Id]<br>) = for {<br>  validJourneyMetricTypeIds &lt;- journeyMetricTypesRepository.validateKeys(journeyMetricTypeIds)<br>  transportType &lt;- repository.create(name, validJourneyMetricTypeIds)<br>} yield transportType</pre><h3>Takeaway</h3><h4>Advantages</h4><p>The key advantage to this design approach is the future flexibility your system gains. By abstracting over the concepts we knew were likely to change our users gained the ability to maintain and manage a part of the system normally locked behind the scenes.</p><p>Developers time can also be spent on the algorithmic parts of the system, creating the concept of an ability to charge for something. Rather than business specific rules of how a skip should be charged.</p><p>It’s also much more fun to build!</p><h4>Caveats</h4><p>The solution does introduce more boiler plate, having to create the models etc to define the type structure and validate the IDs. It also increases the complexity of testing / data generation, you want to ensure your tests are running against data which is valid.</p><p>Also if you ever need to have specific business logic within your code itself that must interact with the type structure this can be fiddly. We solved this issue through the use of Enums and a table which contains those Enums. Which isn’t bullet proof.</p><h4>Thoughts</h4><p>We at AnyJunk are in the privileged situation to be able to design and build another new system which not only solves our immediate requirements but sets us up to have a solution which solves future usages almost free of charge. Designing a more generic system that gives the users power to customise core logic to their needs allows the system to be far more scalable both in terms of current and potential customer usage. Whilst this won’t always be the case we believe the increased code complexity is well worth the future rewards.</p><p>Also as functional developers, who doesn’t prefer to design abstract solutions and leave the implementation details to users?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b300a08df17" width="1" height="1" alt=""><hr><p><a href="https://medium.com/anyjunk/data-driven-system-design-b300a08df17">Data driven system design</a> was originally published in <a href="https://medium.com/anyjunk">AnyJunk</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Composing Maybes in Javascript]]></title>
            <link>https://medium.com/anyjunk/composing-maybes-in-javascript-efe6884f2111?source=rss----a165f627a6fc---4</link>
            <guid isPermaLink="false">https://medium.com/p/efe6884f2111</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[monads]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Dan Hood]]></dc:creator>
            <pubDate>Mon, 23 Apr 2018 15:12:51 GMT</pubDate>
            <atom:updated>2018-04-23T15:12:50.771Z</atom:updated>
            <content:encoded><![CDATA[<h4>Modelled on monadic comprehension in Scala and Haskell</h4><p>In my previous article <a href="https://medium.com/anyjunk/using-maybes-with-react-redux-1c47d4a09ed7">Using Maybes with React-Redux</a> I discussed the use of <em>Maybe</em>s in javascript. If you’re not familiar with what a <em>Maybe</em> is then I urge you to take a look at it and then come back to this article as it will all make more sense then.</p><p>A problem when using <em>Maybe</em>s is that it is awkward to work with many at the same time:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f81a2d6dd4f75d5093c48eff51914fa/href">https://medium.com/media/1f81a2d6dd4f75d5093c48eff51914fa/href</a></iframe><p>This works, but it’s not very easy to read as it involves several layers of indentation and nested brackets when chaining them. What we’d like is a sensible way of combining and working with multiple <em>Maybe</em>s at once. Languages that make heavier use of constructs like <em>Maybe</em> have a way to handle this: in Scala it’s the <em>for</em> keyword and in Haskell it’s <em>do</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b701fcaa56ead72eb17cfdd9b9d36857/href">https://medium.com/media/b701fcaa56ead72eb17cfdd9b9d36857/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3493c75308dea4d2496586cbbcac6247/href">https://medium.com/media/3493c75308dea4d2496586cbbcac6247/href</a></iframe><p>This is a lot neater than nesting but javascript doesn’t offer any equivalent by itself so we’re going to have to make one. I will take you through my process for creating it as I believe it could be helpful for understanding the end result, but if you just want to see the final result then please feel free to skip to the end.</p><p>We’ll start by working out what we want from the end result. As a side note, since <em>for</em> and <em>do</em> are both protected words in javascript we decided to call this <em>mFor</em> at <a href="https://medium.com/anyjunk">AnyJun</a>k. This stands for <em>monadic for comprehension</em>, which is<em> </em>actually a generalisation of a <em>for </em>loop. We want something that will accept as many <em>Maybe</em>s as we need and will combine them all together with <em>flatMap</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8dce1fb49f6eb2957391e6faecccd3b/href">https://medium.com/media/f8dce1fb49f6eb2957391e6faecccd3b/href</a></iframe><p>But what if the calculation for one <em>Maybe</em> needs the value from another one? In that case we’re going to need to collect the values as we go and offer them to the next step, and we’re also going to need the things we’re passing in to be functions from the previous values to the next <em>Maybe</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/334281abad9951cac2d4ccf391b85807/href">https://medium.com/media/334281abad9951cac2d4ccf391b85807/href</a></iframe><p>In the particular example we’re using this just made it more confusing but I hope to demonstrate why this is an important feature later. Also because of the way javascript evaluates functions it’s possible to omit any arguments you aren’t using.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c15a1db70a502388f1c82a4bd13d3d71/href">https://medium.com/media/c15a1db70a502388f1c82a4bd13d3d71/href</a></iframe><p>Now that we know what we want it’s time to work out how to achieve it.</p><p>Since we’ve defined our interface for the function first it makes sense to write that out as a type so that we can check that we’re sticking to our objectives as we go.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22d8985caef15fb6df130a05da34d689/href">https://medium.com/media/22d8985caef15fb6df130a05da34d689/href</a></iframe><p>At first glance it may not be obvious how we got there from our above requirements so I’ll lead you through it. The easiest bit to understand is the final result. We need to get a <em>Maybe</em> out at the end so the final type must be a <em>Maybe</em>. Ignore the <em>T</em> for now.</p><p>The section just before that, <em>((…_: Array&lt;*&gt;) =&gt; T)</em>, is about the last section of brackets where we take the values we’ve collected from the <em>Maybe</em>s and define how to combine them into a single value. The ellipsis … is just the array spread notation which means that you can pass in separate arguments and they’re treated as an array of arguments. So <em>(a, b, c, d)</em> becomes <em>([a, b, c, d]) — </em>this is important when asking for an arbitrary number of arguments, and I prefer it over passing them in as an array. So this will accept any function from an arbitrary number of arguments to a single value. The <em>T</em> in both of these sections says that the type returned by this function will also be the type in the final <em>Maybe</em> we get out as a result.</p><p>Next, the outer part, <em>(…_: Array&lt;MaybeFunction&gt;)</em>, just says that we accept an arbitrary number of whatever this <em>MaybeFunction</em> is. The <em>MaybeFunction</em> is then defined as a function from an arbitrary number of arguments (the previous values) to a <em>Maybe</em> of a new value.</p><p>You might have noticed that the type does not constrain the types of the functions to taking the same types as the previous values, nor does it constrain us to the correct numbers of arguments. This is an unfortunate limitation of the type system as we have used it here — if you can find an improvement that restricts the types further I would be very interested to learn about it.</p><p>Now we know our type it’s time to start actually writing the function.</p><p>We know our type so we know what the arguments are going to look like. We also know that since we’re taking an array of objects and returning one object it’s probably going to require the use of <em>reduce</em>. The final thing that’s easy to see is that the last thing we do will have to be <em>map</em> using the last argument.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7fd8da0337cdb2ae104005fd36137f1/href">https://medium.com/media/e7fd8da0337cdb2ae104005fd36137f1/href</a></iframe><p>As we are collecting values we’re going to need to come out of the reduce with a <em>Maybe</em> holding an array. We want to combine the <em>Maybes</em> with <em>flatMap</em> and we want to pass the current values in as arguments and add the new value to that array of arguments for the next one. That leads us to the following function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e00350a47fad8cc67564d8352222e4c/href">https://medium.com/media/0e00350a47fad8cc67564d8352222e4c/href</a></iframe><p>And we could stop here. We have a function that could be used as we have specified. But there’s actually one further step that we could take.</p><p>A <em>Maybe</em> is actually a particular instance of something called a <em>monad</em>. I won’t get into the exact properties of a <em>monad,</em> but for our purposes it is something that has a <em>flatMap</em>, to combine them and change the value, and a <em>pure</em>, to create a new one. In the case of <em>Maybe</em> the <em>pure</em> is <em>Maybe.Some(x)</em>. From those two functions you can also always create <em>map</em> as well since it is just <em>map(f) = flatMap(pure(f))</em>. So we can also assume that every monad has a <em>map</em> function.</p><p>(Note that <em>flatMap</em> is sometimes known as <em>bind</em>, and <em>pure</em> is sometimes known as <em>unit</em> or <em>identity</em>.)</p><p>Some examples of other <em>monad</em>s include <em>Result</em>, which contains either the value or an error; <em>List</em>, and by extension array if you have access to <em>flatten</em> with which you can define a <em>flatMap</em> using <em>map</em>; and actually <em>Promise</em> as well if you were to rename <em>resolve</em> to <em>pure</em> and <em>then</em> to <em>flatMap</em>.</p><p>This means that if we know the <em>pure</em> function we can generalise our <em>mFor</em> to allow us to use it for any <em>monad</em>. The <em>pure</em> function usually exists as a static method on the class so I find it more readable to pass in the class rather than the <em>pure</em> function directly, but if you prefer to pass in the function then hopefully it is obvious how to change it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a94d2e71dfd379a6863ca310a557380/href">https://medium.com/media/1a94d2e71dfd379a6863ca310a557380/href</a></iframe><p>I also assume in the type that our <em>monad</em>s are all children of a <em>Monad</em> class. This works because we have defined our <em>monad</em>s as such and so we’ve constrained the type this way to denote that we need all the <em>monad</em>s to be the same kind of <em>monad,</em> and we expect the same kind out at the end. You can always just define them as any object that has the properties we listed above.</p><p>We do need the <em>monad</em>s to be the same type because it is not possible to use <em>flatMap</em> to combine two different types of <em>monad</em>.</p><p>So how about a real world example of how to use this <em>mFor? </em>Here is an example we use when we have a route on a map and we need to extrapolate to the rest of the route. Our map component (which will feature in a future blog post) expects a list of the origin and destination of each route.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6dffbf4e92d975ed9e51f6ef296b86f5/href">https://medium.com/media/6dffbf4e92d975ed9e51f6ef296b86f5/href</a></iframe><p>Hopefully you can see how this neatly handles protecting against undefined values, that can come from a range of different sources, with the <em>Maybe</em>s and uses the <em>mFor</em> to make it significantly more readable than it would otherwise be.</p><p>If you were to add those renamed methods to Promise as I mentioned above you could do a similar thing for multiple asynchronous API calls as part of a load function. The difference between the <em>mFor</em> and <em>Promise.all</em> in this case is that <em>mFor</em> executes the Promises one after the other and can use the results of one in the next. It can be combined with <em>Promise.all</em> where appropriate.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3eca2698ede2a6828e32d69fe31713fe/href">https://medium.com/media/3eca2698ede2a6828e32d69fe31713fe/href</a></iframe><p>Both of these functions do the same thing but the second version will execute both <em>loadCollections</em> and <em>loadCustomer</em> simultaneously, while the first does them one at a time. This can only be done if they don’t depend on each other so it still has to wait for <em>loadBooking</em> to finish since the subsequent calls use the result.</p><p>I hope this further encourages you to make use of <em>Maybe</em>s in your code now that there is an easier way of managing then. And if you are using them already, and/or are using other <em>monad</em>s, I hope this helps you improve the maintainability and readability of your code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=efe6884f2111" width="1" height="1" alt=""><hr><p><a href="https://medium.com/anyjunk/composing-maybes-in-javascript-efe6884f2111">Composing Maybes in Javascript</a> was originally published in <a href="https://medium.com/anyjunk">AnyJunk</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sequential Traverse for managing parallel resource usage in Scala]]></title>
            <link>https://medium.com/anyjunk/how-to-traverse-sequentially-a071afacc84d?source=rss----a165f627a6fc---4</link>
            <guid isPermaLink="false">https://medium.com/p/a071afacc84d</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[monads]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[James Phillips]]></dc:creator>
            <pubDate>Fri, 23 Mar 2018 12:26:04 GMT</pubDate>
            <atom:updated>2018-03-23T14:33:20.661Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to `traverse’ sequentially</h3><h4>Using free monads to control the execution of asynchronous tasks in Scala</h4><p>This blog post will assume terminology from the functional scala library Cats. It is easily applicable to Scalaz, too.</p><p>Libraries like Cats and Scalaz give you a verb ‘traverse’, for easier handling of combinations of wrapper types.</p><p>It allows you to turn something clunky like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee20d2b61cd59bbc0c7ec112ee2416f7/href">https://medium.com/media/ee20d2b61cd59bbc0c7ec112ee2416f7/href</a></iframe><p>into this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d20b97fe08a06c0499f33a1b0f7e40a/href">https://medium.com/media/9d20b97fe08a06c0499f33a1b0f7e40a/href</a></iframe><p>That is, it does the map and the sequence for you. It applies the function to the collection, and pulls the wrapper type through to the outside.</p><p>Of course it’s not limited to Future and Seq. The ‘collection’ type can be anything that is “traversable” (lists, options, matrices, tuples, trys — it’s a supertype of foldable). The inner type can be any “applicative” (future/task/IO, matrices, validated/either/disjunction, etc).</p><p>A problem arises however when your ‘collection’ is large, and your function is resource intensive. For example connecting to a database, performing a query, and disconnecting. If your collection had 100,000 elements in it, this would hammer the DB into submission and almost certainly throw an exception somewhere as every single element would try to connect at the same time (up to various pooling limitations).</p><p>The problem boils down to management of side-effects. If we <em>know</em> the side-effect is resource intensive, and we <em>know</em> the collection might be large, we can predict this problem. Ideally we’d like to be able to swap out traverse with a very similar method that guarantees the elements will be executed <em>sequentially</em> rather than in parallel. This would allow our fragile external services a much easier time of it.</p><p>Via batching, it would also allow us to manage concurrent access and keep it reasonably fast.</p><p>We could call this method sequentialTraverse. It would have (nearly) the same signature as traverse, because all it’s doing is executing side effects in a different way. It would do exactly the same thing to the types at compile time.</p><p>I say it would have “nearly” the same signature as traverse, because what we want to do — ensure sequentiality — is inherently a property of a Monad. Our ‘inner’ type will have this stricter condition of sequentiality and so must be a Monad rather than an Applicative. If there is no way to enforce the sequentiality of a series of statements (ie an asynchronous Applicative with no Monad instance) then there certainly is no way to sequentially traverse a structure with it.</p><h3>Naive approach</h3><p>Well, we can jump right in and have something that works but is pretty naive:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7a1652225cd577ffc0a5c97c251b45a/href">https://medium.com/media/a7a1652225cd577ffc0a5c97c251b45a/href</a></iframe><p>We’ve dived right in and just implemented a traverse that must be sequential, because we use flatMap inside the sequence’s foldLeft.</p><p>This works, but obviously is pretty weak since it works only on Seq and Future. It wouldn’t work on Task or IO, or even a different collection like Matrix. Ideally, we would want to be able to use sequentialTraverse in the case where we were working off an <em>arbitrary</em> monad rather than a fixed Future, just in case it <em>might</em> be an asynchronous monad.</p><h3>Slightly less naive approach</h3><p>I mentioned above that traverse works on any collection which has a Traverse instance and any wrapper type that has an Applicative instance. But we apparently can’t use the traverse method, since there’s no guarantee it’s sequential. So how would we generalise the above naive attempt?</p><p>We fold the sequence: this means we need a Foldable. We flatMap the future: this means we need a Monad. And lastly we add an element to the sequence: this means we need a Monoid.</p><p>So:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e30885180bb2799e0449037fd636c84b/href">https://medium.com/media/e30885180bb2799e0449037fd636c84b/href</a></iframe><p>This is almost verbatim just a generalisation of the naive approach.</p><p>And it works! The flatMap ensures sequentiality.</p><p>However, there’s a problem.</p><p>When we break down a collection using Foldable, there’s no guarantee we do it in the same way that the Monoid builds it back up again.</p><p>For example, the normal Foldable[Seq] uses Seq.foldLeft, and the normal Monoid[Seq] uses :+ (append). This would work, since they map onto our original attempt exactly.</p><p>But if we had a custom Monoid[Seq], which built a sequence using <em>prepend</em> rather than <em>append</em>, then our resulting traversed sequence would be in reverse order!</p><p>So this approach is no good either, there’s no guarantee it will work in all cases. There’s no connection at compile time between the way we fold a structure and the way we build the same structure back up.</p><h3>A different approach</h3><p>We need something that guarantees the breaking-down and building-up are inverses of each other.</p><p>Unfortunately, this is the Traverse typeclass itself. We seem to have come full circle; in order to traverse predictably in all situations we need to use Traverse. This appeared to be a no-go because it would greedily execute our asynchronous monads. But all is not lost yet — let’s have a look at the components we have:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4db72821487b348cb3909d7f2b962fd3/href">https://medium.com/media/4db72821487b348cb3909d7f2b962fd3/href</a></iframe><p>We know from our above explorations that traverse has to stay. So what else has changed? The only thing different from the basic traverse use-case is our M is a monad, not an applicative.</p><p>Doesn’t a monad encode sequentiality? Why can’t we just do s.traverse(f)? Won’t this be sequential because M is a monad?</p><p>Unfortunately not, because its underlying Applicative instance (all monads are applicatives) could still be parallel; there’s no guarantee it uses flatMap and therefore be sequential.</p><p>You could by all means override the underlying Applicative methods to force them to be sequential, but that is not a good user experience. Picking a Monad and hoping it’s correct or risk having your code explode. We would rather force the code to work the way we need it to with no choice to be made.</p><p>So the only thing we can do is ensure that when we traverse our s, we ‘trick’ it into <em>not</em> actually executing anything — ie make traverse lazy -<br>and then to somehow execute the result ourselves in a way that ensures sequentiality. That way, the default behaviour of traverse is fine and does not interfere with any potentially external resources.</p><p>As our s gets broken down by traverse, our new lazy construct, whatever it is, would get accumulated — and then we would take our construct apart in a sequential way which we manage ourselves.</p><p>This new object would be a series of lazy instructions built by traverse which<br>we execute afterwards.</p><h3>Free Monad</h3><p>This sounds like a free monad, which is from a certain point of view a way of encoding pure programming instructions free from any execution context. Once you have a free monad, you can execute it with any context you like.</p><p>This sounds like exactly what we need.</p><p>What is a free monad? Loosely, it’s a way of turning a Functor into a Monad, by ignoring nesting during the flatMap operation. The specifics aren’t too important: What we need is that we can get a Monad from a Functor (for free!)</p><p>So let’s quickly recap what we’re working with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e7e75f979f5a50172de655b6bdb182c/href">https://medium.com/media/5e7e75f979f5a50172de655b6bdb182c/href</a></iframe><p>One thing immediately jumps out as being a candidate for a free monad: M. It’s already a Monad, but that means it’s also a Functor. But there’s actually another option: f, our function we wish to traverse with.</p><p>Function1[X, Y] is a Functor in the second argument. Could we use this? Nearly. Remember we want this to be lazy once we apply it, and a function is anything but. As soon as it’s applied, it’s… well, applied.</p><p>Let’s invent something quickly:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d8a7b463b1a1e4d9dd17af1c0aaa341/href">https://medium.com/media/2d8a7b463b1a1e4d9dd17af1c0aaa341/href</a></iframe><p>This is a functor in X — it just delegates to Function1s instance and keeps a fixed. This type allows us to combine a function with its argument and <em>not </em>execute immediately. Sounds very promising.</p><p>Notice that I’ve included an M in the definition. This is purely because we are always working with a function that produces a monad in this blog post.<br>There’s nothing special about the M here at the moment. The class will be defined inside a context where we have M so it needn’t be on the class definition.</p><p>Now let’s create a free monad from LazyFunction:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00f93e3d4e20a4a6174c1d3e33c94cb6/href">https://medium.com/media/00f93e3d4e20a4a6174c1d3e33c94cb6/href</a></iframe><p>FreeLazyFunction is an alias to our Free type, to make the type argument lists a little nicer. lift elevates a function to our Free monad.</p><p>And… that’s all we need. FreeLazyFunction is a Monad already. So let’s use it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b1a526dab97492777936487b82429ec/href">https://medium.com/media/8b1a526dab97492777936487b82429ec/href</a></iframe><p>and we have it! We have built our series of instructions using traverse, and we haven’t yet executed anything.</p><p>We just need a way to collapse our free monad now. This is done via a mapping between our inner Free type and a Monad, <br>in this case M. This mapping is called a natural transformation and looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/239f1b993f88af2225b974f4456e740b/href">https://medium.com/media/239f1b993f88af2225b974f4456e740b/href</a></iframe><p>It’s just finally applying the LazyFunction, and executing the underlying function/argument pairs. Now we can run our free monad:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80fa1a0684eeb3194865ea7bad3f761e/href">https://medium.com/media/80fa1a0684eeb3194865ea7bad3f761e/href</a></iframe><p>The foldMap guarantees that each step of our free monad is only executed (using LazyFunction.apply) once the previous step is finished, using M.flatMap.</p><p>This is precisely what we wanted, forced sequential execution inside traverse with no extra overhead for the end developer!</p><p>The only thing left is the syntax:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a564ef2348d2a2253f90a05a02f762eb/href">https://medium.com/media/a564ef2348d2a2253f90a05a02f762eb/href</a></iframe><p>You can have a quick test that this works as expected:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e876e26959b86473d0dbac9f07a2ad7a/href">https://medium.com/media/e876e26959b86473d0dbac9f07a2ad7a/href</a></iframe><p>sequentialTraverse is minimal — it requires only a Traverse and a Monad instance, and is usable without any extra cognitive overhead or effort from future developers. It ensures sequentiality in the case of an asynchronous monad, and behaves the same as normal traverse in the case where we have a synchronous monad, making it safe for a developer to accidentally misuse, too.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a071afacc84d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/anyjunk/how-to-traverse-sequentially-a071afacc84d">Sequential Traverse for managing parallel resource usage in Scala</a> was originally published in <a href="https://medium.com/anyjunk">AnyJunk</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Demystifying the mobile app dragons]]></title>
            <link>https://medium.com/anyjunk/demystifying-the-mobile-app-dragons-91ce83aec6d?source=rss----a165f627a6fc---4</link>
            <guid isPermaLink="false">https://medium.com/p/91ce83aec6d</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Fabrizio Moscon]]></dc:creator>
            <pubDate>Thu, 08 Mar 2018 12:02:06 GMT</pubDate>
            <atom:updated>2018-03-08T12:02:05.758Z</atom:updated>
            <content:encoded><![CDATA[<h4>When mobile development is a teach/learn experience for the whole team</h4><h3>Context</h3><p>A few months ago I was asked to help <a href="https://www.anyjunk.co.uk/">AnyJunk</a> rewrite their current mobile app for partners using <a href="http://facebook.github.io/react-native/"><em>React-Native</em></a>. The key reasons for this were to a) add new features and b) bring the mobile development knowledge in house through a tight team collaboration. Based on my past experience with these kind of apps at <a href="https://www.bizzby.com/">Bizzby</a> (an on demand services app) and my exposure to <em>React-Native</em> at <a href="https://www.hoxfon.com/">Hoxfon</a> I felt I could deliver on the company’s expectations.</p><p><a href="https://medium.com/anyjunk">AnyJunk</a> is a very small but innovative award winning logistics company, with a great development team who share the same culture and values. Some pillars of the culture are the mutual respect for each other’s ideas and expertise, a healthy practice of code review and pair programming and the same passion for code quality and testing.</p><p>The server applications are written in <a href="https://www.scala-lang.org/"><em>Scala</em></a> and embrace principles of pure functional programming, leveraging the language’s strong types to enforce the correctness of the business domain logic. These principles strongly influenced the way in which the <em>React</em> web app is written. In fact the web app makes extensive use of <a href="https://monet.github.io/monet.js/">monet</a>, a standard library for functional programming in JavaScript that brings monads such as <em>Maybe, Validation, </em>and<em> Either </em>to <em>React</em>. The <em>Javascript</em> codebase is also using <a href="https://flow.org/"><em>flow</em></a><em> </em>to infer data types and code correctness.</p><p>When planning the rewrite of their service partners’ mobile app, a clear objective was to deliver the best mobile user experience. Another important goal on the agenda was to not compromise the team values, philosophy and development practices. AnyJunk wanted to allow both <em>Scala</em> and <em>React</em> developers to review the mobile app’s code, develop in an iterative way coexisting with the weekly sprints and enable automated testing and continuous releases.</p><h3>The React-Native mobile app</h3><p>After comparing a few options of mobile technologies, AnyJunk opted for <em>React-Native</em>. The fact that the team was already familiar with <em>React</em> played an important role in this choice. Meanwhile more companies were sharing their positive experience with <em>React-Native</em> and this convinced both technical and non-technical stakeholders to use it.</p><p>The app uses <a href="https://redux.js.org/"><em>redux</em></a><em> </em>to store the global app state and it can also be used offline thanks to <a href="https://github.com/redux-offline/redux-offline"><em>redux-offline</em></a> which under the hood implements a clever retry with decay strategy mechanism for <em>offline actions</em> that are stored on the device disk with <em>AsyncStorage</em> wrapped by <em>redux-persist. </em>Side effects such as HTTP requests/responses are integrated in <em>redux</em> using <em>redux-thunk</em> and <em>promises. </em>The codebase quality currently meets the highest standards. It uses <a href="https://eslint.org/"><em>eslint</em></a> to detect mistakes and nuisances<em>,</em> it is typed with <em>flow</em> and tested extensively with <a href="https://facebook.github.io/jest/"><em>jest</em></a>. These tools work well together and represent the standard tooling configuration found in many <em>React</em> and <em>React-Native</em> projects<em>. </em>In particular <em>flow</em> has proven itself very useful in two ways. Firstly, we ensured that each <em>component</em> was used with the correct <em>props</em> and <em>state</em> types. This saved us time in development and code review. Extra simulator reloads were avoided and most importantly it has ensured that the end result app was free of types bugs and typos. Secondly, it has helped create close interaction between <em>Scala </em>and <em>JavaScript</em> developers where common ground was found over the nature of strong types within technical discussions.</p><p>Below is a screen shot taken from the final app version showing a service partner job details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8qiK07RPXaMLhXFeG6oUCw.jpeg" /><figcaption>AnyJunk app screenshot</figcaption></figure><h3>Functional programming in JavaScript</h3><p>Before this collaboration I only used functional programming at university with <a href="https://ocaml.org/">OCaml</a> and whilst conducting a few personal experiments with <a href="https://reasonml.github.io/">ReasonML</a>, but I had no previous commercial exposure. At AnyJunk I have been exposed to more advanced concepts and I was taught how to code and design the internal API in a functional way using the <a href="https://github.com/folktale">folktale</a> library.</p><p>The <em>Maybe</em> is the first functional construct that I used in the app. <em>Maybes are </em>returned from a helper function called <em>safeGet</em> and provide a clean and reusable way to check the existence of <em>Object properties</em> and exposed me to another important functional concept: the distinction between <em>nullable</em> and <em>non-nullable</em> data.</p><p>To fully understand what are the pros of using <em>Maybe</em> and why they solve a very common problem found in <em>React</em> applications<em> </em>I suggest to check out <a href="https://medium.com/anyjunk/using-maybes-with-react-redux-1c47d4a09ed7">Dan Hood’s article</a> that explains in detail how <em>Maybe</em> can be used with <em>redux</em> and improve your code correctness. The following code snippets compare imperative and functional approaches to display a validated collection volume which represent the size in yards of a junk collection. I want the reader to appreciate that even though the imperative way has less lines of code it leaks basic validations into the main program forcing code repetition (lines 15–21).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84ebb4c497d951975a571d3421059494/href">https://medium.com/media/84ebb4c497d951975a571d3421059494/href</a></iframe><p>Encapsulating the validation into a function allows for code to be reused easily and makes the app more resilient to future developers’ mistakes. Note how <em>validateCollectionVolume()</em> returns a <em>Result</em> that can be used for presentation purposes inside the <em>render()</em> function of a <em>Component</em> and for business logic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d858642436a72ba4d3cdc07ef364dd6f/href">https://medium.com/media/d858642436a72ba4d3cdc07ef364dd6f/href</a></iframe><h3>The Model layer</h3><p>Another part of the <em>React-Native</em> app where I was able to learn and apply concepts of functional programming was the data bridging between the <em>redux state </em>and the <em>React Component</em> through the use of <em>models.</em> The app uses <em>models</em> to separate data validations from the presentation layer. The <em>models</em> are <em>JavaScript</em> <em>Objects</em> that use <em>Maybes</em> and <em>Results</em> to separate data validation from the <em>React Components</em> and allow the same validation to be used in both <em>Components</em> and <em>thunks.</em> The below example reuses the <em>validateCollectionVolume()</em> function to demonstrate what the purpose of the model is.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3033ae764f1c6dd30e0373bfda54e84/href">https://medium.com/media/c3033ae764f1c6dd30e0373bfda54e84/href</a></iframe><p>After I introduced this pattern in the mobile app the rest of the team and I had few brainstorming sessions to understand why this pattern was useful in the current app structure and how it could be ported and even improved in the web app codebase. Eventually this constructive sessions helped implementing an improved and <a href="https://medium.com/anyjunk/our-react-redux-structure-17bb3ed41488">novel version of this pattern</a> that uses data <em>marshalling </em>to process data between the server and the client. This is an example of cross-pollination of ideas between projects, initiated by peer code review that allowed to improve a shared code pattern through team collaboration. <em>Redux</em> and <em>functional concepts</em> established a common ground for the collaboration that helped to find this new useful pattern. This wouldn’t have happened using a completely different technology for the development of the mobile app.</p><p>Another example of team collaboration is what brought <a href="https://medium.com/@danahood">Dan</a> and I to make a few open source contributions to <a href="https://github.com/redux-offline/redux-offline"><em>redux-offline</em></a><em> </em>and to create <a href="https://www.npmjs.com/package/redux-offline-immutable-config"><em>redux-offline-immutable-config</em></a><em>,</em> a small <em>JavaScript </em>library written with functional programming concepts in mind that allows the use of <a href="https://facebook.github.io/immutable-js/"><em>immutable.js</em></a> with <em>redux-offline</em>.</p><p>We did this by extracting the methods for reading and writing to the state into a <em>lens </em>that could be passed in as part of the configuration, as well as extracting some dependencies. This way the <em>redux-offline</em> library doesn’t depend on things that it only needs when an assumption is made about the type of state it’s being used with. The <em>redux-offline-immutable-config</em> library is then a collection of the parameters and dependencies required to use <em>redux-offline</em> with an <em>immutable</em> state specifically, while the config included within <em>redux-offline</em> itself is an equivalent one for a non-immutable state.</p><h3>Conclusions</h3><p>The first phase of the project is now completed and the service partner’s app is used by all AnyJunk’s service partners every day. Almost a 100% of the code is shared between Android and iOS and thanks to the tight collaboration and code review carried on during these past months, the in-house AnyJunk team is completely confident in extending and maintaining the <em>React-Native </em>code base. During these time I have learnt a lot of new and useful concepts that I will certainly bring to my next projects.</p><p>One of the main benefits of using <em>React-Native</em> at AnyJunk was giving all developers complete visibility of the code and the pattern used. This is a clear improvement over developing Android and iOS apps in isolation, where it is is really hard to communicate and share because of the different languages and paradigms.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91ce83aec6d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/anyjunk/demystifying-the-mobile-app-dragons-91ce83aec6d">Demystifying the mobile app dragons</a> was originally published in <a href="https://medium.com/anyjunk">AnyJunk</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Typesafe Union-type folding in Scala]]></title>
            <link>https://medium.com/anyjunk/typesafe-union-type-folding-in-scala-54d2e44bdce9?source=rss----a165f627a6fc---4</link>
            <guid isPermaLink="false">https://medium.com/p/54d2e44bdce9</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[union-types]]></category>
            <dc:creator><![CDATA[James Phillips]]></dc:creator>
            <pubDate>Fri, 02 Mar 2018 12:45:05 GMT</pubDate>
            <atom:updated>2018-03-02T12:45:04.590Z</atom:updated>
            <content:encoded><![CDATA[<h4>Escaping Union type contexts safely</h4><h3>Goal and ideal syntax</h3><p>In a <a href="https://medium.com/anyjunk/union-types-ffde15fd335">previous blog post</a> I introduced a way to get unboxed arbitrary arity union types in scala. We can write</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/972cc3ece8d93e2772ed59bdf3ecf96e/href">https://medium.com/media/972cc3ece8d93e2772ed59bdf3ecf96e/href</a></iframe><p>foo allows an Int or a String to be passed to it, and nothing else.</p><p>But escaping the union context, ie getting the Int or the String out again from our generic T, wasn’t safe. We had to do this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ac460299402ecbce3c7931f2129a3e5/href">https://medium.com/media/7ac460299402ecbce3c7931f2129a3e5/href</a></iframe><p>The above is not checked by the compiler, either for exhaustivity (if we’d missed Int it wouldn’t tell us) nor precision (if we included an extra type we didn’t need it wouldn’t tell us).</p><p>But it is certainly the easiest way to naively extract our original value again.</p><p>The goal of this article is to be able to &quot;match” a union type in a typesafe manner. We’ll start from an ideal syntax, something equivalent to a normal pattern match, and work outwards from there. I warn you there are some immoral steps along the way — this is more of an experiment in scala than something to be used in production.</p><p>Requirements:</p><ul><li>compile-time exhaustivity matching</li><li>order of types should not matter</li></ul><p>Extra credit:</p><ul><li>precision matching (making sure that if we supply an unneeded case the compiler tells us)</li></ul><h3>Syntax</h3><p>At the moment our syntax is</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ac460299402ecbce3c7931f2129a3e5/href">https://medium.com/media/7ac460299402ecbce3c7931f2129a3e5/href</a></iframe><p>This is our ideal look — pretty minimal. We’re going to try and build something minimal taking this as inspiration, and which is also safe.</p><p>Our general union type is arbitrarily wide, so any construct we write to generically escape a union context must also be arbitrarily wide.</p><p>This means each case must be collapsed iteratively into an object we are constructing as we go. You can’t have a pattern match of arbitrary length after all. It’s going to look a bit like the builder pattern.</p><p>This also means that we need to call an additional run method on this object to actually execute the instruction — we need a separation between constructing our instruction and executing it. This is similar to how one builds and executes free monads, or the builder pattern.</p><p>So the minimal syntax we can hope for looks a little bit like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4cf0dd9ee02a38328e492d740827535d/href">https://medium.com/media/4cf0dd9ee02a38328e492d740827535d/href</a></iframe><p>It looks like a match turned into a builder. .match to initialise, .at to add a case, and .run to signal to the compiler that we’re done building it and return our value.</p><p>This is now looking a lot like a fold.</p><h3>VirtualPolyFunction</h3><p>This thing we’re building with the .at method is like a polymorphic function, a function from many types to one type. <a href="https://github.com/milessabin/shapeless">Shapeless</a> has them, and uses them to fold over their HLists and Coproducts. So we’re probably on the right track.</p><p>Ours is different and slightly more difficult to deal with (not that shapeless Poly is easy) because we are unboxed — we have to handle a completely free<br>type parameter T, constrained only by convention based on what type classes are in scope. I’m going to call it VirtualPolyFunction since it will act on a ‘virtual’ Union. This is my own nomenclature, apologies if ‘virtual’ means something else to you.</p><p>At each .at step, we add a new type to the VirtualPolyFunction we’re building which we have to keep track of. This means the structure of the class must either:</p><ul><li>be parameterised by a Union or HList</li><li>be arbitrarily nested, like a free monad</li></ul><p>I chose to parameterise it by a Union, since that’s what we’re dealing with. Free might give an avenue for avoiding some of the immorality inside our eventual class, but that’s something I have not yet investigated.</p><p>Further requirements for the structure of VirtualPolyFunction are:</p><ul><li>it must at some point reference our union value which we’re folding over, so must be parameterised by some completely free generic type T.</li><li>It must be parameterised by the return type.</li><li>It must also record within it the actual function we’re iteratively building up, from our free domain T to our return type.</li><li>It must have two methods: One to add a new case to the arbitrary-type-arity virtual polymorphic function, and one to execute the function when given a value.</li></ul><p>The minimal possible structure so far is thus:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/baac87acbb107b3e1c214fa658a17adb/href">https://medium.com/media/baac87acbb107b3e1c214fa658a17adb/href</a></iframe><p>A quick run-down:</p><ul><li>U is the Union our VirtualPolyFunction (VPF) is understood to act on — it needn’t yet be connected to T in any way. This will come later</li><li>T is the type of the value our VPF will act on, the union value we want to escape</li><li>Return is the eventual return type</li><li>withCase adds a new case to the VPF — note the new Union type in withCases return type — we’ve added our new type to the head.</li><li>apply is trivial, it just runs the function on a value</li><li>func is some function from a generic T to Return. This is the thing we’re trying to make safe (ie, our original naive pattern match).</li></ul><p>withCase is the only thing we need to actually implement.</p><h3>withCase</h3><p>withCase is unfortunately not nice to implement. Our current way of escaping union types</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ac460299402ecbce3c7931f2129a3e5/href">https://medium.com/media/7ac460299402ecbce3c7931f2129a3e5/href</a></iframe><p>relies on run-time matching. The compiler’s interpretation in this context of the type of union: T is plain old java Object. It takes each of these matches, sees if union: Object satisfies any of the cases and if not throws a MatchError.</p><p>Even though the compiler has lost information about T due to erasure, it still knows that not all Objects are Ints, so it encodes instructions to fall through the case match to the subsequent cases.</p><p>Unfortunately inside withCase[X] we will be matching against a generic type X, the erased type of which is also Object. Thus when we write</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61d71c5e0eee25e452c4a4137b7e5343/href">https://medium.com/media/61d71c5e0eee25e452c4a4137b7e5343/href</a></iframe><p>the compiler reads</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8a301780bd27ab00a02c7cf7ea8f702/href">https://medium.com/media/c8a301780bd27ab00a02c7cf7ea8f702/href</a></iframe><p>To the compiler this is an exhaustive match and we can not add more cases to this. At run-time, if T is not X, we get a ClassCastException — erasure has removed the information we needed.</p><p>Getting around this is the immoral bit. The implementation of withCase is</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1104298915f4225a73c765b59e1cf72b/href">https://medium.com/media/1104298915f4225a73c765b59e1cf72b/href</a></iframe><p>In words,</p><p>- Take a value of type T<br>- See if it is of type X via unsafe runtime casting<br>- If it is (ie no exception), apply our new function that withCase has just supplied<br>- If it is not (ie ClassCastException thrown) apply our previous function</p><p>In this way our final function right at the end recursively tries to type-cast our t to each type in U, catching an exception each time and falling back to the next cast if needed.</p><p>Short of using reflection, I think this is the closest analogy to an arbitrary-length pattern match on a generic there is.</p><h3>UnionFolder</h3><p>VirtualPolyFunction isn’t quite enough. The first problem is that it <strong>takes</strong> a value, where in our ideal syntax we chain methods <strong>on </strong>a value. The second is that, though each step of withCase is in theory perfectly safe given at least one previous step is safe, there’s no guarantee yet that we have at least one safe step.</p><p>In other words, we could construct a value with type VirtualPolyFunction[Int :|: UNil, String, Any], which<br>obviously could not work. We need String to be in the union type but it isn’t. So we need a wrapper layer to make it safe.</p><p>This wrapper layer has the same two method requirements as VPF. Therefore also has the same structural requirements as VPF, and must carry the union value around inside it to let us chain methods together nicely. The minimal structure is therefore:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/07b6e11e07d5567f639709ee3958bc72/href">https://medium.com/media/07b6e11e07d5567f639709ee3958bc72/href</a></iframe><ul><li>U, T and Return are still here</li><li>value: T is the union value we’re escaping</li><li>at just calls withCase</li><li>run is where the safety gets injected; before we can execute this UnionFolder, we need evidence that T belongs to U</li></ul><p>This last point is key. We can build up any VPF / UnionFolder we like, but we can’t actually run it on a T until we have evidence that the union type upon which the VPF acts actually contains T.</p><p>So the following would be unable to be run:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ffc3d6783deb275656e1df570867cbc/href">https://medium.com/media/1ffc3d6783deb275656e1df570867cbc/href</a></iframe><p>but the following would be fine:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d24f46c6e7f9eb4ad67f5d38e7fc4afb/href">https://medium.com/media/d24f46c6e7f9eb4ad67f5d38e7fc4afb/href</a></iframe><p>The implicit that run takes puts back into our construct what the compiler took with erasure: knowledge that our pattern match is safe.</p><h3>foldUnion</h3><p>The very last thing to do is construct the initial syntax. We have a way of building up and executing this object on a value, but we don’t have a way yet of elegantly attaching it <strong>to</strong> a value. I chose the name foldUnion instead of match or fold to reduce confusion and name collision.</p><p>Because our union types are completely free, and to the compiler they are just type T, we must define this method on RichT:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c8ffaae6398082e71e208491310c0ad/href">https://medium.com/media/2c8ffaae6398082e71e208491310c0ad/href</a></iframe><p>Note the signature of foldUnion: X and Y are set by the function we give it, but U is free, the compiler chooses what U is based on what union evidences we find in scope. The one requirement we have on this method is</p><ul><li>tUnion: that the value we’re calling this on must belong to some union (this condition is trivially satisfied by any type, eg Int and Int :|: UNil); but if the compiler finds one passed by a parent method, it prefers that thanks to implicit resolution order</li></ul><p>Thus tUnion is trivial, if the compiler finds no union evidence it will construct a satisfactory one itself.</p><h3>Final syntax</h3><p>That’s it! The final product looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4539ae334315ad4ac824b7abc571d4d3/href">https://medium.com/media/4539ae334315ad4ac824b7abc571d4d3/href</a></iframe><p>Looks pretty good to me.</p><p>Note:</p><ul><li>If we missed a case (ie commented out the Char line) it <em>would not compile</em></li><li>The types we chain together need <strong>not</strong> be in the same order as the union type in the method</li></ul><p>We can add in unnecessary cases (ie we could add a Boolean case in the above example) — we could make this throw a compilation error but it would take longer to compile.</p><p>We would do this by allowing a free union type parameterisation in our UnionFolder.run method, and insisting that whichever one the compiler picks from our T’s implicit OneOf is the same as VPF’s U parameter — this approach would satisfy the precision matching ‘extra credit’ mentioned above.</p><p>As I mentioned above, this was more of a thought experiment to see how far I could push my union type idea. The exception catching in the middle of VPF obviously has overheads you may wish to consider before using this approach.</p><h3>Quirks</h3><p>First: These are total functions not partial functions. If we supplied a case match with a guard it would fail instead of gracefully falling to the subsequent case if the guard failed.</p><p>Second: It is available on any type at all thanks to RichTea. The following compiles.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92f3cf38abc77f57f841f179157162f9/href">https://medium.com/media/92f3cf38abc77f57f841f179157162f9/href</a></iframe><p>It produces &quot;7&quot;. Technically, Int is isomorphic to Int :|: UNil so you could argue it’s doing the right thing by allowing this, if you wanted to. But the following would <em>not</em> compile:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62345340c1c11d00d260b4077d60ca4b/href">https://medium.com/media/62345340c1c11d00d260b4077d60ca4b/href</a></iframe><p>because we haven’t provided an Int case.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54d2e44bdce9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/anyjunk/typesafe-union-type-folding-in-scala-54d2e44bdce9">Typesafe Union-type folding in Scala</a> was originally published in <a href="https://medium.com/anyjunk">AnyJunk</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Static Sorting in Slick]]></title>
            <link>https://medium.com/anyjunk/static-sorting-in-slick-9c5d051cbfb0?source=rss----a165f627a6fc---4</link>
            <guid isPermaLink="false">https://medium.com/p/9c5d051cbfb0</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[slick]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[James Phillips]]></dc:creator>
            <pubDate>Fri, 23 Feb 2018 12:36:02 GMT</pubDate>
            <atom:updated>2018-02-23T17:11:07.367Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to pass sorting all the way through your Scala/Slick stack</h4><p>When writing our project, we wanted to pass a static sorting object all the way through our stack from the API (where it was parsed automatically) to <a href="http://slick.lightbend.com/">Slick</a>, and have the database results be sorted automatically without any extra input from the end-user (ie you, the feature-developer).</p><p>(In this post I will assume you are familiar with Slick 3 and will always have the appropriate slick implicits and classes in scope.)</p><p>It isn’t obvious how to make slick do this automatic sorting for us. Imagine this table set up:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c60a9950479a917bbda5d854d2d71c45/href">https://medium.com/media/c60a9950479a917bbda5d854d2d71c45/href</a></iframe><p>Just a table to store my dogs in. Now imagine I had a static Sorting type, and I wanted to sort a dog query by an object of type Sorting which I’d been supplied by our API:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ae177ad48dfff3f114cadb6ea85ec5f/href">https://medium.com/media/4ae177ad48dfff3f114cadb6ea85ec5f/href</a></iframe><p>That’s a pretty horrendous query. To interpret these sorting objects properly we’d have to do that on <em>every single query</em>.</p><p>If we ever added a new Sorting type we’d need to change every single dogs query to handle it correctly — and there’s no guarantee an Id sort would be interpreted the same way across any two dogs queries. There would be nothing stopping someone sneakily mapping Sorting.Id to Dog.name .</p><h3>Ideal Syntax</h3><p>So what do we actually want? As an end-user of our codebase, we want to be able to do this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/384bb33d708a274c767ae009b3aa92c2/href">https://medium.com/media/384bb33d708a274c767ae009b3aa92c2/href</a></iframe><p>We would want this, somehow, to access a pre-defined mapping of the supplied sorting to the table columns. This would ensure every dog query would interpret Sorting.Id in the same way. We will define this mapping later.</p><p>And if we have told the compiler how to sort dogs by <em>some </em>Sorting but not <em>all</em> Sortings it should gracefully ignore sortings it’s not able to handle:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c2754132e38829200972fc228ed5f75/href">https://medium.com/media/8c2754132e38829200972fc228ed5f75/href</a></iframe><p>Lastly, if we haven’t told the system how to handle any Sorting for a table, it should fail compilation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c24d329db4a51ebc9d321bffb4eb3d47/href">https://medium.com/media/c24d329db4a51ebc9d321bffb4eb3d47/href</a></iframe><p>The above shouldn’t compile since in our imagined perfect world, we’ve only created the mapping for dogs.</p><h3>Defaults and overrides</h3><p>We can imagine the situation where many of our entities all have createdAt: ZonedDateTime / updatedAt: ZonedDateTime fields, and we would<br>have corresponding Sorting objects available for use.</p><p>We don’t want to have to write this out for every table however, since it will always be the same. So we need to be able obtain common functionality either through inheritance or a typeclass interface. I chose inheritance for<br>my implementation since our tables are joined by inheritance (with a shared ancestor HasUpdatedCreated — this could easily be a typeclass instead).</p><p>And in addition to this, we must be able to override this default for any particular table we choose. Just in case dogs.createdAt is filled with nonsense data.</p><h3>Implementation</h3><p>Ok so we know what we want. How does this fit into <a href="http://slick.lightbend.com/">Slick</a>’s types?</p><p>The Sorting type mutates a query, so on the most basic level what we need is some sort of construct representing a function (Query[T, E, Seq], Sorting) =&gt; Query[T, E, Seq] (here Query is Slick’s Query type).</p><p>This is equivalent to Query[T, E, Seq] =&gt; (Sorting =&gt; Query[T, E, Seq]). To see this, curry the initial function and imagine partially applying it with the initial Query only.</p><p>But we’d actually like this to be lazily implemented. Lazy as in ‘least amount of work for future developers’. Imagine if when we added a new Sorting we had to go and add it in to each one of these mappings. It would be a nightmare, especially if we had dozens of tables. We could even add a special sorting, Sorting.Unique, which we only want to interpret on <em>one</em> table. We should be able to have these mappings gracefully fall back to no sorting at all.</p><p>So what we actually want is something which looks a little like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44ab7db9c32658bd5e0c5c3d55309a9c/href">https://medium.com/media/44ab7db9c32658bd5e0c5c3d55309a9c/href</a></iframe><p>which makes sense: If we don’t implement every sort then it’s a partial and not a total function.</p><p>But when we sort a query by a Sorting that has no implementation for our table, we don’t want to return None, we want to return an unmutated Query. So we must have two layers, the inner of which contains a partial function and<br>the outer of which contains a total function, ie the original type:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ae709988bcdf9a3ab4bb064c00cac2d/href">https://medium.com/media/3ae709988bcdf9a3ab4bb064c00cac2d/href</a></iframe><p>So let’s have a stab at it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3d1f904a3ef5db9609acd93a045eb97/href">https://medium.com/media/a3d1f904a3ef5db9609acd93a045eb97/href</a></iframe><p>TableSortingOps looks a bit crowded, but all sortedBy is doing is checking the user’s top level implementation of sort, and falling back to the hidden partialSort if it’s not defined. It then returns the original query if we still didn’t find a match.</p><p>And that’s about it. Let’s implement some default sorts:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1af70051fd73b38916391b403b71b763/href">https://medium.com/media/1af70051fd73b38916391b403b71b763/href</a></iframe><p>There’s nothing surprising there, we just define a mix-in sorter trait for a partial table.</p><p>Now let’s finally implement our Dog instance:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6b9b7f9189eb7ed4316e12a3b65b41f/href">https://medium.com/media/d6b9b7f9189eb7ed4316e12a3b65b41f/href</a></iframe><p>And now, assuming all the implicits we just defined are scope, we can finally do the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eddd38bfdc2fab80a934557c47f51d71/href">https://medium.com/media/eddd38bfdc2fab80a934557c47f51d71/href</a></iframe><p>You don’t need to worry about what sorting is or how to interpret it, it’s all done automatically in the background based on your one-time initial setup. You pass it straight from the API and let the edges of your program handle it.</p><p>What would happen if we added a new sort, Sorting.Name and did not add this to dogSorter?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddedd7b0f06dd269296e59b32b5e988a/href">https://medium.com/media/ddedd7b0f06dd269296e59b32b5e988a/href</a></iframe><p>This would ignore the sort and return dogs.result. This is equivalent to sorting in a sql query by a constant: select * from dogs order by 19;</p><p>This, to me at least, is an important separation of concerns. There are two pieces of work: Implement an API that sorts and returns dogs, and interpret sortings for dogs correctly. A bug or omission in the latter does not mean the former is incorrect, and is arguably a completely separate piece of work with different input needed from the business.</p><p>And, finally, what happens if you add a new table, cats, and do not implement a Sorter[Cats, Cat]?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33667045c197842349c22fe55ed1f961/href">https://medium.com/media/33667045c197842349c22fe55ed1f961/href</a></iframe><p>It doesn’t compile! You must implement a Sorter[Cats, Cat] before you can sort statically. It could be empty if you like, but you must at some point in the development of this cats query make the choice about how to sort.</p><p>If it instead just silently returned the query unsorted it would be confusing to work out which tables actually had a Sorter implemented; the fact sortedBy(sorting) compiles for a table is indication to you that someone has put thought how to sort the table so you don’t have to.</p><p>In other words, a prerequisite for sorting correctly is “Someone must have thought about how to sort things correctly” — which is obvious. The implicit sorter being in scope is how we tell the compiler that this requirement has been satisfied.</p><h3>Asc or Desc?</h3><p>The above has a glaring omission — you cannot specify asc or desc. Sorting.Id always goes to just the one thing.</p><p>I left it out for clarity and brevity; there are many ways you could fit the functionality in. The way we did it was to make eg Sorting.Id contain two properties asc and desc and then our sorters look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/993369b24a62429e73053b4da37c5349/href">https://medium.com/media/993369b24a62429e73053b4da37c5349/href</a></iframe><p>It’s more verbose and not too pretty, but in the case of nullable columns it’s important to have this distinction because you need to choose whether null sorts high or low, and it needn’t be opposite cases for asc and desc — it<br>all depends on the context of your model.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c5d051cbfb0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/anyjunk/static-sorting-in-slick-9c5d051cbfb0">Static Sorting in Slick</a> was originally published in <a href="https://medium.com/anyjunk">AnyJunk</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Maybes with React-Redux]]></title>
            <link>https://medium.com/anyjunk/using-maybes-with-react-redux-1c47d4a09ed7?source=rss----a165f627a6fc---4</link>
            <guid isPermaLink="false">https://medium.com/p/1c47d4a09ed7</guid>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[maybe]]></category>
            <category><![CDATA[monads]]></category>
            <dc:creator><![CDATA[Dan Hood]]></dc:creator>
            <pubDate>Fri, 16 Feb 2018 16:45:03 GMT</pubDate>
            <atom:updated>2018-04-23T15:13:46.055Z</atom:updated>
            <content:encoded><![CDATA[<h4>Moving the handling of undefined to the edges of the stack</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*S4SwKMgzxTPCF1y3cHRD4g.png" /><figcaption>Maybe combination using flatMap</figcaption></figure><p>A pervasive problem with many languages, but particularly with JavaScript, is <em>undefined</em>. You have to consistently check whether something exists every time you interact with it otherwise you will get errors. <a href="https://flow.org/">Flow</a>, and other typing systems, make this a little easier by reminding you that something might be undefined. But you still have to apply the checks everywhere. This is where some functional languages, like <a href="https://www.scala-lang.org/">Scala</a> or <a href="https://www.haskell.org/">Haskell</a>, make use of something called a <em>Maybe</em>. And at <a href="https://medium.com/anyjunk">AnyJunk</a> we apply that same philosophy to the front end.</p><p>First I will explain some features of the <em>Maybe</em> and how it operates. If you have come across, or used, <em>Maybe</em>s before then this next section might be of less interest to you, but I encourage you to read on for the later parts where I will introduce it into React-Redux.</p><p>A <em>Maybe</em> is a construct that can wrap around a potentially undefined value and help you interact with it in a smooth and predictable manner. It also convey some other benefits but I will get onto that later.</p><p>A Maybe has two sub-types: <em>None</em>, and <em>Some</em>. I find the easiest way to think of these are like an empty list, and a list containing a single element respectively:</p><ul><li>None =[]</li><li>Some(x) = [x]</li></ul><p>Importantly, the empty list still has all the same methods as the list that contains an element, but most of them will not actually change it.</p><p>The starting point is usually some helper method that will construct a <em>Maybe</em> from another type that may be null. In our web app we use <a href="https://monet.github.io/monet.js/"><em>monet</em></a> which has a <em>Maybe.fromNull</em> method that accepts a <em>value</em> that might be <em>null</em> or <em>undefined</em>; it returns:</p><ul><li><em>None</em> if <em>value</em> is undefined or null</li><li><em>Some(value)</em> if value is defined.</li></ul><p>(In our mobile app we instead use <a href="http://folktale.origamitower.com/"><em>folktale</em></a> that has a similar method: <em>Maybe.fromNullable</em>.)</p><p>In order to carry out an operation on the value inside a <em>Maybe</em> you can call <em>.map</em> on it, the same way you would to change a value inside a list. If you had a value this will change it, if you didn’t have a value everything still works but nothing changes.</p><p>When you want to actually display the value you will need to deconstruct the Maybe and get the value out if you have it. If you don’t have a value you’ll need to provide some sort of alternative. In <em>monet</em> the method for this is called <em>orSome</em>. To use it you would do something of the form: maybeNumber.orSome(0)</p><p>These three methods (<em>fromNull</em>, <em>map</em>, <em>orSome</em>) come together to mean that you construct a <em>Maybe</em> at one edge of your stack and resolve it at the other edge, passing it through as a <em>Maybe</em> and using <em>.map</em> on it in the intermediate parts. This can mean taking a value that your API may or may not provide and using <em>Maybe.fromNull</em> to wrap it up, passing it through to your component and then calling <em>.orSome</em> to decide what to display as an alternative if you don’t have it. This could still be <em>.orSome(null)</em> but, importantly, you only have to decide that right at the end, nothing before then needs to know what the alternative is.</p><p>One more thing that <em>Maybe</em>s give you that is very helpful is a <em>flatMap (</em>aka<em> chain </em>in<em> folktale)</em>. This acts like <em>map</em> but allows you to combine two <em>Maybe</em>s and it returns <em>None</em> if either of them are <em>None</em> and <em>Some</em> only if they are both <em>Some</em>. For instance:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04047137e707b27b0dfe4e1fdeeb8609/href">https://medium.com/media/04047137e707b27b0dfe4e1fdeeb8609/href</a></iframe><p>If you wanted to extend this to more values that may or may not exist you just include more <em>flatMaps</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f81a2d6dd4f75d5093c48eff51914fa/href">https://medium.com/media/1f81a2d6dd4f75d5093c48eff51914fa/href</a></iframe><p>If you are familiar with <em>flow</em>’s notation for types the types of these are as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/746de4d6512ac794b36a604af77ab8c7/href">https://medium.com/media/746de4d6512ac794b36a604af77ab8c7/href</a></iframe><p>There are further constructs that can help with all that nesting, but I will leave that for a future post as I don’t want to overly complicate matters here.</p><p>Now that we’ve discussed a bit about Maybes and how they work we need to decide where we put them in our structure. At this point I would encourage anyone who has not already done so to read <a href="https://medium.com/anyjunk/our-react-redux-structure-17bb3ed41488">my previous post</a> about structuring <em>react</em> and <em>redux</em> since I will be using some terms that I introduced there and it may help to know exactly what I mean by them.</p><p>If you use <a href="https://facebook.github.io/immutable-js/"><em>immutable</em></a> for your <em>redux</em> state then you know that when you use <em>get</em> or <em>getIn</em> you can’t rely on getting the value you expect, even with <em>flow</em> to help you. We have a helper for that called <em>safeGet</em> which is like <em>getIn</em> except it returns a <em>Maybe</em> containing the value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55307e4e77acc353a5a0edb598510c26/href">https://medium.com/media/55307e4e77acc353a5a0edb598510c26/href</a></iframe><p>If you have a fully typed <em>redux</em> state then you can store objects in it as <em>Maybe</em>s directly. This is particularly helpful for data that needs to be loaded in by an asynchronous call to the API. Indeed, on your first load of a component the data will not be there so your <em>lens</em> will not be able to return it. This means that the <em>lens</em>es that we introduced in the last post should in fact all return <em>Maybe</em>s.</p><p>The <em>getter</em> part of the <em>lens</em> will use <em>safeGet</em> to give you a <em>Maybe</em> of an immutable object and then you use map to apply the <em>marshaller</em> to that object and get a <em>Maybe</em> of your view model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8958afc339bb620617b0270f3c5b836/href">https://medium.com/media/f8958afc339bb620617b0270f3c5b836/href</a></iframe><p>If you have a normalised state there are several different objects that could fail to be present and any one of them means that you can’t build your view model. This is where flatMap can help you.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cef338397e06cba813f889abc007c860/href">https://medium.com/media/cef338397e06cba813f889abc007c860/href</a></iframe><p>Of course if the address here is optional then just use <em>map</em> instead of <em>flatMap</em>.</p><p>The <em>Form</em> decides how to handle the None case, depending on the use case:</p><ul><li>display a message telling the user that no data could be found,</li><li>or load up some defaults for creating a new entry</li></ul><p>Additionally there are certain parts of your model that may be optional and a <em>Maybe</em> might help you handle those, especially if you have a derived field that depends on several optional fields like in the above example (of a full name that depends on a first name and a last name).</p><p>In simple cases where it’s just an optional string you might want to handle it in the view model itself and resolve it to a default like ‘-’ if it’s not there. In more complex cases where an entire section of the form is irrelevant if a piece of information is not present then you can have the <em>Maybe</em> be part of your view model and resolve it in the component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34050f86030b5ba274ee3d909b9c456b/href">https://medium.com/media/34050f86030b5ba274ee3d909b9c456b/href</a></iframe><p>However it is dealt with you always need to use <em>orSome</em> as the final thing you do with your <em>Maybe</em>. <em>Maybe</em>s usually work best when you avoid using <em>orSome</em> for as long as possible.</p><p>So now we’ve seen how we get a <em>Maybe</em> out of the state, let it pass undisturbed through the <em>lens</em> and <em>marshaller</em> and then resolve it in the <em>Form</em>. We’ve also seen how we can combine <em>Maybe</em>s to form a single <em>Maybe</em>. Hopefully you have thought of some places in your code that could be simplified by using <em>Maybe</em>s.</p><p>In the <a href="https://medium.com/anyjunk/composing-maybes-in-javascript-efe6884f2111">next post</a> I discuss a helper function that we use at AnyJunk to simplify the combining of multiple <em>Maybe</em>s. Hopefully that will address any reservations you have about the complexity, level of indentation and number of brackets involved in the process.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c47d4a09ed7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/anyjunk/using-maybes-with-react-redux-1c47d4a09ed7">Using Maybes with React-Redux</a> was originally published in <a href="https://medium.com/anyjunk">AnyJunk</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[FString: String lengths at compile time]]></title>
            <link>https://medium.com/anyjunk/fstring-string-lengths-at-compile-time-81f811750e9e?source=rss----a165f627a6fc---4</link>
            <guid isPermaLink="false">https://medium.com/p/81f811750e9e</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[James Phillips]]></dc:creator>
            <pubDate>Mon, 05 Feb 2018 10:18:12 GMT</pubDate>
            <atom:updated>2018-02-05T16:25:35.748Z</atom:updated>
            <content:encoded><![CDATA[<h4>Preventing `data-too-wide’ SQL exceptions in Scala</h4><p>Most strings that you store in a traditional SQL database have a maximum length allowed. This can frequently cause problems, since this maximum length isn’t known by your code.</p><p>You might have some checks dotted around, maybe on your API or DB layer, that only sensible length strings get into your system — but you probably don’t have them for every single field. Mutating strings, such as adding two fields together, also invalidates such checks — it will likely just cause a run-time error. One of the nice things about a type checker and functional programming in particular is that it enables and encourages you to remove entire classses of exceptions from your code. It would be great to have these maximum lengths encoded in the compiler, say in some class FString[_] (F stands for ‘finite’)</p><p>Then, if your API accepts an FString[50] and your database accepts an FString[50] the compiler will do all the checks for you all the way through your system — at compile time (and at the boundaries to your program). No more run-time data-too-wide exceptions.</p><p>As a bonus you can even make the compiler do arithmetic for you, so you know you cannot add two FString[30]s together and store them in an FString[50]field. You will be made aware of this potential problem and be forced to handle it.</p><p>How would we do this?</p><h3>Nat</h3><p>Getting string lengths into the compiler requires some concept of numbers in the type system. There’s a pretty standard approach to this in libraries such as Shapeless, a type called Nat.</p><p>Nat is essentially just an HList where every constituent type is Unit. Or if you don’t know what an HListis, think of Nat as list of ‘+1’ instructions, where the length denotes the value of the number.</p><p>Eg,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40ecef561df0dbe0d3c91202caf75526/href">https://medium.com/media/40ecef561df0dbe0d3c91202caf75526/href</a></iframe><p>etc. Succ stands for ‘successor’. In the integers, this is just ‘+1’.<br>You can see that ‘3’ is just 3 levels of Succ around the base case _0. We created single values as well as the pure types above.</p><p>These are all generated by a macro in shapeless.</p><p>You can do some pretty cool stuff in the compiler with Nat, moving logic to compile-time:</p><h4>Less than</h4><p>Say we wanted a function that accepted two Nat s - one completely free and the second with a constraint that it must be less than the first. How would we do this?</p><p>We’d need a ‘less than or equal’ type class to begin with, which would act as evidence that the first type is ‘less than’ the second:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b82509218b0f83d48d8a2a9500272343/href">https://medium.com/media/b82509218b0f83d48d8a2a9500272343/href</a></iframe><p>How do we generate the instances of this typeclass? From our definition of Nat, the integer &lt; operation is translated directly into the Succ relation — _1 is less than _3 because _3 is some number of Succs wrapped around _1.</p><p>So we can write the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/479be5c34a1d0c20a3d5e83b21a4f0ab/href">https://medium.com/media/479be5c34a1d0c20a3d5e83b21a4f0ab/href</a></iframe><p>Now we can write the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/572383c5c17c222b6d4072fb19f179bc/href">https://medium.com/media/572383c5c17c222b6d4072fb19f179bc/href</a></iframe><h4>Addition</h4><p>Something else you can do with integers is add them together, so we’d like to be able to do this with Nat too.</p><p>This is a bit more complicated since we have a result to deal with — we generate a completely new Nat which we need to somehow record. Our typeclass representing Sum will then necessarily have three types, the two summands and the result.</p><p>It will use the ‘Aux’ pattern to record the result type. I’ll give a brief justification so skip this if you’re familiar with the ‘Aux’ pattern already.</p><p>We want the compiler to do the arithmetic for us. We don’t want the user (the programmer using this code) to have to do the addition themselves. But if our Sum type class was parameterised by all three types this is exactly what<br>they would need to do; they can’t say Sum[_1, _2, ???]— they would have to always know the result beforehand and always write Sum[_1, _2, _3].</p><p>So that means the result type must be an inner type to Sum (let’s call it Out for ‘output’). However if we do this then the inner type is essentially lost to the compiler, it becomes just sum.Out for some value sum, unable to be usefully compared to any other type.</p><p>To get around this problem the Aux pattern is a little trick to only reveal to the user a typeclass with just the two parameters we’re interested in, while internally maintaining the three explicit type parameters necessary to do<br>useful compile-time type comparisons. It looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47cb49718749cd6d25fe5f7c8e89a095/href">https://medium.com/media/47cb49718749cd6d25fe5f7c8e89a095/href</a></iframe><p>So we have a useful type class. How do we generate the instances? Well, in integer arithmetic there are certain axioms to do with addition:</p><pre>Identity: A + 0 = A, for all A<br>Associativity(ish): (A + 1) + B = C =&gt; A + (B + 1) = C, for all A, B</pre><p>These two are sufficient for integer addition, so all we need to do is put them into code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d40a2dea5c6f96ad115c71f211d3b76/href">https://medium.com/media/0d40a2dea5c6f96ad115c71f211d3b76/href</a></iframe><p>Note that commutativity (A + B = B + A) is a corollary of the two above and does not need its own function.</p><p>Given the above, we can now write the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8eb0d648b476fbdb468df9fe978ad85/href">https://medium.com/media/e8eb0d648b476fbdb468df9fe978ad85/href</a></iframe><h3>SparseNat</h3><p>Nat is great, but pretty cumbersome when some of your max DB string lengths are 255 (or higher). Making the compiler handle a 255-nested type won’t be quick either. The limit on my computer seems to be adding _22 to _22 !</p><p>To get around this, we standardised around a few round(ish) numbers for lengths in our DB, and rolled our own version of Nat which only handles these numbers and none in between. We called it SparseNat (because it’s sparse).</p><p>Here it is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63a5af38fa2f8db80aa51d9227bbbb05/href">https://medium.com/media/63a5af38fa2f8db80aa51d9227bbbb05/href</a></iframe><p>Note there are a few changes from Nat:</p><p>- We introduce _unbound as a way of typing an arbitrary-length string<br>- Each SparseNat carries an (optional) integer within it, representing its length<br>- Each type now has a non-empty object to go with it, so we can implicitly access the integer value it represents<br>- Succ has been renamed Jump, since we’re lurching unpredictably from number to number rather than stepping up by 1 each time<br>- Only a few numbers are represented. Feel free to implement as many as you like</p><h4>Less than</h4><p>Thankfully, Lte is the same as with Nat since it only depends on relative relationships rather than absolute relationships. So we can do this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d0d594274b3ccdf1c9acee74c2d89e4/href">https://medium.com/media/5d0d594274b3ccdf1c9acee74c2d89e4/href</a></iframe><p>and Lte works with SparseNat just as with Nat.</p><h4>Sum</h4><p>Sum is more difficult with SparseNat since we’ve lost our associativity axiom. Observe:</p><pre>Jump[_50] + _100 = _100 + _100 = _255 (minimal SparseNat &gt;= 200)</pre><p>but</p><pre>_50 + Jump[_100] = _50 + _255 = _unbound (minimal SparseNat &gt;= 305)</pre><p>so things are unfortunately more complicated.</p><p>In fact, because of the nature of SparseNat being able to implement any subset of integers you want, there is nothing we can say at all about addition, except commutativity and the identity of 0.</p><p>This means that we need to implement each addition ourselves, manually. Thankfully though this can be a lazy procedure — if you never add _25 and _50 you never have to write its Sum instance.</p><p>SparseNat‘s Sum typeclass therefore looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d29b6c49cb01224e5ebab416f534bfa/href">https://medium.com/media/0d29b6c49cb01224e5ebab416f534bfa/href</a></iframe><p>Note: in a future blog post a method for speeding up Nat compile times will be proposed and can be used as a substitute for SparseNat with no impact on the rest of your code.</p><h3>FString</h3><p>Now we finally have enough to define our FString class. It’s basically just a wrapper around String, parameterised by a SparseNat with a constructor that returns a scalaz Validation, and some<br>utility methods such as ++.</p><p>If you don’t use scalaz validation you can use any similar construct: Either, Try, \/, cats.Validated, etc.</p><p>Here it is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/07a1efce93e38ac6807fd35a72c65077/href">https://medium.com/media/07a1efce93e38ac6807fd35a72c65077/href</a></iframe><p>And that’s it. Now we can have models which have accurate length limitations on their string values, and take care of all validation at the extremes of our program: the API and the database layer. An FString field in a JSON case class would fail the decoding if the string were too long, immediately kicking out a user with a 400. Similarly with the database type mappers.</p><p>And if we tried to insert an FString[_50] from the API into an FString[_25] field in our database, our program would not compile. We’d be forced to either shrink the API field or widen the database field in order to handle the potential exception.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81f811750e9e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/anyjunk/fstring-string-lengths-at-compile-time-81f811750e9e">FString: String lengths at compile time</a> was originally published in <a href="https://medium.com/anyjunk">AnyJunk</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Guide to Separating State and Form with React and Redux]]></title>
            <link>https://medium.com/anyjunk/our-react-redux-structure-17bb3ed41488?source=rss----a165f627a6fc---4</link>
            <guid isPermaLink="false">https://medium.com/p/17bb3ed41488</guid>
            <category><![CDATA[flow]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-redux]]></category>
            <dc:creator><![CDATA[Dan Hood]]></dc:creator>
            <pubDate>Tue, 30 Jan 2018 17:20:23 GMT</pubDate>
            <atom:updated>2018-02-02T09:35:49.394Z</atom:updated>
            <content:encoded><![CDATA[<h4>Our structure at AnyJunk</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tZECQ6AUzdnfJJ95tPf7vA.jpeg" /></figure><p>A problem that often arises with JavaScript is that it can become very tangled and unclear when there are no clear rules for how to structure it. At <a href="http://www.anyjunk.co.uk/">AnyJunk</a> we recently undertook an effort to improve the structure of our JavaScript code to render it more maintainable. The only technologies that this approach requires are <strong>react</strong>, <strong>redux</strong>, <strong>react-redux</strong> and <strong>flow</strong>. This can help when using libraries like <strong>immutable</strong> and <strong>redux-form</strong>, but is of use in any application that needs to talk to an API. At AnyJunk we make use of a whole range of other libraries — which will be part of future blog posts — to improve upon this structure, but none of them are necessary to understand how it works, so I shan’t be introducing them today.</p><p>The AnyJunk web app mainly revolves around forms that receive and send data to a RESTful backend API. This post will focus on the typical case of a form that must load in some existing data, make changes to it, and then send it back to the API. But the form in this example can be interchanged with any component. We will also currently be focusing on the flow of information and will not go into details about the form or the API calls.</p><p>The cycle is started when the form component is mounted and the <em>componentDidMount</em> lifecycle method is called which, via dispatching an action, causes a GET request to the API. The API response is then stored into the global app state via a reducer, much as it is in any redux application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rCR1M3YhviFy78_ruPp6NQ.jpeg" /><figcaption>Initial load</figcaption></figure><p>The interesting bit is then how the form receives this information. Our first concern is that the form itself should not have any knowledge of where the data was stored in the state, or in what shape it was stored in. The form only needs to receive the data that it needs to display and edit. Each aspect of this separation is facilitated by a separate concept, a <em>lens</em> and a <em>marshaller</em> respectively.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BvFe9hZPcoHq6LEyGReNqw.jpeg" /><figcaption>Transforming the state into a usable view model</figcaption></figure><p>A <em>marshaller</em> is an object that describes the transformation in shape from the object that the state holds, i.e. what came from the API’s get request, to the shape that the form requires, the view model. This process is called <em>unmarshalling</em>. We use flow to strongly type the view model so that we can confirm from this point onward that it is always the correct shape.</p><p>A <em>lens</em> is something that knows where the object we want is stored in the state. It also makes use of the <em>marshaller</em> to transform this object, ensuring that the form receives exactly the object it expects just by calling the <em>lens</em> as part of the <em>mapStateToProps</em> method in connect.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PSFNog5TKcgiZDhawLkI2A.jpeg" /><figcaption>Getting the view model into the form</figcaption></figure><p>When the form is complete it needs to send the data back to the API. When the submit method of the form is called we use a <a href="https://github.com/hapijs/joi">Joi</a> schema to validate the inputs before converting the shape from that which the form was using to one that the API expects. This is a process referred to as <em>marshalling</em> and is the other part of the <em>marshaller</em>’s responsibility. Our <em>marshallers</em> in full look like the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1fb632869a49dc8271a02ce9408a6ac9/href">https://medium.com/media/1fb632869a49dc8271a02ce9408a6ac9/href</a></iframe><p>Then the <em>lens</em> would look like the example below. I’ve excluded all the imports so that it is clearer, since they are just from the example above.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a8fc05ff6f0335d461002e68b1dcc45/href">https://medium.com/media/5a8fc05ff6f0335d461002e68b1dcc45/href</a></iframe><p>This can then be used in an example form like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f01c805db873e1dfd836bc442549ba83/href">https://medium.com/media/f01c805db873e1dfd836bc442549ba83/href</a></iframe><p>At AnyJunk, our state is an Immutable Map leading to it being very loosely typed. If your state is strongly typed you may have an additional type that defines how the object looks in the state. The reason for separating the getter from the rest of the <em>lens</em> is because you may need to re-use getters separately if you normalise your state (<a href="https://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html">Normalizing State Shape</a>), which we do at AnyJunk.</p><p>If you have an object that gets re-used in other objects you can also re-use the <em>marshaller</em> for that object in other <em>marshallers</em>. For example we have a standard price object that we <em>marshal</em> and <em>unmarshal</em> in the same way, regardless of what other object it is within.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7555c3df690a82be892855819050fc8/href">https://medium.com/media/c7555c3df690a82be892855819050fc8/href</a></iframe><p>Once the object has been marshalled it can be sent to the API via an action.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tZECQ6AUzdnfJJ95tPf7vA.jpeg" /><figcaption>Returning the altered object to the API</figcaption></figure><p>We group all of these elements into three files based on their responsibilities. If it operates on the state directly it belongs in <em>state.js;</em> if it involves the shape of the view model it belongs in <em>model.js;</em> and the form itself, and its connection functions, belong in <em>Form.jsx</em>. This means that the <em>lens</em>, actions and reducer all belong in <em>state.js</em>. And the flow types, validation schema and <em>marshaller</em> all belong in <em>model.js</em>. This method of separation ensures that there are no circular dependencies: <em>state.js</em> depends on <em>model.js</em> and <em>Form.jsx</em> depends on both other files, but <em>state.js</em> does not depend on <em>Form.jsx</em> and <em>model.js</em> doesn’t depend on either of the others.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bWPXhC1-NYu5gxqdWB1iSA.jpeg" /><figcaption>The breakdown of the cycle by file</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/367/1*NBH06QabM5HyyY8l6PWxCQ.png" /><figcaption>A snippet of the AnyJunk file structure</figcaption></figure><p>This separation of concerns leads to significantly more straightforward testing. The tests of our API all produce example JSON and that is then stored in a <em>resources.js</em> file (which gets ignored by jest) for the front end tests to make use of.</p><p>The file <em>model.spec.js</em> then holds tests that ensure that the <em>marshaller</em> behaves correctly converting that API data to an expected view model and back to an API resource (not necessarily the same resource as an API may provide different data to that which can be sent back). We also test that the schema validates the view model as we expect. In this manner we can have a high degree of certainty that the form will display the correct information in the correct places without having to mount a mock component in our tests and interrogate it.</p><p>Finally <em>state.spec.js</em> holds tests that ensure that the actions and reducer put the object into the state and the <em>lens</em> can get it out again as expected. In this way we ensure that our form will be receiving the data correctly if our API provides it.</p><p>So to summarise our file structure looks like this:</p><p><strong><em>model.js</em></strong> contains:</p><ul><li>flow types for the view model and API resource</li><li>Joi schema for validating the user inputs</li><li><em>marshaller</em> to convert to and from the view model, which may use other <em>marshallers</em> to convert smaller parts of the object</li></ul><p><strong><em>state.js</em></strong> imports the types and <em>marshaller</em> from <em>model.js</em> and contains:</p><ul><li>actions for interacting with the API via GET, POST and PUT</li><li>reducer to handle those actions</li><li><em>lens</em> to get the object out of the state, which uses the <em>marshaller</em> to convert it to the expected type</li></ul><p><strong><em>Form.jsx</em></strong> imports the <em>lens</em> and actions from <em>state.js</em> and imports the types and schema from <em>model.js</em> and contains:</p><ul><li>The form with a componentDidMount that calls the GET action</li><li>mapStateToProps which uses the <em>lens</em> to get the required object out of the state</li><li>mapDispatchToProps which injects the get and submit actions</li></ul><p>I hope that this has been helpful. In future posts I will go into more detail about some of the other libraries and structures that we use in relation to this to further improve it.</p><p>This post and examples are written using the following library versions:</p><ul><li>flow-bin: 0.57.2</li><li>react: 15.5.4</li><li>redux: 3.5.2</li><li>react-redux: 4.4.5</li><li>redux-form: 7.2.0</li><li>immutable: 4.0.0-rc.3</li><li>joi-browser: 10.6.0</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17bb3ed41488" width="1" height="1" alt=""><hr><p><a href="https://medium.com/anyjunk/our-react-redux-structure-17bb3ed41488">A Guide to Separating State and Form with React and Redux</a> was originally published in <a href="https://medium.com/anyjunk">AnyJunk</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Union Types]]></title>
            <link>https://medium.com/anyjunk/union-types-ffde15fd335?source=rss----a165f627a6fc---4</link>
            <guid isPermaLink="false">https://medium.com/p/ffde15fd335</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[union-types]]></category>
            <category><![CDATA[scala]]></category>
            <dc:creator><![CDATA[Daniele Turi]]></dc:creator>
            <pubDate>Fri, 19 Jan 2018 16:06:08 GMT</pubDate>
            <atom:updated>2018-06-22T14:42:47.466Z</atom:updated>
            <content:encoded><![CDATA[<h4>by <a href="https://medium.com/@phillips101">James Phillips</a></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*8YjgCtBKmXm9t79-qt75SA.png" /></figure><p>A union type is something which can be realised as one of many (usually two) types. <strong>Either</strong> is an example in scala — <strong>Either</strong> can be <strong>Left[A]</strong> or <strong>Right[B]</strong>— a value of either type can be used in a place where we ask for an <strong>Either[A,B]</strong>.</p><p>However, this is boxed so it’s a bit clunky to use. Ideally we want to be able to drop an <strong>A</strong> or a <strong>B</strong> directly into the space occupied by <strong>Either[A,B]</strong> without having to wrap it in <strong>Left</strong>/<strong>Right</strong> first. It’s clunky enough doing this<br>once but if you had nested <strong>Either</strong>s it would get tedious very quickly.</p><p>Miles Sabin came up with a <a href="https://milessabin.com/blog/2011/06/09/scala-union-types-curry-howard">great way</a> to represent unboxed union types in scala, a long time ago now.</p><p>His final example is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c38b5716ea1ec96e9c2027c0c5595409/href">https://medium.com/media/c38b5716ea1ec96e9c2027c0c5595409/href</a></iframe><p>One thing I will repeat here from the blog post is the unfortunate state of escaping the union context. When you have a value <strong>t: T </strong>which is understood by the compiler to be <strong>Int or String</strong>, how do you actually get the <strong>Int</strong> or the <strong>String</strong> out?</p><p>The easiest way is to pattern match like in the example given:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48b90952a29d329d5e0efa95efe9d212/href">https://medium.com/media/48b90952a29d329d5e0efa95efe9d212/href</a></iframe><p>This is equivalent to what we might do with an `Either[Int, String]`:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38ae6cb9666354dbc3258c1f41aa193c/href">https://medium.com/media/38ae6cb9666354dbc3258c1f41aa193c/href</a></iframe><p>But unfortunately due to erasure in the first example we lose the compiler’s ability to check the pattern match. If we forget a case, our code will just fail. If we add a case which cannot be reached, the compiler will not warn us about it. We will not be solving this problem (yet) — I am just pointing it out as a drawback to unboxed union types in scala.</p><h3>Arbitrary Arity</h3><p>One downfall with the approach in Miles’ blog post is the fact you’re stuck with just two values in the union. What if we wanted <strong>A</strong> or <strong>B</strong> or <strong>C</strong>? What if we wanted an arbitrarily wide union type? How would you achieve that without defining <strong>Union3</strong> and <strong>Union4</strong>, etc?</p><p>First of all, we must be aware of additional concerns with nested unions that do not exist when dealing with unions of only two types:</p><p>- <strong>Union[A, B]</strong> should be able to be used in place of a <strong>Union[A, B, C]</strong> (if <strong>T</strong> is <strong>A</strong> or <strong>B</strong> it is certainly <strong>A</strong> or <strong>B</strong> or <strong>C</strong>!)<br>- <strong>Union[A, A, B]</strong> should be able to be used in place of a <strong>Union[A, B]</strong></p><p>Our first attempt looked a lot like Miles’ approach, just nested. So one might end up with something that looked a little like this:</p><p><strong>Union[Union[Union[G, H], K], Union[B, C]]</strong></p><p>which would be read as ‘G or H or K or B or C’.</p><p>But that was very noisy, and bug-ridden. The implicits required to generate it were constructive:</p><ul><li><em>Commutativity</em>: <strong>Union[A, B]</strong> =&gt; <strong>Union[B, A]</strong></li><li><em>Associativity</em>: <strong>Union[A, Union[B, C]]</strong> =&gt; <strong>Union[Union[A, B], C]</strong></li><li><em>Point</em>: <strong>A</strong> =&gt; <strong>Union[A, T]</strong> for any <strong>T</strong></li></ul><p>Because these are constructive there are a lot of different ways of combining them to prove the following two types are the ‘same’:</p><ul><li><strong>Union[Union[Union[G, H], K], Union[Union[H, U], C]]</strong></li><li><strong>Union[Union[Union[Union[C, G], H], K], U]</strong></li></ul><p>This led to a lot of diverging implicit expansion compile-time errors in unexpected places. It just wasn’t working, so we adjusted our thinking.</p><p>All those repeated ‘unions’ and brackets are just fluff; implementation details. What we actually want to do is represent the union types as a Set of types: we have this collection of types and we’re ignoring duplicates and the order does not matter.</p><p>A true set of types is difficult, since in any implementation (afaik!) there will be a natural order of types imposed by the compiler. But a list is easy, it’s just an <strong>HList</strong>. As long as our type classes and code around our <strong>HList</strong> implementation ignore repetitions and order, it should represent precisely what we want to represent.</p><h3>HList approach</h3><p>So here’s a basic <strong>Union</strong> implementation, built exactly the same way as an <strong>HList</strong> (but just type-level only, no values allowed)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4c72f9408426dc24e42b7c3bf3bef35/href">https://medium.com/media/b4c72f9408426dc24e42b7c3bf3bef35/href</a></iframe><p>A union type would end up looking like this:</p><ul><li><strong>Int :|: String :|: Boolean :|: UNil</strong></li></ul><p>— easily extendable to an arbitrary number of types.</p><p>Now we just have to be able to use and interpret it. The first thing we did was decide on what we wanted our user-interface to be.<br>We chose the following type class:</p><ul><li><strong>trait OneOf[T, U &lt;: Union]</strong></li></ul><p>which represents evidence that <strong>T</strong> satisfies the union <strong>U</strong> (that is, <strong>T</strong> is one <strong>U</strong>’s constituent types). If we have one of these objects in scope we can proceed and we know that our eventual pattern match will be safe even if the compiler sadly doesn’t.</p><p>A function that uses it might look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ad6b9e1368d954f13930fed9a5454ea/href">https://medium.com/media/5ad6b9e1368d954f13930fed9a5454ea/href</a></iframe><p>Our implicit functions generating <strong>OneOf</strong> instances would only let such a <strong>OneOf</strong> be generated in the case of <strong>T</strong> being <strong>Int</strong> or <strong>String</strong>.<br>So what are these implicits?</p><p>We need some utilities first.</p><h4>MemberOf</h4><p>First, a way of telling if a type <strong>T</strong> is a member of a union <strong>U</strong>. We do this with the following type class:</p><ul><li><strong>trait MemberOf[T, U &lt;: Union]</strong></li></ul><p>If we have an implicit in scope with this type, we understand it to mean ‘T is a member of U’</p><p>We generate these implicit values below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a9c2f156807cb9e0d5362b08a73bd53/href">https://medium.com/media/4a9c2f156807cb9e0d5362b08a73bd53/href</a></iframe><p>This covers all cases for <strong>MemberOf</strong>.</p><h4>SubUnionOf</h4><p>Our next utility type class is a way of proving that a certain <strong>Union</strong> is a subunion of another — that is, every constituent type of the first appears in the second. We do this with the following trait:</p><ul><li><strong>trait SubUnionOf[U &lt;: Union, T &lt;: Union]</strong></li></ul><p>If we have an implicit object in scope with type <strong>SubUnionOf[A, B]</strong> it acts as proof that every member of <strong>A</strong> is also a member of <strong>B</strong>, and that we can freely use an <strong>A</strong> union in place of a <strong>B</strong> union. We generate these implicit values as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/400c8410c3f87bf40786faad37a92c1a/href">https://medium.com/media/400c8410c3f87bf40786faad37a92c1a/href</a></iframe><p>It’s a little bit more complicated but if you ignore the syntax bloat and read it as logical implication, as in my comments, it’s not too bad.</p><p>Note that we have <strong>(A :|: A :|: UNil) SubUnionOf (A :|: UNil)</strong> available for any <strong>A</strong>, thanks to our <strong>MemberOf</strong> type class. This takes care of our ‘repeated type’ requirement. Also note that because we ask for each type’s membership proof individually, the order does not matter either. We have</p><p><strong>(A :|: B :|: UNil) SubUnionOf (B :|: A :|: UNil)</strong></p><p>for any types <strong>A</strong> and <strong>B</strong>.</p><h4>OneOf</h4><p>Finally, we have enough to construct our <strong>OneOf</strong> implicit functions.</p><p>There are two cases we need to construct a <strong>OneOf</strong> for:</p><ul><li>The case where we’re using a type we know about (simple <strong>MemberOf</strong> checking).</li><li>The case where we’re trying to use a union type in place of another union type (<strong>SubUnion</strong> checking).</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f34460f1d4c1d6f2616c9b98f3631cfb/href">https://medium.com/media/f34460f1d4c1d6f2616c9b98f3631cfb/href</a></iframe><p>In words, if we have an existing <strong>OneOf[T, U]</strong> for a certain generic type <strong>T</strong> and some<br>union <strong>U</strong>, we can repurpose it into another <strong>OneOf[T, X]</strong> if we can squeeze <strong>U</strong> into <strong>X </strong>via our utility type classes.</p><h4>Syntax</h4><p>One last thing is the syntax for use. It’s a bit annoying to have a <strong>OneOf</strong> implicit parameter explicitly passed around, we’d ideally like it more obvious that our generic <strong>T</strong> is constrained to be a union type.</p><p>Thankfully we can do the same trick Miles Sabin does in his blog post, and have a type alias:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd68eb77f349e00d8608ee99dbaff64e/href">https://medium.com/media/dd68eb77f349e00d8608ee99dbaff64e/href</a></iframe><p>Now instead of writing<br>- <strong>def foo[T](t: T)(implicit unionEv: OneOf[T, Int :|: String :|: Boolean :|: UNil]): Any = ???</strong></p><p>we can write<br>- <strong>def foo[T: OneOf[Int :|: String :|: Boolean :|: UNil]#l](t: T): Any = ???</strong></p><p>which makes it a bit nicer to read and explain what’s going on with <strong>T</strong>. It’s a shame about the lambda <strong>l</strong> hanging around, but it is what it is.</p><h4>Final code</h4><p>Our final code looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5508a48ddf7540302bc2cc12c6c4dd46/href">https://medium.com/media/5508a48ddf7540302bc2cc12c6c4dd46/href</a></iframe><p>It all works as expected!</p><h4>Restriction</h4><p>Note that because we’re working with an unboxed union type, we can’t use it as a real type. We can’t have a <strong>List</strong> of our union types, or have a function return one. They exist purely as flags in the compiler, interpreted by us<br>in our implicit methods generating our utility type classes.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ffde15fd335" width="1" height="1" alt=""><hr><p><a href="https://medium.com/anyjunk/union-types-ffde15fd335">Union Types</a> was originally published in <a href="https://medium.com/anyjunk">AnyJunk</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>