<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Edouard Tavinor on Medium]]></title>
        <description><![CDATA[Stories by Edouard Tavinor on Medium]]></description>
        <link>https://medium.com/@ehrt74?source=rss-b62de3eae26a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2e-d-lNIwR1f58TtISJJFA.jpeg</url>
            <title>Stories by Edouard Tavinor on Medium</title>
            <link>https://medium.com/@ehrt74?source=rss-b62de3eae26a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 00:16:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ehrt74/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[COBOL copy books]]></title>
            <link>https://ehrt74.medium.com/cobol-copy-books-cda4785716c0?source=rss-b62de3eae26a------2</link>
            <guid isPermaLink="false">https://medium.com/p/cda4785716c0</guid>
            <category><![CDATA[cobol]]></category>
            <category><![CDATA[structured-programming]]></category>
            <dc:creator><![CDATA[Edouard Tavinor]]></dc:creator>
            <pubDate>Sun, 06 Mar 2022 08:42:36 GMT</pubDate>
            <atom:updated>2022-03-06T08:42:36.165Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi everybody!</p><p>Here’s a short post about copy books in COBOL and why you may want to use them.</p><p>Here’s the important stuff:</p><p>this is big_program.cob</p><pre>identification division.<br>program-id. big_program.</pre><pre>environment division.<br>data division.<br>working-storage section.<br>copy &#39;person_dd.cob&#39; replacing ==:block:== by alice.<br>copy &#39;person_dd.cob&#39; replacing ==:block:== by bob.</pre><pre>procedure division.</pre><pre>100-start.<br>    move 32 to age in alice<br>    move &#39;alice&#39; to name in alice<br>    move 27 to age in bob<br>    move &#39;bob&#39; to name in bob</pre><pre>    perform display-alice<br>    perform display-bob</pre><pre>    goback.</pre><pre>copy &#39;person_pd.cob&#39; replacing ==:block:== by alice.<br>copy &#39;person_pd.cob&#39; replacing ==:block:== by bob.</pre><p>and here you can see person_dd.cob`</p><pre>01 :block:.<br>    05 age PIC 9(3).<br>    05 name PIC X(10).</pre><p>and then there’s person_pd.cob`</p><pre>display-:block:.<br>    display &#39;name &#39; name in :block:<br>    display &#39;age &#39; age in :block:<br>    .</pre><p>running the whole thing:</p><pre>$ cobc -x big_program.cob &amp;&amp; ./big_program<br>name alice     <br>age 032<br>name bob       <br>age 027</pre><p>Please note that I’ve only tried this using gnu-cobol. It’s possible that other cobol implementations will complain.</p><p>Now comes the question as to why you would want to do this.</p><p>Consider what the program would look like without copy books:</p><pre>$ cobc -E big_program.cob -o big_program.p &amp;&amp; cat big_program.p<br>#line 1 &quot;big_program.cob&quot;<br> identification division.<br> program-id. big_program.<br> environment division.<br> data division.<br> working-storage section.<br> <br>#line 1 &quot;person_dd.cob&quot;<br> 01 alice.<br> 05 age PIC 9(3).<br> 05 name PIC X(10).<br>#line 6 &quot;big_program.cob&quot;</pre><pre>#line 1 &quot;person_dd.cob&quot;<br> 01 bob.<br> 05 age PIC 9(3).<br> 05 name PIC X(10).<br>#line 7 &quot;big_program.cob&quot;</pre><pre>procedure division.<br> 100-start.<br> move 32 to age in alice<br> move &#39;alice&#39; to name in alice<br> move 27 to age in bob<br> move &#39;bob&#39; to name in bob<br> perform display-alice<br> perform display-bob<br> goback.</pre><pre>#line 1 &quot;person_pd.cob&quot;<br> display-alice.<br> display &#39;name &#39; name in alice<br> display &#39;age &#39; age in alice<br> .<br>#line 18 &quot;big_program.cob&quot;</pre><pre>#line 1 &quot;person_pd.cob&quot;<br> display-bob.<br> display &#39;name &#39; name in bob<br> display &#39;age &#39; age in bob<br> .<br>#line 19 &quot;big_program.cob&quot;</pre><p>Here we are repeating code in the source file. Similar procedures and structures are defined twice. By exporting the code to a copy book we need only maintain one copy of it :) With some clever usage of names you might even be able to get close to object-oriented programming :) You just need one file for each division you want to put the extracted code in (a file for the environment division, for the data division and the procedure division).</p><h3>Notes</h3><p>The copy ... replacing statement in Cobol has a special way of specifying quotes: ==word== . However, if the word in the copy book is surrounded by parenthesis this won’t work (important for array indeces for example). To get around this problem, you can surround the word with colons in the copy book and the main program. I do this in the example above.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cda4785716c0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[simple C systems development in Ubuntu]]></title>
            <link>https://ehrt74.medium.com/simple-c-systems-development-in-ubuntu-cae6f5db5fa5?source=rss-b62de3eae26a------2</link>
            <guid isPermaLink="false">https://medium.com/p/cae6f5db5fa5</guid>
            <category><![CDATA[ubuntu]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[c]]></category>
            <dc:creator><![CDATA[Edouard Tavinor]]></dc:creator>
            <pubDate>Sat, 10 Apr 2021 10:50:55 GMT</pubDate>
            <atom:updated>2021-04-10T10:50:55.721Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi everybody!</p><p>Yesterday I decided to look into simple C development on Linux. In particular I wanted to find out how to write a systems library so I could use it in my C projects. This is what I did:</p><p>Firstly, I started a new virtual machine, because I didn’t want to break anything on my workstation :)</p><p>In the virtual machine I installed sudo apt-get install build-essential</p><p>Then I wrote the following 3 files:</p><p>first, the header file with a function definition: times2.h</p><pre>#ifndef TIMES2_H<br>#define TIMES2_H</pre><pre>extern int times2(int);</pre><pre>#endif</pre><p>and then the implementation of the header: times2.c</p><pre>#include &lt;times2.h&gt;</pre><pre>int times2(int i) {<br>  return i+i;<br>}</pre><p>and finally a file which uses times2.h called uses_times2.c</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;times2.h&gt;</pre><pre>int main(int argc, char **argv) {<br>  int i = 3;<br>  printf(&quot;%d times2: %d\n&quot;, i, times2(i));<br>  return 0;<br>}</pre><p>Then I moved the header file times2.h to /usr/local/include. gcc on Ubuntu will look here for header files. After that I built the implementing code as follows:</p><pre>gcc -shared -o libtimes2.so -fPIC times2.c</pre><p>and moved the compiled library to /usr/local/lib. After that I ran sudo ldconfig to make the system aware of the new library. This meant that I could now build my program:</p><pre>gcc -o uses_times2 uses_times2.c -ltimes2</pre><p>and run it:</p><pre>./uses_times2 <br>3 times2: 6</pre><p>And that’s it :) Maybe this helps someone :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cae6f5db5fa5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A new email account]]></title>
            <link>https://ehrt74.medium.com/a-new-email-account-7df498834b25?source=rss-b62de3eae26a------2</link>
            <guid isPermaLink="false">https://medium.com/p/7df498834b25</guid>
            <category><![CDATA[gmail]]></category>
            <category><![CDATA[free-software]]></category>
            <category><![CDATA[threema]]></category>
            <dc:creator><![CDATA[Edouard Tavinor]]></dc:creator>
            <pubDate>Sun, 24 Jan 2021 17:37:43 GMT</pubDate>
            <atom:updated>2021-01-24T17:37:43.668Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello everybody!</p><p>This is just a short post to explain why I’ve moved to a new email account. I think it’s traditional to add “and why you should too” at the end, but I trust you, dear reader, will be able to read this and decide for yourself.</p><p>First of all, the facts. My main (pretty much only) email account for a long time has been with gmail. Now I’ve set up another account with a paid email provider.</p><p>The reasons for this are varied, but it has to do with free-market competition. Let’s start with the first company you think of when you think of a free-market: Microsoft.</p><p>Back in the day, Microsoft often complained about free software. Why should it be free? How could Microsoft possibly compete with free (in new markets and product categories where Microsoft hadn’t yet locked customers into their products)? I am, like almost all programmers, a big fan of free software and software works very well as a free thing. It costs very little money to write code. If a free-software project already does 99% of what you want, it’s easier to add the missing 1% than to write the whole thing from scratch, etc. etc. I trust you know the arguments for free software, dear reader.</p><p>Internet services are, however, a little different. Operating them actively costs money. Let’s take an example: WhatsApp.</p><p>WhatsApp is a bad idea, and we don’t have to assume that Facebook is doing something nefarious with the information flowing through WhatsApp. It is indisputable that Facebook is bank-rolling WhatsApp to the tune of hundreds of millions of dollars a year. How are other companies meant to compete with this? Once WhatsApp has customer lock-in (which it already does in many communities), what incentive is there for Facebook to make WhatsApp interoperable with other products or to keep on developing it? We saw this with IE6. At the time it was launched, it was a fairly advanced web browser, and then Microsoft did nothing with it. If I was being uncharitable, I would say that their strategy was clear: kill the web. Competition did come in the form of Firefox, but this could only succeed because of three things: firstly, a general hatred of Microsoft amongst many, secondly, the relative simplicity of web pages back then and thirdly, the fact that it was free. If Mozilla had had to raise money for the development of Firefox rather than getting millions from Google, it would never have been able to offer the browser for free.</p><p>It would be even more difficult to make a WhatsApp competitor free. How would you pay for the servers?</p><p>The moral of the story for me: don’t use WhatsApp, and I don’t. Instead I use Threema, which charges a one-time fee for the software (I’d prefer it to charge X Euros per month — it would be more honest). However there’s a problem with Threema, and the example of Google shines a spotlight on it.</p><p>So where does this leave gmail? Well, Google is in a strange position. As internet users we expect certain things to be free, like search engines, email providers, maps, calendars, photo and file storage (up to a certain amount). We expect to be able to access not just the results of millions of programmer-hours of work at the touch of a button, but also raise their running costs by using their servers. We complain when we can’t do this for free. If we were being honest, how much would we pay for the world’s best online maps, the world’s best search engine, the world’s best email?</p><p>Instead, Google has to find a way to make some money off this. They do this by showing you adds. Many people complain about this, basically saying (the most charitable reading): “Google should be forced to spend its money so it can fulfill my bidding”. Or, if I was being less charitable: “poor people should not have access to important internet services”.</p><p>Btw, I was listening to the 1password podcast recently (a software I would very much recommend) and the host said to the guest — an employee of Firefox — the old line about ‘if you are not paying for something, then you are the product’. A rather bad look.</p><p>So why am I paying for email? Well, I like the idea of competition. If somebody has running costs to provide me a service, then I like the transparency of paying directly. I now use a paid password manager, paid note-keeping and productivity software, a paid task-manager, a paid app for learning languages and a paid blogging platform. This works for me because I am, compared with 90% of the world’s population, rich. However, I certainly won’t say to people who use free-as-in-beer services: you are the product! But this is the problem of some paid internet services. Their financing model is transparent, but what if they are incompatible with the free alternatives? It would be classist of me to say that others also have to spend money if they want to communicate with me.</p><p>Fortunately email is an open format, so if you have an account with one email provider can read mails sent from another. This is unlike instant messengers, where they all use mutually incompatible formats (Google tried to get everyone to use the open XMPP format, but nobody else was interested). My email provider is posteo.de btw. They seem to be a nice lot, so check them out!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7df498834b25" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Monoids, functors, monads]]></title>
            <link>https://ehrt74.medium.com/monoids-functors-monads-70b019b59b47?source=rss-b62de3eae26a------2</link>
            <guid isPermaLink="false">https://medium.com/p/70b019b59b47</guid>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Edouard Tavinor]]></dc:creator>
            <pubDate>Wed, 20 Dec 2017 09:03:07 GMT</pubDate>
            <atom:updated>2017-12-20T09:03:07.092Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello everybody!</p><p>This is going to be my attempt to explain these cryptic concepts. I’m going to take examples of things we know well to illustrate the concepts.</p><p>So, let’s start with the monoid. A good example of a monoid is addition. Let’s say we wanted to add a list of numbers:</p><pre>2+3+4+5+6+7+...</pre><p>We could start at the front and just perform the operation, adding 2 to 3 then the result of this to 4 etc. etc. Let’s say, however, that we have a lot of computers and the list of numbers is very long and we want to get this done quickly. Addition is parallelisable. We could distribute chunks of the list as we wished to each computer and have it add its part of the list. We’d give it its chunk and the following instructions:</p><ol><li>Start with 0</li><li>Combine two adjacent elements by adding them</li><li>Carry on adding adjacent elements until you only have 1 element left.</li></ol><p>And that’s all the computer would need to know.</p><p>Congratulations! That’s a monoid. A starting value and an instruction of how to combine adjacent elements that can be parallelised.</p><p>Note that according to this definition, division is not a monoid, because the order of the operations is important. Consider the following list:</p><pre>1/2/3/4/5/6/7/...</pre><p>Its result depends on starting at the left and working your way through. In other words (1/2)/3 is not the same as 1/(2/3).</p><p>Note also that combining words into sentences (or letters into words) is a monoid according to this definition, because we only provide a rule for adding adjacent elements. In other words (“liberté” + “égalité”) + “fraternité” is the same as “liberté” + (“égalité” + “fraternité”). It is however not the same as (“liberté + “fraternité”) + “égalité”. Let’s call this operation collect.</p><p>Ok, let’s move on to functors. A functor transforms each element of the list. Think of multiplying each element in a list of numbers by 3 or taking its sinus, for example. In a number of programming language this is referred to as map. So that the functor is parallelisable it can only depend upon the element of the list it is currently working on and not on any other elements. Another example would be counting the occurances of each letter in a list of words:</p><pre>countOccurances([&quot;foo&quot;, &quot;bar&quot;, &quot;banana&quot;]) =&gt; [{&#39;f&#39;=&gt;1, &#39;o&#39;=&gt;2,}, {&#39;b&#39;=&gt;1, &#39;a&#39;=&gt;1, &#39;r&#39;=&gt;1}, {&#39;b&#39;=&gt;1, &#39;a&#39;=&gt;3, &#39;n&#39;=&gt;2}]</pre><p>You could think of this as being a stage in an assembly line. The objects arrive on a conveyor belt and are then all painted or polished or whatever individually by some big machine. You could parallelise this by just installing more machines next to each other. To be a functor however, the order of the stream of objects before the machine has to be the same as the order afterwards.</p><p>So that just leaves the monad. A monad will turn each element of the list into a list and then string all the lists together. An example would be a function that repeats each number in a list twice:</p><pre>[1,2,3,4,5] =&gt; [1,1,2,2,3,3,4,4,5,5]</pre><p>For lists, this function is called flatmap. It may not be immediately obvious why this is useful. I must admit, I spent a while trying to find an example of where this is used in everyday life and the best I could think of was breaking up large items into small items for easy transport. Maybe taking the freight of a container ship and putting it in lorries which all have to arrive at the same destination in the same order?</p><p>So that’s pretty much all there is to these three concepts as they are applied to lists. One important concept is that all 3 are parallelisable. If you distributed the input list in chunks to 10 machines, you could reduce the time needed to perform the calculation, be it monoidic, functoric (? does that word exist) or monadic.</p><p>However, the three concepts can be applied to things other than a list. To understand how, let’s have a look at what a list is.</p><p>A list contains the idea of an ordered collection of similar things. For example a certain ordering of numbers, or a carpark at a certain moment of time, or my favourite tv series when I was young. The word “list” by itself doesn’t tell you as much as “list of vitam C content of various citrus fruits”. Programming languages have a word for this concept, they call it a parametrised type. List[Int] is a different type from List[String], although both are lists and have a certain ‘list’-iness. If a calculation should be performed on a List[Int], it probably won’t work when you try to perform it on a List[String]. (Exceptions would be calculations which are only interested in the ‘list’-iness, things like “length” or “are you empty?”)</p><p>Can we think of other parametrised types? Well, yes, there are quite a few. A standard example is an Option, which you can think of as a list which either contains one item or is empty. Another possibility is a Try, which you can think of as a box which either contains a value you hope to get, or an explanation as to what went wrong. Or there’s the Future, which is a box which doesn’t contain a value at the moment, but will do at a later date.</p><p>Now comes the interesting thing. If we have code that works on lists by calling the methods collect, map and flatmap, we could use the same code for dealing with Options and Futures and stuff if we found good definitions of these methods for those parametrised types. Let’s take the Option (which we’re thinking of as a list of zero or one element) as an example. Just to make juggling with Options easier, I’m going to call an empty Option None, and an Option which actually contains something Some(contents).</p><p>Monoid =&gt; just return contents, if it exists</p><p>Functor =&gt; perform the given function on contents if it exists. So the Option turns into Some(function applied to contents). If not, the Option remains as None.</p><p>Monad is a bit trickier. Remember with the list we applied a function which turned each element of the list into its own list? With an Option the corresponding function would be one that turned contents (if it exists) into Some(function applied to contents). Why would we want to do this? Well, let’s consider the case that the computation we want to perform may only return a value for certain inputs, like “which is the smallest room in your flat large enough to contain a whale”. The result may be a room, or (more likely) nothing. But this is exactly what an Option itself is — a wrapper around the uncertainty of the existence of something.</p><p>So the monad allows us to chain computations or questions or functions which themselves return Options. Consider the following line of questioning, every step of which could return nothing:</p><ol><li>Name the first animal you saw yesterday (could be None, if you stayed indoors all day)</li><li>Which is the smallest room in your house large enough to contain this animal (could be None, if you saw a whale yesterday morning)</li><li>What is the colour of the wallpaper in this room (could be None if the room doesn’t have wallpaper).</li></ol><p>The result would be an either Some(yellow) or None. The important thing about the Option is that the chain of questioning will complete. It won’t get stuck somewhere not knowing what to do (for example, getting stuck at the second question because you didn’t see an animal yesterday so the question makes no sense). It will just record “no answer found” (i.e. None) and from then on record “no answer found” as the answer to any subsequent question.</p><p>That’s all from me for now. I may try to add stuff later :) Maybe some hints on what a language has to support in order to make monads and things easy to create and work with.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=70b019b59b47" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[go seems to slow down functions passed as arguments? or maybe it’s just optimising my test code …]]></title>
            <link>https://ehrt74.medium.com/go-seems-to-slow-down-functions-passed-as-arguments-9ebaba37fbac?source=rss-b62de3eae26a------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ebaba37fbac</guid>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Edouard Tavinor]]></dc:creator>
            <pubDate>Sat, 15 Jul 2017 20:12:00 GMT</pubDate>
            <atom:updated>2017-07-16T07:26:16.105Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello everybody!</p><p>I’m just writing a post in which I mention that go supports multiple return arguments and I came up with a very simple example of where you could use this:</p><pre>func divide(divisor, dividend int) (quotient, remainder int) {<br>    quotient = divisor/dividend<br>    remainder = divisor - dividend*quotient<br>    return<br>}</pre><p>This sort of made sense to me because the % operation is quite expensive. However, the div operation in x86–64 returns the divisor in one register and the dividend in the other, so I imagine go might well take advantage of this to only do the operation once. It is of course also possible that an integer division on x86–64 may actually be faster than an integer multiplication and subtraction.</p><p>So in order to compare the time I wrote the following:</p><pre>package main</pre><pre>import (<br>    &quot;log&quot;<br>    &quot;time&quot;<br>)</pre><pre>func divide(divisor, dividend int) (quotient, remainder int) {<br>    quotient = divisor/dividend<br>    remainder = divisor - dividend*quotient<br>    return<br>}</pre><pre>func divide2(divisor, dividend int) (quotient, remainder int) {<br>    quotient = divisor/dividend<br>    remainder = divisor%dividend<br>    return<br>}</pre><pre>func main() {<br>    t := time.Now()<br>    for i:=100; i&lt;100000; i++ {<br>        for j:=10; j&lt;100; j++ {<br>            divide(i, j)<br>        }<br>    }<br>    log.Printf(&quot;time passed: %v\n&quot;, time.Now().Sub(t))<br>    t = time.Now()<br>    for i:=100; i&lt;100000; i++ {<br>        for j:=10; j&lt;100; j++ {<br>            divide2(i, j)<br>        }<br>    }<br>    log.Printf(&quot;time passed: %v\n&quot;, time.Now().Sub(t))<br>}   </pre><p>This resulted in:</p><pre>2017/07/15 21:59:06 time passed: 6.039273ms<br>2017/07/15 21:59:06 time passed: 4.400625ms</pre><p>So divide2 (the function using %) is faster.</p><p>Then the functional programmer in me took over, because I didn’t like the repeated code, so I changed it to be like this:</p><pre>package main</pre><pre>import (<br>    &quot;log&quot;<br>    &quot;time&quot;<br>)</pre><pre>func divide(divisor, dividend int) (quotient, remainder int) {<br>    quotient = divisor/dividend<br>    remainder = divisor - dividend*quotient<br>    return<br>}</pre><pre>func divide2(divisor, dividend int) (quotient, remainder int) {<br>    quotient = divisor/dividend<br>    remainder = divisor%dividend<br>    return<br>}</pre><pre>type IntIntToIntInt func(Int, Int) (Int, Int)</pre><pre>func Loop(f IntIntToIntInt) {<br>    for i:=100; i&lt;100000; i++ {<br>        for j:=10; j&lt;100; j++ {<br>            f(i,j)<br>        }<br>    }<br>}</pre><pre>func main() {<br>    t := time.Now()<br>    Loop(divide)<br>    log.Printf(&quot;time passed: %v\n&quot;, time.Now().Sub(t))<br>    t = time.Now()<br>    log.Printf(&quot;time passed: %v\n&quot;, time.Now().Sub(t))<br>}</pre><p>Which resulted in:</p><pre>2017/07/15 22:03:02 time passed: 154.627646ms<br>2017/07/15 22:03:02 time passed: 122.988259ms</pre><p>Which is a whole lot slower. Maybe there’s some secret boxing/unboxing going on here. I can’t find anything on Google about this at the moment. Maybe a more experienced go programmer knows what’s going on here :) Certainly the go runtime seems to be working a lot harder than when the function is called directly without being passed.</p><p>— edit —</p><p>I think I’ve worked out what’s going on. I’ve changed my main function to look like this:</p><pre>var foo int<br>for i:=100; i&lt;100000; i++ {<br>    for j:=10; j&lt;100; j++ {<br>        foo, _ = divide(i, j)<br>    }<br>}<br>log.Print(foo) // need to use variable</pre><p>Now it takes 100ms, which is much closer to 150ms. I imagine the compiler was optimising out the divide(i,j) call. This doesn’t explain why function passing is 50% slower.</p><p>Of course, that also doesn’t explain why when divide(i,j) is optimised out it runs slower than when divide2(i,j) is optimised out.</p><p>I’ve just written similar code in C and found that all methods (using % and using function pointers) take about 80ms on my machine. Go is pretty competitive.</p><p>Meanwhile in news that may not surprise many, scala returns from between 60ms (using % and for comprehensions) up to 350ms (using % and function passing).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ebaba37fbac" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[go generate]]></title>
            <link>https://ehrt74.medium.com/go-generate-89b20a27f7f9?source=rss-b62de3eae26a------2</link>
            <guid isPermaLink="false">https://medium.com/p/89b20a27f7f9</guid>
            <category><![CDATA[generics]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Edouard Tavinor]]></dc:creator>
            <pubDate>Fri, 14 Jul 2017 11:19:44 GMT</pubDate>
            <atom:updated>2017-07-14T11:47:19.514Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello! this is a short post about using <strong>go generate</strong> to generate code.</p><p><strong>go generate</strong> is a tool that comes with the <strong>go</strong> command. You write <strong>go generate</strong> directives in your source code and then run the directives by calling <strong>go generate</strong> on the command line (or using your IDE). Here’s an example <strong>main.go</strong>:</p><pre>package main</pre><pre>import &quot;fmt&quot;</pre><pre>//go:generate ./command.sh</pre><pre>func main() {<br>    fmt.Println(&quot;if you type &#39;go generate&#39; in this directory command.sh will be run&quot;)<br>}</pre><p>And here’s a minimal <strong>command.sh</strong> in the same directory as main.go:</p><pre>#!/bin/bash</pre><pre>echo &quot;hello world!&quot;</pre><p>Now I can run the code as follows:</p><pre>$ ls<br>main.go command.sh<br>$ chmod +x command.sh<br>$ go generate<br>hello world!</pre><p><strong>go generate</strong> also accepts a couple of flags. The only one I use regularly is <strong>-x</strong>, which prints a list of the commands as they are executed to <strong>STDOUT.</strong></p><pre>$ go generate -x<br>./command.sh<br>hello world!</pre><p>And that’s the basic usage of <strong>go generate</strong>. Now to make it do something useful. Let’s say we want to work on slices of struct pointers. We have the following code in the file <strong>./types/person.go</strong>:</p><pre>package types</pre><pre>import (<br>    &quot;fmt&quot;<br>)</pre><pre>type Person struct {<br>    Name string<br>    Age int<br>}</pre><pre>func (p *Person)String() string {<br>    return fmt.Sprintf(&quot;%v (%v)&quot;, p.Name, p.Age)<br>}</pre><pre>type PersonToBool func(*Person) bool<br>type PersonList []*Person</pre><pre>func (pl PersonList)Filter(f PersonToBool) PersonList {<br>    var ret []*Person<br>    for _, p := range pl {<br>        if f(p) {<br>            ret = append(ret, p)<br>        }<br>    }<br>    return ret<br>}</pre><p>And this is our new <strong>main.go</strong>:</p><pre>package main</pre><pre>import (<br>    &quot;fmt&quot;<br>    &quot;./types&quot;<br>)</pre><pre>func main() {<br>    var pl types.PersonList<br>    pl = append(pl, &amp;types.Person{Name:&quot;Jane&quot;, Age:32})<br>    pl = append(pl, &amp;types.Person{Name:&quot;Ed&quot;, Age:27})</pre><pre>    pl2 := pl.Filter( func(p *types.Person) bool {<br>        return p.Age&gt;30<br>    })</pre><pre>    for _, p := range pl2 {<br>        fmt.Println(p)<br>    }<br>}</pre><p>We run the code:</p><pre>$ ls<br>main.go types<br>$ go run<br>Jane (32)</pre><p>Let’s say we also have an Address struct. This is the code for <strong>./types/address.go:</strong></p><pre><strong>package </strong>types<br><br><strong>import &quot;fmt&quot;<br><br>type </strong>Address <strong>struct </strong>{<br>   Street string<br>   Town string<br>}<br><br><strong>func </strong>(a *Address)String() string {<br>   <strong>return </strong>fmt.Sprintf(<strong>&quot;%v\n%v&quot;</strong>, a.Street, a.Town)<br>}<br><br><strong>type </strong>AddressList []*Address<br><br><strong>type </strong>AddressToBool <strong>func</strong>(*Address) bool<br><br><strong>func </strong>(al AddressList)Filter(f AddressToBool) AddressList {<br>   <strong>var </strong>ret AddressList<br>   <strong>for </strong>_, a := <strong>range </strong>al {<br>      <strong>if </strong>f(a) {<br>         ret = append(ret, a)<br>      }<br>   }<br>   <strong>return </strong>ret<br>}</pre><p>As you can see, it looks almost exactly like <strong>./types/person.go</strong>. Is there a way we can make a generic Filter method?</p><p>We can generate the code by calling another program. Here’s the code for <strong>./types/newList.sh</strong>:</p><pre>#!/bin/sh<br><br>TYPE=$1<br><br>cat &gt; ${TYPE}List.go &lt;&lt;EOL</pre><pre>package types</pre><pre>type ${TYPE}List []*${TYPE}<br><br>type ${TYPE}ToBool func(*${TYPE}) bool<br><br>func (al ${TYPE}List)Filter(f ${TYPE}ToBool) ${TYPE}List {<br>   var ret ${TYPE}List<br>   for _, a := range al {<br>      if f(a) {<br>         ret = append(ret, a)<br>      }<br>   }<br>   return ret<br>}<br>EOL</pre><p>And our <strong>./types/person.go</strong> becomes:</p><pre><strong>package </strong>types<br><br><strong>import &quot;fmt&quot;<br><br><br></strong><em>//</em><strong>go:generate</strong><em> ./newList.sh Person<br><br></em><strong>type </strong>Person <strong>struct </strong>{<br>   Name string<br>   Age int<br>}<br><br><strong>func </strong>(p *Person)String() string {<br>   <strong>return </strong>fmt.Sprintf(<strong>&quot;%v (%v)&quot;</strong>, p.Name, p.Age)<br>}</pre><p>Now we can run</p><pre>$ cd types<br>$ chmod +x newList.sh<br>$ go generate<br>$ cd ..<br>$ go run<br>jane (32)</pre><p>So it all works! But there’s a problem. With a very simple shell script, debugging the code to generate the <strong>PersonList</strong> struct and the <strong>Filter</strong> method will be easy enough, but it will get difficult the longer the code is. It would be better to create a dummy struct and use a standard go file. Let’s create a file <strong>./types/list.tmpl.go</strong>:</p><pre><strong>package </strong>types<br><br><strong>type </strong>DUMMYTYPEList []*DUMMYTYPE<br><br><strong>type </strong>DUMMYTYPEToBool <strong>func</strong>(*DUMMYTYPE) bool<br><br><strong>func </strong>(al DUMMYTYPEList)Filter(f DUMMYTYPEToBool) DUMMYTYPEList {<br>   <strong>var </strong>ret DUMMYTYPEList<br>   <strong>for </strong>_, a := <strong>range </strong>al {<br>      <strong>if </strong>f(a) {<br>         ret = append(ret, a)<br>      }<br>   }<br>   <strong>return </strong>ret<br>}</pre><p>And a small file <strong>./types/dummyType.go</strong>:</p><pre><strong>package </strong>types<br><br><strong>type </strong>DUMMYTYPE <strong>interface</strong>{}</pre><p>Now we get syntax highlighting in our IDE! We just have to write a short shell command to exchange <strong>DUMMYTYPE</strong> with the name of another type. This is the new <strong>./types/newList.sh</strong>:</p><pre>#!/bin/sh<br><br>TYPE=$1<br><br>cat list.tmpl.go | sed -e &#39;s/DUMMYTYPE/&#39;${TYPE}&#39;/g&#39; &gt; ${TYPE}List.go</pre><p>We can run the code as above:</p><pre>$ cd types<br>$ go generate<br>$ cd ..<br>$ go run<br>Jane (32)</pre><p>Of course, you could extend the shell script to allow for multiple class arguments for methods like MapToSomeOtherType, or you could use a totally different language to write the commands called by <strong>go generate</strong>. Unfortunately I can’t see a way to use go templates to do this, because go templates are not valid go source code files.</p><p>I hope this helps somebody :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89b20a27f7f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What you have to know before creating a container of a redis docker image]]></title>
            <link>https://ehrt74.medium.com/what-you-have-to-know-before-creating-a-container-of-a-redis-docker-image-674f1c93ede0?source=rss-b62de3eae26a------2</link>
            <guid isPermaLink="false">https://medium.com/p/674f1c93ede0</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Edouard Tavinor]]></dc:creator>
            <pubDate>Tue, 11 Apr 2017 11:47:27 GMT</pubDate>
            <atom:updated>2017-04-11T12:05:26.752Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi everybody!</p><p>I got up this morning and thought “I’ll create a container with a redis docker image and start a little database project using lots of different containers”. Now, 5 hours later, I think I’ve worked out how to create a container with a redis docker image.</p><p>Not that I’m sure, mind you. A lot of people out there know a lot more about this than I do, so if i’m totally wrong, just let me know.</p><p>The first step is to install docker. This is just a simple apt-get or dnf command on an ubuntu/debian/fedora box. That should take about 2 minutes.</p><p>The next step is to download a redis image. You can go <a href="https://hub.docker.com/_/redis/">here</a> and you can see the command at the top right: docker pull redis. So you do that. That should take about a minute.</p><p>Then you scroll down the page and you find docker run --name some-redis -d redis</p><p>So you do that. That takes about a minute. And you end up with a pretty useless container. Why is it pretty useless? Well, you won’t be able to use redis-cli to check if it’s working and you’ll have to do some work to backup the data you store on it.</p><p>No really, don’t do that. There’s a vanishingly small chance that that will do what you want. Here instead is, to the best of my knowledge, what you probably want to do:</p><h3>what you want to do</h3><pre>docker volume create my-data-volume # or any name<br>docker run -d -v my-data-volume:/data --name my-redis-container -p 6379:6379 redis</pre><p>Ok. Let’s have a look at what that monstrosity does and why you need it.</p><p>“run” creates a new container based on the image named in the last word of the command. It’s important that it’s the last word, otherwise the command will silently fail. It then starts the container and runs a command on the container. Here we haven’t given a command, so it won’t do that part.</p><p>In this case you could replace the run command with the following two commands:</p><pre># docker run -d -v my-data-volume:/data --name my-redis-container -p 6379:6379 redis<br># can be replaced with</pre><pre>docker create -v my-data-volume:/data --name my-redis-container -p 6379:6379 redis</pre><pre>docker start my-redis-container</pre><p>which has the advantage of being clearer, if longer.</p><p>“run” takes a -d option. If you don’t stipulate -d your terminal will end up inside the container and you won’t find a way out without backgrounding the process (and nohup/disown etc. etc.). In other words: hassle.</p><p>“-v” allows you to give the name of the volume followed by a colon followed by the path the volume should be mounted to in the new container.</p><p>“ — name” says that the new container will be called my-redis-container.</p><p>“-p port_on_host:port_on_container” is necessary, so that you can check to see if everything’s working by using the redis-cli client.</p><p>Once you’ve done all this, you will have a redis container, open to the outside world on 6379 (the default redis port) and data will be stored persistently to a file on the harddrive of the host. You can find the file by entering:</p><pre>$ docker volume inspect redis-data<br># this will tell you the following (or similar):</pre><pre>[<br>    {<br>        &quot;Driver&quot;: &quot;local&quot;,<br>        &quot;Labels&quot;: {},<br>        &quot;Mountpoint&quot;: &quot;/var/lib/docker/volumes/my-data-volume/_data&quot;,<br>        &quot;Name&quot;: &quot;redis-data&quot;,<br>        &quot;Options&quot;: {},<br>        &quot;Scope&quot;: &quot;local&quot;<br>    }<br>]</pre><p>and your redis data-file is stored under Mountpoint/dump.rdb</p><h3>why you want to do this</h3><p>Good. Now I’ve told you what I think you probably want to do, let me tell you why you want to do this.</p><p>If you got up this morning like I did, you probably regarded a docker container as a simple method to put an application (or a micro-service) in a box and move it from Host A to Host B and deploy it. This model falls apart when it comes to databases.</p><p>The problem is that if data was stored inside the container and the container developed a fault (a bad update, or someone changes a port), you would lose that data (of course, you could argue (and I wouldn’t stop you) that the whole point of containers is that you can snapshot them and revert to a previous working state, however consider a database that’s storing 1000 new pieces of information a second. Snapshotting here would be tricky).</p><p>So instead, docker created the possibility of mounting a directory on the localhost inside the container and additionally you can tell docker that this directory is a “volume”. This is a special name and means that the docker command line tool treats this directory differently. In particular, the docker command line tool will never delete a volume unless you specifically tell it to. It will also exclude volumes from your image snapshots.</p><p>This does however mean that it’s up to you to make sure your database file is safe. Maybe a cronjob, maybe some other more 21st-century solution.</p><p>When you want to move your container from Host A to Host B you’ll have to move your database file as well, and store it in the right place. I’ll give it a try sometime and see if it’s as easy as I imagine it could be.</p><p>I hope this helps someone.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=674f1c93ede0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pipelining for FPGAs]]></title>
            <link>https://ehrt74.medium.com/pipelining-for-fpgas-9342d3a2fdf8?source=rss-b62de3eae26a------2</link>
            <guid isPermaLink="false">https://medium.com/p/9342d3a2fdf8</guid>
            <category><![CDATA[vhdl]]></category>
            <category><![CDATA[fpga]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Edouard Tavinor]]></dc:creator>
            <pubDate>Thu, 30 Mar 2017 19:14:01 GMT</pubDate>
            <atom:updated>2017-04-02T17:36:53.400Z</atom:updated>
            <content:encoded><![CDATA[<p>I thought I’d write an explanation of pipelining for FPGAs and why it’s so important.</p><p>First of all, what is pipelining? An example. Let’s say you have two channels of 64 bit numbers coming into your component and you want to add them up and output them. Let’s assume you’re going to trust the synthesis tools and write minimal code. So you write this in VHDL code:</p><pre>entity my_adder is<br>    Port (<br>        clk : in std_logic;<br>        data1 : in std_logic_vector(63 downto 0);<br>        data2 : in std_logic_vector(63 downto 0);<br>        in_enabled : in std_logic;<br>        data_out : out std_logic_vector(64 downto 0);<br>        out_enabled : out std_logic);<br>end entity;</pre><pre>architecture foo of my_adder is <br>begin</pre><pre>    process(clk)<br>    begin<br>        if (rising_edge(clk)) then<br>            data_out &lt;= std_logic_vector(unsigned(&quot;0&quot; &amp; data1) + unsigned(&quot;0&quot; + data2)); -- result one bit bigger to account for overflows<br>            out_enabled &lt;= in_enabled;<br>    end process;<br>end architecture;</pre><p>and you get the right answer. The problem is, the synthesis software promises to do this in one clock cycle. That means, that if you want to have a clock cycle of 100 MHz for your design, then the synthesis software will say “this isn’t possible. If you expect me to do that addition in one clock cycle then you’re going to have to scale the frequency back a lot”.</p><p>So what can you do? With addition, you can break the calculation up into lots of little chunks and do these chunks simultaneously. You can work out the addition in 16 bit chunks and then combine these chunks to get the result.</p><p>You can work out these 4 16 bit chunks simultaneously. However, you haven’t yet got the correct answer, because one of the chunksums (or even all of them) could overflow, and we don’t know if a chunksum will overflow, until we’ve checked if the chunksum before it overflows. So let’s say we use the first clock cycle to add up these 4 16 bit chunks. At the end of the first clock cycle, the bottom 16 bits of the answer will be correct. Then in the second clock cycle, we can, if necessary add one to the chunksum the second from the end. Now we know the bottom 32 bits of the answer. This will take place while the 4 16 bit chunks are being added for the inputs which arrived one clock cycle later. Then in the next clock cycle we can do the same for the chunk second from the beginning, and finally in the fourth clock cycle we’ll be able to output the correct result.</p><p>This is the basic idea of a pipeline. Data arrives on the input channels every clock cycle. By dividing our program into four parts, every part can be running at the same time, but on different data. The data that arrived on the first clock cycle progresses down the pipeline. One step behind it is the data from the following clock cycle. One step infront of it is data from the previous clock cycle.</p><p>To do this, we need to have enough space reserved so that all of the data needed can exist without being overwritten before it has been outputted.</p><p>So let’s declare an array of 4 records. Each record should be large enough to contain all the state we need. We’ll call that our pipeline. All the state we have for an addition is the 2 inputs, 4 chunksums, space for the data_enabled signal, and space to assemble the result. This results in a structure as follows:</p><pre>constant NUM_CHUNKS : integer := 4;<br>constant PIPELINE_LENGTH : integer := 4;</pre><pre>type chunksums_type is array(NUM_CHUNKS-1 downto 0) of unsigned(16 downto 0);<br>type state_type is record<br>    data1 : std_logic_vector(63 downto 0);<br>    data2 : std_logic_vector(63 downto 0);<br>    chunksums : chunksums_type;<br>    result : unsigned(64 downto 0);<br>    data_enabled : std_logic;<br>end record state_type;<br>type pipeline_type is array(PIPELINE_LENGTH-1 downto 0) of state_type;<br>signal pipeline : pipeline_type;</pre><p>Now, for each clock cycle, we perform an operation on each element of the pipeline and then store the result one slot up in the pipeline.</p><p>Well, that’s a general method that would certainly work. However, it would take up a lot of space. We can reduce the storage necessary by noticing that after the chunksums are calculated, we don’t need to store data1 and data2, so instead, when new data arrives, we’ll store the chunksums directly.</p><p>Another thing to note is that we don’t actually need all the chunksums all the time. The first chunksum can be placed directly in the result, so we don’t need to store it anywhere else. Also, the second chunksum can be discarded after the second clock cycle, because it’s already been inserted into the result and we won’t be using it again. We only need space to store it for one clock cycle. Likewise the third chunksum can be discarded after the third clock cycle. The fourth chunksum can be discarded after the fourth clock cycle, because in this clock cycle it will be inserted into the result.</p><p>This results in the following architecture:</p><pre>architecture foo of my_adder is<br>    type pipeline_type is array(3 downto 0) of unsigned(64 downto 0);<br>    signal pipeline : pipeline_type := (others =&gt; (others =&gt; &#39;0&#39;));<br>    signal part10, part20, part21, part30, part31, part32 : unsigned(16 downto 0) := (others =&gt; &#39;0&#39;);<br>    signal out_en_buffer : std_logic_vector(3 downto 0) := (others =&gt; &#39;0&#39;);<br>begin<br>    process(clk)<br>    begin<br>        if (rising_edge(clk)) then<br>            out_enabled &lt;= out_en_buffer(3);<br>            data_out &lt;= pipeline(3);<br>        end if;<br>    end process;</pre><pre>    process(clk)<br>    begin<br>        if (rising_edge(clk)) then<br>            part10 &lt;= unsigned(&quot;0&quot; &amp; data1(31 downto 16)) + unsigned(&quot;0&quot; &amp; data2(31 downto 16));<br>            part20 &lt;= unsigned(&quot;0&quot; &amp; data1(47 downto 32)) + unsigned(&quot;0&quot; &amp; data2(47 downto 32));<br>            part30 &lt;= unsigned(&quot;0&quot; &amp; data1(63 downto 48)) + unsigned(&quot;0&quot; &amp; data2(63 downto 48));<br>            part21 &lt;= part20;<br>            part31 &lt;= part20;<br>            part32 &lt;= part31;</pre><pre>            pipeline(0)(16 downto 0) &lt;= unsigned(&quot;0&quot; &amp; data1(15 downto 0)) + unsigned(&quot;0&quot; &amp; data2(15 downto 0));<br>            pipeline(1)(15 downto 0) &lt;= pipeline(0)(15 downto 0);<br>            pipeline(1)(32 downto 16) &lt;= pipeline(0)(32 downto 16) + part10;<br>            pipeline(2)(31 downto 0) &lt;= pipeline(1)(31 downto 0);<br>            pipeline(2)(48 downto 32) &lt;= pipeline(1)(48 downto 32) + part21;<br>            pipeline(3)(47 downto 0) &lt;= pipeline(2)(47 downto 0);<br>            pipeline(3)(64 downto 48) &lt;= pipeline(2)(64 downto 48) + part32;<br>            out_en_buffer &lt;= out_en_buffer(2 downto 0) &amp; in_enabled;<br>        end if;<br>    end process;<br>end architecture;</pre><p>Now, no single addition of more than 17 bits needs to be done in any clock cycle. You have to wait 4 clock cycles to get the result of the first addition, but then the results come every clock cycle.</p><p>I hope this will come in useful for somebody :)</p><p>In case anybody’s interested, I calculated the timing for the two different designs by adding a clock constraint, generating a timing report under Synthesis and then looking at the unconstrained paths. Here are the worst timing paths for the two different designs (I reduced the sizes of data_in to 32 bits, because my FPGA doesn’t have enough input and output pins for 64 bit inputs):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wvCxZmMNv40d-NqIcIjYdw.png" /><figcaption>Without pipelining</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fCt1qrarx7hFmlMmxc9Vpw.png" /><figcaption>With pipelining: much shorter :)</figcaption></figure><p>And here’s the worst internal path from one clock cycle to the next for the pipelined design (the design without pipelining has no internal paths from one clock cycle to the next). This shows that we could have run the internal paths at almost 500 MHz :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_Jafw0DRcZd-J7HMUs8ZyA.png" /><figcaption>Worst internal path from one clock to the next.</figcaption></figure><p>One thing that you could do to make this even more performant is add a buffer for data1. If you do this, the constraints on the input signals are relaxed, because they only have to be stable for a much shorter time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9342d3a2fdf8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Assembly on MIPS and Linux syscalls]]></title>
            <link>https://ehrt74.medium.com/assembly-on-mips-and-linux-syscalls-d395a7b54426?source=rss-b62de3eae26a------2</link>
            <guid isPermaLink="false">https://medium.com/p/d395a7b54426</guid>
            <category><![CDATA[assembly]]></category>
            <category><![CDATA[mips]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Edouard Tavinor]]></dc:creator>
            <pubDate>Wed, 29 Mar 2017 07:08:58 GMT</pubDate>
            <atom:updated>2017-03-29T10:29:34.626Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi everybody! I recently got myself a <a href="http://creatordev.io/ci20.html">Creator Ci20 development board</a>. It’s a small board (about 10cm square) with a MIPS processor and enough ressources to run Linux well. I got it because I like interesting architectures, and MIPS was one of the first RISC instruction sets.</p><p>One problem is that I’ve found very few ressources online for assembly programming under Linux on the board. Most websites seem to show code for a MIPS emulator and the interrupts are different. Fortunately there’s an IRC channel for the Ci20 (on freenode and called #ci20) where you can ask for help, and a helpful person put me on the right track :)</p><h4>So here’s the simplest example:</h4><pre>.text<br>    .global __start<br>__start:<br>    li $4, 3<br>    li $2, 4001<br>    syscall</pre><p>If you save this file as 001.s, this can be run as follows:</p><pre>as 001.s -o 001.o<br>ld 001.o -o 001<br>./001<br>echo $? #computer answers &#39;3&#39;</pre><h4>The basic structure is as follows:</h4><p>.text tells the assembler that the processor will start computing here</p><p>.global __start defines a position in the program that you want to refer to as “__start” rather than as a number. It’s a way of naming places to make the life of the programmer easier. “__start” is a special name, because the linker will look for it and issue a warning if it doesn’t find it (it’s like toString() in Java in that certain methods in the standard library will automatically use it).</p><p>li $4 3 means “load immediate” and places the value 3 into register 4.</p><p>li $2, 4001 means “load immediate” and places the value 4001 into register 2.</p><p>syscall means “operating system, I need your help!”</p><h4>Why 4001, why $4, why $2 etc.?</h4><p>Well, now we’re getting to the operating system part. When the process reaches “syscall” the operating system jumps in to help the process out. The process needs a way to tell the operating system what help it needs. These cryptic numbers are the way. First Linux reads the number in register $2, in this case that’s 4001. 4001 means “This process has signalled that it wants to finish. It will return a value stored in register $4”. So Linux reads register $4 (which is a 3) and uses this number as the return value (on Linux, every process that ends has a “return value” which can be used for lots of useful things. It’s a number from 0 to 255. According to convention ‘0’ is used to mean “everything went well” and the other numbers are used to signify that something went wrong. In a terminal, ‘echo $?’ is used to print the last return value).</p><h4>Aren’t these numbers a bit arbitrary?</h4><p>Well, yes, of course they were, when they were decided upon. Now Linux expects certain numbers and will do something different if you use different numbers. A Linux kernel compiled for MIPS has a table with all the different values for register $2 and what they mean, and what the values in other registers mean when the interrupt happens.</p><h4>So where do I find these numbers?</h4><p>As Linus would say, use the source, Luke. Here are the two files you need: <a href="https://github.com/torvalds/linux/blob/master/arch/mips/include/uapi/asm/unistd.h">https://github.com/torvalds/linux/blob/master/arch/mips/include/uapi/asm/unistd.h</a> and <a href="https://github.com/torvalds/linux/blob/master/include/linux/syscalls.h">https://github.com/torvalds/linux/blob/master/include/linux/syscalls.h</a>.</p><h4>Well, now that everything’s clear …</h4><p>Let’s look at the example above. We saved the number 4001 on register $2. You can find this (at time of writing) on line 24 of the first file.</p><pre>#define __NR_Linux   4000<br>#define __NR_syscall   (__NR_Linux + 0)<br>#define __NR_exit   (__NR_Linux + 1)<br>#define __NR_fork   (__NR_Linux + 2)</pre><p>That’s it in the third line! __NR_exit. Now we look at the second file for something called sys_exit and on line 327 you find the following:</p><pre>asmlinkage long sys_exit(int error_code);</pre><p>This means, that the exit syscall takes one argument (error_code), and that argument should be in $4. Two arguments would mean registers $4 and $5. Three arguments would mean $4, $5 and $6 etc. MIPS32 (as found on the Ci20) has syscalls with up to 4 arguments. Other architectures can have more arguments.</p><h4>Aren’t these numbers for registers a bit difficult to remember?</h4><p>If you think that, you’re not the first person to do so. For this reason, the registers have two different names. $4 is also called a0, $5 is a1, $6 is a2 and $7 is a3. As well as this, $2 is v0 (and $3, if you use it) is v1.</p><p>This means that we could write our humble program as follows:</p><pre>.text<br>        .global __start</pre><pre>__start:<br>        li      $a0,    48<br>        li      $v0,    4001<br>        syscall</pre><p>Which looks a bit nicer :)</p><p>So let’s try another call. This time writing to the screen. <a href="https://github.com/torvalds/linux/blob/master/arch/mips/include/uapi/asm/unistd.h">The first file</a> tells us we need interrupt code 4004 (__NR_write). <a href="https://github.com/torvalds/linux/blob/master/include/linux/syscalls.h">The second file</a> says the following in line 568–569</p><pre>asmlinkage long sys_write(unsigned int fd, const char __user *buf,     size_t count);</pre><p>The first argument, fd, is the file discriptor. Stdout is ‘0’. The second argument is the address of a character — the first character of the string. The third argument is the length of the string (in bytes). So we can write the following program:</p><pre>.data<br>mystring:       .asciiz &quot;Hello World!\n&quot;</pre><pre>.text<br>        .global __start</pre><pre>__start:<br>        li      $a0,    0<br>        la      $a1,    mystring<br>        li      $a2,    13<br>        li      $v0,    4004<br>        syscall<br>        li      $a0,    3<br>        li      $v0,    4001<br>        syscall</pre><p>It just remains to assemble and link and run the program:</p><pre>as 002.s -o 002.o<br>ld 002.o -o 002<br>./002 # prints &quot;Hello World!&quot;<br>echo $? # prints &quot;3&quot;</pre><h4>Can I use words for the syscall numbers?</h4><p>Yes, you can. The easy way is to use the preprocessor from gcc. The code will then look like this:</p><pre>#include &lt;regdef.h&gt;<br>        #include &lt;sys/syscall.h&gt;</pre><pre>.data<br>mystring:       .asciiz &quot;Hello World!\n&quot;<br>length:         .word   13</pre><pre>.text<br>        .global __start</pre><pre>__start:<br>        li      a0,     1<br>        la      a1,     mystring<br>        lw      a2,     length<br>        li      v0,     SYS_write<br>        syscall<br>        li      a0,     6<br>        li      v0,     SYS_exit<br>        syscall</pre><p>To get gcc to preprocess a file, you have to name it &lt;filename&gt;.S and not &lt;filename&gt;.s (note the capital S).</p><p>Preprocessing, assembling, linking and running the file would then look like this:</p><pre>gcc -S 003.S &gt; 003.s<br>as 003.s -o 003.o<br>ld 003.o -o 003<br>./003 # prints &quot;Hello World!&quot;</pre><p>There may be a way to do all this with just one gcc command, but I haven’t found it yet. When I try <strong>gcc 003.s</strong> the executable doesn’t call SYS_write.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d395a7b54426" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Typeclass pattern in Scala]]></title>
            <link>https://ehrt74.medium.com/typeclass-pattern-in-scala-995970fc1dff?source=rss-b62de3eae26a------2</link>
            <guid isPermaLink="false">https://medium.com/p/995970fc1dff</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Edouard Tavinor]]></dc:creator>
            <pubDate>Wed, 22 Mar 2017 08:51:01 GMT</pubDate>
            <atom:updated>2017-03-29T07:27:27.561Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi!</p><p>This is a short explanation of the Typeclass pattern in Scala. This is basically a copy of the excellent video on the Typeclass pattern by Seth Tisue that can be found here: <a href="https://www.youtube.com/watch?v=yYo0gANYViE">https://www.youtube.com/watch?v=yYo0gANYViE</a>. However, some of the names he picked for functions and classes didn’t quite gel with me, so I thought I’d change bits of his explanation using names that I find more intuitive.</p><p>First things first: typeclass is here a pattern. That’s what “typeclass” means. It’s not an annotation for a class definition. You could replace the word “typeclass” with “A super method to extend classes without having access to the source code”</p><p>Let’s start with code.</p><pre>case class Person(name : String, age : Int)<br>case class Restaurant(name : String, brunch : Boolean)</pre><p>You have these 2 classes and you’ve been asked to print them out to the screen in a nice format.</p><pre>def serialize(p : Person) = &quot;Person(&quot; +p.name + &quot;, &quot; + p.age + &quot;)&quot;<br>def serialize(r : Restaurant) = &quot;Restaurant(&quot; + r.name + &quot;, &quot; + r.brunch + &quot;)&quot;</pre><p>So you write this code. This relies on overloading ( the compiler will insert the right serialize method based on the class of the argument). The code produces the right output.</p><p>So what’s wrong with this approach? It works and it’s succinct. Well, basically, there’s nothing wrong with this approach, as long as you’re aware of what it’s actually doing.</p><p>What it’s doing is defining 2 different functions that just happen to have the same name. This means that things like import statements won’t be able to tell them apart.</p><p>Here are some problems that may be caused by overloading: <a href="http://stackoverflow.com/questions/2510108/why-avoid-method-overloading">http://stackoverflow.com/questions/2510108/why-avoid-method-overloading</a>.</p><p>Because of these problems, a standard pattern is to introduce a Serializable class or trait. Here’s the code:</p><pre>trait Serializable { def serialize: String }</pre><pre>case class Person(name : String, age : Int) extends Serializable {<br>    override def serialize = &quot;Person(&quot; + name + &quot;, &quot; + age + &quot;)&quot;<br>}</pre><pre>case class Restaurant(name : String, brunch : Boolean) extends Serializable {<br>    override def serialize = &quot;Restaurant(&quot; + name + &quot;, &quot; + age + &quot;)&quot;<br>}</pre><p>And you can now serialize instances of the Person or Restaurant class by calling the serialize method:</p><pre>Person(&quot;me&quot;, 42).serialize // Person(me,42)</pre><p>This is another pattern that works. So what’s wrong with it? Well, let’s say you wanted to not just serialize Persons and Restaurants but also convert them to xml or whatever. You’d do this by adding another trait: Xmlable, for example and then adding this to the class definitions of Person and Restaurant. The result would look like this:</p><pre>trait Serializable { def serialize: String }<br>trait Xmlable { def xmlize: String }</pre><pre>case class Person(name : String, age : int) extends Serializable with Xmlable {<br>    override def serialize = &quot;Person(&quot; + name + &quot;, &quot; + age + &quot;)&quot;<br>    override def xmlize = &quot;&lt;person&gt;&lt;name&gt;&quot;+name+&quot;&lt;/name&gt;&lt;age&gt;&quot; + age + &quot;&lt;/age&gt;&lt;/person&quot;<br>}</pre><p>and similar for Restaurant.</p><p>Now the problem starts to become more clear. In order to stipulate that a class extends a trait you need to modify the class definition. What happens if the code belongs to someone else?</p><p>Another problem is that the Person class is now getting quite large and unwieldy.</p><p>If these things don’t worry you, then it’s a good pattern to use.</p><p>One way of dealing with the second problem is to introduce a Serializer object.</p><pre>sealed trait Serializable<br>case class Person(name: String, age: Int) extends Serializable<br>case class Restaurant(name : String, brunch : Boolean) extends Serializable</pre><pre>object Serializer {<br>    def Serialize(s : Serializable): String = s match {<br>        case p:Person =&gt; &quot;Person(&quot; + p.name + &quot;, &quot; + p.age + &quot;)&quot;<br>        case r:Restaurant =&gt; &quot;Restaurant(&quot; + r.name + &quot;, &quot; + r.brunch + &quot;)&quot;<br>    }<br>}</pre><p>Here we’ve introduced another object which contains the knowledge of how to serialize a particular subclass of Serializable. The “match” statement in Scala helps us here by selecting (at runtime) the right serialization logic. [Don’t worry. This all remains typesafe because of Serializable being defined as ‘sealed’]. In Java this would be “instanceOf”.</p><p>This doesn’t however solve the first problem — you still need to have the ability to modify the code for the Person or Restaurant class if you want to add some other trait.</p><p>So is there a way to keep the simple definition of Person and Restaurant from the top of this article and still have typesafe serialization without overloading? Yes, there is.</p><pre>trait Serializer[T] { <br>    def process(t:T) : String<br>}</pre><pre>def applySerializer[T](tSerializer : Serializer[T], t: T) = tSerializer.process(t)</pre><pre>object PersonSerializer extends Serializer[Person] {<br>    def process(p : Person) = &quot;Person(&quot; + p.name + &quot;, &quot; + p.age + &quot;)&quot;<br>}</pre><pre>object RestaurantSerializer extends Serializer[Restaurant] {<br>    def process(r : Restaurant) = &quot;Restaurant(&quot; + r.name + &quot;, &quot; + r.brunch + &quot;)&quot;<br>}</pre><p>This can now be called as follows:</p><pre>applySerializer[Person](PersonSerializer, Person(&quot;me&quot;, 42))</pre><p>or, because scala is good at type inference:</p><pre>applySerializer(PersonSerializer, Person(&quot;me&quot;, 42))</pre><p>All of which is type-safe, but rather verbose. It is however a nice pattern and can be written in lots of programming languages. It can be extended quite simply: I don’t need to change the Person class, nor the Serializer class, I just need to create a Serializer object for the class I want to serialize.</p><p>Well, this is a good solution. But we can make it a bit less verbose. Scala has an implicit annotation. This looks like this:</p><pre>implicit val i:Int= 3</pre><p>And can be used as follows:</p><pre>def add(i1 : Int)(implicit i2 : Int)= i1 + i2 <br>add(2) // 5</pre><p>Note the function signature. There are some rules about where implicits can be used.</p><p>What the annotation implicit does is register this object/function/value as a default value the compiler can use during compilation. If the code doesn’t compile without looking in the list of implicits, the compiler looks if there’s an implicit which has the right type signature to allow the code to compile. If there is one (and only one), it uses it. If not, it stops compiling and says why the code doesn’t compile.</p><p>Like that, we can produce implicit definitions to remove some keywords.</p><p>Let’s use implicits to remove the need to pass the PersonSerializer and RestaurantSerializer explicitly when using the applySerializer function:</p><pre>trait Serializer[T] { def process(t: T) : String }</pre><pre>def serialize[T](t : T)(implicit s : Serializer[T]) = s.process(t)</pre><pre>implicit object PersonSerializer extends Serializer {<br>    def process(p : Person) = &quot;Person(&quot; + p.name + &quot;, &quot; + p.age + &quot;)&quot;<br>}</pre><pre>implicit object RestaurantSerializer extends Serializer {<br>    def process(r : Restaurant) = &quot;Restaurant(&quot; + r.name + &quot;, &quot; + r.brunch + &quot;)&quot;<br>}</pre><p>And the code can be used like this:</p><pre>serialize(Person(&quot;me&quot;, 42)) // Person(me,42)</pre><p>Well, that all looks a lot neater. If you try to serialize an instance of a class which doesn’t have a Serializer defined, the compiler will complain in a meaningful way:</p><pre>serialize(3) // error: could not find implicit value for parameter s: this.Serializer[Int]</pre><p>You can, of course, instead use implicits to transform a Person object to a Serializer object:</p><pre>trait Serializer { def serialize: String }</pre><pre>implicit def PersonToSerializer(p : Person) = new Serializer {<br>    def serialize = &quot;Person(&quot; + p.name + &quot;, &quot; + p.age + &quot;)&quot;<br>}</pre><pre>implicit def RestaurantToSerializer(r : Restaurant) = new Serializer {<br>    def serialize = &quot;Restaurant(&quot; + r.name + &quot;, &quot; + r.brunch + &quot;)&quot;<br>}</pre><p>And this can be used like this:</p><pre>Person(&quot;me&quot;, 42).serialize // Person(me, 42)</pre><p>The compiler will issue a warning if you do this. Implicit type casts are very powerful and dangerous and the compiler will ask you if you really know what you’re doing.</p><p>The rule of thumb for implicit type casts is : if you’re casting between types that you have written, you’re unlikely to make the job of other programmers more confusing. If you’re casting between types that already exist then programmers who import your code may suddenly find all sorts of odd things happening (this method takes a String so why didn’t it complain when I handed it an Int by accident?).</p><p>Ok, that’s the typeclass pattern in scala.</p><p>Summing up: the typeclass pattern allows you to extend classes without needing to change the source code for the classes. It can do this because of scala’s implicits, otherwise the code would remain verbose. Using implicits also results in good error messages when the code doesn’t work.</p><p>The basic question which this pattern addresses is who should know how an object should be serialized. Should the object itself know? If yes: then you will have to modify the source code of all the classes you want to serialize. Should all the knowledge be stored at one place in a big function? Well, then you’ll have to modify that function if you want to teach it how to serialize a new class. Should you instead have lots of light-weight objects which turn classes into serializable classes so they can be used by a generic serialize method? That will allow you to write the code for serializing the objects you want to serialize without any one file becoming huge. However, in most programming languages this would mean supplying two arguments to the serialize method. Scala implicits allow you to drop the light-weight serializer object from the parameter list.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=995970fc1dff" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>