<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Marco Servetto</title>
    <description>The latest articles on DEV Community by Marco Servetto (@marcoservetto).</description>
    <link>https://dev.to/marcoservetto</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F766386%2Feb8f941c-c87a-4446-825d-7d2bf7afe557.png</url>
      <title>DEV Community: Marco Servetto</title>
      <link>https://dev.to/marcoservetto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marcoservetto"/>
    <language>en</language>
    <item>
      <title>I'm releasing on youtube my  Java parallelism course.</title>
      <dc:creator>Marco Servetto</dc:creator>
      <pubDate>Sun, 06 Mar 2022 06:27:52 +0000</pubDate>
      <link>https://dev.to/marcoservetto/im-releasing-on-youtube-my-java-parallelism-course-3mgn</link>
      <guid>https://dev.to/marcoservetto/im-releasing-on-youtube-my-java-parallelism-course-3mgn</guid>
      <description>&lt;p&gt;I'm releasing on youtube my Java parallelism course, NWEN303 for VUW ECS new zealand. Feel free to learn all you can from it. It is going to be fun!&lt;/p&gt;

&lt;p&gt;Here all the lectures out up to now:&lt;/p&gt;

&lt;p&gt;Lecture 1, with the playlist:&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=nMgXz1iucac&amp;amp;list=PLWsQqjANQic-Hh5w2srBkQro0K0xMvCg0"&gt;https://www.youtube.com/watch?v=nMgXz1iucac&amp;amp;list=PLWsQqjANQic-Hh5w2srBkQro0K0xMvCg0&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lecture 2&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=uUkd0Xuk1jE"&gt;https://www.youtube.com/watch?v=uUkd0Xuk1jE&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=GhWL0W2xWwo"&gt;https://www.youtube.com/watch?v=GhWL0W2xWwo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lecture 3&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=Kgw3aveClTE"&gt;https://www.youtube.com/watch?v=Kgw3aveClTE&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=07JZj32QD6w"&gt;https://www.youtube.com/watch?v=07JZj32QD6w&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lecture 4&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=SA_6Jloh2bA"&gt;https://www.youtube.com/watch?v=SA_6Jloh2bA&lt;/a&gt;&lt;/p&gt;

</description>
      <category>youtube</category>
      <category>java</category>
      <category>parallelism</category>
      <category>concurrency</category>
    </item>
    <item>
      <title>How to use Java from Forty2</title>
      <dc:creator>Marco Servetto</dc:creator>
      <pubDate>Sun, 13 Feb 2022 23:09:38 +0000</pubDate>
      <link>https://dev.to/marcoservetto/how-to-use-java-from-forty2-3pma</link>
      <guid>https://dev.to/marcoservetto/how-to-use-java-from-forty2-3pma</guid>
      <description>&lt;p&gt;With code examples!&lt;br&gt;
(&lt;a href="https://www.youtube.com/watch?v=asXPD879RZo"&gt;https://www.youtube.com/watch?v=asXPD879RZo&lt;/a&gt;)&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Freedom is slavery</title>
      <dc:creator>Marco Servetto</dc:creator>
      <pubDate>Mon, 07 Feb 2022 04:17:57 +0000</pubDate>
      <link>https://dev.to/marcoservetto/freedom-is-slavery-2g6n</link>
      <guid>https://dev.to/marcoservetto/freedom-is-slavery-2g6n</guid>
      <description>&lt;p&gt;There is a fascinating motto in the PL design community: &lt;/p&gt;

&lt;p&gt;Freedom is slavery.&lt;/p&gt;

&lt;p&gt;I think this motto is not popular enough. In this post, I will try to explain it more, hopefully you can learn about it and disseminate it too.&lt;/p&gt;

&lt;p&gt;It is highly connected with the concept of &lt;a href="https://en.wikipedia.org/wiki/Offensive_programming"&gt;offensive programming&lt;/a&gt;: you code should monitor its execution and throw error as soon as anything go out of the predicted path; hopefully with a good error message explaining what is going wrong.&lt;/p&gt;

&lt;p&gt;Offensive programming is fundamental when working on a large project. Consider the following 3 scenarios: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One programmer for a few days with no planned maintenance; usually the customer is the single programmer.&lt;/li&gt;
&lt;li&gt;A single team/company for a few months. We have an actual maintenance plan. Usually the customer is a wealthy individual or a small company.&lt;/li&gt;
&lt;li&gt;A community effort for many years. In this context, maintaining &lt;strong&gt;is&lt;/strong&gt; developing. For example, is 'Gimp' actively maintained or actively developed?
In those cases, usually there is no clear 'customer', but a large user base.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Programming at scale
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Small scale:
programming is mostly to express behavior.
Here offensive programming seams to be just a waste of time.&lt;/li&gt;
&lt;li&gt;Larger scale:
programming is mostly to restrict behavior.
Here offensive programming really pays off: code you wrote long ago, or written by another programmer is actively teaching users how to use it by checking how it gets used.
Sure, there may also be documentation.. but no one actually ever read it. Offensive programming is like a form of active documentation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Programming is a competitive effort, a battle!
&lt;/h1&gt;

&lt;p&gt;Consider a multiplayer shooter or similar game. Today, they just released a new update: yet another kind of weapon.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Attack: cool, now I can do yet another thing; more freedom,
more expressive power!
– Still, I can only keep up to 4 weapons in my inventory&lt;/li&gt;
&lt;li&gt;Defense: Oh, no, now the enemy can kill me in yet another way
– There are many enemies and they may have any weapon at all. How
to defend against all possible weapons?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consider a programming language. There is a new update: yet another kind of statement/expression/features/language-primitive.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initial development: cool, now I can do yet another thing; more
freedom, more expressive power!
– Still, I can keep a few features in my head at once (finite brain).&lt;/li&gt;
&lt;li&gt;Maintenance/Usability: Oh, no, now the users/coworkers can
interact with my code in yet another way.
– Given enough time, all possible ways to interact with my code become relevant. How to make sure my code is correct in all those scenarios?&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Two kinds of gameplay: Cooperative and PvP
&lt;/h1&gt;

&lt;p&gt;Continuing on the video-game metaphors, we can imagine two kinds of Programming environment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cooperative:
This kind of programming environment is what is commonly associated to Python or Ruby: a user of you code can always break/dismantle absolutely everything if they want, and the only documentation giving FULL information on what would happen if they start playing editing the stack trace variable values or monkey patching methods is the actual source code.
This gives the user full freedom, and gives the library writer full slavery. They basically can not truly guarantee anything.
That may not be too bad for small projects, we can rely on conventions and cooperation, and hope for the best.
Minecraft it is great for cooperative gaming, many builders can come together and build amazing things. In Minecraft it is easier to not step on each other feet that while programming: usually if my wall is not very stable, your house will not collapse.
Still, cooperative Mincraft servers usually have a reasonable small number of players.
The more the players, the more the risk of a griefer getting in.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the same for coding. If you project is big enough, or if breaking your project is worth enough money you may not be able to get enough truly trusted programmers to work on it.&lt;br&gt;
Also.. do you know about supply chains attack? when hacker manages to inject adversarial code in your project by cooperating to some of the open source libraries you use.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PvP:
I assume you are now opening you eyes very wide.. what a PvP programming environment can possibly look like?
PvP Mincraft servers are basically giant wastelands. How can PvP programming end up any better?&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  The C model delusion
&lt;/h1&gt;

&lt;p&gt;There is a big lie in programming: The lie is that&lt;br&gt;
'Ultimately everything is broken', thus the best we can do is to just be sensible, practical and try to cooperate.&lt;/p&gt;

&lt;p&gt;Indeed, in the old C/Shell/Lowlevel-OS programming mindset, this is correct. Many languages are just a nice way to generate arbitrary assembler, thus in the end, everything can happen.&lt;/p&gt;

&lt;p&gt;Thankfully we are slowly moving away from it. In Java and C#, a bunch of things are actually guaranteed by the language, &lt;br&gt;
that is why Java has 'security bugs/fix': because they have a security model, some properties that they promise.&lt;br&gt;
In a C/C++ compiler there is no such thing as a security fix, not because C is more secure, but because there is nothing actually promised by the language.&lt;/p&gt;

&lt;h1&gt;
  
  
  Freedom is Slavery
&lt;/h1&gt;

&lt;p&gt;In a language like Java/C#, freedom is slavery: any extra feature could potentially break some property that we had before. In Java it is actually possible to write good Offensive code that is actually guaranteed to work modularly: no matter how adversarial the user context is, your code will still be able to preserve internal constraints and to monitor its behavior against abuse. (Yes, I know, this is technically false, keep reading)&lt;/p&gt;

&lt;p&gt;In my language, I try to get much further then Java: In Java you can still break the JVM by loading broken C code, or (on some OS) by writing on virtual files that modify your actual memory.&lt;/p&gt;

&lt;p&gt;So... in some sense, if arbitrary adversarial code can get into your system, if we are in a real PvP scenario... you are still in the desolate wasteland.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://Forty2.is"&gt;42&lt;/a&gt;, this is not the case, and even with adversarial code injected into your security critical process, you can still build a secure fort, and ensure arbitrary representation invariants. From those, you can build a strong and flexible security system where valid objects are guaranteed to only do valid actions, and objects of those objects are the one ones able to do those kinds of actions (object capabilities).&lt;br&gt;
If you are interested in the idea, please try &lt;a href="https://Forty2.is"&gt;42&lt;/a&gt; and contact me for more infos!&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>offensiveprogramming</category>
    </item>
    <item>
      <title>HashMaps and HashSets in a language with normalization</title>
      <dc:creator>Marco Servetto</dc:creator>
      <pubDate>Mon, 31 Jan 2022 09:19:55 +0000</pubDate>
      <link>https://dev.to/marcoservetto/hashmaps-and-hashsets-in-a-language-with-normalization-5hj6</link>
      <guid>https://dev.to/marcoservetto/hashmaps-and-hashsets-in-a-language-with-normalization-5hj6</guid>
      <description>&lt;p&gt;In this video I explain how in a language with normalization (also known as hashconsing) Maps and Sets are much better/faster.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=Rv1lsKHIQu8"&gt;https://www.youtube.com/watch?v=Rv1lsKHIQu8&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The idea is that you can simply avoid arbitrarily long equality tests.&lt;br&gt;
An object is normalized if it is deeply immutable and we are guaranteed that there is no other structurally equivalent immutable object in memory. Thus all the pointers to something with that structure are pointing to the same object in memory.&lt;br&gt;
If an hashmap/hashset has only normalized keys, we can just check for pointer equality instead of checking the 'equals'.&lt;/p&gt;

</description>
      <category>42</category>
      <category>programming</category>
      <category>hash</category>
    </item>
    <item>
      <title>Security and Correctness: two different concepts.</title>
      <dc:creator>Marco Servetto</dc:creator>
      <pubDate>Thu, 27 Jan 2022 02:50:20 +0000</pubDate>
      <link>https://dev.to/marcoservetto/security-and-correctness-two-different-concepts-3g04</link>
      <guid>https://dev.to/marcoservetto/security-and-correctness-two-different-concepts-3g04</guid>
      <description>&lt;p&gt;A few years ago I started researching on Software correctness. I was actually interested in software security, but I was unaware of the difference at the time. Now, after researching in both areas I can finally discuss the difference between those two concepts.&lt;br&gt;
What does it take to mathematically define if a specific program is correct and/or secure?&lt;/p&gt;
&lt;h2&gt;
  
  
  Fist I will talk about correctness, then security.
&lt;/h2&gt;

&lt;p&gt;We say that a program is correct if it respects its specification.&lt;br&gt;
Thus, it does not even make sense to talk about the correctness of a program on its own. Correctness is actually a consistency property of a pair program + specification.&lt;/p&gt;

&lt;p&gt;Let's consider a very simple program with a simple specification:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    //Post: the result is a positive number
    int foo(){return 5;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is this code correct? That is, is it true that the result of calling that method will always respect the specified post-condition?&lt;/p&gt;

&lt;p&gt;Well... &lt;strong&gt;NO&lt;/strong&gt;. For no real architecture that method will always return a positive number. In some situations calling that method will simply raise a &lt;code&gt;StackOverflow&lt;/code&gt; error, and sometime the physical hardware will crash and burn before being able to compute the result.&lt;br&gt;
However, most developers would agree that the code above is &lt;em&gt;correct&lt;/em&gt;, or at least, as correct as it can possibly be.&lt;br&gt;
That is, we are not considering correctness in an absolute sense, we are considering a simplified execution model with certain simplifications with respect to the hash reality, where anything can fail at any point anyway.&lt;/p&gt;

&lt;p&gt;Many researchers in the area of software verification seams to simply accepting that there is going to be this set of simplifications that hopefully will be irrelevant in most real life situations an focus on mathematically modelling code execution in order to prove correctness (that is, consistency between code and specification) on such a simplified execution model.&lt;br&gt;
Instead, I decided to study and categorize those simplifications: &lt;br&gt;
If we can not make proofs on the actual execution model, what execution model should we use? &lt;br&gt;
I say that an execution model more close to reality has a bigger scope that an execution model that excludes more difficulties.&lt;/p&gt;

&lt;p&gt;Most of the &lt;strong&gt;verification for correctness&lt;/strong&gt; research area works under the following simplifications: &lt;/p&gt;

&lt;p&gt;1.-No StackOverflow: we assume we have an infinite stack&lt;br&gt;
2.-No MemoryOverflow: we assume we have an infinite heap&lt;br&gt;
3.-I/O control: we assume no other process is editing our files&lt;br&gt;
4.-Uncorrupted Root: we assume other processes can not randomly edit our memory bits&lt;br&gt;
5.-Correctness of the OS: OS functionalities behave as expected&lt;br&gt;
6.-Good Hardware: we assume no HW bugs&lt;br&gt;
7.-Small Physics: no cosmic ray bit flips or funky quantum events &lt;br&gt;
8.-Large Physics: no power cuts, no explosions or kicks to the HD&lt;br&gt;
9.-Deployment: the code we checked is the one that will actually run&lt;br&gt;
(Disclamer, my experience is mostly on OO verification)&lt;/p&gt;
&lt;h2&gt;
  
  
  Definitions
&lt;/h2&gt;

&lt;p&gt;Thus, under those simplifications we can define &lt;strong&gt;Correctness&lt;/strong&gt; as follows:&lt;br&gt;
&lt;strong&gt;A program is correct if it always does the right thing&lt;/strong&gt;&lt;br&gt;
Where the specification identifies what is the acceptable range of &lt;em&gt;right things&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now we can contrast the definition of correctness with the one of &lt;br&gt;
&lt;strong&gt;Security&lt;/strong&gt;:&lt;br&gt;
&lt;strong&gt;A program is secure if it never does the wrong thing&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Warning: the scope is implicit.
&lt;/h2&gt;

&lt;p&gt;At a first glance it may look like security is simply a subset of correctness. However, the &lt;strong&gt;SCOPE&lt;/strong&gt; is often different.&lt;br&gt;
When discussing about security we do not accept simplifications 1, 2 and 3; that is, even if there is a &lt;code&gt;StackOverflow&lt;/code&gt;, we still can not accept to perform an invalid action. &lt;br&gt;
In response to a &lt;code&gt;StackOverflow&lt;/code&gt; it is ok to crash , but it is not ok to send our private information to another country.&lt;/p&gt;

&lt;p&gt;Thus, usually we attempt to verify correctness under simplifications 1-2-3 and security without simplifications 1-2-3 (but still with simplifications 4-5-6-7-8-9).&lt;br&gt;
In particular, without 1,2 near every program is simply incorrect.&lt;/p&gt;

&lt;p&gt;While 'the right thing' is often defined using pre/post conditions in some logical framework, 'the wrong thing' is often defined using &lt;strong&gt;actions&lt;/strong&gt;: we abstract over the internal state of the program and we just observe the interaction the program has with the rest of the world. Usually we can represent those interactions as forms of I/O or calls to low level OS functionalities.&lt;br&gt;
Under this lens, terminating the program with an error message is often not considered an action. Going in loop is also often not considered an action.&lt;br&gt;
(Models of security focusing on a unit of code often do not model &lt;em&gt;Denial Of Service&lt;/em&gt; vulnerabilities; they are better addressed considering multiple processes running together, and thus out of scope for this discussion).&lt;/p&gt;

&lt;p&gt;To further clarify the difference of security and correctness, consider the two examples below:&lt;/p&gt;
&lt;h2&gt;
  
  
  An universally secure(but not correct) solution to any problem
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    throw new Error("NOPE!");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That is, we can simply refuse to act. No action means no security violation.&lt;/p&gt;
&lt;h2&gt;
  
  
  An universally correct (but not secure) solution to any problem
&lt;/h2&gt;

&lt;p&gt;Assuming to have a function &lt;code&gt;correctlyDoStuff(..)&lt;/code&gt; that is both correct and secure, we can forge a correct but not secure version of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int doStuff(int x){
        try{rec(aNumber);}
        catch(StackOverflowError o){ badAction(); }
        return correctlyDoStuff(x);
    }
    void rec(int i){ if(i==0){return;} rec(i-1); }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;code&gt;badAction&lt;/code&gt; does a security violation, like formatting the hard drive.&lt;br&gt;
For any real execution environment of our code for our &lt;code&gt;doStuff&lt;/code&gt; function, there exists a number &lt;code&gt;aNumber&lt;/code&gt; that will trigger the &lt;code&gt;StackOverflow&lt;/code&gt;, but in the verification for correctness model, the &lt;code&gt;catch&lt;/code&gt; is recognized as dead code.&lt;br&gt;
Indeed, some code verification tools will accept such a solution as correct.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;There is a lot of confusion between correctness and security.&lt;br&gt;
I hope I clarified the terms a little bit here.&lt;br&gt;
Ultimately, there can not be full agreement on any terminology, but if we want something that can be mathematically modeled and checked/verified, the introduced terminology is a very good starting point.&lt;br&gt;
Also, terminology in this area is so challenging that I do not think it is fair to require developers and companies to have a good grasp on those terms.&lt;br&gt;
 In particular, if you/your company want security and someones tries to sell you correctness without making sure that you understand the difference between the terms, they may be scamming you.&lt;/p&gt;

&lt;p&gt;Overall, in my opinion we should start by writing &lt;strong&gt;SECURE&lt;/strong&gt; software, and only after we are certain that we are well rooted in a secure environment we should start working toward &lt;strong&gt;CORRECTNESS&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>security</category>
      <category>correctness</category>
      <category>programming</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>How to avoid lambdas and still get most of the streams expressive power</title>
      <dc:creator>Marco Servetto</dc:creator>
      <pubDate>Sun, 09 Jan 2022 21:13:10 +0000</pubDate>
      <link>https://dev.to/marcoservetto/how-to-avoid-lambdas-and-still-get-most-of-the-streams-expressive-power-1gh6</link>
      <guid>https://dev.to/marcoservetto/how-to-avoid-lambdas-and-still-get-most-of-the-streams-expressive-power-1gh6</guid>
      <description>&lt;p&gt;Hi Everyone, I've just released a new video, talking about how with the slash we can avoid lambdas and still get most of the streams expressive power.&lt;br&gt;
Also, it is probably the funniest video to date :-)&lt;/p&gt;




&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=3-EwmfpOAnA"&gt;https://www.youtube.com/watch?v=3-EwmfpOAnA&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>lambda</category>
      <category>42</category>
      <category>programming</category>
    </item>
    <item>
      <title>42; multi-for-each and views</title>
      <dc:creator>Marco Servetto</dc:creator>
      <pubDate>Sun, 02 Jan 2022 21:17:26 +0000</pubDate>
      <link>https://dev.to/marcoservetto/42-multi-for-each-and-views-29b5</link>
      <guid>https://dev.to/marcoservetto/42-multi-for-each-and-views-29b5</guid>
      <description>&lt;p&gt;New 42 video OUT NOW&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=jqux-j2wkVc"&gt;https://www.youtube.com/watch?v=jqux-j2wkVc&lt;/a&gt;&lt;br&gt;
Did you ever wanted to iterate on multiple collections at the same time?&lt;br&gt;
Did you never liked the functional 'zip' but you did not know why?&lt;br&gt;
look to the video!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Advent of code, last puzzle</title>
      <dc:creator>Marco Servetto</dc:creator>
      <pubDate>Sat, 25 Dec 2021 08:00:12 +0000</pubDate>
      <link>https://dev.to/marcoservetto/advent-of-code-last-puzzle-5gen</link>
      <guid>https://dev.to/marcoservetto/advent-of-code-last-puzzle-5gen</guid>
      <description>&lt;p&gt;And this is the solution for the last day of advent of code 2021&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reuse [L42.is/AdamsTowel]
Fs = Load:{reuse[L42.is/FileSystem]}

Cell = Collection.Enum:{
  Right={},Down={},Empty={}
  }
Map = (
  input = Fs.Real.#$of().read(\"input")
  var I col = 0I
  var I row = 0I
  for s in input.split(S.nl()) (row+=1I)
  for s in input.split(S.nl())().split() (col+=1I)
  Collection.matrix(Cell.List,row=row,col=col)
  )//row=137, col=139

Right = {class method Map.Coord (Map.Coord that) = 
  that.with(col=(\col+1I).mod(139I))
  }
Down = {class method Map.Coord (Map.Coord that)=
  that.with(row=(\row+1I).mod(137I))
  }

StepRight = {class method Map (Map map) =(
  res = Map(\()(for v in map \add(v)))
  for c in map.coords(), v in map if v==Cell.Right() (
    cr = Right(c)
    go = map.val(cr)==Cell.Empty()
    if go (
      res.set(cr val=v)
      res.set(c val=Cell.Empty())
      )
    )
  res
  )}
StepDown = {class method Map (Map map) =(
  res = Map(\()(for v in map \add(v)))
  for c in map.coords(), v in map if v==Cell.Down() (
    cd = Down(c)
    go = map.val(cd)==Cell.Empty()
    if go (
      res.set(cd val=v)
      res.set(c val=Cell.Empty())
      )
    )
  res
  )}

Main=(
  input = Fs.Real.#$of().read(\"input")
  var imm map = Map(\()(
    for s in input.split(S.nl()) for c in s.split()(
      if c==S"." ( \add(Cell.Empty()) )
      if c==S"&amp;gt;" ( \add(Cell.Right()) )
      if c==S"v" ( \add(Cell.Down()) )
    )))
  res = 0I.acc()(while Bool.true() (
    new = StepDown(map=StepRight(map=map))
    if new==map ( Break() )
    map:=new
    v=\val
    \addOne
    ))
  Debug(res)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Overall, this adventure guided me to try many different kinds of algorithms in 42, and by doing so I discover a bunch of bugs and missing features in AdamsTowel.&lt;/p&gt;

&lt;p&gt;Still, this programming style is very far from the ultimate goal of 42: Enforcing modular security.&lt;br&gt;
This is where 42 shines, but this mostly emerges when the program is large enough that can not be reasonably done by a single programmer and where long term maintenance is kept into account.&lt;/p&gt;

&lt;p&gt;I will be back to Dev.to after the new year break to talk more about the details of 42 and on how it enforces modular security.&lt;/p&gt;

&lt;p&gt;Marco!&lt;/p&gt;

</description>
      <category>adventofcode</category>
      <category>adventofcode2021</category>
      <category>programming</category>
      <category>42</category>
    </item>
    <item>
      <title>Advent of code Day 21</title>
      <dc:creator>Marco Servetto</dc:creator>
      <pubDate>Tue, 21 Dec 2021 09:26:38 +0000</pubDate>
      <link>https://dev.to/marcoservetto/advent-of-code-day-21-51kb</link>
      <guid>https://dev.to/marcoservetto/advent-of-code-day-21-51kb</guid>
      <description>&lt;p&gt;The two parts for the problem of today are well divided, so I will present them one at a time.&lt;br&gt;
Or, if you prefer, you can just look to my comments on you tube:&lt;br&gt;
(&lt;a href="https://www.youtube.com/watch?v=61v1SNA79V0"&gt;https://www.youtube.com/watch?v=61v1SNA79V0&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Part 1 is really trivial.&lt;br&gt;
Note the trick with the modulo arithmetic, where we store a position that is 1 less than the intended position of the player.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;D100 = Data:{
  var I rolled = 0I
  var I seed = 0I
  mut method I roll()=(
    \rolled(\rolled+1I)
    \seed((if \seed&amp;gt;=100I 0I else \seed)+1I)
    \seed
    )
  }
Player = Data:{
  var I pos
  var I points = 0I
  mut method Void turn(mut D100 that) =(
    tot = that.roll()+that.roll()+that.roll()
    \pos((\pos+tot).mod(10I)) //pos from 0--10, real pos is +1
    \points(\points+\pos+1I)
    )
  read method Bool won() = \points&amp;gt;=1000I
  }
Main=(
  p1 = Player(pos=0I) //1-1
  p2 = Player(pos=2I) //3-1
  dice = D100()
  while !p1.won() &amp;amp;&amp;amp; !p2.won() (
    p1.turn(dice)
    if !p1.won() (p2.turn(dice))
    )
  if p1.won() (
    Debug(S"p2 loses for %p2.points(), %dice.rolled(), %(p2.points()*dice.rolled())")
    )
  if p2.won() (
    Debug(S"p1 loses for %p1.points(), %dice.rolled(), %(p1.points()*dice.rolled())")
    )
  //p1 loses for 671, 1338, 897798
  )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Part 2 is much more interesting, and it was feasible thanks to @Cache.Lazy; in this way our 'recursive' call may simply return a pre-computed value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Report = Data:{
  Num p1Wins
  Num p2Wins
  method Bool open() = \p1Wins+\p2Wins==0Num
  method This +(This that) = This(
    p1Wins=this.p1Wins()+that.p1Wins(),
    p2Wins=this.p2Wins()+that.p2Wins()
    )
  }
State = Data:{
  I p1Pos
  I p2Pos
  I p1Score = 0I
  I p2Score = 0I
  method This turn(I p1Roll) = (
    (p1Pos0,p2Pos0,p1Score0,p2Score0) = this
    p1Pos = (p1Pos0+p1Roll).mod(10I) //from 0--10, real pos is +1
    p1Score = p1Score0+p1Pos+1I
    This(p1Pos=p1Pos,p2Pos=p2Pos0,
         p1Score=p1Score,p2Score=p2Score0)
    )
  method This turn(I p2Roll) = (
    (p1Pos0,p2Pos0,p1Score0,p2Score0) = this
    p2Pos = (p2Pos0+p2Roll).mod(10I) //from 0--10, real pos is +1
    p2Score = p2Score0+p2Pos+1I
    This(p1Pos=p1Pos0,p2Pos=p2Pos,
         p1Score=p1Score0,p2Score=p2Score)
    )
  method Report directWin() = {
    (p1Pos,p2Pos,p1Score,p2Score) = this
    X[p1Score&amp;lt;21I || p2Score&amp;lt;21I]
    if p1Score&amp;gt;=21I return \(p1Wins=1\ p2Wins=0\)
    if p2Score&amp;gt;=21I return \(p1Wins=0\ p2Wins=1\)
    return \(p1Wins=0\ p2Wins=0\)
    }
  @Cache.Lazy method Report wins() = {
    Debug(this)
    var res = this.directWin()
    if !res.open() return res
    r = Range(1I to=4I)      
    for a1 in r,for a2 in r,for a3 in r {
      stateA = this.turn(p1Roll=a1+a2+a3)
      resA = stateA.directWin()
      if !resA.open() return res+=resA
      return for b1 in r,for b2 in r,for b3 in r {
        stateB = stateA.turn(p2Roll=b1+b2+b3)
        resB = stateB.directWin()
        if !resB.open() return res+=resB
        return res+=stateB.wins()
        }
      }
    return res
    }
  }
Main = (
  s = State(p1Pos=0I,p2Pos=2I)
  Debug(s.wins())
  //Report(p1Wins=48868319769358,p2Wins=22432440913119)
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What do you think? with minor modifications I could combine automatic parallelism and automatic caching for an even faster version.&lt;/p&gt;

</description>
      <category>adventofcode</category>
      <category>adventofcode2021</category>
      <category>caching</category>
      <category>memoization</category>
    </item>
    <item>
      <title>Advent of code Day 20</title>
      <dc:creator>Marco Servetto</dc:creator>
      <pubDate>Mon, 20 Dec 2021 11:23:05 +0000</pubDate>
      <link>https://dev.to/marcoservetto/advent-of-code-day-20-1n10</link>
      <guid>https://dev.to/marcoservetto/advent-of-code-day-20-1n10</guid>
      <description>&lt;p&gt;Finally a day where I can feel satisfied with my code results.&lt;br&gt;
I explain this code in the detail on my youtube channel, (&lt;a href="https://www.youtube.com/watch?v=lqA_XT0U4ng"&gt;https://www.youtube.com/watch?v=lqA_XT0U4ng&lt;/a&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reuse [L42.is/AdamsTowel]

Fs = Load:{reuse[L42.is/FileSystem]}

Point = Data.AddList:Data:{ I x, I y
  method Point +(Point that) =
    \(x=this.x()+that.x(),y=this.y()+that.y())
  method Point max(Point that)=(
    (x0,y0) = this
    (x1,y1) = that
    Point(x=x0.max(x1),y=y0.max(y1))
    )
  method Point min(Point that) = 
    this.with(x=\x.min(that.x()))&amp;lt;:This.with(y=\y.min(that.y()))
  }

Map = Data:{
  M = Collection.map(key=Point,val=Bool)
  mut M map=\()
  var Bool outer=Bool.false()
  var Point topLeft = Point(x=0I,y=0I)
  var Point bottomRight = Point(x=0I,y=0I)

  mut method Void put(Point key, Bool val) = (
    \#map.put(key=key,val=val)
    \topLeft(\topLeft.min(key))
    \bottomRight(\bottomRight.max(key))
    )
  read method Bool val(Point key) =
    \map.val(key=key).val(orElse=this.outer())

  read method I nearBin(Point that) = 0I.acc()((
    var n = 512I
    for xi in Range(I"-1" to=2I), for yi in Range(I"-1" to=2I) (
      n/=2I
      if this.val(key=Point(x=xi,y=yi)+that) \add(n)
      )
    ))
  read method mut This step(Key that) = (
    mut This new = This()
    (x0,y0) = \topLeft
    (x1,y1) = \bottomRight
    for x in Range(x0-1I to=x1+2I), for y in Range(y0-1I to=y1+2I) (
      key=Point(x=x,y=y)
      i=\nearBin(key)
      X[i.isInRange(0I to=512I)] // is my binary conversion correct?
      new.put(key=key,val=that.val(i))
      )
    new.outer(if this.outer() that.right() else that.left())
    new
    )
  }
Key = Collection.list(Bool)

Main=(
  input = Fs.Real.#$of().read(\"input").split(S.nl()++S.nl())
  imm key = Key()( for i in input().split() \add(i==S"#") )
  X[key.size()==512I]
  var map = Map()
  for line in input().split(S.nl()), li in Range.unbounded() (
    for c in line.split(), ci in Range.unbounded() (
      map.put(key=\(x=li,y=ci),val=c==S"#")
      )
    )
  for i in Range(2I) ( map:=map.step(key) )  //50I
  tot = 0I.acc()(for (val) in map.map() \addIf(val))
  Debug(tot)
  )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>adventofcode</category>
      <category>adventofcode2021</category>
      <category>syntax</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Advent of code Day 18</title>
      <dc:creator>Marco Servetto</dc:creator>
      <pubDate>Sat, 18 Dec 2021 21:41:28 +0000</pubDate>
      <link>https://dev.to/marcoservetto/advent-of-code-day-18-4io2</link>
      <guid>https://dev.to/marcoservetto/advent-of-code-day-18-4io2</guid>
      <description>&lt;p&gt;(Note: for more 42, go to: &lt;a href="https://www.youtube.com/MarcoServetto"&gt;https://www.youtube.com/MarcoServetto&lt;/a&gt;) &lt;br&gt;
Ok, finally I have a solution for day 18.&lt;br&gt;
Those tasks are getting more complex day after day.&lt;br&gt;
This time part 2 was trivial after doing part1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reuse [L42.is/AdamsTowel]

Fs = Load:{reuse[L42.is/FileSystem]}

Unit = Load:{reuse[L42.is/Unit]}

Tree = {interface [HasToS]
  method I magnitude()
  method Tree reduce()
  }  
Trees = Collection.list(Tree)

Leaf = Class:Trait(Unit(I)):{[Tree]
  method magnitude()=\#inner

  method reduce()={
    if \#inner&amp;lt;10I return this
    half1 = \#inner/2I
    half2 = if half1+half1==\#inner half1 else half1+1I
    return Node(left=This(half1),right=This(half2))
    }
  }  
Node = Data:{[Tree,HasToS]
  Tree left, Tree right

  method toS()=S"[%this.left(),%this.right()]"

  method I magnitude() = 
    (\left.magnitude()*3I)+(\right.magnitude()*2I)

  method reduce()={
    Explode(Tree tree) = Explode(k=0I,t=this)
    if tree.toS()!=this.toS() return tree.reduce()
    Tree split = Split(this)
    if split.toS()!=this.toS() return split.reduce()
    return this
    }
  }
Split = {class method Tree(Tree that)={
  if Node(left,right)=that (
    l = This(left)
    if l.toS()!=left.toS() return Node(left=l,right=right)
    return Node(left=l,right=This(right))
    )
  return that.reduce()
  }}
Explode = Data:{
  I left,Tree tree, I right 
  class method This(I k, Tree t) = {
    if t&amp;lt;:Leaf return This(left=0I,tree=t,right=0I)
    if t&amp;lt;:Node (
      return \node(k=k,t=t)
      )
    error X"exaustive Leaf+Node"
    }
  class method This node4(Node t) = {
    if (Leaf left,Leaf right) = t return This(left=left.#inner(),tree=Leaf"0",right=right.#inner())
    error X"exaustive %t"
    }
  class method This node(I k,Node t) = {
    (left,right)=t
    sLeft  = \stable(k=k+1I,t=left)
    sRight = \stable(k=k+1I,t=right) 
    if sLeft &amp;amp;&amp;amp; sRight return This(left=0I,tree=t,right=0I)
    (left1,tree,right2)=This(k=k+1I t=if !sLeft left else right)
    if !sLeft return This(left=left1
    ,,tree=Node(left=tree,right=This.sumLeft(n=right2,t=right)) right=0I)
    return This(left=0I
    ,,tree=Node(left=This.sumRight(n=left1,t=left),right=tree) right=right2)
    }
  class method Bool stable(I k, Tree t) = {
    if Node(left,right)=t return k!=4I &amp;amp;&amp;amp; \stable(k=k+1I,t=left) &amp;amp;&amp;amp; \stable(k=k+1I, t=right)
    return Bool.true()
    }
   class method Tree sumLeft(I n,Tree t) = {
     if t&amp;lt;:Leaf return t+Leaf(n)
     if Node(left,right)=t return Node(left=this.sumLeft(n=n,t=left),right=right)
     error X"exaustive"
     }
   class method Tree sumRight(I n,Tree t) = {
     if t&amp;lt;:Leaf return t+Leaf(n)
     if Node(left,right)=t return Node(left=left, right=this.sumRight(n=n,t=right))
     error X"exaustive"
     }
  }
ParseTree = Data:{
  S string
  var I index = 0I
  mut method Bool check(S that) = 
    \string.startsWith(that leftOffSet=this.index())
  mut method Void pop(S that) = (
     X[this.check(that)]
     \index(\index+that.size())
     )
  mut method S take(S until) = (
    (string,index) = this
    i = string.indexOf(until from=index)
    \index(i)
    string.subString(index to=i)
    )
  mut method Tree(S t) = 
    if !\check(S"[") \leaf(t=t) else \node(t=t)

  mut method Leaf leaf(S t) = \(string=this.take(until=t))

  mut method Node node(S t) = (
    \pop(S"[")
    left = this(t=S",")
    \pop(S",")
    right = this(t=S"]")
    \pop(S"]")
    Node(left=left,right=right)
    )
  }
SumAll = {class method Tree (Trees that) = {
  X[!that.isEmpty()]
  (size,left,withoutLeft) = that
  if size==1I return left
  res  = Node(left=left,right=withoutLeft.left()).reduce()
  return This(withoutLeft.with(left=res))
  }}
SumMax = {
  class method I (Trees that) = (
    exp=This.expand(that)
    0I.acc()(for e in exp, i in Range(exp.size()) (
      \val(\val.max(e.reduce().magnitude()))
      ))
    )
  class method Trees expand(Trees that) = \()(
    for a in that for b in that if a.toS()!=b.toS() (
      \add(Node(left=a,right=b))
      ))
  }
Main=(
  input = Fs.Real.#$of().read(\"input")
  imm trees = Trees()(for line in input.split(S.nl()) \add(ParseTree(string=line)(t=S"")))
  all=SumAll(trees)
  Debug(all.magnitude())//3647
  max=SumMax(trees)
  Debug(max)//4600
  )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, figuring out the explosion algorithm was quite challenging, and I had to switch using the 'formal language' that is commonly used for designing programming languages.&lt;br&gt;
If you are curious, here is explode in formalism:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; #define explode(k,t) = n1 t n2 //0 if none
 explode(k,n) = empty,n,empty
 explode(4,[n1,n2]) = n1,0,n2
 explode(k,[t1,t2]) = [t1,t2]
   k!=4
   stable(k,t1)
   stable(k,t2)
 explode(k,[t1,t2]) = n1,[t1',sumLeft(n2,t2)],0
   k!=4
   !stable(k,t1)
   n1,t1',n2 = explode(k+1,t1)
 explode(k,[t1,t2]) = 0,[sumRight(n1,t1),t2'],n2
   k!=4
   stable(k,t1)
   !stable(k,t2)
   n1,t2',n2 = explode(k+1,t2)   

 stable(k,n)
 stable(k,[t1,t2]) = k!=4 &amp;amp;&amp;amp; stable(k+1,t1) &amp;amp;&amp;amp; stable(k+1,t2)

 sumLeft(n0,n) = n+n0
 sumLeft(n0,[t1,t2]) = [sumLeft(n0,t1),t2]
 sumRight(n0,n) = n+n0
 sumRight(n0,[t1,t2]) = [t1,sumRight(n0,t2)]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, I'm using a functional approach to do the reduction.&lt;br&gt;
I'm not sure if a 'mutation' based approach would have been easier.&lt;/p&gt;

</description>
      <category>adventofcode</category>
      <category>adventofcode2021</category>
      <category>oop</category>
    </item>
    <item>
      <title>Advent of code Day 17</title>
      <dc:creator>Marco Servetto</dc:creator>
      <pubDate>Fri, 17 Dec 2021 21:16:23 +0000</pubDate>
      <link>https://dev.to/marcoservetto/advent-of-code-day-17-4cf6</link>
      <guid>https://dev.to/marcoservetto/advent-of-code-day-17-4cf6</guid>
      <description>&lt;p&gt;Nothing special to discuss this time.&lt;br&gt;
I think I've done the dumbest exhaustive search ever, without even too much consideration to get good bounds.&lt;br&gt;
The interesting bit is the formatting:&lt;br&gt;
do you think this code is formatted ok? in 42 you can write in a very different style, and it is not clear how to format certain things.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reuse [L42.is/AdamsTowel]
Point = Data:{Num x, Num y}
Velocity = Data:{Num x, Num y}
Probe = Data:{
  var Point location = \(x=0Num,y=0Num)
  var Velocity velocity
  mut method Void step() = (
    (x,y) = \velocity
    \location(\location.with(x=\x+x).with(y=\y+y))
    \velocity(\velocity
      .with(x=0Num.max(x-1Num)).with(y=y-1Num))
    )
  }
Square = Data:{
  Num x1, Num x2, Num y1, Num y2
  method Bool #in1 (Point that)=
    that.x().isInRange(this.x1() to=this.x2()) 
    &amp;amp;&amp;amp; that.y().isInRange(this.y1() to=this.y2())
  }

Main = (
  //input=S"target area: x=88..125, y=-157..-103"
  Square target=Square(
    x1=88Num x2=126Num y1=Num"-157" y2=Num"-102")
  result = 0Num.acc()(
    for x in Range(0I to=1000I),
    for y in Range(I"-300" to=1500I) (
      v = Velocity(x=\(x),y=\(y))
      p = Probe(velocity=v)
      res = 0Num.acc()(while !(p.location() in target) 
        ,,&amp;amp;&amp;amp; p.location().y()&amp;gt;=target.y1()
        ,,&amp;amp;&amp;amp; p.location().x()&amp;lt;=target.x2() ( 
        p.step() 
        \val(\val.max(p.location().y()))
        ))
      //Part1
      //if p.location() in target \val(\val.max(res))
      if p.location() in target \addOne()//Part2
      //\val(\val.max(res))
      )
    )
  Debug(result)//12246 and 3528
  )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, consider this part:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;res = 0Num.acc()(while !(p.location() in target) 
  ,,&amp;amp;&amp;amp; p.location().y()&amp;gt;=target.y1()
  ,,&amp;amp;&amp;amp; p.location().x()&amp;lt;=target.x2() ( 
  p.step() 
  \val(\val.max(p.location().y()))
  ))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In 42 the comma (,) is just a spacing character like space and new line, so we can use it as often as we want. In practice, it can be used to distinguish different kinds of indentation.&lt;br&gt;
Here I'm using it to differentiate the indentation of the long while condition and the indentation of the while body.&lt;br&gt;
This is further complicated by the fact that the while is the argument of the #apply method of Num.Acc.&lt;/p&gt;

&lt;p&gt;Ideas?&lt;/p&gt;

</description>
      <category>adventofcode</category>
      <category>adventofcode2021</category>
      <category>programming</category>
      <category>42</category>
    </item>
  </channel>
</rss>
