<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Idle thoughts of a fellow developer</title>
    <description></description>
    <link>https://smaspe.github.io/</link>
    <atom:link href="https://smaspe.github.io/feed.xml" rel="self" type="application/rss+xml" />
    <pubDate>Wed, 08 Apr 2026 10:18:40 +0000</pubDate>
    <lastBuildDate>Wed, 08 Apr 2026 10:18:40 +0000</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>Understand centering</title>
        <description>&lt;p&gt;Once in a while, you stumble upon an article like this. &lt;a href=&quot;https://tonsky.me/blog/centering/&quot;&gt;Hardest Problem in Computer Science: Centering Things - Tonsky&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It contains all you need to know to realize that, centering things is much, much harder than you thought.
There are so many moving pieces, it’s no wonder no-one seems to get it right.&lt;/p&gt;

&lt;p&gt;Anyway. I regularly refer to this article, so I figured I’d keep a link to it somewhere.&lt;/p&gt;
</description>
        <pubDate>Thu, 23 Jan 2025 00:00:00 +0000</pubDate>
        <link>https://smaspe.github.io/2025/01/23/centering-link.html</link>
        <guid isPermaLink="true">https://smaspe.github.io/2025/01/23/centering-link.html</guid>
        
        <category>links</category>
        
        
      </item>
    
      <item>
        <title>TypeScript is lying to you (part III)</title>
        <description>&lt;h1 id=&quot;context&quot;&gt;Context&lt;/h1&gt;

&lt;p&gt;See the &lt;a href=&quot;/the%20lies%20of%20typescript/2023/10/19/typescript-types-ii.html&quot;&gt;previous post&lt;/a&gt; for a bit of context. In a nutshell, TypeScript pretends to be typed, but falls short in many cases.&lt;/p&gt;

&lt;h2 id=&quot;what-is-this&quot;&gt;What is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt;?&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; is a reserved keyword in JavaScript and TypeScript that refers to a few different things. In most cases, the expected behavior is that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; is the object that contains or owns the function you’re calling.&lt;/p&gt;

&lt;p&gt;Typically:&lt;/p&gt;

&lt;div class=&quot;language-ts highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Library&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;books&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;book&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;books&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;book&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;book&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;books&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;book&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print&lt;/code&gt; function, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; is expected to be an instance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Library&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&quot;the-issue-with-this&quot;&gt;The &lt;strong&gt;issue&lt;/strong&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt;&lt;/h1&gt;

&lt;p&gt;I write “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; is expected to be” because that’s the issue we’re exploring today. Coming from many other languages, like Java, Python, even PHP, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; looks like a familiar staple of OOP: the current instance.&lt;/p&gt;

&lt;p&gt;The expectation is strong enough that any IDE will happily tell you that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Library&lt;/code&gt;, and that even the compiler will insist that it is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Library&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, that’s not always the case (!).&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; really is either:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;the object this function is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bound&lt;/code&gt; to (using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fn.bind&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;the object this function is &lt;em&gt;called from&lt;/em&gt; (using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;object.fn()&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;window&lt;/code&gt;, depending on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strict&lt;/code&gt; mode. Let’s assume we’re all reasonable and running in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strict&lt;/code&gt; mode from now on.)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;example&quot;&gt;Example&lt;/h2&gt;

&lt;p&gt;A common pattern in, e.g. Java since Java 8, is to store function references to call them later, for example as a callback.&lt;/p&gt;

&lt;div class=&quot;language-ts highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;grabABook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;book&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;book&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;foobar&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;book&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;library&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Grab a book, and add it to the library&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;grabABook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s remember that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;library.add&lt;/code&gt; is simply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this.books.append(book)&lt;/code&gt;. The result is an Error: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this is undefined&lt;/code&gt; (because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt;!).&lt;/p&gt;

&lt;h2 id=&quot;but-why&quot;&gt;But why?&lt;/h2&gt;

&lt;p&gt;As we’ve seen, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; is not really just the instance on which the function is being called. It depends on binding, and on &lt;em&gt;how&lt;/em&gt;  the function is called. Like the actual syntax used to call the function.&lt;/p&gt;

&lt;p&gt;When calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;callback(book)&lt;/code&gt;:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;The function is not bound&lt;/li&gt;
  &lt;li&gt;There is no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;object.&lt;/code&gt; before the function name&lt;/li&gt;
  &lt;li&gt;Therefore, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;how-to-fix-it&quot;&gt;How to fix it&lt;/h1&gt;

&lt;p&gt;The second way of finding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt;, related to the calling syntax, is, in my opinion, ridiculous (&lt;a href=&quot;https://github.com/Microsoft/TypeScript/wiki/&apos;this&apos;-in-TypeScript&quot;&gt;see here if you still want to know&lt;/a&gt;). Instead let’s focus on binding the function so that it belongs correctly to its object.&lt;/p&gt;

&lt;h2 id=&quot;explicit-bind&quot;&gt;Explicit bind&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grabABook(library.add.bind(library));&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I find this verbose, confusing, and easy to miss. It’s also possible to do this in the class constructor: manually bind all the function to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Note that if you bind a function, you can also re-bind it later. Which is even more confusing. If you think of binding a function, please think again.&lt;/p&gt;

&lt;h2 id=&quot;auto-bind&quot;&gt;Auto bind&lt;/h2&gt;

&lt;p&gt;This is a little more interesting: use the arrow notation for a function&lt;/p&gt;

&lt;div class=&quot;language-ts highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;book&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;books&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;book&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Arrow functions are always auto-bound to their object, and so the ambiguity is removed&lt;/p&gt;

&lt;h1 id=&quot;why-does-it-matter&quot;&gt;Why does it matter&lt;/h1&gt;

&lt;p&gt;The compiler, any smart IDE, and common sense expects a certain behaviour (but one should now know not to rely on common sense when it comes to JS or anything related to it). This sort of error is surprising and highly confusing.&lt;/p&gt;

&lt;p&gt;At the very least, the type verification in the TypeScript compiler should not lie.&lt;/p&gt;

&lt;h1 id=&quot;references&quot;&gt;References&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;https://www.typescriptlang.org/docs/handbook/2/classes.html#this-at-runtime-in-classes&lt;/li&gt;
  &lt;li&gt;https://github.com/Microsoft/TypeScript/wiki/’this’-in-TypeScript&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Mon, 26 Aug 2024 00:00:00 +0000</pubDate>
        <link>https://smaspe.github.io/the%20lies%20of%20typescript/2024/08/26/typescript-types-iii.html</link>
        <guid isPermaLink="true">https://smaspe.github.io/the%20lies%20of%20typescript/2024/08/26/typescript-types-iii.html</guid>
        
        <category>typescript</category>
        
        
        <category>The lies of TypeScript</category>
        
      </item>
    
      <item>
        <title>TypeScript is lying to you (part II)</title>
        <description>&lt;h1 id=&quot;context&quot;&gt;Context&lt;/h1&gt;

&lt;p&gt;See the &lt;a href=&quot;/the%20lies%20of%20typescript/2023/10/17/typescript-types.html&quot;&gt;previous post&lt;/a&gt; for a bit of context. In a nutshell, TypeScript pretends to be typed, but falls short in many cases.&lt;/p&gt;

&lt;h1 id=&quot;the-issue-with-indexable-types&quot;&gt;The &lt;strong&gt;issue&lt;/strong&gt; with indexable types&lt;/h1&gt;

&lt;p&gt;TypeScript supports a few indexable types. By indexable, I mean that we can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[index]&lt;/code&gt; notation to get a value.&lt;/p&gt;

&lt;p&gt;Those types are, well, typed, and the type of the value returned by the index notation is known.&lt;/p&gt;

&lt;p&gt;But it is not correct, in many cases.&lt;/p&gt;

&lt;h2 id=&quot;examples&quot;&gt;Examples&lt;/h2&gt;

&lt;h3 id=&quot;recordstring-string&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Record&amp;lt;string, string&amp;gt;&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;A very common thing to see:&lt;/p&gt;

&lt;div class=&quot;language-ts highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;material&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Record&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Fifer&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Straw&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Fiddler&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Wood&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Practical&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Bricks&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;other&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;material&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Wolf&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;other&lt;/code&gt; is of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string&lt;/code&gt;. But its value is actually &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt;. Its correct type is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string | undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In other languages, requesting an invalid index throws an error.&lt;/p&gt;

&lt;p&gt;Note that it’s not the case when using finite types for the keys:&lt;/p&gt;

&lt;div class=&quot;language-ts highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Pigs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Fifer&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Fiddler&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Practical&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// This is invalid because the record is missing 2 keys&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;material&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Record&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Pigs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Fifer&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Straw&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// This is invalid because &quot;Wolf&quot; is not part of the record key type&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;other&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;material&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Wolf&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[]&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;Arrays suffer the same problem. Consider:&lt;/p&gt;

&lt;div class=&quot;language-ts highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;materials&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Straw&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Wood&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Bricks&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;mat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;materials&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here as well, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mat&lt;/code&gt; is of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string&lt;/code&gt;, but really is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt;. An error indicating that the index is out of bound would make this error easier to debug.&lt;/p&gt;

&lt;p&gt;Note that when using constants, TypeScript is more strict about the inferred type, and can detect index issues:&lt;/p&gt;

&lt;div class=&quot;language-ts highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;materials&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Straw&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Wood&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Bricks&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// This is invalid, because `materials` is now a tuple of size 3&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;mat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;materials&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;what-about-at&quot;&gt;What about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.at()&lt;/code&gt;?&lt;/h2&gt;

&lt;p&gt;Arrays have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.at()&lt;/code&gt; function, which types the output as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string | undefined&lt;/code&gt;. (The function also has other uses, such as negative indices). That’s a little more honest, but arguably a little less practical.&lt;/p&gt;

&lt;p&gt;Records don’t have such a thing.&lt;/p&gt;

&lt;h1 id=&quot;how-to-fix-it&quot;&gt;How to fix it&lt;/h1&gt;

&lt;p&gt;TypeScript has lots of options in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tsconfig&lt;/code&gt;. The one relevant for this case is &lt;a href=&quot;https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--noUncheckedIndexedAccess&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Enabling this option will make indexed access return optional values. I wish TypeScript would just have this enabled by default.&lt;/p&gt;

&lt;h1 id=&quot;why-does-it-matter&quot;&gt;Why does it matter&lt;/h1&gt;

&lt;p&gt;For &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Record&lt;/code&gt;, it matters because depending on the type of the key, it behaves differently.&lt;/p&gt;

&lt;p&gt;For limited types, like unions of literals, it strictly enforces that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Record&lt;/code&gt; can only have those keys, but must have all of them.&lt;/p&gt;

&lt;p&gt;For unlimited types, like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;number&lt;/code&gt;, any number of keys is ok, and all values are assumed to have the right type.&lt;/p&gt;

&lt;p&gt;It’s confusing.&lt;/p&gt;

&lt;p&gt;Next time: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; is not always what you think &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; is.&lt;/p&gt;
</description>
        <pubDate>Thu, 19 Oct 2023 00:00:00 +0000</pubDate>
        <link>https://smaspe.github.io/the%20lies%20of%20typescript/2023/10/19/typescript-types-ii.html</link>
        <guid isPermaLink="true">https://smaspe.github.io/the%20lies%20of%20typescript/2023/10/19/typescript-types-ii.html</guid>
        
        <category>typescript</category>
        
        
        <category>The lies of TypeScript</category>
        
      </item>
    
      <item>
        <title>TypeScript is lying to you (part I)</title>
        <description>&lt;h1 id=&quot;context&quot;&gt;Context&lt;/h1&gt;

&lt;p&gt;TypeScript is presented, &lt;a href=&quot;https://www.typescriptlang.org/]&quot;&gt; by the authors&lt;/a&gt;, as “a strongly typed programming language”.&lt;/p&gt;

&lt;p&gt;For anyone who has worked with other strongly typed languages, like Java, Kotlin, C#, this normally means that types are checked before runtime, and guaranteed, unless the users &lt;em&gt;explicitely&lt;/em&gt; tries something dangerous like casting.&lt;/p&gt;

&lt;p&gt;In TypeScript however, there are several ways of ending, without being warned, with completely wrong types at runtime.&lt;/p&gt;

&lt;p&gt;This article presents one. There are more, which will be the subjects of more articles in the near future.&lt;/p&gt;

&lt;h1 id=&quot;the-issue-with-any&quot;&gt;The &lt;strong&gt;issue&lt;/strong&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt;&lt;/h1&gt;

&lt;p&gt;There are a few ways to break the type system in TypeScript, but for ńow, I’m focussing on the fact that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt; can be &lt;em&gt;implicitely&lt;/em&gt; casted into anything else.&lt;/p&gt;

&lt;p&gt;No-one in their right mind would, on purpose and for anything other than a throw-away script, write something like this:&lt;/p&gt;

&lt;div class=&quot;language-ts highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It doesn’t however, need to be that basic.&lt;/p&gt;

&lt;h2 id=&quot;examples&quot;&gt;Examples&lt;/h2&gt;

&lt;p&gt;A couple of example to illustrate slightly less obvious cases that can go un-noticed.&lt;/p&gt;

&lt;h3 id=&quot;jsonparse&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JSON.parse&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JSON.parse&lt;/code&gt; is a built-in function that returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-ts highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;MyType&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;parsed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;MyType&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;JSON&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;{&quot;value&quot;: &quot;key&quot;}&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// parsed is actually { value: string }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are libraries that do things like generating JSON schema, validating an input, and inferring a type, but out of the box, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt; is what you get.&lt;/p&gt;

&lt;h3 id=&quot;arraynfillvalue&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array(n).fill(&quot;value&quot;)&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array(n)&lt;/code&gt; returns an array of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; undefined values, typed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any[]&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fill(value)&lt;/code&gt; fills the array with the given value, without changing its type.&lt;/p&gt;

&lt;div class=&quot;language-ts highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// array is actually a number[]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;what-about-unknown&quot;&gt;What about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unknown&lt;/code&gt;?&lt;/h2&gt;

&lt;p&gt;TypeScript has another type that is used when the specific type is not known: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unknown&lt;/code&gt;. It is equivalent to Java’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object&lt;/code&gt;. And it requires an explicit cast to be assigned to another type:&lt;/p&gt;

&lt;div class=&quot;language-ts highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;unknown&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;casted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// This cast is necessary&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;why-does-it-matter&quot;&gt;Why does it matter&lt;/h1&gt;

&lt;p&gt;This confuses people (e.g. &lt;a href=&quot;https://stackoverflow.com/questions/77117464/why-does-typescript-donst-do-typecheck-on-a-2d-array&quot;&gt;this question on StackOverflow&lt;/a&gt;). There are a few lint directive to restrict the usage of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt;, but it is still fairly easy to miss one.&lt;/p&gt;

&lt;p&gt;Once &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt; is somewhere, the error will only show up at runtime, making the whole point of a typed system moot.&lt;/p&gt;

&lt;p&gt;To be honest, I don’t fully understand why we need to have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt; in typescript, and why &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unknown&lt;/code&gt; is not sufficient.&lt;/p&gt;

&lt;p&gt;Next time: how indexed types lie about what they return.&lt;/p&gt;
</description>
        <pubDate>Tue, 17 Oct 2023 00:00:00 +0000</pubDate>
        <link>https://smaspe.github.io/the%20lies%20of%20typescript/2023/10/17/typescript-types.html</link>
        <guid isPermaLink="true">https://smaspe.github.io/the%20lies%20of%20typescript/2023/10/17/typescript-types.html</guid>
        
        <category>typescript</category>
        
        
        <category>The lies of TypeScript</category>
        
      </item>
    
      <item>
        <title>More bipartite graphs</title>
        <description>&lt;p&gt;Yesterday, on &lt;a href=&quot;https://codefights.com/&quot;&gt;CodeFights&lt;/a&gt;, against a company bot, I was confronted to an interesting problem. I will try as much as possible to avoid revealing which company that was and what problem that was.&lt;/p&gt;

&lt;p&gt;It can be formulated like this:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Let &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; be a set of intervals&lt;/li&gt;
  &lt;li&gt;Let &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; be a set of intervals, each intervals being associated with a length&lt;/li&gt;
  &lt;li&gt;An element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; can be matched with an element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; if and only if the intersection of the 2 intervals is longer than or equal to the length associated with the element in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Determine if it is possible to match each element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; with an element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u = [1, 4]&lt;/code&gt; is not compatible with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v = [3, 5], length = 2&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u = [1, 6]&lt;/code&gt; is compatible with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v = [3, 5], length = 1&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From this formulation, it appears rapidly that this describes a &lt;a href=&quot;https://en.wikipedia.org/wiki/Bipartite_graph&quot;&gt;bipartite graph&lt;/a&gt;. The condition of matching indicates if there is an edge between a vertex in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; are disjoint sets.&lt;/p&gt;

&lt;p&gt;This starts to look a lot like &lt;a href=&quot;/2013/05/31/cat-vs-dogs.html&quot;&gt;cat vs dogs&lt;/a&gt;, a problem from Spotify I solved a few years ago.&lt;/p&gt;

&lt;p&gt;And indeed it does. The problem asked is simply to find if a &lt;a href=&quot;https://en.wikipedia.org/wiki/Matching_(graph_theory)#Maximal_matchings&quot;&gt;maximum matching&lt;/a&gt;: a maximum matching matches exactly &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; distinct elements from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; distinct elements from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; edges.&lt;/p&gt;

&lt;p&gt;The solution is therefore to measure is the cardinal of the &lt;strong&gt;maximum matching&lt;/strong&gt; is the same as the cardinal of the set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A link to an implementation of the Hopcroft-Karp algorithm, which computes the maximum matching of a bipartite graph can be found at the end of the Cat vs Dog problem article.&lt;/p&gt;

&lt;p&gt;Maximal matchings are very useful to solve optimization problem where you have to find the better arrangement for a set of elements to another set.&lt;/p&gt;
</description>
        <pubDate>Tue, 01 Mar 2016 00:00:00 +0000</pubDate>
        <link>https://smaspe.github.io/2016/03/01/more-bipartite-graphs.html</link>
        <guid isPermaLink="true">https://smaspe.github.io/2016/03/01/more-bipartite-graphs.html</guid>
        
        <category>algorithm</category>
        
        
      </item>
    
      <item>
        <title>Publishing for Java (and Android)</title>
        <description>&lt;p&gt;As mentioned in a &lt;a href=&quot;/2016/02/25/publish-a-library.html&quot;&gt;previous article&lt;/a&gt;, writing and publishing an open-source library is good for a lot of reasons.&lt;/p&gt;

&lt;p&gt;There are a few things to do in order to make sure the publication of your library makes sense, here is how to do that in Java.&lt;/p&gt;

&lt;h1 id=&quot;open-sourcing&quot;&gt;Open sourcing&lt;/h1&gt;
&lt;p&gt;My library is &lt;a href=&quot;https://github.com/smaspe/FunctionalIterables&quot;&gt;here&lt;/a&gt;. The issues are public and anyone can add one.&lt;/p&gt;

&lt;p&gt;The shields on the readme page also indicate the public status of the testing, code coverage, and publication.&lt;/p&gt;

&lt;h1 id=&quot;testing&quot;&gt;Testing&lt;/h1&gt;
&lt;p&gt;For testing, I use &lt;a href=&quot;http://junit.org/&quot;&gt;JUnit&lt;/a&gt;. It is the most common testing framework for plain Java.&lt;/p&gt;

&lt;p&gt;To ensure that the tests are always run, I integrated the project with a &lt;a href=&quot;https://en.wikipedia.org/wiki/Continuous_integration&quot;&gt;continuous integration&lt;/a&gt; server. I chose &lt;a href=&quot;https://travis-ci.org/&quot;&gt;Travis CI&lt;/a&gt; because it integrates easily with GitHub. All is required is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.travis.yml&lt;/code&gt; file with the language declaration and the which JDK to use.&lt;/p&gt;

&lt;div class=&quot;language-yml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;language&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;java&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;jdk&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;oraclejdk8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Whenever a commit is pushed on a branch, Travis pulls it, compiles it, runs the tests, and reports the result to GitHub. It ensures that you always know immediately when a branch does not compiles, or fails any test.&lt;/p&gt;

&lt;h1 id=&quot;coverage&quot;&gt;Coverage&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://eclemma.org/jacoco/&quot;&gt;JaCoCo&lt;/a&gt; is a code coverage library for Java. It generates XML and HTML reports based on the tests integrated in your project. It has a Gradle plugin, which adds a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jacocoTestReport&lt;/code&gt; task. I add this task as a dependency of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;check&lt;/code&gt; task, which already encompasses the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test&lt;/code&gt; task, and is run by the CI server.&lt;/p&gt;

&lt;div class=&quot;language-groovy highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;apply&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;plugin:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;jacoco&apos;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;jacocoTestReport&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;reports&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;xml&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;dependsOn&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jacocoTestReports&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To make sense of the report, and to automatize the validation, I integrate &lt;a href=&quot;https://codecov.io&quot;&gt;Codecov&lt;/a&gt;. Again, pretty easy, just send the result of the tests to Codecov using their provided script.&lt;/p&gt;

&lt;div class=&quot;language-yml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;after_success&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;bash &amp;lt;(curl -s https://codecov.io/bash)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can then access other features, such as comparing the coverage from a branch to another, or rapidly see which parts of a pull request are not being tested. This is particularly important if you receive outside contributions to your project.&lt;/p&gt;

&lt;h1 id=&quot;publication&quot;&gt;Publication&lt;/h1&gt;
&lt;p&gt;Gradle build system uses Maven dependency management system, and connects to any maven repository. The most common are &lt;a href=&quot;https://search.maven.org/&quot;&gt;Maven Central&lt;/a&gt; and &lt;a href=&quot;https://bintray.com/bintray/jcenter&quot;&gt;jCenter&lt;/a&gt;. I use jCenter because it is included by default in Android Studio in new Android projects.&lt;/p&gt;

&lt;p&gt;To publish on jCenter, here is how&lt;/p&gt;

&lt;h2 id=&quot;get-an-api-key&quot;&gt;Get an API key&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Register on &lt;a href=&quot;https://bintray.com/&quot;&gt;Bintray&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Get your API key from your &lt;a href=&quot;https://bintray.com/profile/edit&quot;&gt;profile&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Add your username and API key to a file that you add to your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.gitignore&lt;/code&gt;. The key is obviously secret and should not be shared with anyone.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-properties highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;py&quot;&gt;bintray.user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;smaspe&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;bintray.apikey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;API key goes here&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;prepare-the-package&quot;&gt;Prepare the package&lt;/h2&gt;
&lt;p&gt;Publishing on jCenter requires that you include the JavaDoc and the source together with the build. There are a few things to add to your build file.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Include the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maven&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maven-publish&lt;/code&gt; plugins&lt;/li&gt;
  &lt;li&gt;To actually create the JavaDoc and the source bundle, define the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;javadocJar&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sourcesJar&lt;/code&gt; tasks&lt;/li&gt;
  &lt;li&gt;Declare those tasks as being archives in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;artifacts&lt;/code&gt; of the publication&lt;/li&gt;
  &lt;li&gt;Configure the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;group&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;version&lt;/code&gt; of your library&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-groovy highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;com.example.group&apos;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;X.Y.Z&apos;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;apply&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;plugin:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;maven&apos;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;apply&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;plugin:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;maven-publish&apos;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;javadocJar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;type:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Jar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;dependsOn:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;javadoc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;classifier&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;javadoc&apos;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;build/docs/javadoc&apos;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sourcesJar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;type:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Jar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sourceSets&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;allSource&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;classifier&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;sources&apos;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;artifacts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;archives&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;javadocJar&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;archives&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sourcesJar&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;uploading&quot;&gt;Uploading&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Get the jCenter plugin&lt;/li&gt;
  &lt;li&gt;Read the properties file with your API key&lt;/li&gt;
  &lt;li&gt;Configure the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bintray&lt;/code&gt; task&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-groovy highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;plugins&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;com.jfrog.bintray&quot;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;1.4&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Properties&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;properties&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Properties&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;properties&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;project&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;rootProject&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;local.properties&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;newDataInputStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ignored&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;bintray&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;properties&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getProperty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;bintray.user&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;properties&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getProperty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;bintray.apikey&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;configurations&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;archives&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pkg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;repo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;maven&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;The name of your library&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;publish&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// There are other parameters you can set, but only those are mandatory&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You now only have to call the tasks whenever you want to publish a new version&lt;/p&gt;

&lt;div class=&quot;language-nginx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;./gradlew&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;bintrayUpload&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That’s about it. Refer to my complete &lt;a href=&quot;https://github.com/smaspe/FunctionalIterables/blob/master/build.gradle&quot;&gt;Gradle file&lt;/a&gt; to see the complete configuration I use.&lt;/p&gt;
</description>
        <pubDate>Sat, 27 Feb 2016 00:00:00 +0000</pubDate>
        <link>https://smaspe.github.io/2016/02/27/publish-android-library.html</link>
        <guid isPermaLink="true">https://smaspe.github.io/2016/02/27/publish-android-library.html</guid>
        
        <category>open-source</category>
        
        <category>java</category>
        
        <category>android</category>
        
        
      </item>
    
      <item>
        <title>Publish an open-source library</title>
        <description>&lt;p&gt;So you want to publish a library?&lt;/p&gt;

&lt;p&gt;There are a few questions you should be asking yourself.&lt;/p&gt;

&lt;h1 id=&quot;what&quot;&gt;What&lt;/h1&gt;
&lt;p&gt;What library. What does it do, why is it different/more relevant than existing libraries, how is it useful to other people. Jesse Wilson said during one of his presentations that you should not write yet another JSON parsing library. But then he presented &lt;a href=&quot;https://github.com/square/moshi&quot;&gt;moshi&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A new library can be a different approach to a known problem that suits you better. Or a totally new thing. Or an SDK to use another of your products. Or a bunch of things you often need in a project, and that you think fit together as a consistent entity.&lt;/p&gt;

&lt;h1 id=&quot;who&quot;&gt;Who&lt;/h1&gt;
&lt;p&gt;Everyone, really. Companies, individuals, hobbyist, students…&lt;/p&gt;

&lt;h1 id=&quot;why&quot;&gt;Why&lt;/h1&gt;
&lt;p&gt;Why are you writing a library? Here a few good reasons, that apply differently depending on who you are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Better modularity and separation of concern in your code&lt;/li&gt;
  &lt;li&gt;Possibility of peer review&lt;/li&gt;
  &lt;li&gt;Open source is cool. It can be a part of your port-folio, or carry a positive image for a company.&lt;/li&gt;
  &lt;li&gt;Give back something to the community. You probably have dozens of open-source libraries in your own projects.&lt;/li&gt;
  &lt;li&gt;Make that part of your code a proper product so it gets the documentation and QA attention it deserves&lt;/li&gt;
  &lt;li&gt;Make it easier to reuse it in other projects&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;when&quot;&gt;When&lt;/h1&gt;
&lt;p&gt;How do you know when it is time to write a library?&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;When there is a component that naturally emerges from your code, and it starts to look like it has a personality&lt;/li&gt;
  &lt;li&gt;When you architecture your project, and realize that one of the module is really independent from the rest&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;how&quot;&gt;How&lt;/h1&gt;
&lt;p&gt;There are a few things to do in order to ensure your library is in good shape to put out there.&lt;/p&gt;

&lt;h2 id=&quot;open-sourcing&quot;&gt;Open sourcing&lt;/h2&gt;
&lt;p&gt;That’s the obvious step. If you are not publishing the library itself, I don’t know what you are doing here, and if you are not open-sourcing it, you loose most of the benefits of the publication.&lt;/p&gt;

&lt;p&gt;The current place to be is github, but it could also be sourceforge, bitbucket, or any mean you want.&lt;/p&gt;

&lt;h2 id=&quot;documenting&quot;&gt;Documenting&lt;/h2&gt;
&lt;p&gt;If you want people (including your future self who does not remember how you wrote this thing) to be able to use the library, well, obviously, you need to document it. Also, because it is now open-source, you are responsible for it.&lt;/p&gt;

&lt;h2 id=&quot;testing&quot;&gt;Testing&lt;/h2&gt;
&lt;p&gt;A code that has tests is immediately perceived as more reliable.&lt;/p&gt;

&lt;h2 id=&quot;making-sure-you-test-it-coverage&quot;&gt;Making sure you test it (coverage)&lt;/h2&gt;
&lt;p&gt;A single test that passes is not a very good guarantee. Try to aim for 100% coverage. Various tools can help you measure that, depending on your platform, and can help you identify the edge cases you may have missed.&lt;/p&gt;

&lt;h2 id=&quot;publish-it&quot;&gt;Publish it&lt;/h2&gt;
&lt;p&gt;To help people use it (including yourself), as most modern build systems have dependency management integrated with publication systems. (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npm&lt;/code&gt; for JavaScript, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip&lt;/code&gt; for Python, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maven&lt;/code&gt; for Java…)&lt;/p&gt;

&lt;h1 id=&quot;example&quot;&gt;Example&lt;/h1&gt;
&lt;p&gt;I recently published a Java/Android library of higer-order functions for functional programming. It is a Java library, and uses Gradle as build system, so most of the information here are related to that. It is described in a separate &lt;a href=&quot;/2016/02/27/publish-android-library.html&quot;&gt;article&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Thu, 25 Feb 2016 00:00:00 +0000</pubDate>
        <link>https://smaspe.github.io/2016/02/25/publish-a-library.html</link>
        <guid isPermaLink="true">https://smaspe.github.io/2016/02/25/publish-a-library.html</guid>
        
        <category>open-source</category>
        
        
      </item>
    
      <item>
        <title>Functional Iterables</title>
        <description>&lt;p&gt;A few month ago, while working on an app, I realized Java was lacking higher-order functions.&lt;/p&gt;

&lt;h1 id=&quot;why&quot;&gt;Why&lt;/h1&gt;

&lt;p&gt;I realized this because of 2 reasons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Around me a lot of people were using JavaScript, and discussing whether to use &lt;a href=&quot;http://ramdajs.com/&quot;&gt;Ramda&lt;/a&gt; or &lt;a href=&quot;https://lodash.com/&quot;&gt;Lodash&lt;/a&gt;, both being JavaScript libraries providing higer-order functions&lt;/li&gt;
  &lt;li&gt;I had been playing with &lt;a href=&quot;https://github.com/orfjackal/retrolambda&quot;&gt;retrolambda&lt;/a&gt;, which backports in particular lambdas and function references from Java 8&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, having functions close enough to being &lt;a href=&quot;/2016/02/09/functional-programming.html&quot;&gt;first-class objects&lt;/a&gt;, I was in need of higher-order functions to use them.&lt;/p&gt;

&lt;p&gt;As a reminder, a few common higher-order functions:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;all&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce&lt;/code&gt; (more on this one later, in a dedicated post)
…&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;how&quot;&gt;How&lt;/h1&gt;

&lt;p&gt;Because I like simple things, there was a few principles:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The library should be based on existing things. Less code to write, less bugs.&lt;/li&gt;
  &lt;li&gt;I need to be able to use it in code very easily. The library must be usable in an existing project, and must not shape the project.&lt;/li&gt;
  &lt;li&gt;It must be short and self-explanatory. This is a utility library, it should do what you think it should do, and you shouldn’t have to read the documentation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;existing-things&quot;&gt;Existing things&lt;/h2&gt;

&lt;p&gt;It revolves mostly around the the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Iterable&lt;/code&gt; interface.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Collection&lt;/code&gt; interface extends &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Iterable&lt;/code&gt;, so all collections, sets, lists,… could be used there. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Iterable&lt;/code&gt; defines only one method, so it is easy and short to implement.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FuncIter&lt;/code&gt; class is a &lt;a href=&quot;https://en.wikipedia.org/wiki/Decorator_pattern&quot;&gt;decorator&lt;/a&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Iterable&lt;/code&gt;, it adds features to an existing instance. Those features being, of course, the aforementioned higher-order functions.&lt;/p&gt;

&lt;p&gt;Because of this, it takes very little code to write a function. As a simple example, here is the basic implementation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Iterable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Iterable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nc&quot;&gt;Iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

      &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
      &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hasNext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;hasNext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

      &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
      &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;(The actual implementation includes a wrapping in the decorator, omitted here for clarity.)&lt;/p&gt;

&lt;p&gt;It is a grand total of 5 instructions, and it does exactly what it should do. No ifs, no loop, no error.&lt;/p&gt;

&lt;h2 id=&quot;easily-integrated&quot;&gt;Easily integrated&lt;/h2&gt;

&lt;p&gt;Integration of the library is easy, as it should be. It is published on &lt;a href=&quot;https://bintray.com/bintray/jcenter&quot;&gt;jCenter&lt;/a&gt;, so it is a matter of one line in your Gradle file (or a few in your Maven file).&lt;/p&gt;

&lt;p&gt;As per the code itself, all functions are instance methods and static methods (taking the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Iterable&lt;/code&gt; as the first parameter). You can chain them or call them in one shot.&lt;/p&gt;

&lt;p&gt;Because it is a decorator of one of the most common interfaces, and because it also come with a wrapper around &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T[]&lt;/code&gt;, you can use it with most of your existing objects.&lt;/p&gt;

&lt;p&gt;Now, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Iterable&lt;/code&gt; is usually not the most useful output. This is why there are a few &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collect&lt;/code&gt; functions to &lt;em&gt;collect&lt;/em&gt; the iterable in an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrayList&lt;/code&gt; or a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashMap&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A quick note on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collect&lt;/code&gt;. This method returns an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrayList&lt;/code&gt;, rather than a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt;. I covered that in a &lt;a href=&quot;/2016/02/18/issues_with_java.html&quot;&gt;previous article&lt;/a&gt;.
By returning an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrayList&lt;/code&gt;, I guarantee that the returned object is a mutable list, with, for example, a complexity for getting an arbitrary item of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(1)&lt;/code&gt;.
It also means that I am not allowed to change it, ever. But I am OK with that.&lt;/p&gt;

&lt;h2 id=&quot;short&quot;&gt;Short&lt;/h2&gt;

&lt;p&gt;The whole library fits in a single file, and has no dependency. It is compiled for Java 7 as it is what Android uses. If for some reason adding the Gradle dependency or including the jar file is not convenient, you can simply drop the unique source file in your project.&lt;/p&gt;

&lt;p&gt;Each function itself is only a few lines long. Because of that, reading that piece of code will tell you more clearly what it does than the documentation could.&lt;/p&gt;

&lt;p&gt;Because it is so short, I can guarantee that it is thoroughly tested, with the tests having 100% coverage (I am not just saying it, &lt;a href=&quot;https://codecov.io/github/smaspe/FunctionalIterables?branch=master&quot;&gt;codecov&lt;/a&gt; attests to it).&lt;/p&gt;

&lt;h1 id=&quot;last-word&quot;&gt;Last word&lt;/h1&gt;

&lt;p&gt;So there you have it, a 300-line library (at the time of the writing), 100% tested, compatible with Java 7, containing the most common higher-order functions for your daily functional programming needs.&lt;/p&gt;
</description>
        <pubDate>Tue, 23 Feb 2016 00:00:00 +0000</pubDate>
        <link>https://smaspe.github.io/2016/02/23/funciter.html</link>
        <guid isPermaLink="true">https://smaspe.github.io/2016/02/23/funciter.html</guid>
        
        <category>java</category>
        
        <category>android</category>
        
        <category>functional programming</category>
        
        
      </item>
    
      <item>
        <title>The Top 5 things I dislike about Java</title>
        <description>&lt;p&gt;I have been working with Java for about 12 years, starting with Java 2. I have been using other languages, Python, JavaScript, C, Swift,…&lt;/p&gt;

&lt;p&gt;There are a few things that are frustrating when writing Java that are not present in other languages.&lt;/p&gt;

&lt;h1 id=&quot;here-is-my-top-5&quot;&gt;Here is my Top 5&lt;/h1&gt;

&lt;h2 id=&quot;void-is-special&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt; is special&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt; is only ever used to indicate that a method does not return anything. It also means that a method defined as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public &amp;lt;T&amp;gt; T func();&lt;/code&gt; must return something, and can never return a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There is even a &lt;a href=&quot;https://developer.android.com/reference/java/lang/Void.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Void&lt;/code&gt;&lt;/a&gt; type when you want a generic function to not really have a return value. It is just a placeholder, though, and your function still needs to return something. Usually &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;primitives&quot;&gt;Primitives&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;long&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;char&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bool&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;byte&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;float&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;double&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;short&lt;/code&gt; all have equivalent objects, and are auto-boxed and -unboxed. Still, it is a bit weird, and can lead to unexpected behavior:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;causes a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NullPointerException&lt;/code&gt; because you can’t assign &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; to an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Other surprise:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// But&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because of how small integers are cached and the same &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Integer&lt;/code&gt; objects are returned. Of course this is implementation specific.&lt;/p&gt;

&lt;h2 id=&quot;operators-are-special-too&quot;&gt;Operators are special too!&lt;/h2&gt;

&lt;p&gt;Operators in Java work by combining one (for unary operators) or two (for binary operators) operands of compatible types into a result of the same type as one of the operand. Only primitives (and String for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;) are usuable with operators, or their Object equivalent, through auto-unboxing.&lt;/p&gt;

&lt;p&gt;This means you cannot use an operator with generic objects. Your operands have to be of the proper type, and they have to be so at compile time.&lt;/p&gt;

&lt;p&gt;And also that means that you cannot refer to an operator, because it is not a function.&lt;/p&gt;

&lt;h2 id=&quot;functions-as-first-class-objects&quot;&gt;Functions as first-class objects&lt;/h2&gt;

&lt;p&gt;To access a function and learn things about it, like its return type and its arguments, you need to use &lt;a href=&quot;https://docs.oracle.com/javase/tutorial/reflect/&quot;&gt;reflexion&lt;/a&gt;, which is tedious.&lt;/p&gt;

&lt;p&gt;With Java 8, there are function references, lambdas, and functional interfaces. It is much better, but still not perfect.&lt;/p&gt;

&lt;p&gt;While &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html&quot;&gt;Functional Interfaces&lt;/a&gt; provide a way to reference a function, they do not provide direct access to the function itself. You still need a functional interface that matches your function’s signature to create a variable that holds it.&lt;/p&gt;

&lt;h2 id=&quot;list-is-broken&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt; is broken&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/util/List.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt;&lt;/a&gt; is an interface for ordered collections. You can iterate on it, or access the elements based on their position.&lt;/p&gt;

&lt;p&gt;But a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt; does not tell you if it is mutable or not. In other words, all the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;remove&lt;/code&gt; operations are marked &lt;em&gt;(optional operation)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Arrays&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;asList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;one&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;two&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;three&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;strings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;four&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Throws an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UnsupportedOperationException&lt;/code&gt;. Whenever you receive a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt; as a parameter, you do not know if you are capable of mutating it. What is worse, there are several implementations of mutable lists. &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrayList&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LinkedList&lt;/code&gt;&lt;/a&gt; being the more famous. But there is no way to indicate in your method signature that you need a &lt;strong&gt;mutable&lt;/strong&gt; list, short of choosing one of those.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt; is also very generic in the sense that it does not give any indication of how is performing the underlying implementation. Every operation in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt; has a complexity that ranges from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(1)&lt;/code&gt; to usually &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(n)&lt;/code&gt; depending on what type of list it is. See &lt;a href=&quot;http://bigocheatsheet.com/&quot;&gt;Big-O notation&lt;/a&gt; for each type of list. Not knowing which list is used prevents from knowing the complexity of an algorithm.&lt;/p&gt;

&lt;p&gt;This is why I always tend to try to accept the most generic interface I &lt;em&gt;know&lt;/em&gt; I can work with (i.e., not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt; if I need to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add&lt;/code&gt; to it), but return the most specific type I can commit to. My methods never return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt;, unless I really need to. (Typically returning the result from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Arrays.asList&lt;/code&gt;). In which case, the returned &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt; should not be considered mutable.&lt;/p&gt;

&lt;h1 id=&quot;how-other-languages-do-it&quot;&gt;How other languages do it&lt;/h1&gt;

&lt;p&gt;As I mentionned, I have use other languages, because all languages have unique features that make them interesting, and it is by comparing languages that I get to see what is specific, what is good, and what I like less about a language.&lt;/p&gt;

&lt;h2 id=&quot;python&quot;&gt;Python&lt;/h2&gt;

&lt;h3 id=&quot;void&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;There is no such thing as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt; type, nor as a return type. There is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;None&lt;/code&gt;, but it can be affected to any variable, and is simply a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; pointer. It is implicitely returned by functions that have otherwise not returned anything.&lt;/p&gt;

&lt;h3 id=&quot;primitives-1&quot;&gt;Primitives&lt;/h3&gt;

&lt;p&gt;There is no such thing. There are literals, which allow you to express certain types (numbers, lists, dicts, strings, regex…), which is convenient, but all types are objects. Try &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(1).__doc__&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;operators&quot;&gt;Operators&lt;/h3&gt;

&lt;p&gt;Operators are shorthand for functions. This is &lt;em&gt;really neat&lt;/em&gt;. Because there are no primitives, all things are objects, and all objects have methods. And because Python ducktypes and does not need interfaces, all you need to do is implement the function matching your operator. For example the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+=&lt;/code&gt; operator (in-place addition) is implemented by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__iadd__&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Also, because operators are functions and are also defined in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;operator&lt;/code&gt; module, you can refer to them, and use them in higher-order functions: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;functools.reduce(operator.iadd, [1,2,3])&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;functions&quot;&gt;Functions&lt;/h3&gt;

&lt;p&gt;Are a type in python. The have properties, and even functions.&lt;/p&gt;

&lt;h3 id=&quot;list&quot;&gt;List&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;list&lt;/code&gt; in python is an array-backed list. Other types of elements you may use are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;subscriptable&lt;/code&gt; (can be accessed by key or index) and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iterable&lt;/code&gt; (can be iterated sequentially).&lt;/p&gt;

&lt;h2 id=&quot;javascript&quot;&gt;JavaScript&lt;/h2&gt;

&lt;h3 id=&quot;void-1&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;Return statement are not mandatory, and return types are not a thing, so nothing more to say here.&lt;/p&gt;

&lt;h3 id=&quot;primitives-2&quot;&gt;Primitives&lt;/h3&gt;

&lt;p&gt;Some types are represented by &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Glossary/Primitive&quot;&gt;primitives&lt;/a&gt; similarly to Java. Fortunately, the dynamic typing prevents us from having to worry about it.&lt;/p&gt;

&lt;h3 id=&quot;operators-1&quot;&gt;Operators&lt;/h3&gt;

&lt;p&gt;All types can be used with all operators. But watch your step. For fun, try things like&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;functions-1&quot;&gt;Functions&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;Every function in JavaScript is a Function object.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;in &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions&quot;&gt;Mozilla functions reference&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;array&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;The basic list type in JavaScript is Array. To use other types, define it yourself, or use a library. There is no indication to the developer, however what type of list (or, come to that, of what type of variable at all) they are receiving in a function, but it is generally admitted that unless indicated otherwise, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt; is used.&lt;/p&gt;
</description>
        <pubDate>Thu, 18 Feb 2016 00:00:00 +0000</pubDate>
        <link>https://smaspe.github.io/2016/02/18/issues_with_java.html</link>
        <guid isPermaLink="true">https://smaspe.github.io/2016/02/18/issues_with_java.html</guid>
        
        <category>java</category>
        
        <category>python</category>
        
        <category>js</category>
        
        
      </item>
    
      <item>
        <title>Quick note on Functional Programming</title>
        <description>&lt;p&gt;Functional Programming is nice.&lt;/p&gt;

&lt;p&gt;It helps you avoid all those loop, removes lots of conditions, and gets the code clearer as it enables very good separation of concern.&lt;/p&gt;

&lt;p&gt;This article presents quickly what functional programming is, and how it is useful, in order to introduce the &lt;a href=&quot;https://github.com/smaspe/FunctionalIterables&quot;&gt;functional programming library&lt;/a&gt; that I wrote for Java 7, and that I’ll present next time.&lt;/p&gt;

&lt;p&gt;There are 2 important things you want to have for functional programming&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;First-class functions&lt;/li&gt;
  &lt;li&gt;Higher-order functions&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;first-class-functions&quot;&gt;First-class functions&lt;/h1&gt;

&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/First-class_function&quot;&gt;First-class functions&lt;/a&gt; is when you can refer to a function.&lt;/p&gt;

&lt;p&gt;A lot of languages have First-class functions. A few: JavaScript, Swift, Python, Go…&lt;/p&gt;

&lt;p&gt;Here, a Python example:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 1764
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;func&lt;/code&gt; is a variable that references the function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;square&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can then pass it to another function, use it in cases like this trivial and pretty much useless example:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 1764
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; takes a functions as a parameter, it is a &lt;strong&gt;higher-order function&lt;/strong&gt;, which is a perfect transition to the next section&lt;/p&gt;

&lt;h1 id=&quot;higher-order-functions&quot;&gt;Higher-order functions&lt;/h1&gt;

&lt;p&gt;In most cases, a &lt;a href=&quot;https://en.wikipedia.org/wiki/Higher-order_function&quot;&gt;higher-order function&lt;/a&gt; is a function that takes a function as one of its parameters, and apply it in various ways to the other parameter(s).&lt;/p&gt;

&lt;p&gt;The best known example is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 1, 4, 9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It replaces the more verbose&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are plenty other such functions.&lt;/p&gt;

&lt;h1 id=&quot;in-java&quot;&gt;In Java&lt;/h1&gt;

&lt;h2 id=&quot;first-class-functions-1&quot;&gt;First-class functions&lt;/h2&gt;

&lt;p&gt;Until Java 7, the way to reference a function is to create an interface that has one function, then create an anonymous implementation of that interface, then call the function on that object. Think about all those &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnClickListener&lt;/code&gt;, for example.&lt;/p&gt;

&lt;p&gt;In Java 8, 3 new things appeared:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Functional Interfaces. An interface with a single method. The function and the interface are synecdoches&lt;/li&gt;
  &lt;li&gt;Lambdas. The&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;() -&amp;gt; {}&lt;/code&gt; notation basically allows you to implement a functional interface without the verbosity&lt;/li&gt;
  &lt;li&gt;Function reference. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;::&lt;/code&gt; notation allows you to refer directly to a function instead of implementing a functional interface.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both lambdas and function references can be assigned to Functional Interfaces, provided that the signatures are compatible.&lt;/p&gt;

&lt;p&gt;This is as close as it gets to First-class functions, as far as Java is concerned.&lt;/p&gt;

&lt;h2 id=&quot;higher-order-functions-1&quot;&gt;Higher-order functions&lt;/h2&gt;

&lt;p&gt;Java 8 has streams, which has a lot of higher-order functions. &lt;a href=&quot;https://github.com/ReactiveX/RxJava&quot;&gt;RxJava&lt;/a&gt; has a lot things too. And of course, my own &lt;a href=&quot;https://github.com/smaspe/FunctionalIterables&quot;&gt;Functional Iterables&lt;/a&gt; which leverages the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Iterable&lt;/code&gt; interface to implement higher-order functions in Java 7, but that’s for another post.&lt;/p&gt;

&lt;h2 id=&quot;example&quot;&gt;Example&lt;/h2&gt;

&lt;p&gt;Here is a quick example of what first-class functions look like in Java&lt;/p&gt;

&lt;h3 id=&quot;before&quot;&gt;Before&lt;/h3&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setOnClickListener&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;OnClickListener&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onClick&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;anotherFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For every single listener.&lt;/p&gt;

&lt;h3 id=&quot;after&quot;&gt;After&lt;/h3&gt;

&lt;p&gt;The lambda:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setOnClickListener&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;anotherFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;or the function reference:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setOnClickListener&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;anotherFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Much better.&lt;/p&gt;
</description>
        <pubDate>Tue, 09 Feb 2016 00:00:00 +0000</pubDate>
        <link>https://smaspe.github.io/2016/02/09/functional-programming.html</link>
        <guid isPermaLink="true">https://smaspe.github.io/2016/02/09/functional-programming.html</guid>
        
        <category>java</category>
        
        <category>functional programming</category>
        
        
      </item>
    
  </channel>
</rss>
