<?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: Alex Esoposting</title>
    <description>The latest articles on DEV Community by Alex Esoposting (@olus2000).</description>
    <link>https://dev.to/olus2000</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%2F627763%2F3318a5a4-c71a-46c6-92b4-54f8203e41f8.png</url>
      <title>DEV Community: Alex Esoposting</title>
      <link>https://dev.to/olus2000</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/olus2000"/>
    <language>en</language>
    <item>
      <title>Designing an Elm component</title>
      <dc:creator>Alex Esoposting</dc:creator>
      <pubDate>Sat, 20 Aug 2022 14:42:23 +0000</pubDate>
      <link>https://dev.to/olus2000/designing-an-elm-component-1meb</link>
      <guid>https://dev.to/olus2000/designing-an-elm-component-1meb</guid>
      <description>&lt;p&gt;In my previous article I have told you a lot of theory about Elm, but to make you see why I like it so much I want to walk you through the process of designing a component for one of my projects. Hopefully you will see how the design choices are influenced by the language and reflected in the code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Goal
&lt;/h3&gt;

&lt;h6&gt;
  
  
  What I'm trying to build
&lt;/h6&gt;

&lt;p&gt;I'm a player of &lt;a href="//np.ironhelmet.com"&gt;Neptune's Pride&lt;/a&gt; - an online strategy game set in space where players fight over stars. I am hosting &lt;a href="https://olus2000.pl/nptimelapse"&gt;a tool&lt;/a&gt; that lets players record their games and renders animated replays (timelapses) of them in an .mp4 format. I want to use Elm to provide timelapses online, animated as an SVG controlled by a slider.&lt;/p&gt;

&lt;h3&gt;
  
  
  View
&lt;/h3&gt;

&lt;h6&gt;
  
  
  How I want the user to see it
&lt;/h6&gt;

&lt;p&gt;There are tree core elements of Elm application design: model, view and controller. View is the part that translates your model into DOM elements that your user can see and interact with, so to design the view it's best to try and imagine what you want your users to experience. This is how I saw it for the online timelapse tool:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The user enters the online timelapse page with a button from a game's page.&lt;/li&gt;
&lt;li&gt;The user sees a loading screen.&lt;/li&gt;
&lt;li&gt;The screen changes to contain an SVG picture of the starting state of the game and animation controls.&lt;/li&gt;
&lt;li&gt;The user can control which tick of the game is displayed with a slider.&lt;/li&gt;
&lt;li&gt;The user can play and pause the animation using a button.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These user stories will not only describe the design of the view, but also be a starting point for designing the model, and help with the controller. At this point I usually draw myself a sketch of how the page will look like, and I keep adjusting it as I progress through the design process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qqiMxV5q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0mqag2kejxkl5vi3emlt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qqiMxV5q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0mqag2kejxkl5vi3emlt.png" alt="Sample sketch" width="398" height="540"&gt;&lt;/a&gt;&lt;/p&gt;
Don't put too much effort into your sketches



&lt;h3&gt;
  
  
  Model
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Turining your view into data
&lt;/h6&gt;

&lt;p&gt;With some user stories and a rough idea of the layout of the component I proceed to design the model: a data structure that will hold the state of the app at all times. This can be done very well using Elm's type system, so I create a file &lt;code&gt;scratchpad.elm&lt;/code&gt; to hold my notes before I create the project. From the user stories we see that the model should contain:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Timelapse data (possibly unloaded)&lt;/li&gt;
&lt;li&gt;Currently displayed tick&lt;/li&gt;
&lt;li&gt;State of the animation (Played/Paused)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Which translates to the following Elm code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;stars&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Stars&lt;/span&gt;
  &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tick&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
  &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;playing&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This model is incomplete. First of all it will definitely get expanded as the project progresses, but for now it uses a type &lt;code&gt;Stars&lt;/code&gt; that has not been defined. In my game recorder I store data about stars by assigning each star a list of pairs indicating when the star got captured by which player. Lets use this representation here, but also take into account the possibilities that star data may have not yet been loaded or that loading may have failed with some error message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Stars&lt;/span&gt;
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Stars&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="kt"&gt;Star&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Loading&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Failed&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Star&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Float&lt;/span&gt;
  &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Float&lt;/span&gt;
  &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;owners&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is enough of a model for now. Knowing what data we operate on and what our users might want to do with it let's proceed to the controller design.&lt;/p&gt;

&lt;h3&gt;
  
  
  Controller
&lt;/h3&gt;

&lt;h6&gt;
  
  
  What makes stuff happen
&lt;/h6&gt;

&lt;p&gt;This is the part that is responsible for reacting to events and updating the model. In Elm the controller is represented by an &lt;code&gt;update&lt;/code&gt; function that takes messages generated by the runtime system, but I also consider the initial processing to be a part of the controller.&lt;/p&gt;

&lt;p&gt;First lets tackle the messages. They represent events that our application might update on, which are: downloading star data finishing, time progression, manually changing the slider and pressing the play/pause button. This leaves us with a small &lt;code&gt;Msg&lt;/code&gt; type, and we don't have to worry about implementing reactions to these messages at the design level.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Msg&lt;/span&gt;
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;SliderChange&lt;/span&gt; &lt;span class="kt"&gt;Float&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Tick&lt;/span&gt; &lt;span class="kt"&gt;Time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Posix&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;PauseTrigger&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;GotStars&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Result&lt;/span&gt; &lt;span class="kt"&gt;Http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Error&lt;/span&gt; &lt;span class="kt"&gt;Stars&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;init&lt;/code&gt; function can take some arguments and must return a tuple containing an initial model and a command to execute by the runtime. The initial model is straightforward for now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;defaultModel&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt;
&lt;span class="n"&gt;defaultModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;stars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Loading&lt;/span&gt;
  &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;playing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;False&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command to be executed should fetch the stars info for us, so we need an address to fetch it from. The address will be passed as an argument to the &lt;code&gt;init&lt;/code&gt; function so that we can reuse the same static application for animating many different games. The resulting function is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;init&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Cmd&lt;/span&gt; &lt;span class="kt"&gt;Msg&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;init&lt;/span&gt; &lt;span class="n"&gt;sourceURL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;defaultModel&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;getStars&lt;/span&gt; &lt;span class="n"&gt;sourceURL&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This concludes the design of the controller. We can proceed to make the application!&lt;/p&gt;

&lt;h3&gt;
  
  
  Design complete!
&lt;/h3&gt;

&lt;h6&gt;
  
  
  That's it for now
&lt;/h6&gt;

&lt;p&gt;In this article I have shown an example of my design process when working with Elm. I hope you now have a better idea of how Elm encourages and supports design of the app with its application design model and rich type system. In the next article I will create an Elm project and start converting this scratchpad code into an actual working page component. See you soon!&lt;/p&gt;

</description>
      <category>elm</category>
      <category>webdev</category>
      <category>design</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Elm: a good frontend language?</title>
      <dc:creator>Alex Esoposting</dc:creator>
      <pubDate>Thu, 11 Aug 2022 06:18:23 +0000</pubDate>
      <link>https://dev.to/olus2000/elm-a-good-frontend-language-19bk</link>
      <guid>https://dev.to/olus2000/elm-a-good-frontend-language-19bk</guid>
      <description>&lt;p&gt;Recently a friend recommended me &lt;a href="https://elm-lang.org/"&gt;Elm&lt;/a&gt;, a pure functional language for developing web applications. I'd like to compare it to JavaScript and show how the functional paradigm can work in practical applications.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you need a refresher on what functional programming is check out &lt;a href="https://dev.to/olus2000/what-is-functional-programming-3fkg"&gt;my previous blog post&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Still just JavaScript
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Everything is JavaScript...
&lt;/h6&gt;

&lt;p&gt;JS is the only* language standard that is supported by all major browsers and allows for interactive apps. If you want something except from a static HTML you need to use JS. To write an app in another language you need to compile it to pure JS, like many web languages do. Most of them, like TypeScript, are based on JS and offer limited improvement over JS problems. Elm is something entirely different, having no trace of JS's imperative syntax and error prone type coercion, but still compiles to JS in the end to be run by the browser.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Web assembly is starting to gain popularity, but it's still in developement&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  No runtime errors
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Everything works
&lt;/h6&gt;

&lt;p&gt;By being a pure functional language Elm can guarantee that no matter the time of day, the randomness or the user input, the app will always be ready for whatever comes and will process it without breaking. That doesn't mean &lt;em&gt;any&lt;/em&gt; input is valid, just that invalid input will produce error &lt;em&gt;values&lt;/em&gt;, which must be explicitly handled in the code or else it won't compile.&lt;/p&gt;

&lt;p&gt;Let's take string to number conversion for example. In JS there are multiple ways of doing it: &lt;code&gt;Number(x)&lt;/code&gt;, &lt;code&gt;parseInt(x)&lt;/code&gt;, &lt;code&gt;+x&lt;/code&gt; among others. All of them return a value of type "number", but they can actually output what is literally Not a Number, and in case of &lt;code&gt;+x&lt;/code&gt; they may even just silently ignore invalid strings and return &lt;code&gt;0&lt;/code&gt;. This may lead to some problems in later stages of the program which will be hard to debug because of those silent errors. Solutions to this include a separate check if the string is numeric, which arguably should be a part of the parsing function, and checking for &lt;code&gt;NaN&lt;/code&gt;s every time you handle a "number", which should not be necessary because &lt;code&gt;NaN&lt;/code&gt; is Not a Number.&lt;/p&gt;

&lt;p&gt;In contrast, Elm's &lt;code&gt;String.toInt&lt;/code&gt; function returns a &lt;code&gt;Maybe Int&lt;/code&gt; type, which can have values &lt;code&gt;Nothing&lt;/code&gt; or &lt;code&gt;Just Int&lt;/code&gt;. This is an explicit sign: "Something can go wrong here and requires handling!". This value can be then passed along as &lt;code&gt;Maybe Int&lt;/code&gt;, or it can be branched on and some handling can be done in case of &lt;code&gt;Nothing&lt;/code&gt;, and in case of &lt;code&gt;Just Int&lt;/code&gt; just the &lt;code&gt;Int&lt;/code&gt; portion can be returned. In fact this pattern is so common that there already are functions for handling &lt;code&gt;Maybe&lt;/code&gt; values, the most obvious one being &lt;code&gt;withDefault&lt;/code&gt; which takes a default value and substitutes it in case of &lt;code&gt;Nothing&lt;/code&gt;. This is again explicit and allows more control over the behaviour of the process instead of silently passing erroneous values and creating an undebuggable mess.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Elm Architecture
&lt;/h3&gt;

&lt;h6&gt;
  
  
  That apparently everyone uses?
&lt;/h6&gt;

&lt;p&gt;Being a pure functional language Elm can't have functions that rely on data outside their inputs or change data outside their outputs. This means working with time, randomness, user input and DOM shouldn't be possible to implement. Elm solves this problem in a very elegant way: you don't write the entire application. It has a runtime system, and your job as a programmer is to provide this system with three things: the initial application state represented in Elm values, a function from state to DOM that will be displayed to the user, and another function from the state and messages that the user may send to a new, updated state. The runtime then renders the page using the initial state and the view function and handles any user interaction by sending appropriate messages that you defined to the update function. All impurity is abstracted to the runtime, so the user can stay in the pure language of Elm.&lt;/p&gt;

&lt;p&gt;There are multiple benefits from this. First of all getting to stay pure guarantees no runtime errors. All the input data comes as messages that you defined and are forced to handle in the update function. Pure functions also benefit heavily from compiler optimisation and caching results: even though the view function is called after every update the code is compiled to only calculate parts of the page that changed which keeps the system efficient.&lt;/p&gt;

&lt;p&gt;The Elm architecture also serves as a good design pattern that makes thinking about your code easier. It makes you make a model - a representation of the application state in terms of in-language values - and consider what states your app can be in and what can change about it. It forces you to consider all transitions of this state: what to show the user when the additional content loads? How to react when it fails to load? All this is turned into messages that have to be handled or the code doesn't compile. According to the Elm website many JS frameworks and other application languages have adopted the Elm architecture, more or less openly. It's a design pattern that any language can benefit from.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;h6&gt;
  
  
  I like Elm
&lt;/h6&gt;

&lt;p&gt;I hate JavaScript and believe that it's a fundamentally badly designed language, so in my personal projects I have been going out of my way to minimize its use. Elm offers a much better designed alternative despite also having flaws. I can't say about enterprise use, but I definitely recommend Elm for the interactive components of your pages, and If you want a taste of it I will be showing how I designed the &lt;a href="https://olus2000.pl/nptimelapse/game/6611290288816128/interactive"&gt;online timelapse visualisation&lt;/a&gt; for one of my projects in my future posts. Stay tuned!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>elm</category>
      <category>javascript</category>
      <category>functional</category>
    </item>
    <item>
      <title>What is functional programming?</title>
      <dc:creator>Alex Esoposting</dc:creator>
      <pubDate>Tue, 02 Aug 2022 17:26:20 +0000</pubDate>
      <link>https://dev.to/olus2000/what-is-functional-programming-3fkg</link>
      <guid>https://dev.to/olus2000/what-is-functional-programming-3fkg</guid>
      <description>&lt;p&gt;I'm planning on doing a couple of posts on the language Elm and why it may be better than JS, but to fully understand what happens in Elm you need to know some things about the &lt;strong&gt;functional paradigm&lt;/strong&gt; - a clean view on what programming is, designed with mathematical perfection.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functions
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Functions everywhere.
&lt;/h6&gt;

&lt;p&gt;As the name implies functional programming is all about functions. Everything is done using them, and applying the function to a parameter is the fundamental operation. In imperative languages like Python, C++ or JS, to apply a function &lt;code&gt;foo&lt;/code&gt; to &lt;code&gt;x&lt;/code&gt; you use parenthesis: &lt;code&gt;foo(x)&lt;/code&gt;, but in functional languages it's sufficient to write the function next to the value: &lt;code&gt;foo x&lt;/code&gt;. Parenthesis come up when applying a function to another function's result. For a bigger example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Imperative:
&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;vs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- Functional:
foo x (bar y) baz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This syntax is cleaner and more readable when most of the program is function application: not even commas are required to separate arguments.&lt;/p&gt;

&lt;p&gt;Everything in the functional paradigm is a function. Even normal operators like &lt;code&gt;+&lt;/code&gt; or &lt;code&gt;*&lt;/code&gt; are considered functions. In most functional languages you can put parenthesis around them and apply them like any other function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(x + y) = (+) x y
(x * y) = (*) x y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Functions can be passed as arguments and assigned to variables. This allows for some powerful operators that I will discuss in later posts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Purity
&lt;/h3&gt;

&lt;h6&gt;
  
  
  No side effects allowed
&lt;/h6&gt;

&lt;p&gt;Functional languages deal with &lt;strong&gt;pure functions&lt;/strong&gt;, which are not allowed to alter any internal or external state. This means a pure function's output can only depend on its inputs, not on time of execution or randomness. It can also not affect outcomes of other operations in any way other than through returning values.&lt;/p&gt;

&lt;p&gt;This may seem as overly restrictive: after all the screen is an external state, and how are we supposed to write an application that doesn't depend on time? Most functional languages have their mechanisms for dealing with those problems, usually behind the scenes, without introducing impurity to the programmer's code.&lt;/p&gt;

&lt;p&gt;Purity has its benefits: a pure function for a given input will always return the same output, so there may not be a need to recalculate it later. The execution of a part of the program never depends on anything outside of it. This opens opportunities for optimizing compilation and makes thinking about code way easier.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strong Types
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Like in C or Java, but good.
&lt;/h6&gt;

&lt;p&gt;Another property of most functional languages is that they are &lt;strong&gt;strongly typed&lt;/strong&gt;. What this means is that all data flowing through the program has to be assigned a type, like &lt;code&gt;String&lt;/code&gt; or &lt;code&gt;Int&lt;/code&gt; or &lt;code&gt;Float&lt;/code&gt;. Every value traveling along some path in the program has to always have the same type.&lt;/p&gt;

&lt;p&gt;In languages like C++ or Java this leads to a ton of unnecessary type annotations that are annoying and obscure the meaning of the code. In functional programs types can almost always be inferred from their surroundings, lifting the burden of attaching labels to everything from the programmer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Type inference&lt;/strong&gt; means that from knowing what type the basic literals are (like &lt;code&gt;21&lt;/code&gt; or &lt;code&gt;"Hello"&lt;/code&gt;), and what types the basic operators take and return the compiler can reason about types of other values and functions, in theory requiring almost no type annotations. It's still a good practice to annotate your function definitions so that the compiler can warn you when the type you want doesn't match what is inferred.&lt;/p&gt;

&lt;h3&gt;
  
  
  Algebraic types
&lt;/h3&gt;

&lt;h6&gt;
  
  
  We need to go deeper...
&lt;/h6&gt;

&lt;p&gt;In imperative languages data types reflect how a value is stored in computer memory: &lt;code&gt;int&lt;/code&gt; is a four byte number, &lt;code&gt;string&lt;/code&gt; is an array of bytes and &lt;code&gt;float&lt;/code&gt; is a floating point number as described by &lt;a href="https://en.wikipedia.org/wiki/IEEE_754"&gt;The Standard&lt;/a&gt;. In functional languages data types reflect what the value represents. You have basic types that all have some implementation-defined representation in memory, but you can also define your own types, usually on top of existing ones.&lt;/p&gt;

&lt;p&gt;Types are defined with constructor functions. For example &lt;code&gt;Boolean&lt;/code&gt; may be defined as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Boolean = True | False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means the &lt;code&gt;Boolean&lt;/code&gt; type can be constructed by either &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt; constructors. Neither of them take any arguments, so they always return the same values which can later be interpreted as "true" and "false". A more complex example that uses arguments is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Webpage
  = Loading
  | Loaded Html
  | LoadingError String
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This represents the fact that a &lt;code&gt;Webpage&lt;/code&gt; may be one of three:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Loading&lt;/code&gt;, in which case no additional info about it is available,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Loaded&lt;/code&gt;, in which case it has a value of type &lt;code&gt;Html&lt;/code&gt; to display,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;LoadingError&lt;/code&gt;, in which case there's an error message available describing what went wrong. This allows for precise modelling of what your data means and can possibly be.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Pattern matching
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Elegant conditionality
&lt;/h6&gt;

&lt;p&gt;Algebraic types naturally represent options available to a given value, so the most used conditional construction in functional programming is one that branches based on the constructor. The usual syntax looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;case value of
  Some pattern -&amp;gt; result
  Other pattern -&amp;gt; different result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Going with the &lt;code&gt;Webpage&lt;/code&gt; example we could construct a function that takes a &lt;code&gt;Webpage&lt;/code&gt; value and returns a rendered view like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;renderPage : Webpage -&amp;gt; Rendered
renderPage page =
  case page of
    Loading -&amp;gt; renderText "Loading..."
    Loaded html -&amp;gt; renderHtml html
    LoadingError message -&amp;gt; renderText message
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This may be a bit to take in, so let's go through it line by line:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;renderPage : Webpage -&amp;gt; Rendered&lt;/code&gt;&lt;br&gt;
This is how type annotations look like for functions. &lt;code&gt;renderPage&lt;/code&gt; is a function that takes a &lt;code&gt;Webpage&lt;/code&gt; and returns &lt;code&gt;Rendered&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;renderPage page =&lt;/code&gt;&lt;br&gt;
Start of the function definition. &lt;code&gt;page&lt;/code&gt; is the function argument.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;case page of&lt;/code&gt;&lt;br&gt;
Start of a &lt;code&gt;case&lt;/code&gt; expression.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Loading -&amp;gt; renderText "Loading..."&lt;/code&gt;&lt;br&gt;
If &lt;code&gt;page&lt;/code&gt; was made with the &lt;code&gt;Loading&lt;/code&gt; constructor display the loading text.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Loaded html -&amp;gt; renderHtml html&lt;/code&gt;&lt;br&gt;
If &lt;code&gt;page&lt;/code&gt; was made with the &lt;code&gt;Loaded&lt;/code&gt; constructor render it's parameter as HTML.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;LoadingError message -&amp;gt; renderText message&lt;/code&gt;&lt;br&gt;
Finally if &lt;code&gt;page&lt;/code&gt; was made with the &lt;code&gt;LoadingError&lt;/code&gt; then display the error message.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can see how a &lt;code&gt;case&lt;/code&gt; expression can be used to &lt;em&gt;deconstruct&lt;/em&gt; the value and assign its contents to variables (&lt;code&gt;html&lt;/code&gt; and &lt;code&gt;message&lt;/code&gt; in the example). This way of branching can also make sure that the program &lt;em&gt;never breaks&lt;/em&gt;, because every possibility has to be accounted for. Any errors have to be explicit (like &lt;code&gt;LoadingError&lt;/code&gt; in the example) and passed around openly as values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sidenote: Type theory
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Very rough explanation.
&lt;/h6&gt;

&lt;p&gt;Algebraic data types are such a powerful concept that you don't need any builtin types to make programs. You can define numbers and lists and operate on them without referencing any existing types. I have already shown the easiest example, the &lt;code&gt;Boolean&lt;/code&gt; type, but let me explore this further.&lt;/p&gt;

&lt;p&gt;The second simplest example is natural numbers. If you think about it, any natural numbers can be defined as either zero, or a &lt;em&gt;successor&lt;/em&gt; of some other number. This translates to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Nat = Zero | Succ Nat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This type is defined recursively, which lets us define an infinite amount of numbers with a finite set of symbols. With this whenever you encounter a number you can split the logic into two &lt;code&gt;case&lt;/code&gt;s: a simple one when the number is zero, and a recursive one when it isn't. For example addition would work like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add : Nat -&amp;gt; Nat
add x y =
  case x of
    Zero -&amp;gt; y
    Succ x' -&amp;gt; add x' (Succ y)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When adding zero to &lt;code&gt;y&lt;/code&gt; the answer is just &lt;code&gt;y&lt;/code&gt;, and when adding a successor of something the &lt;code&gt;Succ&lt;/code&gt; can be "moved" to &lt;code&gt;y&lt;/code&gt;, decreasing &lt;code&gt;x&lt;/code&gt; which guarantees that it finally reaches the base case.&lt;/p&gt;

&lt;p&gt;As a closing remark I will add an example of a &lt;code&gt;List&lt;/code&gt; type with a &lt;code&gt;map&lt;/code&gt; function that transforms each element of the list. This example has some more advanced notation, so don't worry if you don't get it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type List a = Empty | Cat a List

map : (a -&amp;gt; b) -&amp;gt; List a -&amp;gt; List b
map func list =
  case list of
    Empty -&amp;gt; Empty
    Cat head tail -&amp;gt; Cat (func head) (map func tail)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;h6&gt;
  
  
  I'm back to writing!
&lt;/h6&gt;

&lt;p&gt;I hope this gave you a vague idea of what you could stumble upon when dealing with functional languages. If you want some more concrete and in-depth examples look forward to my series on the programming language Elm. I barely have any formal education in functional programming and category or type theory, so if you spot any mistakes please notify me in the comments. See you soon!&lt;/p&gt;

</description>
      <category>functional</category>
      <category>programming</category>
      <category>paradigms</category>
    </item>
    <item>
      <title>Quick minilanguage to steer a submarine with Quackery</title>
      <dc:creator>Alex Esoposting</dc:creator>
      <pubDate>Fri, 03 Dec 2021 11:46:00 +0000</pubDate>
      <link>https://dev.to/olus2000/quick-minilanguage-to-steer-a-submarine-with-quackery-5401</link>
      <guid>https://dev.to/olus2000/quick-minilanguage-to-steer-a-submarine-with-quackery-5401</guid>
      <description>&lt;p&gt;Every year I try to solve &lt;a href="//adventofcode.com"&gt;Advent of Code&lt;/a&gt; with some esolang for as long as possible. This year's language of choice was &lt;a href="//github.com/GordonCharlton/Quackery"&gt;Quackery&lt;/a&gt;: a cute concatenative language based on Lisp and Forth, and when I saw the second puzzle I knew that language was perfect.&lt;/p&gt;

&lt;h3&gt;
  
  
  The problem
&lt;/h3&gt;

&lt;p&gt;Puzzle for day 2 of this year's AoC is about steering a submarine and figuring up what coordinates you end up on. Input is a set of simple commands like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forward 5
down 5
forward 8
up 3
down 8
forward 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each command consists of one of three directions: &lt;code&gt;forward&lt;/code&gt;, &lt;code&gt;up&lt;/code&gt; or &lt;code&gt;down&lt;/code&gt;, and a number indicating how far in this direction to swim. The important note is that we care about &lt;strong&gt;depth&lt;/strong&gt; and &lt;strong&gt;horizontal distance&lt;/strong&gt;, so &lt;code&gt;down&lt;/code&gt; would &lt;strong&gt;increase&lt;/strong&gt; depth, and &lt;code&gt;up&lt;/code&gt; would &lt;strong&gt;decrease&lt;/strong&gt; it.&lt;/p&gt;

&lt;h3&gt;
  
  
  The idea
&lt;/h3&gt;

&lt;p&gt;A standard approach to such problem would be to iterate over the input string word by word and changing some state based on what words are read. For anyone accustomed to concatenative languages this should sound familiar: it's exactly what the interpreter does to execute code! Given that one of the rules of concatenative programming is to never write the same code twice we should use the interpreter itself to interpret our input for us.&lt;/p&gt;

&lt;h3&gt;
  
  
  About Quackery
&lt;/h3&gt;

&lt;p&gt;Quackery is a stack based concatenative language. Every word executed interacts with the main Data Stack: numbers put themselves on the stack and operators and user defined words act on top values of the stack. There are plenty of stack shuffling words to arrange the top of the stack for other operators. Most notable are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;dup&lt;/code&gt; - duplicate the top of the stack&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;drop&lt;/code&gt; - remove the top of the stack&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;swap&lt;/code&gt; - swap top two values on the stack&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rest will be explained when used.&lt;/p&gt;

&lt;h3&gt;
  
  
  The implementation
&lt;/h3&gt;

&lt;p&gt;The Quackery inerpreter already recognises numbers so the only thing we need to define are direction words: &lt;code&gt;forward&lt;/code&gt;, &lt;code&gt;up&lt;/code&gt; and &lt;code&gt;down&lt;/code&gt;. In addition to that we need some state that these words would update, which I chose to be two numbers on the stack indicating depth and horizontal distance.&lt;/p&gt;

&lt;p&gt;To make things nicer I will define a state constructor word which just puts two zeroes on the stack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;state:&lt;/span&gt; &lt;span class="nv"&gt;depth,&lt;/span&gt; &lt;span class="nv"&gt;horizontal-distance&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;new-state&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;state&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Parenthesis in Quackery indicate comments, just as in Forth.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;up&lt;/code&gt; and &lt;code&gt;down&lt;/code&gt; should take a number that follows them and add or subtract from the second item on the stack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;swap&lt;/span&gt; &lt;span class="n"&gt;]'[&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;swap&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;down&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;state&lt;/span&gt; &lt;span class="nv"&gt;'n&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;state&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;swap&lt;/span&gt; &lt;span class="n"&gt;]'[&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;swap&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;up&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;state&lt;/span&gt; &lt;span class="nv"&gt;'n&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;state&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;]'[&lt;/code&gt; is used to take the item following the word currently executed, so it would be the number after &lt;code&gt;up&lt;/code&gt; or &lt;code&gt;down&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;forward&lt;/code&gt; is a bit tricky because it's already defined as a &lt;em&gt;builder word&lt;/em&gt; in Quackery, and redefining builders requires some trickery. I need to first define a normal word with the behaviour I want &lt;code&gt;forward&lt;/code&gt; to have and then redefine the builder &lt;code&gt;forward&lt;/code&gt; to build that behaviour:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;]'[&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;forward-behaviour&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;state&lt;/span&gt; &lt;span class="nv"&gt;'n&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;state&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;dip&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;'&lt;/span&gt; &lt;span class="n"&gt;forward-behaviour&lt;/span&gt;
        &lt;span class="n"&gt;nested&lt;/span&gt; &lt;span class="nb"&gt;join&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;      &lt;span class="n"&gt;builds&lt;/span&gt; &lt;span class="n"&gt;forward&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that the interpreter can understand our input as source code we just need to run it on said source code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;dip&lt;/span&gt; &lt;span class="n"&gt;new-state&lt;/span&gt;
  &lt;span class="n"&gt;quackery&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;   &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;solve-puzzle&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;input&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;n&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;dip&lt;/code&gt; takes the top item off the stack, runs the operator that follows it (in this case &lt;code&gt;new-state&lt;/code&gt;) and puts the item back on the stack. Coordinates are multiplied at the end to produce the answer that AoC will accept.&lt;/p&gt;

&lt;p&gt;Full code, along with the second part of the puzzle, can be found in my &lt;a href="https://github.com/olus2000/AoC/tree/main/2021/Quackery"&gt;AoC github repo&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This solution is a very minimalistic way of showing how easy and convenient it is to implement new languages in stack languages. This is a very useful feature because you don't need to implement a whole new interpreter, just define what each command does and tell the user what commands you support.&lt;/p&gt;

&lt;p&gt;If you want to fully understand the code I wrote there check out &lt;a href="https://github.com/GordonCharlton/Quackery/blob/main/The%20Book%20of%20Quackery.pdf"&gt;The Book of Quackery&lt;/a&gt;, a 100 page tutorial and documentation for the Quackery language written by it's author and available for free as a PDF. It's very newbie-friendly.&lt;/p&gt;

</description>
      <category>adventofcode</category>
      <category>concatenative</category>
      <category>esoteric</category>
      <category>programming</category>
    </item>
    <item>
      <title>Factor app - http requests</title>
      <dc:creator>Alex Esoposting</dc:creator>
      <pubDate>Mon, 08 Nov 2021 13:58:32 +0000</pubDate>
      <link>https://dev.to/olus2000/factor-app-http-requests-ego</link>
      <guid>https://dev.to/olus2000/factor-app-http-requests-ego</guid>
      <description>&lt;p&gt;The app I described in the previous tutorial is supposed to fetch data about a game from some server. Before writing any code to process this data it would be nice to know what kind of data we're dealing with and how to find it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Target API
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Where we get the data from
&lt;/h6&gt;

&lt;p&gt;The server we're trying to talk to is a simple HTTPS server with an impossible to remember address of &lt;code&gt;a766-95-160-157-211.ngrok.io&lt;/code&gt;. Most of its endpoints provide nice HTML pages, very useful for the browser but entirely useless for us. What we need can be found under &lt;code&gt;/api/game/&amp;lt;game-nr&amp;gt;&lt;/code&gt; which is supposed to provide us with all data relevant to the given game number in JSON format.&lt;/p&gt;

&lt;p&gt;To summarise: we need to make an HTTP GET request to the given address and receive a JSON in return.&lt;/p&gt;

&lt;h3&gt;
  
  
  Our toolbox for today
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Factor http.client and json.reader vocabularies
&lt;/h6&gt;

&lt;p&gt;Communicating over HTTP can be a huge pain because of the amount of things you need to worry about like headers or where to put data into the request. Fortunately Factor will do most of that for us so that we only need to use exactly one word from each of these libraries.&lt;/p&gt;

&lt;p&gt;For GET requests there is a word &lt;code&gt;http-get&lt;/code&gt;, but it's not what we need there as it will raise an error if the request is not successful. &lt;code&gt;http-get&lt;/code&gt; is actually a wrapper over the word &lt;code&gt;htpp-get*&lt;/code&gt; which just performs a request and returns whatever the outcome is.&lt;/p&gt;

&lt;p&gt;To decode JSON into meaningful Factor objects there is a word &lt;code&gt;json&amp;gt;&lt;/code&gt; which converts a JSON string to a Factor object. If the request is successful then &lt;code&gt;http-get*&lt;/code&gt; leaves returned data as a byte array. We then just need to convert it to a string to be able to extract Factor objects out of it. Our json extracting pipeline is two words: &lt;code&gt;&amp;gt;string json&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Putting knowledge to use
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Actual code time!
&lt;/h6&gt;

&lt;p&gt;The word &lt;code&gt;http-get*&lt;/code&gt; has a stack effect &lt;code&gt;( url -- response data )&lt;/code&gt;. To modify it to fetch game data we need to provide it with the correct url joined with the game number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="kn"&gt;USE:&lt;/span&gt; &lt;span class="nn"&gt;http.client&lt;/span&gt;

&lt;span class="k"&gt;CONSTANT:&lt;/span&gt; &lt;span class="n"&gt;api-url&lt;/span&gt; &lt;span class="s"&gt;"https://a766-95-160-157-211.ngrok.io/api/game/"&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;get-game*&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;game-id-string&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;response&lt;/span&gt; &lt;span class="nv"&gt;data&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;api-url&lt;/span&gt; &lt;span class="nb"&gt;prepend&lt;/span&gt; &lt;span class="n"&gt;http-get*&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that's not enough. Notice that &lt;code&gt;get-game*&lt;/code&gt; returns two values: response and data. If everything is ok then &lt;code&gt;code&lt;/code&gt; slot of the response will be 200 and data will be a byte array representing a JSON with game data. A lot of things can go wrong though: the client may be offline, the server may be offline, the game id may be incorrect and so on.&lt;/p&gt;

&lt;p&gt;For these cases we need a wrapper word for &lt;code&gt;get-game*&lt;/code&gt; that will check for any HTTP errors. That word will then extract any relevant information - either game data or HTTP error code - and return it for further interpretation by the app. HTTP error codes start from 400 and go up so this check is very simple. If it's lower than 400 we can use the words &lt;code&gt;&amp;gt;string json&amp;gt;&lt;/code&gt; to convert response data to string and then to a Factor object. The function should also return a flag indicating whether the request was successful.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="kn"&gt;USE:&lt;/span&gt; &lt;span class="nn"&gt;json.reader&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;get-game&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;game-id-string&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;error/game-object&lt;/span&gt; &lt;span class="nv"&gt;f/t&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;get-game*&lt;/span&gt; &lt;span class="nb"&gt;swap&lt;/span&gt; &lt;span class="n"&gt;code&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;dup&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt; &lt;span class="nb"&gt;&amp;gt;string&lt;/span&gt; &lt;span class="n"&gt;json&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;t&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;nip&lt;/span&gt; &lt;span class="no"&gt;f&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;if&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Final vocabulary
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Just code
&lt;/h6&gt;

&lt;p&gt;I put this code in the &lt;code&gt;nptimelapse.api&lt;/code&gt; vocabulary generated with &lt;code&gt;scaffold-work&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="c"&gt;! Copyright (C) 2021 Aleksander Sabak.&lt;/span&gt;
&lt;span class="c"&gt;! See http://factorcode.org/license.txt for BSD license.&lt;/span&gt;
&lt;span class="kn"&gt;USING:&lt;/span&gt; &lt;span class="nn"&gt;kernel&lt;/span&gt; &lt;span class="nn"&gt;strings&lt;/span&gt; &lt;span class="nn"&gt;accessors&lt;/span&gt; &lt;span class="nn"&gt;http.client&lt;/span&gt; &lt;span class="nn"&gt;json.reader&lt;/span&gt; &lt;span class="k"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;IN:&lt;/span&gt; &lt;span class="nn"&gt;nptimelapse.api&lt;/span&gt;

&lt;span class="k"&gt;CONSTANT:&lt;/span&gt; &lt;span class="n"&gt;api-url&lt;/span&gt; &lt;span class="s"&gt;"https://a766-95-160-157-211.ngrok.io/api/game/"&lt;/span&gt;


&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;get-game*&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;game-id-string&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;response&lt;/span&gt; &lt;span class="nv"&gt;data&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;api-url&lt;/span&gt; &lt;span class="nb"&gt;prepend&lt;/span&gt; &lt;span class="n"&gt;http-get*&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;


&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;get-game&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;game-id-string&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;error/game-object&lt;/span&gt; &lt;span class="nv"&gt;f/t&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;get-game*&lt;/span&gt; &lt;span class="nb"&gt;swap&lt;/span&gt; &lt;span class="n"&gt;code&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;dup&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt; &lt;span class="nb"&gt;&amp;gt;string&lt;/span&gt; &lt;span class="n"&gt;json&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;t&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;nip&lt;/span&gt; &lt;span class="no"&gt;f&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;if&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;h6&gt;
  
  
  A short chapter, wasn't it?
&lt;/h6&gt;

&lt;p&gt;Fetching data from a HTTP server turned out to be quite easy given Factor's high-level vocabularies. I chose the easiest part for the beginning though, so don't expect everything to go as smoothly from now on. We are about to get into some UI stuff, and that's never easy in any language.&lt;/p&gt;

</description>
      <category>esoteric</category>
      <category>concatenative</category>
      <category>tutorial</category>
      <category>factor</category>
    </item>
    <item>
      <title>Factor app - Preparation</title>
      <dc:creator>Alex Esoposting</dc:creator>
      <pubDate>Sun, 24 Oct 2021 09:18:30 +0000</pubDate>
      <link>https://dev.to/olus2000/factor-app-preparation-3k7c</link>
      <guid>https://dev.to/olus2000/factor-app-preparation-3k7c</guid>
      <description>&lt;p&gt;My previous posts covered the basics of programming in Factor. Tat was necessary information to start playing around with the language and you wouldn't go far without it. Now that I've laid a foundation it's time to set a goal and learn towards it.&lt;/p&gt;

&lt;h3&gt;
  
  
  The goal
&lt;/h3&gt;

&lt;h6&gt;
  
  
  What it is and why it's that
&lt;/h6&gt;

&lt;p&gt;My goal for this part of the tutorial is to make a desktop app that interactively visualises history of games of Neptune's Pride. You don't need to know anything about NP except from that it's a game about conquering stars in space. I've chosen this project for a couple reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It touches a couple useful aspects of programming in Factor, http requests and UI among others.&lt;/li&gt;
&lt;li&gt;There is an easily accessible source of online data to test the project.&lt;/li&gt;
&lt;li&gt;I wanted to do it anyway 😋&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It's both something I am competent to explain and something I have motivation for doing. I believe that things learned during this project are also useful in other applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  What we will learn
&lt;/h3&gt;

&lt;h6&gt;
  
  
  I'm going to be learning too!
&lt;/h6&gt;

&lt;p&gt;The project tackles two big subjects I haven't covered yet: UI and http communication. The latter will only be required on a basic level so that's what I will start with. For UI Factor offers an extensive gadget-based system that I will spend a lot of time exploring and explaining, and I hope I will manage to get pretty deep into it in the process. Gadgets are also tied closely to models which are worth at least one article by themselves.&lt;/p&gt;

&lt;p&gt;As often as possible the articles will contain actual progress on building the app, so there will be no shortage of practical examples to show how to make use of each vocabulary or concept.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;h6&gt;
  
  
  That was a short one
&lt;/h6&gt;

&lt;p&gt;I want to make something and share what I learned in the process, which is the main premise of this blog. All source code will be available in the &lt;a href="https://github.com/olus2000/Esoposting-Examples"&gt;Esoposting Examples repository&lt;/a&gt; on github as soon as the next article is published at some point in the next two weeks. Hope to see you there!&lt;/p&gt;

</description>
      <category>esoteric</category>
      <category>factor</category>
    </item>
    <item>
      <title>Factor pt. 4 - Vocabularies</title>
      <dc:creator>Alex Esoposting</dc:creator>
      <pubDate>Sat, 16 Oct 2021 11:41:59 +0000</pubDate>
      <link>https://dev.to/olus2000/factor-pt-4-vocabularies-476h</link>
      <guid>https://dev.to/olus2000/factor-pt-4-vocabularies-476h</guid>
      <description>&lt;p&gt;Over the last three tutorials I dumped a lot of information at you, explained it semi-coherently and only gave a couple practical examples. Now is the time to fix this all at once: this article is all about a big practical example that will touch on everything I have already covered and expand on it a bit. Today we are writing our own Factor vocabulary.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a vocabulary?
&lt;/h3&gt;

&lt;h6&gt;
  
  
  A bit of theory before the practice
&lt;/h6&gt;

&lt;p&gt;In most languages only the most essential part of available functionality is present by default and if you need any specialised functions or objects you need to &lt;code&gt;import&lt;/code&gt; or &lt;code&gt;#include&lt;/code&gt; or whatever your favourite language does to load features from an external file. Most languages call those extensions "libraries" or "packages", but because in Factor everything is a word these files are just lists of words: vocabularies.&lt;/p&gt;

&lt;p&gt;If you followed the examples I gave on your computer you may have already used vocabularies. Whenever the Listener encounters a word that is in an unloaded vocabulary it will throw an error and present options, one of which will be to use the library including that word. It's useful to not load all libraries by default because it would take up a lot of space to compile all Factor libraries, and some of them use conflicting word names (for example &lt;code&gt;range&lt;/code&gt; is in &lt;code&gt;math.ranges&lt;/code&gt;, &lt;code&gt;math.statistics&lt;/code&gt; and &lt;code&gt;model.range&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;You can order Factor to load vocabularies with the words &lt;code&gt;USE:&lt;/code&gt; and &lt;code&gt;USING:&lt;/code&gt;. Their syntax is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="kn"&gt;USE:&lt;/span&gt; &lt;span class="nn"&gt;vocab-name&lt;/span&gt;
&lt;span class="kn"&gt;USING:&lt;/span&gt; &lt;span class="nn"&gt;vocab-names...&lt;/span&gt; &lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usually a Factor program will have a big &lt;code&gt;USING:&lt;/code&gt; line at its start that loads all the vocabularies it needs. Words you define in the Listener go into the &lt;code&gt;scratchpad&lt;/code&gt; vocabulary which exists as long as the Listener is open.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the vocabulary
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Let's get down to business
&lt;/h6&gt;

&lt;p&gt;A user created vocabulary usually sits in the "work" folder in your Factor installation directory. Vocabularies are stored in plaintext source files so in theory you could just find the folder and create the required structure yourself, but Factor conveniently has a vocabulary for defining vocabularies so it can do the hard work for us.&lt;/p&gt;

&lt;p&gt;Before we do anything we need to &lt;code&gt;USE: tools.scaffold&lt;/code&gt; because this vocabulary will not be automatically suggested by the Listener. I will be making a library-managing vocabulary, so I will call it "esoposting-library". To scaffold a library push it's name as a string onto the stack and call &lt;code&gt;scaffold-work&lt;/code&gt;. Now you can do it again, but call &lt;code&gt;edit&lt;/code&gt; instead and we will be ready to start.&lt;/p&gt;

&lt;p&gt;In summary this is what you should type into the listener to create and edit your vocabulary:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="kn"&gt;USE:&lt;/span&gt; &lt;span class="nn"&gt;tools.scaffold&lt;/span&gt;
&lt;span class="s"&gt;"esoposting-library"&lt;/span&gt; &lt;span class="n"&gt;scaffold-work&lt;/span&gt;
&lt;span class="s"&gt;"esoposting-library"&lt;/span&gt; &lt;span class="n"&gt;edit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point Factor may ask you for your editor of choice which should launch immediately, provided it is installed. The initialised file should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="c"&gt;! Copyright (C) 2021 Your name.&lt;/span&gt;
&lt;span class="c"&gt;! See http://factorcode.org/license.txt for BSD license.&lt;/span&gt;
&lt;span class="kn"&gt;USING:&lt;/span&gt; &lt;span class="k"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;IN:&lt;/span&gt; &lt;span class="nn"&gt;esoposting-library&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Feel free to substitute your actual name for "Your name" in the header.&lt;/p&gt;

&lt;h3&gt;
  
  
  Designing the vocabulary
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Design first, code second.
&lt;/h6&gt;

&lt;p&gt;Before coding it's good to have an idea what the code is for and how it should work. My idea is a program to keep track of my personal library. It should support adding new books and removing old ones, searching for the book by title, and borrowing books: keeping track of whether a book is currently borrowed and to whom and how many times it has been borrowed. I should also be able to see a list of books I own in alphabetical order.&lt;/p&gt;

&lt;p&gt;A more serious approach would look at Factor's database support, but I'll settle on a data structure holding the books that will sit atop the stack. Book management words will act on that object and if I choose the structure well they should mostly turn out to be aliases of already existing words. Because I want the books to be searchable by title I will implement the library with a hashtable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Classes
&lt;/h3&gt;

&lt;h6&gt;
  
  
  What objects do we need?
&lt;/h6&gt;

&lt;p&gt;From the problem specification two object types are apparent: books and the library containing them. Let's start from the simpler one.&lt;/p&gt;

&lt;h4&gt;
  
  
  Book
&lt;/h4&gt;

&lt;p&gt;What information should a book carry? From what was explained earlier we need to know a book's title, how many times it was borrowed, is it borrowed now and if so then who borrowed it. In addition to that I want know the book's author and genre. That would give us 6 fields for class &lt;code&gt;book&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Not all of these have to be slots of the book tuple though. Info on whether the book is borrowed and by whom could be stored by having a false value when the book is in the library and a string with the borrower's name if it's out. That is actually its own type of data called "maybe string". Additionally the title will be stored as a key to the library hashtable, but I want the redundancy of keeping it in the tuple as well. That way every word that uses a book can automatically use its title.&lt;/p&gt;

&lt;p&gt;The final &lt;code&gt;book&lt;/code&gt; tuple class declaration looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="kn"&gt;USING:&lt;/span&gt; &lt;span class="nn"&gt;classes.maybe&lt;/span&gt; &lt;span class="nn"&gt;math&lt;/span&gt; &lt;span class="nn"&gt;strings&lt;/span&gt; &lt;span class="k"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;TUPLE:&lt;/span&gt; &lt;span class="nc"&gt;book&lt;/span&gt;
    &lt;span class="nv"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nv"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;read-only&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt;
    &lt;span class="nv"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;author&lt;/span&gt; &lt;span class="nv"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;read-only&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt;
    &lt;span class="nv"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;genre&lt;/span&gt; &lt;span class="nv"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;read-only&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt;
    &lt;span class="nv"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;times-borrowed&lt;/span&gt; &lt;span class="nv"&gt;integer&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt;
    &lt;span class="nv"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;borrower&lt;/span&gt; &lt;span class="nv"&gt;maybe{&lt;/span&gt; &lt;span class="nv"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A "maybe string" class in Factor is written as &lt;code&gt;maybe{ string }&lt;/code&gt; and that's the type of the &lt;code&gt;borrower&lt;/code&gt; slot. Also note that we already need to use some vocabularies. All of them should be gathered in the third line of the generated vocabulary header. To check if everything works correctly call &lt;code&gt;"esoposting-library" reload&lt;/code&gt; in the listener.&lt;/p&gt;

&lt;h4&gt;
  
  
  Library
&lt;/h4&gt;

&lt;p&gt;The library object doesn't need to be anything more than a collection of books searchable by the title. We can get away with not implementing a separate class for this purpose because a hashtable does exactly what we want. A drawback of not declaring a new class is that words we define for working with the library will also attempt to work with other objects if used incorrectly which can lead to hidden bugs and unhelpful error messages.&lt;/p&gt;

&lt;p&gt;In our case the project is too small for that to matter. If our vocabulary was supposed to be a part of some bigger application it would be a good practice to define a &lt;code&gt;library&lt;/code&gt; class tuple, but it would be an overkill for this purpose.&lt;/p&gt;

&lt;h3&gt;
  
  
  Words
&lt;/h3&gt;

&lt;h6&gt;
  
  
  A top-down approach
&lt;/h6&gt;

&lt;p&gt;I have already mentioned in a previous article that when writing code I like to start with the top level words I need and see what other words will be necessary to implement them. For our application there will be a few of those.&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating the library
&lt;/h4&gt;

&lt;p&gt;First the user needs to be able to &lt;code&gt;init-library&lt;/code&gt; to get the library object and not need to care about how it's implemented:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="kn"&gt;USE:&lt;/span&gt; &lt;span class="nn"&gt;kernel&lt;/span&gt;
&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;init-library&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;H{&lt;/span&gt; &lt;span class="n"&gt;}&lt;/span&gt; &lt;span class="nb"&gt;clone&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;H{ }&lt;/code&gt; is syntax for a default empty hashtable, and &lt;code&gt;clone&lt;/code&gt; makes sure every call to &lt;code&gt;init-library&lt;/code&gt; creates a new hashtable and not just a reference to the same one. It's a preferred way of initialising hashtables if you don't need to specify capacity.&lt;/p&gt;

&lt;p&gt;Then an &lt;code&gt;add-new-book&lt;/code&gt; word would be nice that would take a title, author and genre, create a new book and add it to the library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="kn"&gt;USE:&lt;/span&gt; &lt;span class="nn"&gt;accessors&lt;/span&gt;
&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;add-new-book&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nv"&gt;author&lt;/span&gt; &lt;span class="nv"&gt;genre&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;&amp;lt;book&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;swap&lt;/span&gt; &lt;span class="nb"&gt;over&lt;/span&gt;
    &lt;span class="n"&gt;title&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;swap&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;set-at&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;keep&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To add a key-pair value to a hashtable you use the word &lt;code&gt;set-at&lt;/code&gt; which stack effect is &lt;code&gt;( value key assoc -- )&lt;/code&gt;. The first problem is that both value (the book) and key (the title) have to be below the assoc (the library hashtable), and this is dealt with by some stack shuffling. The second problem is that &lt;code&gt;set-at&lt;/code&gt; consumes the hashtable, but it can be remedied by enclosing it in a quotation and calling via the &lt;code&gt;keep&lt;/code&gt; combinator which saves the top value, executes the quotation and restores the saved value.&lt;/p&gt;

&lt;p&gt;The third problem with &lt;code&gt;add-new-book&lt;/code&gt; is that we didn't define the constructor &lt;code&gt;&amp;lt;book&amp;gt;&lt;/code&gt; yet, so let's do that. It needs to take the initial data (title, author and genre), fill the default values of the other slots and use a &lt;code&gt;boa&lt;/code&gt; constructor to make a &lt;code&gt;book&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;&amp;lt;book&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nv"&gt;author&lt;/span&gt; &lt;span class="nv"&gt;genre&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;book&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="no"&gt;f&lt;/span&gt; &lt;span class="n"&gt;book&lt;/span&gt; &lt;span class="nb"&gt;boa&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Accessing the library
&lt;/h4&gt;

&lt;p&gt;After adding all the books to the library it would be nice to be able to interact with them. From what I specified in design we need words for searching, deleting and borrowing books. Let's also make them user friendly, so if I try to interact with a nonexistent book I get a warning message. For these I will use the &lt;code&gt;print&lt;/code&gt; word from the &lt;code&gt;io&lt;/code&gt; vocabulary which takes a string and displays it on the console.&lt;/p&gt;

&lt;p&gt;To get a book from the library we need to pass it's title and the library itself to the generic word &lt;code&gt;at&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="kn"&gt;USE:&lt;/span&gt; &lt;span class="nn"&gt;io&lt;/span&gt;
&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;find-book&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;book&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;over&lt;/span&gt; &lt;span class="nb"&gt;at&lt;/span&gt; &lt;span class="nb"&gt;dup&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="s"&gt;"WARNING: a book of this title was not found!"&lt;/span&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
    &lt;span class="nb"&gt;unless&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;at&lt;/code&gt; will not throw any errors when the book is not found, it will simply return &lt;code&gt;f&lt;/code&gt;. This means we can check the returned value and warn the user &lt;code&gt;unless&lt;/code&gt; the book was found.&lt;/p&gt;

&lt;p&gt;To delete a book from the library we need the same information as when searching passed to the generic word &lt;code&gt;delete-at&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;delete-book&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;over&lt;/span&gt; &lt;span class="nb"&gt;at&lt;/span&gt; &lt;span class="nb"&gt;dup&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;title&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;over&lt;/span&gt; &lt;span class="nb"&gt;delete-at&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="s"&gt;"WARNING: a book of this title was not found!"&lt;/span&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
    &lt;span class="nb"&gt;if&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of simply deleting the book, which would work even if the book was not present, I check if the books exists beforehand so I can warn the user about it. The action they attempted was not carried out successfully, so they need to know that. Maybe they made a typo?&lt;/p&gt;

&lt;p&gt;The final utility expected from this vocabulary is handling borrowed books. For this I will define two words: &lt;code&gt;borrow-book&lt;/code&gt; and &lt;code&gt;return-book&lt;/code&gt; that will adjust appropriate slots of the book's slot. In addition to that there are two new warnings: &lt;code&gt;borrow-book&lt;/code&gt; should not work if the book is already borrowed, and the same for &lt;code&gt;return-book&lt;/code&gt; and not borrowed books.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;borrow-book&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nv"&gt;borrower&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt; &lt;span class="n"&gt;find-book&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;keep&lt;/span&gt; &lt;span class="nb"&gt;over&lt;/span&gt;    &lt;span class="c"&gt;! Fetch the book and warn if isn't found&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;over&lt;/span&gt; &lt;span class="n"&gt;borrower&amp;gt;&amp;gt;&lt;/span&gt;               &lt;span class="c"&gt;! If is found check if already borrowed&lt;/span&gt;
        &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="s"&gt;"WARNING: the book has already been borrowed!"&lt;/span&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="nb"&gt;2drop&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;&amp;gt;&amp;gt;borrower&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;change-times-borrowed&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
        &lt;span class="nb"&gt;if&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;2drop&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;if&lt;/span&gt;                    &lt;span class="c"&gt;! If not just do nothing&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;return-book&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;over&lt;/span&gt; &lt;span class="nb"&gt;at&lt;/span&gt; &lt;span class="nb"&gt;dup&lt;/span&gt;             &lt;span class="c"&gt;! Fetch the book&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;dup&lt;/span&gt; &lt;span class="n"&gt;borrower&amp;gt;&amp;gt;&lt;/span&gt;        &lt;span class="c"&gt;! If found check if borrowed&lt;/span&gt;
        &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="no"&gt;f&lt;/span&gt; &lt;span class="n"&gt;&amp;gt;&amp;gt;borrower&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;    &lt;span class="c"&gt;! If borrowed return&lt;/span&gt;
        &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="s"&gt;"WARNING: the book is not borrowed!"&lt;/span&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
        &lt;span class="nb"&gt;if&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="s"&gt;"WARNING: a book of this title was not found!"&lt;/span&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
    &lt;span class="nb"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now hold up. Remember the guidelines? Whenever a definition is big enough that comments are needed to explain what is going on it's a sign that you need to factor something out.&lt;/p&gt;

&lt;h3&gt;
  
  
  Factorisation
&lt;/h3&gt;

&lt;h6&gt;
  
  
  The best practice of all
&lt;/h6&gt;

&lt;p&gt;There is a big and obvious factor common to all words in our vocabulary: the "book not found" warning. It could be factored out using just &lt;code&gt;find-book&lt;/code&gt;, but there is a better way. Let's define a combinator that will take the library, title and a quotation, call the quotation if the book is found and otherwise warn the user and do nothing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;if-book-exists&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nv"&gt;quot:&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;book&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;x&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;x|0&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt; &lt;span class="nb"&gt;over&lt;/span&gt; &lt;span class="nb"&gt;at&lt;/span&gt; &lt;span class="nb"&gt;dup&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;keep&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="s"&gt;"WARNING: a book of a given title was not found!"&lt;/span&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
    &lt;span class="nb"&gt;if&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This word, like all combinators, is and &lt;code&gt;inline&lt;/code&gt; word. It means that before compiling all occurrences of it will be replaced with its body. This lets the compiler handle stack effects more efficiently and is required for most words that take quotations as arguments.&lt;/p&gt;

&lt;p&gt;Let's now rewrite previous words in terms of this new combinator. Most of them will have a form of &lt;code&gt;[ body ] if-book-exists&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;find-book&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;book&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;if-book-exists&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;delete-book&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;title&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;over&lt;/span&gt; &lt;span class="nb"&gt;delete-at&lt;/span&gt; &lt;span class="no"&gt;f&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;if-book-exists&lt;/span&gt;
    &lt;span class="nb"&gt;drop&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;return-book&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;dup&lt;/span&gt; &lt;span class="n"&gt;borrower&amp;gt;&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="no"&gt;f&lt;/span&gt; &lt;span class="n"&gt;&amp;gt;&amp;gt;borrower&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="s"&gt;"WARNING: the book is not borrowed!"&lt;/span&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
    &lt;span class="nb"&gt;if&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;if-book-exists&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Returning the book takes an additional argument - borrower - but the body quotation's stack effect for &lt;code&gt;if-book-exists&lt;/code&gt; must only take a book as an argument. To make this happen we need to construct a quotation that already contains the borrower. This brings us to the concept of partial application, or currying. It's a functional programming term which is most easily explained by example.&lt;/p&gt;

&lt;p&gt;Addition is a function that takes two numbers and returns a number. But it can sometimes be useful to first supply it with only one argument and then use as a single argument function. For example if we know that a function that adds 5 to a number will be useful we can apply + to 5 getting &lt;code&gt;[ 5 + ]&lt;/code&gt; which still requires one argument to return a number. This can be done with any function, producing a new function that takes one less argument and in Factor currying is achieved with the word &lt;code&gt;curry&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;curry&lt;/span&gt;
&lt;span class="c"&gt;! S: [ value body ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to make a quotation with stack effect &lt;code&gt;( book borrower -- x )&lt;/code&gt; and partially apply it to the borrower producing a quotation with stack effect &lt;code&gt;( book -- x )&lt;/code&gt; which is what we need for &lt;code&gt;if-book-exists&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;borrow-book&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nv"&gt;borrower&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;over&lt;/span&gt; &lt;span class="n"&gt;borrower&amp;gt;&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt; &lt;span class="s"&gt;"WARNING: the book has already been borrowed!"&lt;/span&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;&amp;gt;&amp;gt;borrower&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;change-times-borrowed&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
    &lt;span class="nb"&gt;if&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;curry&lt;/span&gt; &lt;span class="n"&gt;if-book-exists&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Final code
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Works for me
&lt;/h6&gt;

&lt;p&gt;This is how the "esoposting-library.factor" file looks like after all the words have been added:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="c"&gt;! Copyright (C) 2021 Aleksander Sabak.&lt;/span&gt;
&lt;span class="c"&gt;! See http://factorcode.org/license.txt for BSD license.&lt;/span&gt;
&lt;span class="kn"&gt;USING:&lt;/span&gt; &lt;span class="nn"&gt;accessors&lt;/span&gt; &lt;span class="nn"&gt;assocs&lt;/span&gt; &lt;span class="nn"&gt;classes.maybe&lt;/span&gt; &lt;span class="nn"&gt;io&lt;/span&gt; &lt;span class="nn"&gt;kernel&lt;/span&gt; &lt;span class="nn"&gt;math&lt;/span&gt; &lt;span class="nn"&gt;strings&lt;/span&gt; &lt;span class="k"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;IN:&lt;/span&gt; &lt;span class="nn"&gt;esoposting-library&lt;/span&gt;

&lt;span class="k"&gt;TUPLE:&lt;/span&gt; &lt;span class="nc"&gt;book&lt;/span&gt;
    &lt;span class="nv"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nv"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;read-only&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt;
    &lt;span class="nv"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;author&lt;/span&gt; &lt;span class="nv"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;read-only&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt;
    &lt;span class="nv"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;genre&lt;/span&gt; &lt;span class="nv"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;read-only&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt;
    &lt;span class="nv"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;times-borrowed&lt;/span&gt; &lt;span class="nv"&gt;integer&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt;
    &lt;span class="nv"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;borrower&lt;/span&gt; &lt;span class="nv"&gt;maybe{&lt;/span&gt; &lt;span class="nv"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;init-library&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;H{&lt;/span&gt; &lt;span class="n"&gt;}&lt;/span&gt; &lt;span class="nb"&gt;clone&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;&amp;lt;book&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nv"&gt;author&lt;/span&gt; &lt;span class="nv"&gt;genre&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;book&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="no"&gt;f&lt;/span&gt; &lt;span class="n"&gt;book&lt;/span&gt; &lt;span class="nb"&gt;boa&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;add-new-book&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nv"&gt;author&lt;/span&gt; &lt;span class="nv"&gt;genre&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;&amp;lt;book&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;swap&lt;/span&gt; &lt;span class="nb"&gt;over&lt;/span&gt;
    &lt;span class="n"&gt;title&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;swap&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;set-at&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;keep&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;if-book-exists&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nv"&gt;quot:&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;book&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;x&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;x|0&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt; &lt;span class="nb"&gt;over&lt;/span&gt; &lt;span class="nb"&gt;at&lt;/span&gt; &lt;span class="nb"&gt;dup&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;keep&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="s"&gt;"WARNING: a book of a given title was not found!"&lt;/span&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
    &lt;span class="nb"&gt;if&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;find-book&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;book&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;if-book-exists&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;delete-book&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;title&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;over&lt;/span&gt; &lt;span class="nb"&gt;delete-at&lt;/span&gt; &lt;span class="no"&gt;f&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;if-book-exists&lt;/span&gt;
    &lt;span class="nb"&gt;drop&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;borrow-book&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nv"&gt;borrower&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;over&lt;/span&gt; &lt;span class="n"&gt;borrower&amp;gt;&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt; &lt;span class="s"&gt;"WARNING: the book has already been borrowed!"&lt;/span&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;&amp;gt;&amp;gt;borrower&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;change-times-borrowed&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
    &lt;span class="nb"&gt;if&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;curry&lt;/span&gt; &lt;span class="n"&gt;if-book-exists&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;return-book&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;library&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;dup&lt;/span&gt; &lt;span class="n"&gt;borrower&amp;gt;&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="no"&gt;f&lt;/span&gt; &lt;span class="n"&gt;&amp;gt;&amp;gt;borrower&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="s"&gt;"WARNING: the book is not borrowed!"&lt;/span&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
    &lt;span class="nb"&gt;if&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;if-book-exists&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All libraries I am &lt;code&gt;USING:&lt;/code&gt; are specified at the top in alphabetical order. I encourage you to try and play around with this vocabulary or whatever you came up with during this tutorial. Remember that you can prettyprint whatever is on the stack with &lt;code&gt;.&lt;/code&gt;, even tuples.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;h6&gt;
  
  
  That was a lot of writing
&lt;/h6&gt;

&lt;p&gt;I hope you enjoyed this more practical tutorial. You are now ready to go and experiment with writing your own vocabularies. As always remember to make use of the built in help, there is a lot of knowledge to be found in the docs. This is the end of the first chapter of this series, next time I will discuss Factor's GUI vocabularies and start building towards making a desktop application.&lt;/p&gt;

</description>
      <category>esoteric</category>
      <category>concatenative</category>
      <category>factor</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Factor pt. 3 - Tuple classes</title>
      <dc:creator>Alex Esoposting</dc:creator>
      <pubDate>Mon, 11 Oct 2021 12:40:30 +0000</pubDate>
      <link>https://dev.to/olus2000/factor-pt-3-tuple-classes-41ff</link>
      <guid>https://dev.to/olus2000/factor-pt-3-tuple-classes-41ff</guid>
      <description>&lt;p&gt;From my previous tutorials you should already know how to operate with values on the stack and use sequences and combinators to make control structures. Data types paid a crucial role in both of these articles and are very important all over Factor. Today I will explain how Factor class system works based on tuple classes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Object oriented programming
&lt;/h3&gt;

&lt;h6&gt;
  
  
  What I know about it
&lt;/h6&gt;

&lt;p&gt;As far as I know OOP has been the most popular programming paradigm for some time. It's sometimes presented in opposition to Functional Programming, but although both of them are called "paradigms" they touch very different aspects of programming and can easily coexist. Factor manages to be both an object oriented and a concatenative language.&lt;/p&gt;

&lt;p&gt;In OOP programs are based on the concept of "objects" that contain data (called fields) and code (called methods). These objects interact with each other and create new objects to perform required tasks. In most languages, including Factor, each object is an instance of a class which tells us its type, what data it holds and what we can do with it. For example &lt;code&gt;1&lt;/code&gt; is an instance of an integer class. It represents a number and arithmetic operations can be performed on it. &lt;code&gt;[ dup * ]&lt;/code&gt; is an instance of quotation. It holds a snippet of code and can be passed as an argument to combinators.&lt;/p&gt;

&lt;p&gt;The thing that makes OOP so useful is that a program may define new classes with their own fields and methods. In Factor this is done using class tuples.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining a class
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Just fields for now
&lt;/h6&gt;

&lt;p&gt;Just as with most definitions in Factor a syntax word is used to create new tuple classes: &lt;code&gt;TUPLE:&lt;/code&gt;. It's syntax is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;TUPLE:&lt;/span&gt; &lt;span class="nc"&gt;class-name&lt;/span&gt; &lt;span class="nv"&gt;slots...&lt;/span&gt; &lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Slots define fields of a class and can take one of the following forms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt; - A slot that can hold any object&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{ name attributes... }&lt;/code&gt; - A slot that can hold any object with optional attributes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{ name class attributes... }&lt;/code&gt; - A slot that can hold an instance of the class with optional attributes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All this can sound very confusing, so let's look at an example. Let's define a class for books. Let's say each book shall hold information about it's title, author, genre, and how many times it has been borrowed. A tuple class for such books would be defined like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;TUPLE:&lt;/span&gt; &lt;span class="nc"&gt;book&lt;/span&gt;
    &lt;span class="nv"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nv"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;read-only&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt;
    &lt;span class="nv"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;author&lt;/span&gt; &lt;span class="nv"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;read-only&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt;
    &lt;span class="nv"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;genre&lt;/span&gt; &lt;span class="nv"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;read-only&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt;
    &lt;span class="nv"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;times-borrowed&lt;/span&gt; &lt;span class="nv"&gt;integer&lt;/span&gt; &lt;span class="nv"&gt;initial:&lt;/span&gt; &lt;span class="nv"&gt;0&lt;/span&gt; &lt;span class="nv"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each slot of this tuple can only store a particular type of data. It may seem like an unnecessary restriction, but it makes code more readable and can prevent some mistakes later on in the program. Three slots have a &lt;code&gt;read-only&lt;/code&gt; attribute which means once they are set they can't be changed. The last slot has an &lt;code&gt;initial:&lt;/code&gt; attribute which specifies what value it should have at the start&lt;/p&gt;

&lt;h3&gt;
  
  
  Instantiating classes
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Making actual objects holding data
&lt;/h6&gt;

&lt;p&gt;There are two basic constructor words in Factor that let you create class instances. The simpler one, &lt;code&gt;new ( class -- tuple )&lt;/code&gt;, only takes a class as an argument and creates a new tuple with default slot values. Continuing the book example this is how to create a default book:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;book&lt;/span&gt; &lt;span class="nb"&gt;new&lt;/span&gt;
&lt;span class="c"&gt;! S: T{ book f "" "" "" 0 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This book's prettyprint shows three empty strings and a zero respectively. To actually insert some information into that book we need to use the other constructor: &lt;code&gt;boa ( slots... class -- tuple )&lt;/code&gt;. In addition to a class it requires values to be put into the slots &lt;strong&gt;b&lt;/strong&gt;y &lt;strong&gt;o&lt;/strong&gt;rder of &lt;strong&gt;a&lt;/strong&gt;rguments - hence the name. For example to describe the toki pona dictionary one would write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="s"&gt;"Toki Pona Dictionary"&lt;/span&gt; &lt;span class="s"&gt;"Sonja Lang"&lt;/span&gt; &lt;span class="s"&gt;"Dictionary"&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;book&lt;/span&gt; &lt;span class="nb"&gt;boa&lt;/span&gt;
&lt;span class="c"&gt;! S: T{ book f "Toki Pona Dictionary" "Sonja Lang" "Dictionary" 1 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time you can see contents of each of the slots initialised to a proper value. Disregard the &lt;code&gt;f&lt;/code&gt; between the class name and the title: it's just a quirk of Factor literal tuple syntax. For more info visit &lt;code&gt;\ T{ help&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getters and setters
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Working with objects
&lt;/h6&gt;

&lt;p&gt;It is useful to change values of slots of a tuple after we created it. In our example that would be incrementing the &lt;code&gt;times-borrowed&lt;/code&gt; slot whenever someone borrows a book. It would also be nice to extract values from the object to use in the program, that's what they are there for after all. To facilitate that each class in factor comes with a set of getter and setter words.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Getters&lt;/em&gt; extract values of slots from an object. They are defined automatically based on object's slot names. They consume the object and return the value of a corresponding slot.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;slot-name&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;object&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;value&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example to get a book's title you would use a word &lt;code&gt;title&amp;gt;&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Setters&lt;/em&gt; (as the name suggests) set a slot to a given value. They are also defined by the system for each class but only for writeable slots: those without the &lt;code&gt;read-only&lt;/code&gt; attribute. They consume an object and a value and return the same object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;&amp;gt;&amp;gt;slot-name&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;object&lt;/span&gt; &lt;span class="nv"&gt;value&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;object&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The only writeable slot in our example is &lt;code&gt;times-borrowed&lt;/code&gt;, and its setter is &lt;code&gt;&amp;gt;&amp;gt;times-borrowed&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Setters are actually a wrapper over &lt;em&gt;writers&lt;/em&gt;: lower level words that consume a value and an object and return nothing. They aren't commonly used in programs because it's usually more convenient to keep the object on the stack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;slot-name&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;value&lt;/span&gt; &lt;span class="nv"&gt;object&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is one more group of accessor words: &lt;em&gt;changers&lt;/em&gt;. They take a quotation and an object and apply the quotation to the slot updating it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;change-slot-name&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;object&lt;/span&gt; &lt;span class="nv"&gt;quot&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;object&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;change-times-borrowed&lt;/code&gt; changer will be very useful to update the counter whenever a book is borrowed.&lt;/p&gt;

&lt;p&gt;It is important to remember that most of the time if an object is copied (for example by &lt;code&gt;dup&lt;/code&gt;) it is actually still the same object existing on the stack twice. If its slots are changed they are changed for all copies. This is called a "shallow copy" and it is common among high level languages. For those of you familiar with C++ it's comparable to copying a pointer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Constructors
&lt;/h3&gt;

&lt;h6&gt;
  
  
  How to actually instantiate the class
&lt;/h6&gt;

&lt;p&gt;Using only &lt;code&gt;new&lt;/code&gt; and &lt;code&gt;boa&lt;/code&gt; can get tedious, especially if data needs some processing before being put into slots or if you need some slots to have default initial values. In the book example whenever I get a new book it's &lt;code&gt;times-borrowed&lt;/code&gt; value is always 0. Based on the premise that every time some code is about to be repeated it should be factored out I should write a better constructing word for my book class.&lt;/p&gt;

&lt;p&gt;By convention constructors that create an instance of a class are called &lt;code&gt;&amp;lt;class-name&amp;gt;&lt;/code&gt;. I want my constructor to have a stack effect of &lt;code&gt;( title author genre -- book )&lt;/code&gt;. A constructor word can be defined using &lt;code&gt;new&lt;/code&gt; or &lt;code&gt;boa&lt;/code&gt;, but in our case &lt;code&gt;new&lt;/code&gt; is not useful: it will set read-only slots to their default values before my defined constructor can affect them. In this case a constructor using &lt;code&gt;boa&lt;/code&gt; is quite simple: it only needs to add a default 0 and the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;&amp;lt;book&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="nv"&gt;author&lt;/span&gt; &lt;span class="nv"&gt;genre&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;book&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;book&lt;/span&gt; &lt;span class="nb"&gt;boa&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is another constructor naming convention used in Factor: &lt;code&gt;new-class-name&lt;/code&gt;. These constructors, apart from usual arguments, also take a class as an argument. They are mostly useful when subclassing which is out of scope of this tutorial so I won't be presenting an example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining methods
&lt;/h3&gt;

&lt;h6&gt;
  
  
  How to make the same word do different things
&lt;/h6&gt;

&lt;p&gt;Usefulness of methods in OOP comes from the fact that you don't need to know anything about the object except that it implements a particular method to use that method. For example if a program is to do something based on length of an object it can call a &lt;code&gt;length&lt;/code&gt; word on it, not matter if it's an array, a quotation, a linked list or a vector. Calculating length works differently for each of these classes, but it is always invoked by the same name.&lt;/p&gt;

&lt;p&gt;Usually this is implemented using the dot operator: &lt;code&gt;class.method()&lt;/code&gt;. In Factor however there are no operators, only words. Instead of the dot operator Factor allows to define &lt;em&gt;generic words&lt;/em&gt;: words that first check what type are their arguments and execute different functions associated with that type. I've already shown examples of generic words: all accessors are generic and they will work on any object that has slots of a certain name.&lt;/p&gt;

&lt;p&gt;A generic words are declared with syntax words &lt;code&gt;GENERIC:&lt;/code&gt; and &lt;code&gt;GENERIC#:&lt;/code&gt;. The first one creates words that check the type of the top value on the stack. The other allows to specify which value to check. Their syntax is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;GENERIC:&lt;/span&gt; &lt;span class="nf"&gt;word&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;stack&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;effect&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;GENERIC#:&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;stack&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;effect&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that generic words have their stack effects defined at declaration. Each method connected to the same generic word must have the same stack effect. What is not declared is the body of the generic word. There will be multiple bodies defined for each class separately.&lt;/p&gt;

&lt;p&gt;To attach a method to a generic word we need a syntax word &lt;code&gt;M:&lt;/code&gt; with syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;M:&lt;/span&gt; &lt;span class="nc"&gt;class&lt;/span&gt; &lt;span class="nf"&gt;generic&lt;/span&gt; &lt;span class="n"&gt;definition...&lt;/span&gt; &lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Stack effect of the word were declared with the word itself. This only serves to attach a method to a word and a class.&lt;/p&gt;

&lt;p&gt;As an example let's define a method that would be called whenever someone borrows a book. It needs to take a book as input, increase its &lt;code&gt;times-borrowed&lt;/code&gt; by one and not return anything. It's declaration and definition will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;GENERIC:&lt;/span&gt; &lt;span class="nf"&gt;borrow&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;object&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;M:&lt;/span&gt; &lt;span class="nc"&gt;book&lt;/span&gt; &lt;span class="nf"&gt;borrow&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;change-times-borrowed&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For now &lt;code&gt;borrow&lt;/code&gt; only has effect on &lt;code&gt;book&lt;/code&gt; objects, but it could be implemented for other classes in ways that make sense for these classes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;h6&gt;
  
  
  The tip of the iceberg
&lt;/h6&gt;

&lt;p&gt;What I explained barely scratches the surface of what Factor can do with classes. It is heavily based on them and even though you can do without this knowledge you will be using classes without knowing. This article hopefully shown you enough that you will be able to noticed Object Oriented features and start using them in your own programs. Next time I will be explaining vocabularies.&lt;/p&gt;

</description>
      <category>factor</category>
      <category>esoteric</category>
      <category>oop</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Factor pt. 2 - Sequences and Control Structures</title>
      <dc:creator>Alex Esoposting</dc:creator>
      <pubDate>Sun, 03 Oct 2021 15:18:17 +0000</pubDate>
      <link>https://dev.to/olus2000/factor-pt-2-sequences-and-control-structures-4ea9</link>
      <guid>https://dev.to/olus2000/factor-pt-2-sequences-and-control-structures-4ea9</guid>
      <description>&lt;p&gt;In the first part of the tutorial I have shown how to use Factor words to put numbers on the stack, shuffle them and perform arithmetic operations on them. I also presented how you can define your own words if you want to reuse code often. All of this only involved numbers on a stack, but Factor can do much more than that. Today I will show you basic sequence types and how to use them for flow control, but first I need to cover something I forgot to mention in the previous article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Boolean values
&lt;/h3&gt;

&lt;h6&gt;
  
  
  And operations
&lt;/h6&gt;

&lt;p&gt;Factor recognises two boolean values: &lt;code&gt;t&lt;/code&gt; for true and &lt;code&gt;f&lt;/code&gt; for false. In addition to that &lt;code&gt;f&lt;/code&gt; is a general value representing lack of other suitable values like Nil or None in other languages, and any other value will be interpreted as &lt;code&gt;t&lt;/code&gt; in tests and boolean arithmetics. Factor provides all standard boolean arithmetics operations: &lt;code&gt;not&lt;/code&gt;, &lt;code&gt;or&lt;/code&gt;, &lt;code&gt;and&lt;/code&gt; and &lt;code&gt;xor&lt;/code&gt;. Words &lt;code&gt;or&lt;/code&gt;, &lt;code&gt;and&lt;/code&gt; and &lt;code&gt;xor&lt;/code&gt; don't convert their inputs to booleans for testing so their output can also be non-boolean (this behaviour is out of scope of this series, but if you want to know more, check their respective help pages for more information). For the sake of clarity I will only be using them for operations on booleans, but remember that any value that is not &lt;code&gt;f&lt;/code&gt; is considered true.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="no"&gt;t&lt;/span&gt; &lt;span class="no"&gt;t&lt;/span&gt; &lt;span class="nb"&gt;and&lt;/span&gt; &lt;span class="no"&gt;f&lt;/span&gt; &lt;span class="nb"&gt;or&lt;/span&gt; &lt;span class="no"&gt;t&lt;/span&gt; &lt;span class="nb"&gt;xor&lt;/span&gt;
&lt;span class="c"&gt;! S: t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Numbers can be compared to produce boolean values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="nb"&gt;&amp;gt;=&lt;/span&gt;
&lt;span class="c"&gt;! S: f t t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that out of the way let's proceed to sequences.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quotations
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Lambdas, anonymous functions, etc.
&lt;/h6&gt;

&lt;p&gt;The most commonly used sequence type in Factor is a &lt;strong&gt;quotation&lt;/strong&gt; which stores a series of words. The word that creates quotations, as well as most other collections, is a syntax word. Syntax word for literal quotations is &lt;code&gt;[&lt;/code&gt; with syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;elements...&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note spaces separating brackets from the elements.&lt;br&gt;
Quotations store words for later execution on the stack. You can think of them as anonymous functions or lambdas if it helps you.&lt;br&gt;
Words that operate on quotations are called combinators and control structures are implemented using them.&lt;/p&gt;

&lt;p&gt;The simplest combinator is &lt;code&gt;call&lt;/code&gt; which simply executes contents of the quotation on top of the stack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;call&lt;/span&gt;
&lt;span class="c"&gt;! S: 2 9&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Way more useful are the cleave combinators: &lt;code&gt;bi&lt;/code&gt;, &lt;code&gt;tri&lt;/code&gt; and &lt;code&gt;cleave&lt;/code&gt;. They take some quotations and execute them one after another on separate copies of the stack element that is just beneath them. &lt;code&gt;bi&lt;/code&gt; works with two quotations, &lt;code&gt;tri&lt;/code&gt; with three and &lt;code&gt;cleave&lt;/code&gt; works with a sequence of quotations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;bi&lt;/span&gt;
&lt;span class="c"&gt;! S: 5 2&lt;/span&gt;
&lt;span class="nb"&gt;2dup&lt;/span&gt;

&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;tri&lt;/span&gt;
&lt;span class="c"&gt;! S: 5 2 12&lt;/span&gt;
&lt;span class="nb"&gt;3dup&lt;/span&gt;

&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;dup&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;cleave&lt;/span&gt;
&lt;span class="c"&gt;! S: 5 2 12 9&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: I used a quotation containing quotations for &lt;code&gt;cleave&lt;/code&gt; but normally an array of quotations is used instead.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you have the Listener handy I encourage you to try experimenting with the cleave combinators, they are used all the time and can let you skip some janky stack shuffling. Also make sure to take a look at their help pages, as well as some similar words like &lt;code&gt;2bi&lt;/code&gt; or &lt;code&gt;bi@&lt;/code&gt;. Help is invoked by executing &lt;code&gt;\ bi help&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Control flow
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Using combinators
&lt;/h6&gt;

&lt;p&gt;Factor has combinators that can serve the functions of standard control flow structures: if-else, while, for and for-each. Let's first focus on the first three, as for-each requires other sequences and I will touch upon it later.&lt;/p&gt;

&lt;p&gt;If-else behaviour is achieved using words &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;when&lt;/code&gt;, &lt;code&gt;unless&lt;/code&gt; or any of the other conditional combinators. Word &lt;code&gt;if&lt;/code&gt; expects a flag (&lt;code&gt;t&lt;/code&gt; or &lt;code&gt;f&lt;/code&gt;) and two quotations. The bottom one will be called when the flag is true, else the top one will be executed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="no"&gt;f&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;if&lt;/span&gt;
&lt;span class="c"&gt;! S: 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is important that both quotations have the same stack effects or the compiler will not be able to unambiguously derive the stack effect of the construction and will throw an error. &lt;code&gt;when&lt;/code&gt; and &lt;code&gt;unless&lt;/code&gt; are variants of &lt;code&gt;if&lt;/code&gt; with no false or true quotations respectively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="no"&gt;t&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;when&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="no"&gt;f&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;unless&lt;/span&gt;
&lt;span class="c"&gt;! S: 9 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Quotations supplied to &lt;code&gt;when&lt;/code&gt; and &lt;code&gt;unless&lt;/code&gt; have to have the same stack effects as their nonexistent counterparts, which means they mustn't change the depth of the stack.&lt;/p&gt;

&lt;p&gt;While and for behaviour can be implemented using looping combinators like &lt;code&gt;while&lt;/code&gt;, &lt;code&gt;until&lt;/code&gt; or &lt;code&gt;loop&lt;/code&gt;. The simplest of them, &lt;code&gt;loop&lt;/code&gt;, takes a quotation and executes it until it leaves an &lt;code&gt;f&lt;/code&gt; on top of the stack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="nb"&gt;swap&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;dup&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;loop&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt;
&lt;span class="c"&gt;! S: 1 1 1 1 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The other two looping combinators take two quotations - a predicate and a body - and execute them repeatedly until (or while) the predicate leaves a true value on top of the stack. I'm not going to provide any examples for these because I have never used them and they aren't very useful in practice. A much more important behaviour of for-each will be discussed later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic sequences
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Arrays and strings
&lt;/h6&gt;

&lt;p&gt;The most basic sequence type taught for most languages is usually an &lt;strong&gt;array&lt;/strong&gt;. It's a simple ordered sequence of values which can be anything you can put on the stack. Literal arrays are created by a syntax word &lt;code&gt;{&lt;/code&gt; with syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;{&lt;/span&gt; &lt;span class="n"&gt;elements...&lt;/span&gt; &lt;span class="n"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Strings&lt;/strong&gt; are arrays of numbers representing unicode characters.&lt;br&gt;
Literal strings are created by a syntax word &lt;code&gt;"&lt;/code&gt; which has the usual syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="s"&gt;"string..."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the lack of spaces separating the quotes from the string itself. Factor strings have full unicode support and allow a lot of escape sequences to specify various characters. For more info check &lt;code&gt;\ " help&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Iterating over sequences
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Sequences plus combinators
&lt;/h6&gt;

&lt;p&gt;Sequences come with their own words like &lt;code&gt;nth&lt;/code&gt;, &lt;code&gt;length&lt;/code&gt; or &lt;code&gt;set-nth&lt;/code&gt;, but here I would like to focus on sequence combinators, because they allow us to implement the for-each behaviour. There are many words used to iterate over contents of sequences, I will focus on the three most common: &lt;code&gt;each&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;filter&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;each&lt;/code&gt; takes a sequence and a quotation and executes this quotation on every element of the sequence in order. The quotation can use other elements of the stack but it has to consume exactly one - the sequence element. It can be used for example to implement a sequence reduce combinator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="n"&gt;}&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;each&lt;/span&gt;
&lt;span class="c"&gt;! S: 45&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example each of the numbers is added to 0 resulting in a sum of all numbers from 0 to 9. This usage actually has its own word, &lt;code&gt;reduce&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;map&lt;/code&gt; will apply the quotation to each element of the sequence producing new elements that are then collected into a new sequence. It lets you apply a transformation on many values at once. For example if you wanted an array of squares of numbers up to 10:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="n"&gt;}&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;sq&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt;
&lt;span class="c"&gt;! S: { 0 1 4 9 16 25 36 49 64 81 100 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;sq&lt;/code&gt; is a word I mentioned in the previous tutorial. It executes &lt;code&gt;dup *&lt;/code&gt; which multiplies the number by itself.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;filter&lt;/code&gt; will apply the quotation to each element of the sequence and create a new sequence with elements for which the quotation returned a true value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="mi"&gt;925&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;94&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;474&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="n"&gt;}&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;even?&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;filter&lt;/span&gt;
&lt;span class="c"&gt;! S: { 6 2 94 0 4 474 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;even?&lt;/code&gt;, as the name suggests, returns &lt;code&gt;t&lt;/code&gt; for even inputs and &lt;code&gt;f&lt;/code&gt; for odd. It is used here to filter out the odds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Calculating primes
&lt;/h3&gt;

&lt;h6&gt;
  
  
  A practical example
&lt;/h6&gt;

&lt;p&gt;Given what we learned today we can finally write something useful. I will be using a top-down design order, meaning I will start with the most general word and figure out what helper words will be needed to implement it. Almost everything I come up in this section is already available in the &lt;code&gt;math.primes&lt;/code&gt; vocabulary, but it's a good example so let's pretend we don't know that.&lt;/p&gt;

&lt;p&gt;Let's say we want to write a word that will filter only prime numbers from a sequence. For that we can use the word &lt;code&gt;filter&lt;/code&gt; with a predicate quotation that returns true if a number is prime. Our general word looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;filter-primes&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;seq&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;new-seq&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;prime?&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;filter&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you write it like this into the listener it will complain that word &lt;code&gt;prime?&lt;/code&gt; is not in the currently used vocabularies, so let's define &lt;code&gt;prime?&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The simplest way to check if a number is prime is to check if any number from 2 to its square root is its divisor. This method is called a trial division and it's not the most efficient way of looking for primes, but it is simple. To do this we will first transform a sequence of numbers from 2 to sqrt(n) into boolean values indicating divisibility and then reduce this sequence using the function &lt;code&gt;or&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;prime?&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;n&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;dup&lt;/span&gt; &lt;span class="n"&gt;[2,sqrt(n)]&lt;/span&gt;             &lt;span class="c"&gt;! Construct the sequence&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;over&lt;/span&gt; &lt;span class="n"&gt;multiple?&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt; &lt;span class="nb"&gt;nip&lt;/span&gt;  &lt;span class="c"&gt;! Check divisibility&lt;/span&gt;
    &lt;span class="no"&gt;f&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;or&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;reduce&lt;/span&gt; &lt;span class="nb"&gt;not&lt;/span&gt;         &lt;span class="c"&gt;! Take a logic or of the whole sequence&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This didn't help at all! Now we have two undefined words: &lt;code&gt;[2,sqrt(n)]&lt;/code&gt; and &lt;code&gt;multiple?&lt;/code&gt;! The solution is to keep defining until everything works out.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[2,sqrt(n)]&lt;/code&gt; should return a sequence of integers from 2 to sqrt(n). Procedurally generating sequences is not an easy topic so I will yield and use a word from &lt;code&gt;math.ranges&lt;/code&gt; vocabulary: &lt;code&gt;[a,b]&lt;/code&gt;. This word constructs a sequence of numbers starting at it's first argument and incrementing by one up to but not exceeding the second argument. It's now only a matter of calculating these arguments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="kn"&gt;USING:&lt;/span&gt; &lt;span class="nn"&gt;math.ranges&lt;/span&gt; &lt;span class="nn"&gt;math.functions&lt;/span&gt; &lt;span class="k"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;[2,sqrt(n)]&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;n&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;range&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sqrt&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="nb"&gt;swap&lt;/span&gt; &lt;span class="n"&gt;[a,b]&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;USING:&lt;/code&gt; and &lt;code&gt;USE:&lt;/code&gt; are syntax words that tell Factor in what vocabularies it should search for words we use. Here &lt;code&gt;[a,b]&lt;/code&gt; is from &lt;code&gt;math.ranges&lt;/code&gt; and &lt;code&gt;sqrt&lt;/code&gt; is from &lt;code&gt;math.functions&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we are again left with one undefined word: &lt;code&gt;multiple?&lt;/code&gt;. It's function is to check whether its second argument is a multiple of the first. A usual way to check for divisibility is by using the modulo function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;multiple?&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;n&lt;/span&gt; &lt;span class="nv"&gt;m&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;swap&lt;/span&gt; &lt;span class="nb"&gt;mod&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it! We're done! If you enter those definitions in a correct order you will have a working &lt;code&gt;filter-primes&lt;/code&gt; word!&lt;/p&gt;

&lt;p&gt;Now, this code has a bunch of problems. For starters it considers 2 to be composite. It will also break when checking if zero is prime. Check if you can use some constructions we learned today to fix these problems. Bonus points if you can keep all word definitions to three short lines (tip: define more words).&lt;/p&gt;

&lt;h4&gt;
  
  
  Practical example code
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="kn"&gt;USING:&lt;/span&gt; &lt;span class="nn"&gt;math.functions&lt;/span&gt; &lt;span class="nn"&gt;math.ranges&lt;/span&gt; &lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;multiple?&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;n&lt;/span&gt; &lt;span class="nv"&gt;m&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;swap&lt;/span&gt; &lt;span class="nb"&gt;mod&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;[2,sqrt(n)]&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;n&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;range&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sqrt&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="nb"&gt;swap&lt;/span&gt; &lt;span class="n"&gt;[a,b]&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;prime?&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;n&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;dup&lt;/span&gt; &lt;span class="n"&gt;[2,sqrt(n)]&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;over&lt;/span&gt; &lt;span class="n"&gt;multiple?&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt; &lt;span class="nb"&gt;nip&lt;/span&gt;
    &lt;span class="no"&gt;f&lt;/span&gt; &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="nb"&gt;or&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;reduce&lt;/span&gt; &lt;span class="nb"&gt;not&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;filter-primes&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;seq&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;new-seq&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;[&lt;/span&gt; &lt;span class="n"&gt;prime?&lt;/span&gt; &lt;span class="n"&gt;]&lt;/span&gt; &lt;span class="nb"&gt;filter&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;

&lt;span class="c"&gt;! Example use:&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt; &lt;span class="n"&gt;[a,b]&lt;/span&gt; &lt;span class="n"&gt;filter-primes&lt;/span&gt;
&lt;span class="c"&gt;! S: V{ 3 5 7 11 13 17 19 23 29 31 37 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't worry about &lt;code&gt;V{&lt;/code&gt;. It's a syntax word for literal vectors and it does the same thing as &lt;code&gt;{&lt;/code&gt; for arrays. The difference isn't important here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;h6&gt;
  
  
  We did something!
&lt;/h6&gt;

&lt;p&gt;In this tutorial you learned about basic sequences in Factor as well as quotations and how to use them with control flow combinators. I went through a top-down design process for a simple word. Next time I will be looking into Factor's object system: class tuples.&lt;/p&gt;

&lt;p&gt;P.S.  You may have noticed a style change in the middle of this article. That's because my editor/corrector unfortunately no longer has enough time to edit my articles as fast as I write them. Subsequent articles will be released uncorrected and (hopefully) edited at a later date.&lt;/p&gt;

</description>
      <category>factor</category>
      <category>concatenative</category>
      <category>tutorial</category>
      <category>esoteric</category>
    </item>
    <item>
      <title>Factor pt. 1 - Concatenative basics</title>
      <dc:creator>Alex Esoposting</dc:creator>
      <pubDate>Sun, 05 Sep 2021 07:48:26 +0000</pubDate>
      <link>https://dev.to/olus2000/factor-pt-1-concatenative-basics-gpd</link>
      <guid>https://dev.to/olus2000/factor-pt-1-concatenative-basics-gpd</guid>
      <description>&lt;p&gt;As I mentioned in my previous post, concatenative programming (CP) is cool in theory, but it is different enough that it requires breaking out of the standard imperative mindset. Concatenative advocates claim (quite boldly) that thinkng in CP enforces clean code because ugly code is never more efficient. Let me show you how to think concatenatively so you can decide for yourself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Terminology and language choice
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Things I should've mentioned in the first paragraph
&lt;/h6&gt;

&lt;p&gt;In my CP tutorials I will be using a language called Factor, for the following reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As far as I'm aware, Factor is the most developed concatenative language&lt;/li&gt;
&lt;li&gt;It has a lot of useful high level libraries&lt;/li&gt;
&lt;li&gt;It is one of two concatenative languages I can program in&lt;/li&gt;
&lt;li&gt;It has syntax highlighting on dev.to&lt;/li&gt;
&lt;li&gt;You can easily &lt;a href="//factorcode.org"&gt;download it&lt;/a&gt; for most systems&lt;/li&gt;
&lt;li&gt;It has a great interactive environment with all docs built in&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Factor can be used as a high level language but exposes low level functionality for the brave folks and compiles to efficient machine code. If you prefer something meant for low level programming try Forth - much older but still well documented and with similar syntax.&lt;/p&gt;

&lt;p&gt;Speaking of syntax: Factor doesn't have much of it. In CP there are only functions operating on the program's state, so there is no need for any operators, parentheses or indentation. Because of that Factor functions are called "words" and their names can contain any non-white characters. Every program is just a sequence of words separated with white characters.&lt;/p&gt;

&lt;p&gt;Why don't you go and install Factor and open Listener for yourself so that we can take a look at some code!&lt;br&gt;
If you have any burning questions regarding any Factor word just type &lt;code&gt;\ questionable-word help&lt;/code&gt; into the Listener and you will get an interactive help that will hopefully satisfy your appetite for information.&lt;/p&gt;
&lt;h3&gt;
  
  
  The stack
&lt;/h3&gt;
&lt;h6&gt;
  
  
  Our lord and saviour
&lt;/h6&gt;

&lt;p&gt;Factor is a stack-based language, meaning its primary way of storing data is on a stack. Factor's stack is like a pile of books: you can put values on top of it and take values from the top. It is easy to access objects near the top of the stack, but trying to operate on deeper stuff is risky. Factor's stack doesn't consist of books though, it holds values like integers, strings or arrays.&lt;/p&gt;

&lt;p&gt;In my code examples I will show code and it's effect on the stack like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="n"&gt;Code&lt;/span&gt; &lt;span class="n"&gt;will&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;shown&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt;
&lt;span class="c"&gt;! S: The final stack state from bottom to top goes here&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So code that puts some literal integers onto the stack will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="c"&gt;! S: 9 3 2 5 1 2 5 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any number in Factor is a word that pushes this number onto the stack. In this example &lt;code&gt;9&lt;/code&gt; was executed first so number 9 is at the bottom of the stack and &lt;code&gt;0&lt;/code&gt; was executed last so number 0 is at the top. &lt;code&gt;!&lt;/code&gt; is a word indicating that the rest of the line is a comment and shouldn't be executed.&lt;/p&gt;

&lt;p&gt;So far we've seen how to put numbers on a stack. To operate on them Factor offers all the usual arithmetic operations: &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt; and so forth. To add 1 and 2 together you would write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
&lt;span class="c"&gt;! S: 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"What is that weird order?" you might ask? It's called Reverse Polish Notation and it's the natural way of ordering stuff for a stack-based language. What the &lt;code&gt;+&lt;/code&gt; word does is take two values on top of the stack and replace them with their sum. It is not an operator - it doesn't care what words are around it. It only operates on the stack, so in order to add 1 and 2 they have to already be on the stack.&lt;/p&gt;

&lt;p&gt;This order - operands first and then operator - facilitates arithmetics without parenthesis. Any order of operations is obvious from the order in which those operations are written. No ambiguity, no precedence rules, just execution from left to right. For example, an operation (3 + 4) * 2 - 5 would be written as &lt;code&gt;3 4 + 2 * 5 -&lt;/code&gt;, and 3 + 4 * (2 - 5) would be &lt;code&gt;3 4 2 5 - * +&lt;/code&gt;. If you have the Listener running (or rather - listening) you can enter each of these examples word by word and see how it ends up with the correct answer in the correct order. You can get accustomed to the idea by trying it out on a piece of paper as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stack shuffling
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Getting things where we need them
&lt;/h6&gt;

&lt;p&gt;Because the stack is used for most of the operations, the values we need to operate on at a given moment aren't always in the right positions. This is where the stack shuffling words come into play: they are used to rearrange the top of the stack to fit our needs. Most popular shuffling words include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;dup ( a -- a a )&lt;/code&gt; - duplicates the top value on the stack&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;drop ( a -- )&lt;/code&gt; - removes the top value from the stack&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;swap ( a b -- b a )&lt;/code&gt; - swaps two values on top of the stack&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;over ( a b -- a b a )&lt;/code&gt; - copies the second value from the top and puts it on top of the stack&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nip ( a b -- b )&lt;/code&gt; - removes the second value from the top of the stack&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of them has a version that works on two values at once:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;2dup ( a b -- a b a b)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;2drop ( a b -- )&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;2swap ( a b c d -- c d a b)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And so on...&lt;/p&gt;

&lt;p&gt;All of these are used constantly in Factor programs. You want to square the number on top of the stack?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="nb"&gt;dup&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="c"&gt;! S: 9&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You want to subtract the number on top of the stack from 10?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="nb"&gt;swap&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;
&lt;span class="c"&gt;! S: 7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You want to get rid of all the stuff you put on the stack when playing with the Listener? Just dump a couple of &lt;code&gt;4dup&lt;/code&gt;s and enjoy the clean slate!&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining new words
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Factorisation incoming
&lt;/h6&gt;

&lt;p&gt;In CP it is very easy to take any piece of code and define a new word that executes it. You don't have to care about giving your operators all operands or naming variables, after all you can split the code at (almost) any point and both parts would be valid Factor programs. That's where the name concatenative comes from.&lt;/p&gt;

&lt;p&gt;Defining a new word uses a syntax word &lt;code&gt;:&lt;/code&gt;. Syntax words work differently from normal words - they work with what is written after them instead of with the stack. They introduce the tiny amount of syntax Forth has and are usually used to define new words. Syntax of &lt;code&gt;:&lt;/code&gt; is &lt;code&gt;: name stack-effect body ;&lt;/code&gt;. Let's see this on the squaring example from before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;sq&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;x&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;x^2&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;dup&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;;&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="nb"&gt;sq&lt;/span&gt;
&lt;span class="c"&gt;! S: 9&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;:&lt;/code&gt; is the syntax word that parses the definition&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;( x -- x^2 )&lt;/code&gt; is a stack effect declaration. I will talk about them in a minute&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dup *&lt;/code&gt; is the body of the word. These words will be invoked each time &lt;code&gt;sq&lt;/code&gt; is called&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;;&lt;/code&gt; indicates the end of the &lt;code&gt;:&lt;/code&gt; definition. Code after the semicolon resumes normal interpretation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You should define a new word every time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There is a piece of code you are using in three or more places&lt;/li&gt;
&lt;li&gt;You feel the need to use a comment to explain what a piece of code does&lt;/li&gt;
&lt;li&gt;Your word definition has more than three lines&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is called factoring and it is the essence of writing clean and self-commenting code. Factor is optimised for calling words from within words so don't worry about inefficiency.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stack effects
&lt;/h3&gt;

&lt;h6&gt;
  
  
  A wannabe type system
&lt;/h6&gt;

&lt;p&gt;You have definitely noticed the syntax &lt;code&gt;( x y -- z )&lt;/code&gt; used in a couple of places in this article and maybe you have even guessed what it means. It's called a "stack effect description" and it shows what effect the function has on the stack. It is required in every &lt;code&gt;:&lt;/code&gt; definition to make compilation more efficient and to make sure you know what you are doing with your code.&lt;/p&gt;

&lt;p&gt;The part before the &lt;code&gt;--&lt;/code&gt; needs to include a word for every stack element expected by the word and the part after it includes a word for every stack element left by the word, including all expected elements. You have already seen a lot of examples in the stack-shuffling section. The stack effect description can be as verbose as you like, ranging from &lt;code&gt;( url http-server -- post-request )&lt;/code&gt; to &lt;code&gt;( a b -- c )&lt;/code&gt;. It is only required that the number of words matches affected elements, but good stack effects make the code cleaner and more understandable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;h6&gt;
  
  
  And what's coming next
&lt;/h6&gt;

&lt;p&gt;This article explained the very basics of concatenative programming in Forth: literals, stack shuffling and word definitions. In the next tutorial I will show you how Factor handles control flow and collections such as arrays and hashtables. Stay tuned for the next week!&lt;/p&gt;

</description>
      <category>factor</category>
      <category>concatenative</category>
      <category>esoteric</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What are concatenative languages?</title>
      <dc:creator>Alex Esoposting</dc:creator>
      <pubDate>Sat, 28 Aug 2021 13:46:10 +0000</pubDate>
      <link>https://dev.to/olus2000/what-are-concatenative-languages-8od</link>
      <guid>https://dev.to/olus2000/what-are-concatenative-languages-8od</guid>
      <description>&lt;p&gt;Probably all of you are familiar with the imperative programming paradigm and some of you may know about the functional paradigm, but I recently discovered and fell in love with the third one: concatenative. Let me tell you all about it using opaque examples and bad simplifications!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Trigger warning: section about functional programming is very simplified and I don't have proper education to go in more depth on this topic.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Imperative paradigm
&lt;/h3&gt;

&lt;h6&gt;
  
  
  The easy one
&lt;/h6&gt;

&lt;p&gt;&lt;strong&gt;Imperative programming&lt;/strong&gt; (IP) is what most people are familiar with as the majority of the most popular languages heavily support this paradigm. An algorithm in this paradigm is described as a series of orders (imperatives) that the machine executes to get the desired result. &lt;/p&gt;

&lt;p&gt;Its popularity comes from the fact that hardware languages are imperative in nature: the processor reads commands one after another executing them in order. Higher level languages were built on top of lower level ones by people familiar with this low level imperativeness. They made these languages imperative too to facilitate efficient compilation or interpretation.&lt;/p&gt;

&lt;p&gt;IP has a theoretical basis on &lt;em&gt;Alan Turing&lt;/em&gt;'s interpretation of computation - the Turing Machine. It's a hypothetical machine that consists of a memory that can be read one cell at a time and directives, which based on the current cell's content can change its value or move to another cell. That's roughly what happens in the processor, albeit in a much more sophisticated way. Not much thought is actually given to Turing's computationnal model directly when programming imperatively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functional paradigm
&lt;/h3&gt;

&lt;h6&gt;
  
  
  The elegant one
&lt;/h6&gt;

&lt;p&gt;&lt;strong&gt;Functional programming&lt;/strong&gt; (FP) stems from a different basis for computation theory - &lt;em&gt;Alonzo Church&lt;/em&gt;'s Lambda Calculus. In this model everything is a function that takes a single argument (another function) and returns a new function as a result. That may seem useless - what can you possibly do with just functions? - until you realize that you can define certain functions to represent numbers. If you do this well it is easy to define functions that act as mathematical operations, tuples, arrays, classes and any other thing you know from imperative programming and the whole algorithm ends up being defined as a function from program input to program output. In practice languages define these core concepts for you so you can use your familiar mathematical notation and focus on higher level stuff.&lt;/p&gt;

&lt;p&gt;At first this looks like a weird way of writing normal code which doesn't translate that well into machine language. There is actually a grain of truth in the second statement, functional environments have their ways of efficiently translating their code into imperative machine language, though.&lt;/p&gt;

&lt;p&gt;The power of FP lays not in how well it can emulate IP, but in what is different between them: just like imperative is close to the hardware, functional is close to maths. This puts off many people. Why would I care about mathematical basis of my code? It is supposed to operate on real data on a physical machine, not in the realm of theoretical constructs. And to that functional programmers answer: why not both?&lt;/p&gt;

&lt;p&gt;People who actually learned this in college could tell you a lot about advantages of FP, but there are a couple of things I find particularly neat about it (except for the quirkiness of everything being a function): it doesn't allow for global variables* (or any variables at all), every function works only with information it got on its input, and types.&lt;/p&gt;

&lt;p&gt;*If you don't know yet why globals are bad then &lt;a href="https://googlethatforyou.com?q=why%20global%20variables%20are%20bad"&gt;let me google that for you&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Types
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Basically all I know about FP
&lt;/h6&gt;

&lt;p&gt;In IP types are tied to the physical representation of data: integers are a couple of bytes, strings are arrays of bytes, floats are also a couple of bytes but interpreted differently and so on. In FP there is no data, just functions, so types are based on certain properties of these functions, and data is defined as functions that fit these types. Integer is anything that has properties of an integer and can be used as an integer in functions that require integers as input. This makes types just sets of things that belong in this type. If something belongs to a set of integers it is an integer.&lt;/p&gt;

&lt;p&gt;It allows to assign types to functions based on the input they require and output they give. Negation for example can have type &lt;code&gt;int -&amp;gt; int&lt;/code&gt; because it takes an integer and returns an integer. Addition would be &lt;code&gt;int -&amp;gt; int -&amp;gt; int&lt;/code&gt; because it takes an integer and returns a function that takes another integer that returns an integer - a sum of the previous ones. It is not defined for any other input, so it doesn't make sense to give it any other input. It's not that the function will throw an error if this happens: because you know exactly which function uses which types you can check during compile-time if a function receives input that is outside its input type and raise a compilation error. This prevents bad practices based on passing around data of unknown type and with careful code almost eliminates runtime errors.&lt;/p&gt;

&lt;p&gt;From this comes the most amazing thing ever: because your whole program is a function in a pure mathematical sense you can &lt;em&gt;prove its correctness&lt;/em&gt;. It's not easy and some languages hide the mathematical concepts so deep that it becomes impossible, but in some functional languages you can &lt;em&gt;definitely prove your program does what you think it does&lt;/em&gt;. Ever got tired of writing unit tests for your application? Switch to FP and prove that your program will give correct output &lt;em&gt;every time&lt;/em&gt;! Isn't that cool?&lt;/p&gt;

&lt;h3&gt;
  
  
  Why need anything else?
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Isn't two enough?
&lt;/h6&gt;

&lt;p&gt;You might think that between hardware efficiency of IP and mathematical purity of FP everyone should find their perfect way of coding, but we can do better! The solution would be to take the imperative paradigm and make it functional. Every IP program can be translated to FP by making each order a function that takes the program's state, transforms it and outputs a new state. If you do that to a normal imperative program then the state would contain all the declared variables and each function would have access to any of them making global variables a problem again. It requires some changes.&lt;/p&gt;

&lt;p&gt;The solution most concatenative languages go for is to replace the variables with a stack. Each function can only access the top of the stack (actually a couple of top values), but getting to deeper values is hard which discourages attempts to recreate global variables. Each function can be treated as a functions that takes some arguments and returns some values depending on how it modifies the top of the stack. In this scheme it is even possible to make a whole type system by stating exactly what values the function expects at the top of the stack and what values it leaves there, and all that while retaining the imperative "do this then that" style of coding.&lt;/p&gt;

&lt;h3&gt;
  
  
  Concatenative syntax
&lt;/h3&gt;

&lt;h6&gt;
  
  
  What makes it so awesome
&lt;/h6&gt;

&lt;p&gt;&lt;em&gt;Concatenative programming&lt;/em&gt; (CP) takes from FP the fact that a program consists entirely of functions. If you join it with the fact that each function always takes in the state and spits out another state for the next function to take you realize there is no need for parentheses at all - you can just list functions one by one as they should be executed and the state passes through all of them in order. It is reflected by naming conventions: CP functions are called words, and can be any string of non-white characters because only them have any syntactical meaning.&lt;/p&gt;

&lt;p&gt;Because each word exists as a standalone function that doesn't really care what's to the left or to the right you can freely mix and match words to create programs. If you have two programs then just writing them one after another will result in a program that runs the first one and passes its output to the second one -- hence the name "concatenative". This freedom of splitting and joining means that if you use certain word sequences often you can (and you should) define a new word that does the same job. Then you can assign this new word a (possibly) more verbose name, which makes the code cleaner - this is called factoring and is the superpower of CP. In fact you should be doing it in every paradigm, but CP makes it blatantly easy.&lt;/p&gt;

&lt;p&gt;With good factoring the code should be self-documenting and each word definition shouldn't exceed 3 lines of code. Here you can see an example of a word that takes a game ID and an API key for Neptune's Pride and fetches game info using the API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight factor"&gt;&lt;code&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;fetch-game&lt;/span&gt; &lt;span class="nf"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;game-id&lt;/span&gt; &lt;span class="nv"&gt;api-key&lt;/span&gt; &lt;span class="nf"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;game-json/error-json&lt;/span&gt; &lt;span class="nf"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;prepare-params&lt;/span&gt;
    &lt;span class="n"&gt;prepare-post-data&lt;/span&gt;
    &lt;span class="n"&gt;game-url&lt;/span&gt; &lt;span class="n"&gt;fetch-json&lt;/span&gt;
&lt;span class="k"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Obviously each of those four words used here is defined in an earlier section of the vocabulary, but they are of similar size and complexity. The programming language I'm using is called Factor to emphasize how important it is to factor out common code, or just any code that looks too complicated. Factoring groups code into small, understandable words which then make bigger words, which then make bigger words and so on. It is truly a marvel.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which languages are concatenative?
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Not many actually...
&lt;/h6&gt;

&lt;p&gt;Most modern popular languages support both FP and IP despite being IP-based, but there is no viable way to support CP without huge syntax changes. This means we are left with whatever languages are designed to be CP from the start, and most of them are getting pretty old.&lt;/p&gt;

&lt;p&gt;The first CP language (so old that they didn't use the word "concatenative" for it yet) is &lt;a href="https://www.forth.com/forth/"&gt;Forth&lt;/a&gt;. It's pretty low level and compiles to very fast code but lacks many modern features and useful libraries. Despite that it can be a good intro to the CP world.&lt;/p&gt;

&lt;p&gt;A semi-modern language based on Forth is &lt;a href="https://factorcode.org/"&gt;Factor&lt;/a&gt; which I presented earlier. Factor incorporates a primitive type system and a more functional-style control flow. It has a lot of useful and well documented libraries as its community was very active back in the day. It's not entirely dead now, but it's not very alive either.&lt;/p&gt;

&lt;p&gt;There is a (not very long) list of concatenative languages at &lt;a href="//concatenative.org"&gt;concatenative wiki&lt;/a&gt;, although most of them are either dead or unfinished. I say it with sorrow, but despite its power the concatenative paradigm is just another esoteric idea now.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;h6&gt;
  
  
  What follows
&lt;/h6&gt;

&lt;p&gt;Despite lack of support for the concatenative paradigm I'm still charmed by its simplicity and ease of factorisation and I will continue to play with concatenative languages for a while which may result in a couple more posts of a more practical nature. If you know a concatenative language that you can recommend (even as a curiosity) please drop a line below. See you soon with some tutorials on an old, unusable language.&lt;/p&gt;

</description>
      <category>concatenative</category>
      <category>esoteric</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Brainfuck Assembly Language Tutorial</title>
      <dc:creator>Alex Esoposting</dc:creator>
      <pubDate>Sat, 17 Jul 2021 18:06:04 +0000</pubDate>
      <link>https://dev.to/olus2000/brainfuck-assembly-language-tutorial-3lf6</link>
      <guid>https://dev.to/olus2000/brainfuck-assembly-language-tutorial-3lf6</guid>
      <description>&lt;p&gt;So you have your personal Brainfuck Processing Unit (or at least its simulation) and you want to do something with it. BAL is based on brainfuck, a language famous for its mind breaking programming experiences, but if you learn to think in a certain way neither of them is that hard. Don't raise your expectations too high though, I'm not building a game or a web app in this tutorial.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basics
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Like brainfuck but better!
&lt;/h6&gt;

&lt;p&gt;BAL uses eight one-character commands: &lt;code&gt;+-&amp;gt;&amp;lt;[],.&lt;/code&gt;. Each of them can take an argument represented by a number directly after the command. Just like in brainfuck the commands operate on a linear memory using a single pointer. Anything that's not a command or a number is a comment and is ignored by compilers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;+&lt;/code&gt;/&lt;code&gt;-&lt;/code&gt;: add/subtract the argument from the selected cell.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;gt;&lt;/code&gt;/&lt;code&gt;&amp;lt;&lt;/code&gt;: move the pointer up or down the memory according to the argument.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[&lt;/code&gt;/&lt;code&gt;]&lt;/code&gt;: jump forwards/backwards some number of commands if the selected cell is zero/nonzero.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;,&lt;/code&gt;/&lt;code&gt;.&lt;/code&gt;: input/output the selected cell. Argument use depends on the BPU build, I will ignore it completely here.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;+-&amp;gt;&amp;lt;[]&lt;/code&gt; commands are called "add commands" and their argument must be at least one. It's also assumed to be one if you don't specify it, but that's a bad practice and leads to less readable code. &lt;code&gt;,.&lt;/code&gt; commands are "I/O commands" ant their default is zero. There will also be a maximum argument depending on the BPU architecture. This tutorial assumes an 8-bit BPU, so max arguments are 32 for add commands and 31 for I/O commands.&lt;/p&gt;

&lt;h3&gt;
  
  
  Simple loops
&lt;/h3&gt;

&lt;h6&gt;
  
  
  The most basic thing in BAL
&lt;/h6&gt;

&lt;p&gt;Having seen such a limited set of tools you might start to question whether it's possible to program in BAL at all! How can one write a complex program if every command only ever involves a single cell? The answer is &lt;em&gt;loops&lt;/em&gt;. While it's true that any single command only uses one cell at most if you wrap them in a loop you can create commands that use multiple cells and involve conditions.&lt;/p&gt;

&lt;h4&gt;
  
  
  Set cell to zero
&lt;/h4&gt;

&lt;h6&gt;
  
  
  Loop base
&lt;/h6&gt;

&lt;p&gt;Let's look at the simplest useful loop: setting a cell to zero. There is no command in BAL that would allow to set a cell to a constant value. Instead you need to use subtraction in a loop that repeats until the cell is zero. You should also only subtract one to make sure you don't jump over zero. Here's the code with comments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;In memory explanations cells are divided with pipes (|)
and the current cell is marked by *asterisks*.
Starting memory: *some number*
[3    Jump over the loop if the cell is already zero
  -1  Subtract one
]1    Jump back until the cell is zero
Final memory: *zero*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might recognise this construction as an inverse case of a &lt;em&gt;for loop&lt;/em&gt;. In another language this could be written as &lt;code&gt;for (i = cell; i &amp;gt; 0; i--) pass;&lt;/code&gt;. It's very important to think about actions in BAL in terms of "repeat something N times" and most loops will end with &lt;code&gt;-1 ]X&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Moving values between cells
&lt;/h4&gt;

&lt;h6&gt;
  
  
  Loop body
&lt;/h6&gt;

&lt;p&gt;Again, there's no single command to move a value from cell to cell. Thinking with fors, what is the action to repeat N times to move N somewhere? It's not hard to realise it's just "add 1", which gives us the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Starting memory: *N* | zero 
[6    Jump over the loop if the cell is already zero
  &amp;gt;1  Move over to the cell that the value is moved to
  +1  Increment it
  &amp;lt;1  Move back to the original cell
  -1  Decrement it
]4    Repeat until original cell is zero
Final memory: *zero* | N
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code actually doesn't move the value between cells, it increments zero by the value of a cell. If the target cell was something other than zero it would still end up being incremented by the same amount, giving us addition for free.&lt;/p&gt;

&lt;p&gt;Another thing achievable for free with this code is multiplication by a constant. If instead of adding one N times you add a constant N times you get N multiplied by the constant.&lt;/p&gt;

&lt;h4&gt;
  
  
  Multiplication
&lt;/h4&gt;

&lt;h6&gt;
  
  
  Nested loops
&lt;/h6&gt;

&lt;p&gt;Let's again start by thinking with fors: what's the action to repeat N times to get M*N? It's adding M, and we already know how to do that, so let's see the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Starting memory: *M* | N | target (zero)
[11
  &amp;gt;1  Move over to N
  Add N to the target
  [6 &amp;gt;1 +1 &amp;lt;1 -1 ]4
  &amp;lt;1  Get back to M
  -1 Close up the loop
]9
Final memory: *zero* | zero | N
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looked promising, but the N was moved from source to the target, not copied, so it wasn't there to be added on the next loops. Instead of just moving N to target you need to use another helper cell initialised to zero. Then you move N to target and helper, and after that move helper back to N which leaves it ready for the next loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Starting memory: *M* | N | target (zero) | helper (also zero)
[20
  &amp;gt;1  Move over to N
  Move N to the target and helper
  [8
    &amp;gt;1 +1  Increment target
    &amp;gt;1 +1  Increment helper
    &amp;lt;2 -1  Decrement N
  ]6
  &amp;gt;2 Move to helper
  Move helper back to N
  [6 &amp;lt;2 +1 &amp;gt;2 -1 ]4
  &amp;lt;3  Get back to M
  -1 Close up the loop
]18
Final memory: *zero* | N | N*M
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conditional jumps
&lt;/h3&gt;

&lt;h6&gt;
  
  
  AKA Ifs
&lt;/h6&gt;

&lt;p&gt;In the previous section most of the work was done by the &lt;code&gt;]&lt;/code&gt; command which jumps back and facilitates looping: executing the same code multiple times. Now is the time to take a closer look at the &lt;code&gt;[&lt;/code&gt; command which jumps forward and facilitates ifs: skipping parts of the code based on a condition.&lt;/p&gt;

&lt;h4&gt;
  
  
  Skipping loops
&lt;/h4&gt;

&lt;h6&gt;
  
  
  Been there, done that
&lt;/h6&gt;

&lt;p&gt;I've already shown examples of the most commonly used forward jumps in BAL: skipping over loops that shouldn't be entered. This is exactly how they are used in pure brainfuck as well. Usually they aren't necessary for the program to work correctly in BAL but they can make it several times faster, depending on the loop.&lt;/p&gt;

&lt;p&gt;Let's consider the simplest loop: &lt;code&gt;-1 ]1&lt;/code&gt;. It starts by subtracting one, and then repeats it until the cell is zero. The problem arises when the cell is zero before this loop starts. Since BAL is supposed to be executed on hardware solutions, subtracting one from zero will result in overflow and wrap back to the maximum value the cell can hold (255 for 8-byte solutions), and then repeat the loop the maximum possible number of times until it's back at zero. That's why it's usually worth it to spare this one additional memory cell for a &lt;code&gt;[&lt;/code&gt; command before most loops.&lt;/p&gt;

&lt;h4&gt;
  
  
  Standard ifs
&lt;/h4&gt;

&lt;h6&gt;
  
  
  Normal constructions that you know and love
&lt;/h6&gt;

&lt;p&gt;The only data type brainfuck can handle is integers, and the only comparison operation available is "equals". That doesn't sound like much, but it's enough to make basic "if" blocks that branch depending on a number in a cell. In normal languages it might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if n == 0 then
  do something
else if n == 1 then
  do something else
else if n == 2 then
  do something even more different
else
  do the final thing
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Execution would skip over sections until finding the one that does fulfill the condition, execute it and skip the rest to continue execution after the end of the if block. BAL only allows jumps when the equality condition is &lt;em&gt;fulfilled&lt;/em&gt;, but not on "is not equal to" condition. This follows from the fact that &lt;code&gt;[&lt;/code&gt; offers jumping if the number is equal to zero. This means the code requires some reorganisation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if n == 0 goto N0
if n == 1 goto N1
if n == 2 goto N2
  do the final thing
  goto END
N0:
  do something
  goto END
N1:
  do something else
  goto END
N2:
  do something even more different
END:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This still includes comparisons to numbers other than zero, but checking if N is equal to X can be converted to checking if N-X is equal to zero which can be easily done in BAL. Let's see what the code looks like using BAL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[9     Jump if N equals zero
-1 [9  Jump if N minus one equals zero
-1 [9  Jump if N minus two equals zero
do the final thing
-1 ]1  Set N to zero for the jump
[6
do something
[4  N is already zero here so it can be used for jump
do something else
[2
do something even more different
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this example I considered every "do" a single command so jump arguments are quite small, but they will need to be adjusted to whatever happens in each branch and to the amount of branches. Jumping to the end can be done using a cell different than N, but it's important that every branch ends on the same cell unless you deliberately want the program to move.&lt;/p&gt;

&lt;h3&gt;
  
  
  Moving loops
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Complicated stuff
&lt;/h6&gt;

&lt;p&gt;In the first section I talked about BAL loops as for loops, but actually they are more like &lt;em&gt;while loops&lt;/em&gt;. They can repeat something N times if you go back to the same cell and decrement it at the end of the loop, but but it's sometimes useful to change the cell in another way or even not come back to the same cell altogether.&lt;/p&gt;

&lt;h4&gt;
  
  
  Text reversing
&lt;/h4&gt;

&lt;h6&gt;
  
  
  Ultimate test of skill
&lt;/h6&gt;

&lt;p&gt;The task is to read a string of bytes representing ASCII symbols terminated by a newline, and then return that string in reverse order.&lt;/p&gt;

&lt;p&gt;To reverse a piece of text you need to take each letter as input, store them all and return in reverse order. Seems like a trivial task, but storing a variable number of letters and reading them means moving through a variable number of cells which requires moving loops.&lt;/p&gt;

&lt;p&gt;Let's break down the task into smaller parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Setup&lt;/li&gt;
&lt;li&gt;Reading loop

&lt;ul&gt;
&lt;li&gt;Read a byte&lt;/li&gt;
&lt;li&gt;Check if it's newline&lt;/li&gt;
&lt;li&gt;If not then move to the next cell&lt;/li&gt;
&lt;li&gt;If yes then move to the writing loop&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Writing loop

&lt;ul&gt;
&lt;li&gt;Move to the next saved byte&lt;/li&gt;
&lt;li&gt;Check if it's the end&lt;/li&gt;
&lt;li&gt;If not write the byte&lt;/li&gt;
&lt;li&gt;If yes finish the program&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Because newline (ASCII 10) terminates the string it will never appear in it and can be safely used as a bumper telling the writing loop to stop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Starting memory: *zero* | anything
+10 &amp;gt;1  Set cell to the bumper value and move over
Intermediate memory: bumper | *anything*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the reading loop starts. It checks for the ASCII code for newline.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Intermediate memory: bumper | *anything* | more of anything
,       Read a byte
-10 [6  Don't enter the loop if newline
  +10   Restore original value
  &amp;gt;1 ,  Move over and read a new byte
-10 ]4  Stop looping if newline
Memory after one cycle: bumper | byte | *anything minus ten* | more of anything
Intermediate memory after the loop terminates: bumper | many bytes | *zero*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The loop moves one cell right each time, saving respective bytes to consecutive cells. The writing loop reverses this process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Intermediate memory: bumper | many bytes | *zero*
&amp;lt;1      Move to the last byte
-10 [6  Don't enter the loop if already at the end
  +10   Restore original value
  . &amp;lt;1  Write and move to the next byte
-10 ]4 Stop looping if at the end
Memory after one cycle: bumper | many bytes | *byte* | zero
Final memory: *zero* | many bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Moving loops are a powerful tool with many use cases. It's important to always know that the loop will either hit a bumper or terminate in some other way, otherwise it could overwrite other data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Final remarks
&lt;/h3&gt;

&lt;h6&gt;
  
  
  You're almost ready
&lt;/h6&gt;

&lt;p&gt;The text reversing program could almost be a valid program for the BPU, but it needs some minor corrections. The first one is that after finishing the program the computer would not stop but go on to execute whatever else is stored in memory, and in this case it is the text given by the user. When interpreted as code it is just garbage and would lead to unexpected behaviour.&lt;/p&gt;

&lt;p&gt;To prevent this from happening each program should include a main loop that loops always, no matter what. In our case it just requires to add some code at the end:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+10 .  Set the bumper back to newline and print it
]19    Jump back to the start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the jump is too big to fit in a single argument you can insert intermediate jumps right after jumps in the code. For example if you have this piece of code: &lt;code&gt;]3 ]30&lt;/code&gt; then when it's executed normally either the first jump will take place or no jump will take place. On the other hand if you jump directly into the &lt;code&gt;]30&lt;/code&gt; it will continue jumping back allowing for jumps bigger than whatever your maximum argument is.&lt;/p&gt;

&lt;p&gt;The other minor issue with the program is that both the program pointer and the data pointer start at the first memory cell which is occupied by the first command of the program. The data pointer has to be moved to a code-free area first, and only then the program can be executed. This is accomplished by adding a &lt;code&gt;&amp;gt;22&lt;/code&gt; at the start of the setup, which gives the complete and working program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;22
+10 &amp;gt;1  Set cell to the bumper value and move over
,       Read a byte

-10 [6  Don't enter the loop if newline
  +10   Restore original value
  &amp;gt;1 ,  Move over and read a new byte
-10 ]4  Stop looping if newline

&amp;lt;1      Move to the last byte
-10 [6  Don't enter the loop if already at the end
  +10   Restore original value
  . &amp;lt;1  Write and move to the next byte
-10 ]4 Stop looping if at the end

+10 .  Set the bumper back to newline and print it
]19    Jump back to the start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Go and write code!
&lt;/h6&gt;

&lt;p&gt;Now you know how to use &lt;code&gt;[&lt;/code&gt; and &lt;code&gt;]&lt;/code&gt; commands to create advanced loops and conditionals. With such powerful tools you can write anything! Just remember to carefully plan your memory usage and the BPU is your oyster.&lt;/p&gt;

&lt;p&gt;You can find the BPU simulation as well as a compiler and some examples in the &lt;a href="https://github.com/olus2000/BPU"&gt;PBU github repo&lt;/a&gt;. Have fun with the language!&lt;/p&gt;

</description>
      <category>esoteric</category>
      <category>brainfuck</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
