<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>tag:blog.danieljanus.pl,2019:category:lisp</id>
  <title>Daniel Janus – Lisp</title>
  <link href="http://blog.danieljanus.pl/category/lisp/"/>
  <updated>2020-05-08T00:00:00Z</updated>
  <author>
    <name>Daniel Janus</name>
    <uri>http://danieljanus.pl</uri>
    <email>dj@danieljanus.pl</email>
  </author>
  <entry>
    <id>tag:blog.danieljanus.pl,2020-05-08:post:making-of-clojure-dependency</id>
    <title>Making of “Clojure as a dependency”</title>
    <link href="http://blog.danieljanus.pl/making-of-clojure-dependency/"/>
    <updated>2020-05-08T00:00:00Z</updated>
    <content type="html">&lt;div&gt;&lt;p&gt;In my previous post, &lt;a href="/2020/05/02/clojure-dependency/"&gt;“Clojure as a dependency”&lt;/a&gt;, I’ve presented the results of some toy research on Clojure version numbers seen in the wild. I’m a big believer in &lt;a href="https://en.wikipedia.org/wiki/Reproducibility#Reproducible_research"&gt;reproducible research&lt;/a&gt;, so I’m making available a &lt;a href="https://github.com/nathell/versions"&gt;Git repo&lt;/a&gt; that contains code you can run yourself to reproduce these results. This post is an experience report from writing that code.&lt;/p&gt;&lt;p&gt;There are two main components to this project: acquisition and analysis of data (implemented in the namespaces &lt;code&gt;versions.scrape&lt;/code&gt; and &lt;code&gt;versions.analyze&lt;/code&gt;, respectively). Let’s look at each of these in turn.&lt;/p&gt;&lt;h2 id="data-acquisition"&gt;Data acquisition&lt;/h2&gt;&lt;p&gt;This step uses the &lt;a href="https://developer.github.com/v3/"&gt;GitHub API v3&lt;/a&gt; to:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span&gt;retrieve the 1000 most popular Clojure repositories (using the &lt;a href="https://developer.github.com/v3/search/#search-repositories"&gt;Search repositories&lt;/a&gt; endpoint and going through all &lt;a href="https://developer.github.com/v3/#pagination"&gt;pages&lt;/a&gt; of the paginated result);&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;for each of these repositories, look at its file list (in the master branch) and pick up any files named &lt;code&gt;project.clj&lt;/code&gt; or &lt;code&gt;deps.edn&lt;/code&gt; in the root directory, using the &lt;a href="https://developer.github.com/v3/repos/contents/"&gt;Contents&lt;/a&gt; endpoint);&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;parse each of these files and extract the list of dependencies.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;As hinted by the namespace, I’ve opted to use &lt;a href="https://github.com/nathell/skyscraper"&gt;Skyscraper&lt;/a&gt; to orchestrate the process. It would arguably have been simpler to use GitHub’s &lt;a href="https://developer.github.com/v4/"&gt;GraphQL v4 API&lt;/a&gt;, but I wanted to showcase Skyscraper’s custom parsing facilities.&lt;/p&gt;&lt;p&gt;There’s no actual HTML scraping going on (all processors use either JSON or Clojure parsers), but Skyscraper is still able to “restructure” the result – traverse the graph endpoint in a manner similar to that of GraphQL – with very little effort. It would have been possible with any other RESTful API. Plus, we get goodies like caching or tree pruning for free.&lt;/p&gt;&lt;p&gt;Most of the code is straightforward, but parsing of &lt;code&gt;project.clj&lt;/code&gt; merits some explanation. Some of my initial assumptions proved incorrect, and it’s fun to see how. I initially tried to use &lt;a href="https://clojure.github.io/clojure/clojure.edn-api.html#clojure.edn/read"&gt;&lt;code&gt;clojure.edn&lt;/code&gt;&lt;/a&gt;, but Leiningen project definitions are not actually EDN – they are Clojure code, which is a superset of EDN. So I had to resort to &lt;code&gt;read-string&lt;/code&gt; from core – with &lt;code&gt;*read-eval*&lt;/code&gt; bound to nil (otherwise the code would have a Clojure injection vulnerability – think &lt;a href="https://xkcd.com/327/"&gt;Bobby Tables&lt;/a&gt;). Needless to say, some &lt;code&gt;project.clj&lt;/code&gt;s turned out to depend on read-eval.&lt;/p&gt;&lt;p&gt;Some projects (I’m looking at you, &lt;a href="https://github.com/dundalek/closh"&gt;Closh&lt;/a&gt;, &lt;a href="https://github.com/borkdude/babashka"&gt;Babashka&lt;/a&gt; and &lt;a href="https://github.com/borkdude/sci"&gt;sci&lt;/a&gt;) keep the version number outside of &lt;code&gt;project.clj&lt;/code&gt;, in a text file (typically in &lt;code&gt;resources/&lt;/code&gt;), and slurp it back into &lt;code&gt;project.clj&lt;/code&gt; with a read-eval’d expression:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;(&lt;span class="hljs-name"&gt;defproject&lt;/span&gt; closh-sci
  #=(&lt;span class="hljs-name"&gt;clojure.string/trim&lt;/span&gt;
     #=(&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;slurp&lt;/span&gt;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;resources/CLOSH_VERSION&amp;quot;&lt;/span&gt;))
  …)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A trick employed by one project, &lt;a href="https://github.com/metabase/metabase"&gt;Metabase&lt;/a&gt;, is to dynamically generate JVM options containing a port number at parse time, so that test suites running at the same time don’t clash with each other:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;#=(&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;eval&lt;/span&gt;&lt;/span&gt; (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;format&lt;/span&gt;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;-Dmb.jetty.port=%d&amp;quot;&lt;/span&gt; (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;+&lt;/span&gt;&lt;/span&gt; &lt;span class="hljs-number"&gt;3001&lt;/span&gt; (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;rand-int&lt;/span&gt;&lt;/span&gt; &lt;span class="hljs-number"&gt;500&lt;/span&gt;))))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Finally, it turned out that &lt;code&gt;defproject&lt;/code&gt; is not always a first form in &lt;code&gt;project.clj&lt;/code&gt;. Some projects, like &lt;a href="https://github.com/robert-stuttaford/bridge"&gt;bridge&lt;/a&gt;, only contain a placeholder &lt;code&gt;project.clj&lt;/code&gt; with no forms; others, like &lt;a href="https://github.com/ztellman/aleph"&gt;aleph&lt;/a&gt;, first define some constants, and then refer to them in a &lt;code&gt;defproject&lt;/code&gt; form. If those constants contain parts of the dependencies list, then those dependencies won’t be processed correctly. Fortunately, not a lot of projects do this, so it doesn’t skew the results much.&lt;/p&gt;&lt;p&gt;Anyway, the end result of the acquisition phase is a sequence of maps describing project definitions. They look like this:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;{&lt;span class="hljs-symbol"&gt;:name&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;clojure-koans&amp;quot;&lt;/span&gt;&lt;span class="hljs-punctuation"&gt;,&lt;/span&gt;
 &lt;span class="hljs-symbol"&gt;:full-name&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;functional-koans/clojure-koans&amp;quot;&lt;/span&gt;&lt;span class="hljs-punctuation"&gt;,&lt;/span&gt;
 &lt;span class="hljs-symbol"&gt;:deps-type&lt;/span&gt; &lt;span class="hljs-symbol"&gt;:leiningen&lt;/span&gt;&lt;span class="hljs-punctuation"&gt;,&lt;/span&gt;
 &lt;span class="hljs-symbol"&gt;:page&lt;/span&gt; &lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-punctuation"&gt;,&lt;/span&gt;
 &lt;span class="hljs-symbol"&gt;:deps&lt;/span&gt; {org.clojure/clojure #:mvn{&lt;span class="hljs-symbol"&gt;:version&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;1.10.0&amp;quot;&lt;/span&gt;}&lt;span class="hljs-punctuation"&gt;,&lt;/span&gt;
        koan-engine #:mvn{&lt;span class="hljs-symbol"&gt;:version&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;0.2.5&amp;quot;&lt;/span&gt;}}}&lt;span class="hljs-punctuation"&gt;,&lt;/span&gt;
 &lt;span class="hljs-symbol"&gt;:profile-deps&lt;/span&gt; {&lt;span class="hljs-symbol"&gt;:dev&lt;/span&gt; {lein-koan #:mvn{&lt;span class="hljs-symbol"&gt;:version&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;0.1.5&amp;quot;&lt;/span&gt;}}}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Homogeneity is important: every dependency description has been converted to the cli-tools format, even if it comes from a &lt;code&gt;project.clj&lt;/code&gt;.&lt;/p&gt;&lt;h2 id="data-analysis"&gt;Data analysis&lt;/h2&gt;&lt;p&gt;I’ve long been searching for a way to do exploratory programming in Clojure without turning the code to a tangled mess, portable only along with my computer.&lt;/p&gt;&lt;p&gt;Exploratory (or research) programming is very different from “normal” programming. In the latter, most of the time you typically focus on a coherent project – a program or a library. In contrast, in the former, you spend a lot of time in the REPL, trying all sorts of different things and &lt;code&gt;def&lt;/code&gt;ing new values derived from already computed ones.&lt;/p&gt;&lt;p&gt;This is very convenient, but it’s extremely easy to get carried away in the REPL and get lost in a sea of &lt;code&gt;def&lt;/code&gt;s. If you want to redo your computations from scratch, just about your only option is to take your REPL transcript and re-evaluate the expressions one by one, in the correct order. Cleaning up the code (e.g. deglobalizing) as you go is very difficult.&lt;/p&gt;&lt;p&gt;I’ve found an answer: &lt;a href="https://plumatic.github.io/prismatics-graph-at-strange-loop"&gt;Plumatic Graph&lt;/a&gt;, part of the &lt;a href="https://github.com/plumatic/plumbing"&gt;plumbing&lt;/a&gt; library. There are a plethora of uses for it: for example, at &lt;a href="https://iamfy.co"&gt;Fy&lt;/a&gt;, my current workplace, we’re using it to define our test fixtures. But as it turns out, it makes exploratory programming enjoyable.&lt;/p&gt;&lt;p&gt;The bulk of code in &lt;a href="https://github.com/nathell/versions/blob/master/src/clj/versions/analyze.clj#L41"&gt;&lt;code&gt;versions.analyze&lt;/code&gt;&lt;/a&gt; consists of a big definition of a graph, with nodes representing computations – things that I’d normally have &lt;code&gt;def&lt;/code&gt;’d in a REPL. Consequently, most of these definitions are short and to the point. I also gave the nodes verbose, descriptive, explicit names. Name and conquer. &lt;code&gt;raw-repos&lt;/code&gt; is the output from data acquisition, &lt;code&gt;repos&lt;/code&gt; is an all-important node containing those &lt;code&gt;raw-repos&lt;/code&gt; that were successfully parsed, and most other things depend on it.&lt;/p&gt;&lt;p&gt;It also doesn’t obstruct much the normal REPL research flow. My normal workflow with REPL and Graph is something along the lines of:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;span&gt;&lt;code&gt;(def result (main))&lt;/code&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;evaluate something using inputs from &lt;code&gt;result&lt;/code&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;nah, it leads nowhere&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;evaluate something else&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;hey, that’s interesting!&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;add a new node to the graph definition&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;GOTO 1&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Thanks to Graph’s lazy compiler, I can re-evaluate anything at need and have it evaluate only the things needed, and nothing else. Also, because the graph is explicit, it’s fairly easy to &lt;a href="https://github.com/RedBrainLabs/graph-fnk-viz"&gt;visualize it&lt;/a&gt;. (Click the image to open it in full-size in another tab.)&lt;/p&gt;&lt;p&gt;&lt;a href="/img/blog/computation-graph.png" target="_blank"&gt;&lt;img src="/img/blog/computation-graph.png"&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Because it’s lazy, it doesn’t hurt to put extra things in there just in case, even when you’re not going to report them. For example, I was curious what things besides a version number people put in dependencies. &lt;code&gt;:exclusions&lt;/code&gt;, for sure, but what else? This is the &lt;code&gt;:what-other-things-besides-versions&lt;/code&gt; node.&lt;/p&gt;&lt;p&gt;Imagine my surprise when I found &lt;code&gt;:exlusions&lt;/code&gt; (&lt;em&gt;sic&lt;/em&gt;) in there, which turned out to be a typo in shadow-cljs’ &lt;code&gt;project.clj&lt;/code&gt;! I submitted &lt;a href="https://github.com/thheller/shadow-cljs/pull/699"&gt;a PR&lt;/a&gt;, and Thomas Heller merged it a few days after.&lt;/p&gt;&lt;p&gt;My only gripe with Graph is that it runs somewhat contrary to the current trends in the Clojure community: for example, it doesn’t support namespaced keywords (although there’s an &lt;a href="https://github.com/plumatic/plumbing/issues/126"&gt;open ticket&lt;/a&gt; for that). But on the whole, I’m sold. I’ll definitely be using it in the next piece of research in Clojure, and I’m on a lookout for something similar in pure R. If you know something, do tell me!&lt;/p&gt;&lt;h2 id="some-words-on-plotting"&gt;Some words on plotting&lt;/h2&gt;&lt;p&gt;The plot from previous post has been generated in pure R, using &lt;a href="https://ggplot2.tidyverse.org"&gt;ggplot2&lt;/a&gt; (an extremely versatile API). Clojure generates a CSV with munged data, and then R reads that CSV as a data frame and generates the plot in a few lines.&lt;/p&gt;&lt;p&gt;I’ve briefly played around with &lt;a href="https://github.com/scicloj/clojisr"&gt;clojisr&lt;/a&gt;, a bridge between Clojure and R. It was an enlightening experiment, and it would let me avoid the intermediate CSV, but I decided to ditch it for a few reasons:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span&gt;It pulls in quite a few dependencies (I wanted to keep them down to a minimum), and requires some previous setup on the R side.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;I’d much rather write my R as R, since I’m comfortable with it, rather than spend time wondering how it maps to Clojure. This is similar to the SQL story: these days I prefer &lt;a href="https://www.hugsql.org"&gt;HugSQL&lt;/a&gt; over &lt;a href="https://github.com/korma/Korma"&gt;Korma&lt;/a&gt;, unless I have good reasons to choose otherwise.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;clojisr opens up a child R process just by &lt;code&gt;require&lt;/code&gt;ing a namespace. I’m not a fan of that.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;But it’s definitely very promising! I applaud the effort and I’ll keep a close eye on it.&lt;/p&gt;&lt;h2 id="key-takeaways"&gt;Key takeaways&lt;/h2&gt;&lt;ul&gt;&lt;li&gt;&lt;span&gt;Skyscraper makes data acquisition bearable, if not fun.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;Plumatic Graph makes writing research code in Clojure fun.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;ggplot makes plotting data fun.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;Clojure makes programming fun. (But you knew that already.)&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;</content>
  </entry>
  <entry>
    <id>tag:blog.danieljanus.pl,2020-05-02:post:clojure-dependency</id>
    <title>Clojure as a dependency</title>
    <link href="http://blog.danieljanus.pl/clojure-dependency/"/>
    <updated>2020-05-02T00:00:00Z</updated>
    <content type="html">&lt;div&gt;&lt;p&gt;I have a shameful confession to make: I have long neglected an open-source library that I maintain, &lt;a href="https://github.com/nathell/clj-tagsoup"&gt;clj-tagsoup&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;This would have been less of an issue, but this is my second-most-starred project on GitHub. Granted, I don’t feel a need for it anymore, but apparently people do. I wish I had spent some time reviewing and merging the incoming PRs.&lt;/p&gt;&lt;p&gt;Anyway, I’ve recently been prompted to revive it, and I’m preparing a new release. While on it, I’ve been updating dependencies to their latest versions, and upon seeing a dependency on &lt;code&gt;[org.clojure/clojure "1.2.0"]&lt;/code&gt; in &lt;code&gt;project.clj&lt;/code&gt; (yes, it’s been neglected for that long), I started wondering: which Clojure to depend on? Actually, should Clojure itself be a dependency at all?&lt;/p&gt;&lt;p&gt;I’ve googled around for best practices, but with no conclusive answer. So I set out to do some research.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;TLDR:&lt;/strong&gt; with Leiningen, add it with &lt;code&gt;:scope "provided"&lt;/code&gt;; with cli-tools, you don’t have to, unless you want to be explicit.&lt;/p&gt;&lt;h2 id="is-it-possible-for-a-clojure-project-to-declare-no-dependency-on-clojure-at-all?"&gt;Is it possible for a Clojure project to declare no dependency on Clojure at all?&lt;/h2&gt;&lt;p&gt;Quite possible, as it turns out. But the details depend on the build tool.&lt;/p&gt;&lt;p&gt;Obviously, this only makes sense for libraries. Or, more broadly, for projects that are not meant to be used standalone, but rather included in other projects (which will have a Clojure dependency of their own).&lt;/p&gt;&lt;h3 id="leiningen"&gt;Leiningen&lt;/h3&gt;&lt;p&gt;If you try to create a Leiningen project that has no dependencies:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;(&lt;span class="hljs-name"&gt;defproject&lt;/span&gt; foo &lt;span class="hljs-string"&gt;&amp;quot;0.1.0&amp;quot;&lt;/span&gt;
  &lt;span class="hljs-symbol"&gt;:dependencies&lt;/span&gt; [])
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;then Leiningen (as of version 2.9.3, but I’d guess older versions behave similarly) won’t allow you to launch a REPL:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;$ lein repl
Error: Could not find or load main class clojure.main
Caused by: java.lang.ClassNotFoundException: clojure.main
Subprocess failed (exit code: 1)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;But all is not lost: &lt;code&gt;lein jar&lt;/code&gt; works just fine (as long as you don’t AOT-compile any namespaces), as does &lt;code&gt;lein install&lt;/code&gt;. The resulting library will happily function as a dependency of other projects.&lt;/p&gt;&lt;p&gt;The upside of depending on no particular Clojure version is that you don’t impose it on your consumers. If a library depends on Clojure 1.9.0, but a project that uses it depends on Clojure 1.10.1, then Leiningen will fetch 1.9.0’s &lt;code&gt;pom.xml&lt;/code&gt; (it’s smart enough to figure out that the jar itself won’t be needed, as the conflict will always be resolved in favour of the direct dependency), and &lt;code&gt;lein deps :tree&lt;/code&gt; will report “possibly confusing dependencies”.&lt;/p&gt;&lt;p&gt;It’s not very useful to have a library that you can’t launch a REPL against, though. So what some people do is declare a dependency on Clojure not in the main &lt;code&gt;:dependencies&lt;/code&gt;, but in a profile.&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;(&lt;span class="hljs-name"&gt;defproject&lt;/span&gt; foo &lt;span class="hljs-string"&gt;&amp;quot;0.1.0&amp;quot;&lt;/span&gt;
  &lt;span class="hljs-symbol"&gt;:dependencies&lt;/span&gt; []
  &lt;span class="hljs-symbol"&gt;:profiles&lt;/span&gt; {&lt;span class="hljs-symbol"&gt;:dev&lt;/span&gt; {&lt;span class="hljs-symbol"&gt;:dependencies&lt;/span&gt; [[org.clojure/clojure &lt;span class="hljs-string"&gt;&amp;quot;1.10.1&amp;quot;&lt;/span&gt;]]}})
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This avoids conflicts and brings back the possibility to launch a REPL. Sometimes, people create multiple profiles for different Clojure versions; &lt;a href="https://github.com/technomancy/leiningen/blob/master/doc/PROFILES.md"&gt;Leiningen’s documentation&lt;/a&gt; mentions this possibility.&lt;/p&gt;&lt;p&gt;Unfortunately, with this approach it’s still not possible to AOT-compile things or create uberjars with Leiningen. (Putting Clojure in the &lt;code&gt;:provided&lt;/code&gt; profile causes building the uberjar to succeed, but the resulting &lt;code&gt;-standalone&lt;/code&gt; jar doesn’t actually contain Clojure).&lt;/p&gt;&lt;p&gt;Another option is to add Clojure to the main &lt;code&gt;:dependencies&lt;/code&gt;, but with &lt;code&gt;:scope "provided"&lt;/code&gt;. Per the &lt;a href="http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html"&gt;Maven documentation&lt;/a&gt;, this means:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;This is much like &lt;code&gt;compile&lt;/code&gt;, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope &lt;code&gt;provided&lt;/code&gt; because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The key are the last words: “not transitive.” If project A depends on a library B that declares a “provided” dependency C, then C won’t be automatically put in A’s dependencies, and A is expected to explicitly declare its own C.&lt;/p&gt;&lt;p&gt;This means that it’s adequate for both libraries and standalone projects when it comes to declaring a Clojure dependency. It doesn’t break anything, doesn’t cause any ephemeral conflicts, and can be combined with the profiles approach when multiple configurations are called for.&lt;/p&gt;&lt;h3 id="cli-tools"&gt;cli-tools&lt;/h3&gt;&lt;p&gt;cli-tools will accept a &lt;code&gt;deps.edn&lt;/code&gt; as simple as &lt;code&gt;{}&lt;/code&gt;. Even passing &lt;code&gt;-Srepro&lt;/code&gt; to &lt;code&gt;clojure&lt;/code&gt; or &lt;code&gt;clj&lt;/code&gt; (which excludes the Clojure dependency that you probably have in your &lt;code&gt;~/.clojure/deps.edn&lt;/code&gt;) doesn’t break anything: cli-tools will just use 1.10.1 (at least as of version 1.10.1.536).&lt;/p&gt;&lt;p&gt;With cli-tools, as a library author you probably don’t have to declare a Clojure dependency at all. But things are less uniform in this land than they are in Leiningen (for example, there are quite a few uberjarrers to choose from), so it’s reasonable to check with your tooling first.&lt;/p&gt;&lt;h3 id="boot"&gt;Boot&lt;/h3&gt;&lt;p&gt;I’m no longer a Boot user, so I can’t tell. But from what I know, it uses Aether just like Leiningen and Maven do, so I’d wager a guess the same caveats apply as for Leiningen. Haven’t checked, though.&lt;/p&gt;&lt;h2 id="so-what-do-the-existing-projects-do?"&gt;So what do the existing projects do?&lt;/h2&gt;&lt;p&gt;I figured it would be a fun piece of research to examine how the popular projects depend (or don’t depend) on Clojure. I queried GitHub’s API for the 1000 most starred Clojure projects, fetched and parsed their &lt;code&gt;project.clj&lt;/code&gt;s and/or &lt;code&gt;deps.edn&lt;/code&gt;s, and tallied things up.&lt;/p&gt;&lt;p&gt;I’ll write a separate “making of” post, because it turned out to be an even more fun weekend project than I had anticipated. But for now, let me share the conclusions.&lt;/p&gt;&lt;p&gt;I ended up with 968 project definition files that I was able to successfully parse: 140 &lt;code&gt;deps.edn&lt;/code&gt;s and 828 &lt;code&gt;project.clj&lt;/code&gt;s. Here’s a breakdown of Clojure version declared as a “main” dependency (i.e., not in a profile or alias):&lt;/p&gt;&lt;img src="/img/blog/clojure-versions.png"&gt;
&lt;p&gt;N/A means that there’s no dependency on Clojure declared, and “other” is an umbrella for the zoo of alphas, betas and snapshots.&lt;/p&gt;&lt;p&gt;As expected, not depending on Clojure is comparatively more popular in the cli-tools land: almost half (48.6%) of cli-tools projects don’t declare a Clojure dependency, versus 21.5% (174 projects) for Leiningen.&lt;/p&gt;&lt;p&gt;That Leiningen number still seemed quite high to me, so I dug a little deeper. Out of those 174 projects, 100 have Clojure somewhere in their &lt;code&gt;:profiles&lt;/code&gt;. The remaining 74 are somewhat of outliers:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span&gt;some, like Ring or Pedestal, are umbrella projects composed of sub-projects (with the &lt;code&gt;lein-sub&lt;/code&gt; plugin) that have actual dependencies themselves;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;some, like Klipse or Reagent, are essentially ClojureScript-only;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;some, like Overtone, use the &lt;code&gt;lein-tools-deps&lt;/code&gt; plugin to store their dependencies in &lt;code&gt;deps.edn&lt;/code&gt; while using Leiningen for other tasks.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Finally, the popularity of &lt;code&gt;:scope "provided"&lt;/code&gt; is much lower. Only 68 Leiningen projects specify it (8.9% of those that declare any dependencies), and only two &lt;code&gt;deps.edn&lt;/code&gt; files do so (re-frame and fulcro – note that re-frame actually has both a &lt;code&gt;project.clj&lt;/code&gt; and a &lt;code&gt;deps.edn&lt;/code&gt;).&lt;/p&gt;&lt;/div&gt;</content>
  </entry>
  <entry>
    <id>tag:blog.danieljanus.pl,2020-02-10:post:cond-indentation</id>
    <title>Indenting cond forms</title>
    <link href="http://blog.danieljanus.pl/cond-indentation/"/>
    <updated>2020-02-10T00:00:00Z</updated>
    <content type="html">&lt;div&gt;&lt;p&gt;Indentation matters when reading Clojure code. It is the primary visual cue that helps the reader discern the code structure. Most Clojure code seen in the wild conforms to either the &lt;a href="https://github.com/bbatsov/clojure-style-guide#source-code-layout-organization"&gt;community style guide&lt;/a&gt; or the proposed &lt;a href="https://tonsky.me/blog/clojurefmt/"&gt;simplified rules&lt;/a&gt;; the existing editors make it easy to reformat code to match them.&lt;/p&gt;&lt;p&gt;I find both these rulesets to be helpful when reading code. But there’s one corner-case that’s been irking me: &lt;code&gt;cond&lt;/code&gt; forms.&lt;/p&gt;&lt;p&gt;&lt;code&gt;cond&lt;/code&gt; takes an even number of arguments: alternating test-expression pairs. They are commonly put next to each other, two forms per line.&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;(&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;cond&lt;/span&gt;&lt;/span&gt;
  test expr-1
  another-test expr-2
  &lt;span class="hljs-symbol"&gt;:else&lt;/span&gt; expr-3)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Sometimes, people align the expressions under one another, in a tabular fashion:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;(&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;cond&lt;/span&gt;&lt;/span&gt;
  test         expr-1
  another-test expr-2
  &lt;span class="hljs-symbol"&gt;:else&lt;/span&gt;        expr-3)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;But things get out of hand when either &lt;code&gt;tests&lt;/code&gt; or &lt;code&gt;exprs&lt;/code&gt; get longer and call for multiple lines themselves. There are several options here, all of them less than ideal.&lt;/p&gt;&lt;h3 id="tests-and-expressions-next-to-each-other"&gt;Tests and expressions next to each other&lt;/h3&gt;&lt;p&gt;In other words, keep the above rule. Because we’ll have multiple lines in a form, this tends to make the resulting code axe-shaped:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;(&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;cond&lt;/span&gt;&lt;/span&gt;
  (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;=&lt;/span&gt;&lt;/span&gt; (&lt;span class="hljs-name"&gt;some-function&lt;/span&gt; something) expected-value) (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;do&lt;/span&gt;&lt;/span&gt;
                                                 (&lt;span class="hljs-name"&gt;do-this&lt;/span&gt;)
                                                 (&lt;span class="hljs-name"&gt;and-also-do-that&lt;/span&gt;))
  (&lt;span class="hljs-name"&gt;another-predicate&lt;/span&gt; something-else) (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;try&lt;/span&gt;&lt;/span&gt;
                                       (&lt;span class="hljs-name"&gt;do-another-thing&lt;/span&gt;)
                                       (&lt;span class="hljs-name"&gt;catch&lt;/span&gt; Exception _
                                         (&lt;span class="hljs-name"&gt;println&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;Whoops!&amp;quot;&lt;/span&gt;))))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This yields code that is indented abnormally far to the right, forcing the reader’s eyeballs to move in two dimensions – even more so if the tabular feel is desired. If &lt;em&gt;both&lt;/em&gt; the test and the expression is multi-lined, it just looks plain weird.&lt;/p&gt;&lt;h3 id="stack-all-forms-vertically,-no-extra-spacing"&gt;Stack all forms vertically, no extra spacing&lt;/h3&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;(&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;cond&lt;/span&gt;&lt;/span&gt;
  (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;=&lt;/span&gt;&lt;/span&gt; (&lt;span class="hljs-name"&gt;some-function&lt;/span&gt; something) expected-value)
  (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;do&lt;/span&gt;&lt;/span&gt;
    (&lt;span class="hljs-name"&gt;do-this&lt;/span&gt;)
    (&lt;span class="hljs-name"&gt;and-also-do-that&lt;/span&gt;))
  (&lt;span class="hljs-name"&gt;another-predicate&lt;/span&gt; something-else)
  (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;try&lt;/span&gt;&lt;/span&gt;
    (&lt;span class="hljs-name"&gt;do-another-thing&lt;/span&gt;)
    (&lt;span class="hljs-name"&gt;catch&lt;/span&gt; Exception _
      (&lt;span class="hljs-name"&gt;println&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;Whoops!&amp;quot;&lt;/span&gt;))))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This gets rid of the long lines, but introduces another problem: it’s hard to tell at a glance&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span&gt;where a given test or expression starts or ends;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;which tests are paired with which expression;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;whether a given line corresponds to a test or an expression, and which one.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h3 id="stack-all-forms-vertically,-blank-lines-between-test/expr-pairs"&gt;Stack all forms vertically, blank lines between test/expr pairs&lt;/h3&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;(&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;cond&lt;/span&gt;&lt;/span&gt;
  (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;=&lt;/span&gt;&lt;/span&gt; (&lt;span class="hljs-name"&gt;some-function&lt;/span&gt; something) expected-value)
  (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;do&lt;/span&gt;&lt;/span&gt;
    (&lt;span class="hljs-name"&gt;do-this&lt;/span&gt;)
    (&lt;span class="hljs-name"&gt;and-also-do-that&lt;/span&gt;))

  (&lt;span class="hljs-name"&gt;another-predicate&lt;/span&gt; something-else)
  (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;try&lt;/span&gt;&lt;/span&gt;
    (&lt;span class="hljs-name"&gt;do-another-thing&lt;/span&gt;)
    (&lt;span class="hljs-name"&gt;catch&lt;/span&gt; Exception _
      (&lt;span class="hljs-name"&gt;println&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;Whoops!&amp;quot;&lt;/span&gt;))))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The Style Guide &lt;a href="https://github.com/bbatsov/clojure-style-guide#short-forms-in-cond"&gt;says&lt;/a&gt; that this is an “ok-ish” thing to do.&lt;/p&gt;&lt;p&gt;But with the added blank lines, logical structure of the code is much more apparent. However, it breaks another assumption that I make when reading the code: &lt;em&gt;functions contain no blank lines.&lt;/em&gt; The Style Guide even &lt;a href="https://github.com/bbatsov/clojure-style-guide#no-blank-lines-within-def-forms"&gt;mentions it&lt;/a&gt;, saying that &lt;code&gt;cond&lt;/code&gt; forms are an acceptable exception.&lt;/p&gt;&lt;p&gt;It is now harder to tell at a glance where the enclosing function starts or ends. And once this assumption is broken once, the brain expects it to be broken again, causing reading disruption across the entire file.&lt;/p&gt;&lt;h3 id="forms-one-under-another,-extra-indentation-for-expressions-only"&gt;Forms one under another, extra indentation for expressions only&lt;/h3&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;(&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;cond&lt;/span&gt;&lt;/span&gt;
  (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;=&lt;/span&gt;&lt;/span&gt; (&lt;span class="hljs-name"&gt;some-function&lt;/span&gt; something) expected-value)
    (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;do&lt;/span&gt;&lt;/span&gt;
      (&lt;span class="hljs-name"&gt;do-this&lt;/span&gt;)
      (&lt;span class="hljs-name"&gt;and-also-do-that&lt;/span&gt;))
  (&lt;span class="hljs-name"&gt;another-predicate&lt;/span&gt; something-else)
    (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;try&lt;/span&gt;&lt;/span&gt;
      (&lt;span class="hljs-name"&gt;do-another-thing&lt;/span&gt;)
      (&lt;span class="hljs-name"&gt;catch&lt;/span&gt; Exception _
        (&lt;span class="hljs-name"&gt;println&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;Whoops!&amp;quot;&lt;/span&gt;))))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I resorted to this several times. The lines are not too long; the visual cues are there; it’s obvious what is the condition, what is the test, and what goes with what.&lt;/p&gt;&lt;p&gt;Except… it’s against the rules. List items stacked vertically should be aligned one under the other. I have to actively fight my Emacs to enforce this formatting, and it will be lost next time I press &lt;code&gt;C-M-q&lt;/code&gt; on this form. No good.&lt;/p&gt;&lt;h3 id="forms-one-under-another,-expressions-prefixed-by-#-=&amp;gt;"&gt;Forms one under another, expressions prefixed by &lt;code&gt;#_=&gt;&lt;/code&gt;&lt;/h3&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;(&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;cond&lt;/span&gt;&lt;/span&gt;
  (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;=&lt;/span&gt;&lt;/span&gt; (&lt;span class="hljs-name"&gt;some-function&lt;/span&gt; something) expected-value)
  #_=&amp;gt; (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;do&lt;/span&gt;&lt;/span&gt;
         (&lt;span class="hljs-name"&gt;do-this&lt;/span&gt;)
         (&lt;span class="hljs-name"&gt;and-also-do-that&lt;/span&gt;))
  (&lt;span class="hljs-name"&gt;another-predicate&lt;/span&gt; something-else)
  #_=&amp;gt; (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;try&lt;/span&gt;&lt;/span&gt;
         (&lt;span class="hljs-name"&gt;do-another-thing&lt;/span&gt;)
         (&lt;span class="hljs-name"&gt;catch&lt;/span&gt; Exception _
           (&lt;span class="hljs-name"&gt;println&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;Whoops!&amp;quot;&lt;/span&gt;))))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This one is my own invention: I haven’t seen it anywhere else. But I think it manages to avoid most problems.&lt;/p&gt;&lt;p&gt;&lt;code&gt;#_&lt;/code&gt; is a reader macro that causes the next form to be elided and not seen by the compiler. &lt;code&gt;=&gt;&lt;/code&gt; is a valid form. Thus, &lt;code&gt;#_=&gt;&lt;/code&gt; is effectively whitespace as far as the compiler is concerned, and the indentation rules treat it as yet another symbol (although it technically isn’t one). No tooling is broken, no assumptions are broken, and the &lt;code&gt;#_=&gt;&lt;/code&gt; tends to be syntax-highlighted unintrusively so it doesn’t stand out. I tend to read it aloud as “then.”&lt;/p&gt;&lt;h3 id="meanwhile,-in-another-galaxy"&gt;Meanwhile, in another galaxy&lt;/h3&gt;&lt;p&gt;Other Lisps (Scheme and CL) wrap each test/expression pair in an extra pair of parens, thereby avoiding the blending of conditions and expressions when indented one under the other. But I’m still happy Clojure went with fewer parens. As I say, this is a corner case where additional pair of parens would somewhat help, but most of the time I find them less aesthetic and a visual clutter.&lt;/p&gt;&lt;/div&gt;</content>
  </entry>
  <entry>
    <id>tag:blog.danieljanus.pl,2014-05-20:post:you-already-use-lisp-syntax</id>
    <title>You already use Lisp syntax</title>
    <link href="http://blog.danieljanus.pl/you-already-use-lisp-syntax/"/>
    <updated>2014-05-20T00:00:00Z</updated>
    <content type="html">&lt;div&gt;&lt;p&gt;&lt;strong&gt;Unix Developer:&lt;/strong&gt; I’m not going to touch Lisp. It’s horrible!&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; Why so?&lt;/p&gt;&lt;p&gt;&lt;strong&gt;UD:&lt;/strong&gt; The syntax! This illegible prefix-RPN syntax that nobody else uses. And just look at all these parens!&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; Well, many people find it perfectly legible, although most agree that it takes some time to get accustomed to. But I think you’re mistaken. Lots of people are using Lisp syntax on a daily basis…&lt;/p&gt;&lt;p&gt;&lt;strong&gt;UD:&lt;/strong&gt; I happen to know no one doing this.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; …without actually realizing this. In fact, I think &lt;em&gt;you&lt;/em&gt; yourself are using it.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;UD:&lt;/strong&gt; Wait, &lt;em&gt;what&lt;/em&gt;?!&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; And the particular variant of Lisp syntax you’re using is called Bourne shell.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;UD:&lt;/strong&gt; Now I don’t understand. What on earth does the shell have to do with Lisp?&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; Just look: in the shell, you put the name of the program first, followed by the arguments, separated by spaces. In Lisp it’s exactly the same, except that you put an opening paren at the beginning and a closing paren at the end.&lt;/p&gt;&lt;p&gt;Shell: &lt;code&gt;run-something arg1 arg2 arg3&lt;/code&gt;&lt;/p&gt;&lt;p&gt;Lisp: &lt;code&gt;(run-something arg1 arg2 arg3)&lt;/code&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;UD:&lt;/strong&gt; I still don’t get the analogy.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; Then you need a mechanism for expression composition — putting the output of one expression as an input to another. In Lisp, you just nest the lists. And in the shell?&lt;/p&gt;&lt;p&gt;&lt;strong&gt;UD:&lt;/strong&gt; Backticks.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; That’s right. Or &lt;code&gt;$()&lt;/code&gt;, which has the advantage of being more easily nestable. Let’s try arithmetic. How do you do arithmetic in the shell?&lt;/p&gt;&lt;p&gt;&lt;strong&gt;UD:&lt;/strong&gt; &lt;code&gt;expr&lt;/code&gt;. Or the Bash builtin &lt;code&gt;let&lt;/code&gt;. For example,&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs bash"&gt;$ &lt;span class="hljs-built_in"&gt;let&lt;/span&gt; x=&lt;span class="hljs-string"&gt;&amp;#x27;2*((10+4)/7)&amp;#x27;&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;echo&lt;/span&gt; &lt;span class="hljs-variable"&gt;$x&lt;/span&gt;
4
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; Now wouldn’t it be in line with the spirit of Unix — to have programs do just one thing — if we had one program to do addition, and another to do subtraction, and yet another to do multiplication and division?&lt;/p&gt;&lt;p&gt;It’s trivial to write it in C:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs c"&gt;&lt;span class="hljs-meta"&gt;#&lt;span class="hljs-keyword"&gt;include&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="hljs-meta"&gt;#&lt;span class="hljs-keyword"&gt;include&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="hljs-meta"&gt;#&lt;span class="hljs-keyword"&gt;include&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class="hljs-type"&gt;int&lt;/span&gt; &lt;span class="hljs-title function_"&gt;main&lt;/span&gt;&lt;span class="hljs-params"&gt;(&lt;span class="hljs-type"&gt;int&lt;/span&gt; argc, &lt;span class="hljs-type"&gt;char&lt;/span&gt; **argv)&lt;/span&gt; {
  &lt;span class="hljs-type"&gt;int&lt;/span&gt; mode = &lt;span class="hljs-number"&gt;-1&lt;/span&gt;, cnt = argc - &lt;span class="hljs-number"&gt;1&lt;/span&gt;, val, i;
  &lt;span class="hljs-type"&gt;char&lt;/span&gt; **args = argv + &lt;span class="hljs-number"&gt;1&lt;/span&gt;;
  &lt;span class="hljs-keyword"&gt;switch&lt;/span&gt; (argv[&lt;span class="hljs-number"&gt;0&lt;/span&gt;][&lt;span class="hljs-built_in"&gt;strlen&lt;/span&gt;(argv[&lt;span class="hljs-number"&gt;0&lt;/span&gt;]) - &lt;span class="hljs-number"&gt;1&lt;/span&gt;]) {
    &lt;span class="hljs-keyword"&gt;case&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;#x27;+&amp;#x27;&lt;/span&gt;: mode = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;break&lt;/span&gt;;
    &lt;span class="hljs-keyword"&gt;case&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;#x27;-&amp;#x27;&lt;/span&gt;: mode = &lt;span class="hljs-number"&gt;1&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;break&lt;/span&gt;;
    &lt;span class="hljs-keyword"&gt;case&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;#x27;x&amp;#x27;&lt;/span&gt;: mode = &lt;span class="hljs-number"&gt;2&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;break&lt;/span&gt;;
    &lt;span class="hljs-keyword"&gt;case&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;#x27;d&amp;#x27;&lt;/span&gt;: mode = &lt;span class="hljs-number"&gt;3&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;break&lt;/span&gt;;
  }
  &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; (mode == &lt;span class="hljs-number"&gt;-1&lt;/span&gt;) {
    &lt;span class="hljs-built_in"&gt;fprintf&lt;/span&gt;(&lt;span class="hljs-built_in"&gt;stderr&lt;/span&gt;, &lt;span class="hljs-string"&gt;&amp;quot;invalid math operation\n&amp;quot;&lt;/span&gt;);
    &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; &lt;span class="hljs-number"&gt;1&lt;/span&gt;;
  }
  &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; ((mode == &lt;span class="hljs-number"&gt;1&lt;/span&gt; || mode == &lt;span class="hljs-number"&gt;3&lt;/span&gt;) &amp;amp;&amp;amp; !cnt) {
    &lt;span class="hljs-built_in"&gt;fprintf&lt;/span&gt;(&lt;span class="hljs-built_in"&gt;stderr&lt;/span&gt;, &lt;span class="hljs-string"&gt;&amp;quot;%s requires at least one arg\n&amp;quot;&lt;/span&gt;, argv[&lt;span class="hljs-number"&gt;0&lt;/span&gt;]);
    &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; &lt;span class="hljs-number"&gt;1&lt;/span&gt;;
  }
  &lt;span class="hljs-keyword"&gt;switch&lt;/span&gt; (mode) {
    &lt;span class="hljs-keyword"&gt;case&lt;/span&gt; &lt;span class="hljs-number"&gt;0&lt;/span&gt;: val = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;break&lt;/span&gt;;
    &lt;span class="hljs-keyword"&gt;case&lt;/span&gt; &lt;span class="hljs-number"&gt;2&lt;/span&gt;: val = &lt;span class="hljs-number"&gt;1&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;break&lt;/span&gt;;
    &lt;span class="hljs-keyword"&gt;default&lt;/span&gt;: val = atoi(*args++); cnt--; &lt;span class="hljs-keyword"&gt;break&lt;/span&gt;;
  }
  &lt;span class="hljs-keyword"&gt;while&lt;/span&gt; (cnt--) {
    &lt;span class="hljs-keyword"&gt;switch&lt;/span&gt; (mode) {
      &lt;span class="hljs-keyword"&gt;case&lt;/span&gt; &lt;span class="hljs-number"&gt;0&lt;/span&gt;: val += atoi(*args++); &lt;span class="hljs-keyword"&gt;break&lt;/span&gt;;
      &lt;span class="hljs-keyword"&gt;case&lt;/span&gt; &lt;span class="hljs-number"&gt;1&lt;/span&gt;: val -= atoi(*args++); &lt;span class="hljs-keyword"&gt;break&lt;/span&gt;;
      &lt;span class="hljs-keyword"&gt;case&lt;/span&gt; &lt;span class="hljs-number"&gt;2&lt;/span&gt;: val *= atoi(*args++); &lt;span class="hljs-keyword"&gt;break&lt;/span&gt;;
      &lt;span class="hljs-keyword"&gt;case&lt;/span&gt; &lt;span class="hljs-number"&gt;3&lt;/span&gt;: val /= atoi(*args++); &lt;span class="hljs-keyword"&gt;break&lt;/span&gt;;
    }
  }
  &lt;span class="hljs-built_in"&gt;printf&lt;/span&gt;(&lt;span class="hljs-string"&gt;&amp;quot;%d\n&amp;quot;&lt;/span&gt;, val);
  &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; &lt;span class="hljs-number"&gt;0&lt;/span&gt;;
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This dispatches on the last character of its name, so it can be symlinked to &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;d&lt;/code&gt; (I picked unusual names for multiplication and division to make them legal and avoid escaping).&lt;/p&gt;&lt;p&gt;Now behold:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs bash"&gt;$ x 2 $(d $(+ 10 4) 7)
4
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;UD:&lt;/strong&gt; Wow, this sure looks a lot like Lisp!&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; And yet it’s the shell. Our two basic rules — program-name-first and &lt;code&gt;$()&lt;/code&gt;-for-composition — allowed us to explicitly specify the order of evaluation, so there was no need to do any fancy parsing beyond what the shell already provides.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;UD:&lt;/strong&gt; So is the shell a Lisp?&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; Not really. The shell is &lt;a href="http://blog.codinghorror.com/new-programming-jargon/"&gt;stringly typed&lt;/a&gt;: a program takes textual parameters and produces textual output. To qualify as a Lisp, it would have to have a composite type: a list or a cons cell to build lists on top of. Then, you’d be able to represent code as this data structure, and write programs to transform code to other code.&lt;/p&gt;&lt;p&gt;But the Tao of Lisp lingers in the shell syntax.&lt;/p&gt;&lt;hr&gt;
&lt;p&gt;I know I’ve glossed over many details here, like the shell syntax for redirection, globbing, subprocesses, the fact that programs have standard input in addition to command-line arguments, pipes, etc. — all these make the analogy rather weak. But I think it’s an interesting way to teach Lisp syntax to people.&lt;/p&gt;&lt;/div&gt;</content>
  </entry>
  <entry>
    <id>tag:blog.danieljanus.pl,2011-12-09:post:combining-virtual-sequences</id>
    <title>Combining virtual sequences&lt;br&gt;or, Sequential Fun with Macros&lt;br&gt;or, How to Implement Clojure-Like Pseudo-Sequences with Poor Man’s Laziness in a Predominantly Imperative Language</title>
    <link href="http://blog.danieljanus.pl/combining-virtual-sequences/"/>
    <updated>2011-12-09T00:00:00Z</updated>
    <content type="html">&lt;div&gt;&lt;h2 id="sequences-and-iteration"&gt;Sequences and iteration&lt;/h2&gt;&lt;p&gt;There are a number of motivations for this post. One stems from my extensive exposure to Clojure over the past few years: this was, and still is, my primary programming language for everyday work. Soon, I realized that much of the power of Clojure comes from a &lt;em&gt;sequence&lt;/em&gt; abstraction being one of its central concepts, and a standard library that contains many sequence-manipulating functions. It turns out that by combining them it is possible to solve a wide range of problems in a concise, high-level way. In contrast, it pays to think in terms of whole sequences, rather than individual elements.&lt;/p&gt;&lt;p&gt;Another motivation comes from a classical piece of functional programming humour, [The Evolution of a Haskell Programmer][1]. If you don’t know it, go check it out: it consists of several Haskell implementations of factorial, starting out from a straightforward recursive definition, passing through absolutely hilarious versions involving category-theoretical concepts, and finally arriving at this simple version that is considered most idiomatic:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs haskell"&gt;&lt;span class="hljs-title"&gt;fac&lt;/span&gt; n = product [&lt;span class="hljs-number"&gt;1&lt;/span&gt;..n]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is very Clojure-like in that it involves a sequence (a list comprehension). In Clojure, this could be implemented as&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;(&lt;span class="hljs-keyword"&gt;defn&lt;/span&gt; &lt;span class="hljs-title"&gt;fac&lt;/span&gt; [n]
  (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;reduce&lt;/span&gt;&lt;/span&gt; * &lt;span class="hljs-number"&gt;1&lt;/span&gt; (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;range&lt;/span&gt;&lt;/span&gt; &lt;span class="hljs-number"&gt;1&lt;/span&gt; (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;inc&lt;/span&gt;&lt;/span&gt; n)))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now, I thought to myself, how would I write factorial in an imperative language? Say, Pascal?&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs pascal"&gt;&lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; &lt;span class="hljs-title"&gt;fac&lt;/span&gt;&lt;span class="hljs-params"&gt;(n : integer)&lt;/span&gt; :&lt;/span&gt; integer;
&lt;span class="hljs-keyword"&gt;var&lt;/span&gt;
  i, res : integer;
&lt;span class="hljs-keyword"&gt;begin&lt;/span&gt;
  res := &lt;span class="hljs-number"&gt;1&lt;/span&gt;;
  &lt;span class="hljs-keyword"&gt;for&lt;/span&gt; i := &lt;span class="hljs-number"&gt;1&lt;/span&gt; &lt;span class="hljs-keyword"&gt;to&lt;/span&gt; n &lt;span class="hljs-keyword"&gt;do&lt;/span&gt;
    res := res * i;
  fac := res;
&lt;span class="hljs-keyword"&gt;end&lt;/span&gt;;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is very different from the functional version that works with sequences. It is much more elaborate, introducing an explicit loop. On the other hand, it’s memory efficient: it’s clear that its memory requirements are O(1), whereas a naïve implementation of a sequence would need O(n) to construct it all in memory and then reduce it down to a single value.&lt;/p&gt;&lt;p&gt;Or is it really that different? Think of the changing values of &lt;code&gt;i&lt;/code&gt; in that loop. On first iteration it is 1, on second iteration it’s 2, and so on up to n. Therefore, one can really think of a &lt;code&gt;for&lt;/code&gt; loop as a sequence! I call it a “virtual” sequence, since it is not an actual data structure; it’s just a snippet of code.&lt;/p&gt;&lt;p&gt;To rephrase it as a definition: a virtual sequence is a snippet of code that (presumably repeatedly) &lt;em&gt;yields&lt;/em&gt; the member values.&lt;/p&gt;&lt;h2 id="let’s-write-some-code!"&gt;Let’s write some code!&lt;/h2&gt;&lt;p&gt;To illustrate it, throughout the remainder of this article I will be using Common Lisp, for the following reasons:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span&gt;It allows for imperative style, including GOTO-like statements. This will enable us to generate very low-level code.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;Thanks to macros, we will be able to obtain interesting transformations.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Okay, so let’s have a look at how to generate a one-element sequence. Simple enough:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defmacro&lt;/span&gt; vsingle (&lt;span class="hljs-name"&gt;x&lt;/span&gt;)
 `(yield ,x))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The name &lt;code&gt;VSINGLE&lt;/code&gt; stands for “Virtual sequence that just yields a SINGLE element”. (In general, I will try to define virtual sequences named and performing similarly to their Clojure counterparts here; whenever there is a name clash with an already existing CL function, the name will be prefixed with &lt;code&gt;V&lt;/code&gt;.) We will not concern ourselves with the actual definition of &lt;code&gt;YIELD&lt;/code&gt; at the moment; for debugging, we can define it just as printing the value to the standard output.&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defun&lt;/span&gt; yield (&lt;span class="hljs-name"&gt;x&lt;/span&gt;)
  (&lt;span class="hljs-name"&gt;format&lt;/span&gt; &lt;span class="hljs-literal"&gt;t&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;~A~%&amp;quot;&lt;/span&gt; x))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can also convert a Lisp list to a virtual sequence which just yields each element of the list in turn:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defmacro&lt;/span&gt; vseq (&lt;span class="hljs-name"&gt;list&lt;/span&gt;)
  `(loop for x in ,list do (yield x)))

(&lt;span class="hljs-name"&gt;defmacro&lt;/span&gt; vlist (&lt;span class="hljs-name"&gt;&amp;amp;rest&lt;/span&gt; elems)
  `(vseq (list ,@elems)))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now let’s try to define &lt;code&gt;RANGE&lt;/code&gt;. We could use &lt;code&gt;loop&lt;/code&gt;, but for the sake of example, let’s pretend that it doesn’t exist and write a macro that expands to low-level GOTO-ridden code. For those of you who are not familiar with Common Lisp, &lt;code&gt;GO&lt;/code&gt; is like GOTO, except it takes a label that should be established within a &lt;code&gt;TAGBODY&lt;/code&gt; container.&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defmacro&lt;/span&gt; range (&lt;span class="hljs-name"&gt;start&lt;/span&gt; &lt;span class="hljs-symbol"&gt;&amp;amp;optional&lt;/span&gt; end (&lt;span class="hljs-name"&gt;step&lt;/span&gt; &lt;span class="hljs-number"&gt;1&lt;/span&gt;))
  (&lt;span class="hljs-name"&gt;unless&lt;/span&gt; end
    (&lt;span class="hljs-name"&gt;setf&lt;/span&gt; end start start &lt;span class="hljs-number"&gt;0&lt;/span&gt;))
  (&lt;span class="hljs-name"&gt;let&lt;/span&gt; ((&lt;span class="hljs-name"&gt;fv&lt;/span&gt; (&lt;span class="hljs-name"&gt;gensym&lt;/span&gt;)))
    `(let ((,fv ,start))
       (tagbody
        loop
          (when (&amp;gt;= ,fv ,end)
            (go out))
          (yield ,fv)
          (incf ,fv ,step)
          (go loop)
       out))))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;em&gt;Infinite&lt;/em&gt; virtual sequences are also possible. After all, there’s nothing preventing us from considering a snippet of code that loops infinitely, executing &lt;code&gt;YIELD&lt;/code&gt;, as a virtual sequence! We will define the equivalent of Clojure’s iterate: given a function &lt;code&gt;fun&lt;/code&gt; and initial value &lt;code&gt;val&lt;/code&gt;, it will repeatedly generate &lt;code&gt;val&lt;/code&gt;, &lt;code&gt;(fun val)&lt;/code&gt;, &lt;code&gt;(fun (fun val))&lt;/code&gt;, etc.&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defmacro&lt;/span&gt; iterate (&lt;span class="hljs-name"&gt;fun&lt;/span&gt; val)
  (&lt;span class="hljs-name"&gt;let&lt;/span&gt; ((&lt;span class="hljs-name"&gt;fv&lt;/span&gt; (&lt;span class="hljs-name"&gt;gensym&lt;/span&gt;)))
    `(let ((,fv ,val))
       (tagbody loop
          (yield ,fv)
          (setf ,fv (funcall ,fun ,fv))
          (go loop)))))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So far, we have defined a number of ways to create virtual sequences. Now let’s ask ourselves: is there a way, given code for a virtual sequence, to yield only the elements from the original that satisfy a certain predicate? In other words, can we define a &lt;code&gt;filter&lt;/code&gt; for virtual sequences? Sure enough. Just replace every occurrence of &lt;code&gt;yield&lt;/code&gt; with code that checks whether the yielded value satisfies the predicate, and only if it does invokes &lt;code&gt;yield&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;First we write a simple code walker that applies some transformation to every &lt;code&gt;yield&lt;/code&gt; occurrence in a given snippet:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defun&lt;/span&gt; replace-yield (&lt;span class="hljs-name"&gt;tree&lt;/span&gt; replace)
  (&lt;span class="hljs-name"&gt;if&lt;/span&gt; (&lt;span class="hljs-name"&gt;consp&lt;/span&gt; tree)
      (&lt;span class="hljs-name"&gt;if&lt;/span&gt; (&lt;span class="hljs-name"&gt;eql&lt;/span&gt; (&lt;span class="hljs-name"&gt;car&lt;/span&gt; tree) &amp;#x27;yield)
          (&lt;span class="hljs-name"&gt;funcall&lt;/span&gt; replace (&lt;span class="hljs-name"&gt;cadr&lt;/span&gt; tree))
          (&lt;span class="hljs-name"&gt;loop&lt;/span&gt; for x in tree collect (&lt;span class="hljs-name"&gt;replace-yield&lt;/span&gt; x replace)))
      tree))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can now write &lt;code&gt;filter&lt;/code&gt; like this:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defmacro&lt;/span&gt; filter (&lt;span class="hljs-name"&gt;pred&lt;/span&gt; vseq &lt;span class="hljs-symbol"&gt;&amp;amp;environment&lt;/span&gt; env)
  (&lt;span class="hljs-name"&gt;replace-yield&lt;/span&gt; (&lt;span class="hljs-name"&gt;macroexpand&lt;/span&gt; vseq env)
                 (&lt;span class="hljs-name"&gt;lambda&lt;/span&gt; (&lt;span class="hljs-name"&gt;x&lt;/span&gt;) `(when (funcall ,pred ,x) (yield ,x)))))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It is important to point out that since &lt;code&gt;filter&lt;/code&gt; is a macro, the arguments are passed to it unevaluated, so if &lt;code&gt;vseq&lt;/code&gt; is a virtual sequence definition like &lt;code&gt;(range 10)&lt;/code&gt;, we need to macroexpand it before replacing &lt;code&gt;yield&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;We can now verify that &lt;code&gt;(filter #'evenp (range 10))&lt;/code&gt; works. It macroexpands to something similar to&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;LET&lt;/span&gt; ((&lt;span class="hljs-name"&gt;#&lt;/span&gt;&lt;span class="hljs-symbol"&gt;:G70192&lt;/span&gt; &lt;span class="hljs-number"&gt;0&lt;/span&gt;))
  (&lt;span class="hljs-name"&gt;TAGBODY&lt;/span&gt;
    LOOP (&lt;span class="hljs-name"&gt;IF&lt;/span&gt; (&lt;span class="hljs-name"&gt;&amp;gt;=&lt;/span&gt; #&lt;span class="hljs-symbol"&gt;:G70192&lt;/span&gt; &lt;span class="hljs-number"&gt;10&lt;/span&gt;)
           (&lt;span class="hljs-name"&gt;PROGN&lt;/span&gt; (&lt;span class="hljs-name"&gt;GO&lt;/span&gt; OUT)))
         (&lt;span class="hljs-name"&gt;IF&lt;/span&gt; (&lt;span class="hljs-name"&gt;FUNCALL&lt;/span&gt; #&amp;#x27;EVENP #&lt;span class="hljs-symbol"&gt;:G70192&lt;/span&gt;)
           (&lt;span class="hljs-name"&gt;PROGN&lt;/span&gt; (&lt;span class="hljs-name"&gt;YIELD&lt;/span&gt; #&lt;span class="hljs-symbol"&gt;:G70192&lt;/span&gt;)))
         (&lt;span class="hljs-name"&gt;SETQ&lt;/span&gt; #&lt;span class="hljs-symbol"&gt;:G70192&lt;/span&gt; (&lt;span class="hljs-name"&gt;+&lt;/span&gt; #&lt;span class="hljs-symbol"&gt;:G70192&lt;/span&gt; &lt;span class="hljs-number"&gt;1&lt;/span&gt;))
         (&lt;span class="hljs-name"&gt;GO&lt;/span&gt; LOOP)
    OUT))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;concat&lt;/code&gt; is extremely simple. To produce all elements of &lt;code&gt;vseq1&lt;/code&gt; followed by all elements of &lt;code&gt;vseq2&lt;/code&gt;, just execute code corresponding to &lt;code&gt;vseq1&lt;/code&gt; and then code corresponding to &lt;code&gt;vseq2&lt;/code&gt;. Or, for multiple sequences:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defmacro&lt;/span&gt; concat (&lt;span class="hljs-name"&gt;&amp;amp;rest&lt;/span&gt; vseqs)
  `(progn ,@vseqs))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To define &lt;code&gt;take&lt;/code&gt;, we’ll need to wrap the original code in a block that can be escaped from by means of &lt;code&gt;return-from&lt;/code&gt; (which is just another form of &lt;code&gt;goto&lt;/code&gt;). We’ll add a counter that will start from &lt;code&gt;n&lt;/code&gt; and keep decreasing on each &lt;code&gt;yield&lt;/code&gt;; once it reaches zero, we escape the block:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defmacro&lt;/span&gt; take (&lt;span class="hljs-name"&gt;n&lt;/span&gt; vseq &lt;span class="hljs-symbol"&gt;&amp;amp;environment&lt;/span&gt; env)
  (&lt;span class="hljs-name"&gt;let&lt;/span&gt; ((&lt;span class="hljs-name"&gt;x&lt;/span&gt; (&lt;span class="hljs-name"&gt;gensym&lt;/span&gt;))
        (&lt;span class="hljs-name"&gt;b&lt;/span&gt; (&lt;span class="hljs-name"&gt;gensym&lt;/span&gt;)))
    `(let ((,x ,n))
       (block ,b
         ,(replace-yield (macroexpand vseq env)
                         (lambda (y) `(progn (yield ,y)
                                             (decf ,x)
                                             (when (zerop ,x)
                                               (return-from ,b)))))))))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;rest&lt;/code&gt; (or, rather, &lt;code&gt;vrest&lt;/code&gt;, as that name is taken) can be defined similarly:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defmacro&lt;/span&gt; vrest (&lt;span class="hljs-name"&gt;vseq&lt;/span&gt; &lt;span class="hljs-symbol"&gt;&amp;amp;environment&lt;/span&gt; env)
  (&lt;span class="hljs-name"&gt;let&lt;/span&gt; ((&lt;span class="hljs-name"&gt;skipped&lt;/span&gt; (&lt;span class="hljs-name"&gt;gensym&lt;/span&gt;)))
    (&lt;span class="hljs-name"&gt;replace-yield&lt;/span&gt;
     `(let ((,skipped &lt;span class="hljs-literal"&gt;nil&lt;/span&gt;)) ,(macroexpand vseq env))
     (&lt;span class="hljs-name"&gt;lambda&lt;/span&gt; (&lt;span class="hljs-name"&gt;x&lt;/span&gt;) `(if ,skipped (yield ,x) (setf ,skipped &lt;span class="hljs-literal"&gt;t&lt;/span&gt;))))))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;vfirst&lt;/code&gt; is another matter. It should return a value instead of producing a virtual sequence, so we need to actually execute the code — but with &lt;code&gt;yield&lt;/code&gt; bound to something else. We want to establish a block as with &lt;code&gt;take&lt;/code&gt;, but our &lt;code&gt;yield&lt;/code&gt; will immediately return from the block once the first value is yielded:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defmacro&lt;/span&gt; vfirst (&lt;span class="hljs-name"&gt;vseq&lt;/span&gt;)
  (&lt;span class="hljs-name"&gt;let&lt;/span&gt; ((&lt;span class="hljs-name"&gt;block-name&lt;/span&gt; (&lt;span class="hljs-name"&gt;gensym&lt;/span&gt;)))
   `(block ,block-name
      (flet ((yield (x) (return-from ,block-name x)))
        ,vseq))))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Note that so far we’ve seen three classes of macros:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span&gt;macros that create virtual sequences;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;macros that transform virtual sequences to another virtual sequences;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;and finally, vfirst is our first example of a macro that produces a result out of a virtual sequence.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Our next logical step is &lt;code&gt;vreduce&lt;/code&gt;. Again, we’ll produce code that rebinds &lt;code&gt;yield&lt;/code&gt;: this time to a function that replaces the value of a variable (the accumulator) by result of calling a function on the accumulator’s old value and the value being yielded.&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defmacro&lt;/span&gt; vreduce (&lt;span class="hljs-name"&gt;f&lt;/span&gt; val vseq)
  `(let ((accu ,val))
     (flet ((yield (x) (setf accu (funcall ,f accu x))))
       ,vseq
       accu)))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can now build a constructs that executes a virtual sequence and wraps the results up as a Lisp list, in terms of &lt;code&gt;vreduce&lt;/code&gt;.&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defun&lt;/span&gt; conj (&lt;span class="hljs-name"&gt;x&lt;/span&gt; y)
  (&lt;span class="hljs-name"&gt;cons&lt;/span&gt; y x))

(&lt;span class="hljs-name"&gt;defmacro&lt;/span&gt; realize (&lt;span class="hljs-name"&gt;vseq&lt;/span&gt;)
 `(nreverse (vreduce #&amp;#x27;conj &lt;span class="hljs-literal"&gt;nil&lt;/span&gt; ,vseq)))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Let’s verify that it works:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;CL-USER&amp;gt; (&lt;span class="hljs-name"&gt;realize&lt;/span&gt; (&lt;span class="hljs-name"&gt;range&lt;/span&gt; &lt;span class="hljs-number"&gt;10&lt;/span&gt;))
(&lt;span class="hljs-number"&gt;0&lt;/span&gt; &lt;span class="hljs-number"&gt;1&lt;/span&gt; &lt;span class="hljs-number"&gt;2&lt;/span&gt; &lt;span class="hljs-number"&gt;3&lt;/span&gt; &lt;span class="hljs-number"&gt;4&lt;/span&gt; &lt;span class="hljs-number"&gt;5&lt;/span&gt; &lt;span class="hljs-number"&gt;6&lt;/span&gt; &lt;span class="hljs-number"&gt;7&lt;/span&gt; &lt;span class="hljs-number"&gt;8&lt;/span&gt; &lt;span class="hljs-number"&gt;9&lt;/span&gt;)

CL-USER&amp;gt; (&lt;span class="hljs-name"&gt;realize&lt;/span&gt; (&lt;span class="hljs-name"&gt;take&lt;/span&gt; &lt;span class="hljs-number"&gt;5&lt;/span&gt; (&lt;span class="hljs-name"&gt;filter&lt;/span&gt; #&amp;#x27;oddp (&lt;span class="hljs-name"&gt;iterate&lt;/span&gt; #&amp;#x27;&lt;span class="hljs-number"&gt;1&lt;/span&gt;+ &lt;span class="hljs-number"&gt;0&lt;/span&gt;))))
(&lt;span class="hljs-number"&gt;1&lt;/span&gt; &lt;span class="hljs-number"&gt;3&lt;/span&gt; &lt;span class="hljs-number"&gt;5&lt;/span&gt; &lt;span class="hljs-number"&gt;7&lt;/span&gt; &lt;span class="hljs-number"&gt;9&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Hey! Did we just manipulate an &lt;em&gt;infinite&lt;/em&gt; sequence and got the result in a &lt;em&gt;finite&lt;/em&gt; amount of time? And that without explicit support for laziness in our language? How cool is that?!&lt;/p&gt;&lt;p&gt;Anyway, let’s finally define our factorial:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defun&lt;/span&gt; fac (&lt;span class="hljs-name"&gt;n&lt;/span&gt;)
  (&lt;span class="hljs-name"&gt;vreduce&lt;/span&gt; #&amp;#x27;* &lt;span class="hljs-number"&gt;1&lt;/span&gt; (&lt;span class="hljs-name"&gt;range&lt;/span&gt; &lt;span class="hljs-number"&gt;1&lt;/span&gt; (&lt;span class="hljs-number"&gt;1&lt;/span&gt;+ n))))
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id="benchmarking"&gt;Benchmarking&lt;/h2&gt;&lt;p&gt;Factorials grow too fast, so for the purpose of benchmarking let’s write a function that adds numbers from 0 below n, in sequence-y style. First using Common Lisp builtins:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defun&lt;/span&gt; sum-below (&lt;span class="hljs-name"&gt;n&lt;/span&gt;)
  (&lt;span class="hljs-name"&gt;reduce&lt;/span&gt; #&amp;#x27;+ (&lt;span class="hljs-name"&gt;loop&lt;/span&gt; for i from &lt;span class="hljs-number"&gt;0&lt;/span&gt; below n collect i) &lt;span class="hljs-symbol"&gt;:initial-value&lt;/span&gt; &lt;span class="hljs-number"&gt;0&lt;/span&gt;))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And now with our virtual sequences:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defun&lt;/span&gt; sum-below-2 (&lt;span class="hljs-name"&gt;n&lt;/span&gt;)
  (&lt;span class="hljs-name"&gt;vreduce&lt;/span&gt; #&amp;#x27;+ &lt;span class="hljs-number"&gt;0&lt;/span&gt; (&lt;span class="hljs-name"&gt;range&lt;/span&gt; n)))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Let’s try to time the two versions. On my Mac running Clozure CL 1.7, this gives:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;CL-USER&amp;gt; (&lt;span class="hljs-name"&gt;time&lt;/span&gt; (&lt;span class="hljs-name"&gt;sum-below&lt;/span&gt; &lt;span class="hljs-number"&gt;10000000&lt;/span&gt;))
(&lt;span class="hljs-name"&gt;SUM-BELOW&lt;/span&gt; &lt;span class="hljs-number"&gt;10000000&lt;/span&gt;) took &lt;span class="hljs-number"&gt;8&lt;/span&gt;,&lt;span class="hljs-number"&gt;545&lt;/span&gt;,&lt;span class="hljs-number"&gt;512&lt;/span&gt; microseconds (&lt;span class="hljs-number"&gt;8.545512&lt;/span&gt; seconds) to run
                    with &lt;span class="hljs-number"&gt;2&lt;/span&gt; available CPU cores.
During that period, &lt;span class="hljs-number"&gt;2&lt;/span&gt;,&lt;span class="hljs-number"&gt;367&lt;/span&gt;,&lt;span class="hljs-number"&gt;207&lt;/span&gt; microseconds (&lt;span class="hljs-number"&gt;2.367207&lt;/span&gt; seconds) were spent in user mode
                    &lt;span class="hljs-number"&gt;270&lt;/span&gt;,&lt;span class="hljs-number"&gt;481&lt;/span&gt; microseconds (&lt;span class="hljs-number"&gt;0.270481&lt;/span&gt; seconds) were spent in system mode
&lt;span class="hljs-number"&gt;5&lt;/span&gt;,&lt;span class="hljs-number"&gt;906&lt;/span&gt;,&lt;span class="hljs-number"&gt;274&lt;/span&gt; microseconds (&lt;span class="hljs-number"&gt;5.906274&lt;/span&gt; seconds) was spent in GC.
 &lt;span class="hljs-number"&gt;160&lt;/span&gt;,&lt;span class="hljs-number"&gt;000&lt;/span&gt;,&lt;span class="hljs-number"&gt;016&lt;/span&gt; bytes of memory allocated.
 &lt;span class="hljs-number"&gt;39&lt;/span&gt;,&lt;span class="hljs-number"&gt;479&lt;/span&gt; minor page faults, &lt;span class="hljs-number"&gt;1&lt;/span&gt;,&lt;span class="hljs-number"&gt;359&lt;/span&gt; major page faults, &lt;span class="hljs-number"&gt;0&lt;/span&gt; swaps.
&lt;span class="hljs-number"&gt;49999995000000&lt;/span&gt;

CL-USER&amp;gt; (&lt;span class="hljs-name"&gt;time&lt;/span&gt; (&lt;span class="hljs-name"&gt;sum-below-2&lt;/span&gt; &lt;span class="hljs-number"&gt;10000000&lt;/span&gt;))
(&lt;span class="hljs-name"&gt;SUM-BELOW-2&lt;/span&gt; &lt;span class="hljs-number"&gt;10000000&lt;/span&gt;) took &lt;span class="hljs-number"&gt;123&lt;/span&gt;,&lt;span class="hljs-number"&gt;081&lt;/span&gt; microseconds (&lt;span class="hljs-number"&gt;0.123081&lt;/span&gt; seconds) to run
                    with &lt;span class="hljs-number"&gt;2&lt;/span&gt; available CPU cores.
During that period, &lt;span class="hljs-number"&gt;127&lt;/span&gt;,&lt;span class="hljs-number"&gt;632&lt;/span&gt; microseconds (&lt;span class="hljs-number"&gt;0.127632&lt;/span&gt; seconds) were spent in user mode
                    &lt;span class="hljs-number"&gt;666&lt;/span&gt; microseconds (&lt;span class="hljs-number"&gt;0.000666&lt;/span&gt; seconds) were spent in system mode
 &lt;span class="hljs-number"&gt;4&lt;/span&gt; minor page faults, &lt;span class="hljs-number"&gt;0&lt;/span&gt; major page faults, &lt;span class="hljs-number"&gt;0&lt;/span&gt; swaps.
&lt;span class="hljs-number"&gt;49999995000000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;As expected, &lt;code&gt;SUM-BELOW-2&lt;/code&gt; is much faster, causes less page faults and presumably conses less. (Critics will be quick to point out that we could idiomatically write it using &lt;code&gt;LOOP&lt;/code&gt;’s &lt;code&gt;SUM/SUMMING&lt;/code&gt; clause, which would probably be yet faster, and I agree; yet if we were reducing by something other than &lt;code&gt;+&lt;/code&gt; — something that &lt;code&gt;LOOP&lt;/code&gt; has not built in as a clause — this would not be an option.)&lt;/p&gt;&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;&lt;p&gt;We have seen how snippets of code can be viewed as sequences and how to combine them to produce other virtual sequences. As we are nearing the end of this article, it is perhaps fitting to ask: what are the limitations and drawbacks of this approach?&lt;/p&gt;&lt;p&gt;Clearly, this kind of sequences is less powerful than “ordinary” sequences such as Clojure’s. The fact that we’ve built them on macros means that once we escape the world of code transformation by invoking some macro of the third class, we can’t manipulate them anymore. In Clojure world, &lt;code&gt;first&lt;/code&gt; and &lt;code&gt;rest&lt;/code&gt; are very similar; in virtual sequences, they are altogether different: they belong to different worlds. The same goes for &lt;code&gt;map&lt;/code&gt; (had we defined one) and &lt;code&gt;reduce&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;But imagine that instead of having just one programming language, we have a high-level language A in which we are writing macros that expand to code in a low-level language B. It is important to point out that the generated code is very low-level. It could almost be assembly: in fact, most of the macros we’ve written don’t even require language B to have composite data-types beyond the type of elements of collections (which could be simple integers)!&lt;/p&gt;&lt;p&gt;Is there a practical side to this? I don’t know: to me it just seems to be something with hack value. Time will tell if I can put it to good use.&lt;/p&gt;&lt;/div&gt;</content>
  </entry>
  <entry>
    <id>tag:blog.danieljanus.pl,2010-05-04:post:defnk</id>
    <title>Keyword arguments</title>
    <link href="http://blog.danieljanus.pl/defnk/"/>
    <updated>2010-05-04T00:00:00Z</updated>
    <content type="html">&lt;div&gt;&lt;p&gt;There’s been an &lt;a href="http://stuartsierra.com/2010/01/15/keyword-arguments-in-clojure"&gt;ongoing&lt;/a&gt; &lt;a href="http://www.fatvat.co.uk/2009/01/passing-parameters-in-clojure.html"&gt;debate&lt;/a&gt; about how to pass optional named arguments to Clojure functions. One way to do this is the &lt;a href="http://richhickey.github.com/clojure-contrib/def-api.html#clojure.contrib.def/defnk"&gt;defnk&lt;/a&gt; macro from &lt;code&gt;clojure.contrib.def&lt;/code&gt;; I hesitate to call it &lt;em&gt;canonical&lt;/em&gt;, since apparently not everyone uses it, but I’ve found it useful a number of times. Here’s a sample:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;user&amp;gt; (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;use&lt;/span&gt;&lt;/span&gt; &amp;#x27;clojure.contrib.def)
&lt;span class="hljs-literal"&gt;nil&lt;/span&gt;
user&amp;gt; (&lt;span class="hljs-name"&gt;defnk&lt;/span&gt; f [&lt;span class="hljs-symbol"&gt;:b&lt;/span&gt; &lt;span class="hljs-number"&gt;43&lt;/span&gt;] (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;inc&lt;/span&gt;&lt;/span&gt; b))
#&amp;#x27;user/f
user&amp;gt; (&lt;span class="hljs-name"&gt;f&lt;/span&gt;)
&lt;span class="hljs-number"&gt;44&lt;/span&gt;
user&amp;gt; (&lt;span class="hljs-name"&gt;f&lt;/span&gt; &lt;span class="hljs-symbol"&gt;:b&lt;/span&gt; &lt;span class="hljs-number"&gt;100&lt;/span&gt;)
&lt;span class="hljs-number"&gt;101&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is an example of &lt;em&gt;keyword arguments&lt;/em&gt; in action. Keyword arguments are a core feature of some languages, notably &lt;a href="http://www.gigamonkeys.com/book/functions.html#keyword-parameters"&gt;Common Lisp&lt;/a&gt; and &lt;a href="http://caml.inria.fr/pub/docs/manual-ocaml/manual006.html#htoc38"&gt;Objective Caml&lt;/a&gt;. Clojure doesn’t have them, but it’s pretty easy to emulate their basic usage with macros, as &lt;code&gt;defnk&lt;/code&gt; does.&lt;/p&gt;&lt;p&gt;But there’s more to Common Lisp’s keyword arguments than &lt;code&gt;defnk&lt;/code&gt; provides. In CL, the default value of a keyword argument can be an expression referring to other arguments of the same function. For example:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;CL-USER&amp;gt; (&lt;span class="hljs-name"&gt;defun&lt;/span&gt; f (&lt;span class="hljs-name"&gt;&amp;amp;key&lt;/span&gt; (&lt;span class="hljs-name"&gt;a&lt;/span&gt; &lt;span class="hljs-number"&gt;1&lt;/span&gt;) (&lt;span class="hljs-name"&gt;b&lt;/span&gt; a))
           (&lt;span class="hljs-name"&gt;+&lt;/span&gt; a b))
F
CL-USER&amp;gt; (&lt;span class="hljs-name"&gt;f&lt;/span&gt;)
&lt;span class="hljs-number"&gt;2&lt;/span&gt;
CL-USER&amp;gt; (&lt;span class="hljs-name"&gt;f&lt;/span&gt; &lt;span class="hljs-symbol"&gt;:a&lt;/span&gt; &lt;span class="hljs-number"&gt;45&lt;/span&gt;)
&lt;span class="hljs-number"&gt;90&lt;/span&gt;
CL-USER&amp;gt; (&lt;span class="hljs-name"&gt;f&lt;/span&gt; &lt;span class="hljs-symbol"&gt;:b&lt;/span&gt; &lt;span class="hljs-number"&gt;101&lt;/span&gt;)
&lt;span class="hljs-number"&gt;102&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I wish &lt;code&gt;defnk&lt;/code&gt; had this feature. Or is there some better way that I don’t know of?&lt;/p&gt;&lt;/div&gt;</content>
  </entry>
  <entry>
    <id>tag:blog.danieljanus.pl,2008-08-09:post:small-lisp</id>
    <title>Who said Common Lisp programs cannot be small?</title>
    <link href="http://blog.danieljanus.pl/small-lisp/"/>
    <updated>2008-08-09T00:00:00Z</updated>
    <content type="html">&lt;div&gt;&lt;p&gt;So, how much disk space does your average CL image eat up? A hundred megs? Fifty? Twenty? Five, perhaps, if you’re using LispWorks with a tree-shaker? Well then, how about this?&lt;/p&gt;&lt;pre&gt;&lt;code&gt;[nathell@chamsin salza2-2.0.4]$ ./cl-gzip closures.lisp test.gz
[nathell@chamsin salza2-2.0.4]$ gunzip test
[nathell@chamsin salza2-2.0.4]$ diff closures.lisp test
[nathell@chamsin salza2-2.0.4]$ ls -l cl-gzip
-rwxr-xr-x 1 nathell nathell 386356 2008-08-09 11:08 cl-gzip
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;That’s right. A standalone executable of a mini-gzip, written in Common Lisp, taking up &lt;em&gt;under 400K!&lt;/em&gt; And it only depends on glibc and GMP, which are available by default on pretty much every Linux installation. (This is on a 32-bit x86 machine, by the way).&lt;/p&gt;&lt;p&gt;I used the most recent version of &lt;a href="http://ecls.sourceforge.net/"&gt;ECL&lt;/a&gt; for compiling this tiny example. The key to the size was configuring ECL with &lt;code&gt;--disable-shared --enable-static CFLAGS="-Os -ffunction-sections -fdata-sections" LDFLAGS="-Wl,-gc-sections"&lt;/code&gt;. This essentially gives you a poor man’s tree shaker for free at a linker level. And ECL in itself produces comparatively tiny code.&lt;/p&gt;&lt;p&gt;I built this example from &lt;a href="http://www.xach.com/lisp/salza2"&gt;Salza2&lt;/a&gt;’s source by loading the following code snippet:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;defvar&lt;/span&gt; salza
  &amp;#x27;(&lt;span class="hljs-string"&gt;&amp;quot;package&amp;quot;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;reset&amp;quot;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;specials&amp;quot;&lt;/span&gt;
    &lt;span class="hljs-string"&gt;&amp;quot;types&amp;quot;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;checksum&amp;quot;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;adler32&amp;quot;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;crc32&amp;quot;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;chains&amp;quot;&lt;/span&gt;
    &lt;span class="hljs-string"&gt;&amp;quot;bitstream&amp;quot;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;matches&amp;quot;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;compress&amp;quot;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;huffman&amp;quot;&lt;/span&gt;
    &lt;span class="hljs-string"&gt;&amp;quot;closures&amp;quot;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;compressor&amp;quot;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;utilities&amp;quot;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;zlib&amp;quot;&lt;/span&gt;
    &lt;span class="hljs-string"&gt;&amp;quot;gzip&amp;quot;&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;user&amp;quot;&lt;/span&gt;))

(&lt;span class="hljs-name"&gt;defvar&lt;/span&gt; salza2
  (&lt;span class="hljs-name"&gt;mapcar&lt;/span&gt; (&lt;span class="hljs-name"&gt;lambda&lt;/span&gt; (&lt;span class="hljs-name"&gt;x&lt;/span&gt;) (&lt;span class="hljs-name"&gt;format&lt;/span&gt; &lt;span class="hljs-literal"&gt;nil&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;~A.lisp&amp;quot;&lt;/span&gt; x))
          salza))

(&lt;span class="hljs-name"&gt;defvar&lt;/span&gt; salza3
  (&lt;span class="hljs-name"&gt;mapcar&lt;/span&gt; (&lt;span class="hljs-name"&gt;lambda&lt;/span&gt; (&lt;span class="hljs-name"&gt;x&lt;/span&gt;) (&lt;span class="hljs-name"&gt;format&lt;/span&gt; &lt;span class="hljs-literal"&gt;nil&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;~A.o&amp;quot;&lt;/span&gt; x))
          salza))

(&lt;span class="hljs-name"&gt;defun&lt;/span&gt; build-cl-gzip ()
  (&lt;span class="hljs-name"&gt;dolist&lt;/span&gt; (&lt;span class="hljs-name"&gt;x&lt;/span&gt; salza2)
          (&lt;span class="hljs-name"&gt;load&lt;/span&gt; x)
          (&lt;span class="hljs-name"&gt;compile-file&lt;/span&gt; x &lt;span class="hljs-symbol"&gt;:system-p&lt;/span&gt; &lt;span class="hljs-literal"&gt;t&lt;/span&gt;))
  (&lt;span class="hljs-name"&gt;c&lt;/span&gt;&lt;span class="hljs-symbol"&gt;:build-program&lt;/span&gt;
   &lt;span class="hljs-string"&gt;&amp;quot;cl-gzip&amp;quot;&lt;/span&gt;
   &lt;span class="hljs-symbol"&gt;:lisp-files&lt;/span&gt; salza3
   &lt;span class="hljs-symbol"&gt;:epilogue-code&lt;/span&gt;
     &amp;#x27;(progn
       (in-package :salza2)
       (gzip-file (second (si::command-args))
                  (third (si::command-args))))))

(&lt;span class="hljs-name"&gt;build-cl-gzip&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;(Sadly enough, there’s no ASDF in here. I have yet to figure out how to leverage ASDF to build small binaries in this constrained environment.)&lt;/p&gt;&lt;p&gt;This gave me a standalone executable 1.2 meg in size. I then proceeded to compress it with &lt;a href="http://upx.sourceforge.net/"&gt;UPX&lt;/a&gt; (with arguments &lt;code&gt;--best --crp-ms=999999&lt;/code&gt;) and got the final result. How cool is that?&lt;/p&gt;&lt;p&gt;I am actively looking for a new job. If you happen to like my writings and think I might be just the right man for the team you’re building up, please feel free to consult my &lt;a href="http://bach.ipipan.waw.pl/~nathell/cv-en.pdf"&gt;résumé&lt;/a&gt; or pass it on.&lt;/p&gt;&lt;p&gt;&lt;em&gt;Update 2010-Jan-17&lt;/em&gt;: the above paragraph is no longer valid.&lt;/p&gt;&lt;/div&gt;</content>
  </entry>
  <entry>
    <id>tag:blog.danieljanus.pl,2008-06-23:post:cl-morfeusz</id>
    <title>cl-morfeusz: A ninety minutes’ hack</title>
    <link href="http://blog.danieljanus.pl/cl-morfeusz/"/>
    <updated>2008-06-23T00:00:00Z</updated>
    <content type="html">&lt;div&gt;&lt;p&gt;Here’s what I came up with today, after no more than 90 minutes of coding (complete with comments and all):&lt;/p&gt;&lt;pre&gt;&lt;code&gt;MORFEUSZ&gt; (morfeusz-analyse "zażółć gęślą jaźń")
((0 1 "zażółć" "zażółcić" "impt:sg:sec:perf")
 (1 2 "gęślą" "gęśl" "subst:sg:inst:f")
 (2 3 "jaźń" "jaźń" "subst:sg:nom.acc:f"))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is &lt;a href="http://danieljanus.pl/code/morfeusz.lisp"&gt;cl-morfeusz&lt;/a&gt; in action, a Common Lisp interface to &lt;a href="http://nlp.ipipan.waw.pl/~wolinski/morfeusz/"&gt;Morfeusz&lt;/a&gt;, the morphological analyser for Polish.&lt;/p&gt;&lt;p&gt;It’s a single Lisp file, so there’s no ASDF system definition or asdf-installability for now. I’m not putting it under version control, either. Or, should I say, not yet. When I get around to it, I plan to write a simple parser and write a Polish-language version of &lt;a href="http://en.wikipedia.org/wiki/Colossal_Cave_Adventure"&gt;the text adventure that started it all&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Meanwhile, you may use cl-morfeusz for anything you wish (of course, as long as you comply with Morfeusz’s license). Have fun!&lt;/p&gt;&lt;p&gt;&lt;em&gt;Update 2010-Jan-17&lt;/em&gt;: With the advent of UTF-8 support in CFFI, the ugly workarounds in the code are probably no longer necessary; I don’t have time to check it right now, though.&lt;/p&gt;&lt;/div&gt;</content>
  </entry>
  <entry>
    <id>tag:blog.danieljanus.pl,2008-04-30:post:cl-netstrings</id>
    <title>cl-netstrings</title>
    <link href="http://blog.danieljanus.pl/cl-netstrings/"/>
    <updated>2008-04-30T00:00:00Z</updated>
    <content type="html">&lt;div&gt;&lt;p&gt;I’ve just packaged up the Common Lisp netstring handling code that I &lt;a href="http://blog.danieljanus.pl/hacking-away-with-json-rpc.html"&gt;wrote a week ago&lt;/a&gt; into a neat library. Unsurprisingly enough, it is called cl-netstrings and has its own &lt;a href="http://github.com/nathell/cl-netstrings"&gt;home on the Web&lt;/a&gt;. It’s even asdf-installable! I wonder whether this one turns out to be useful for anybody besides me…&lt;/p&gt;&lt;p&gt;The other thing I’ve been working on is a new build system for Poliqarp. But that’s the story for another post — most probably I will write about it when it gets out of a state of constant flux.&lt;/p&gt;&lt;p&gt;&lt;em&gt;Update 2010-Jan-17&lt;/em&gt;: cl-netstrings is now hosted on GitHub; I’ve updated the link.&lt;/p&gt;&lt;/div&gt;</content>
  </entry>
  <entry>
    <id>tag:blog.danieljanus.pl,2008-04-24:post:hacking-away-with-json-rpc</id>
    <title>Hacking away with JSON-RPC</title>
    <link href="http://blog.danieljanus.pl/hacking-away-with-json-rpc/"/>
    <updated>2008-04-24T00:00:00Z</updated>
    <content type="html">&lt;div&gt;&lt;p&gt;Let’s try:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs lisp"&gt;(&lt;span class="hljs-name"&gt;let&lt;/span&gt; ((&lt;span class="hljs-name"&gt;s&lt;/span&gt; (&lt;span class="hljs-name"&gt;socket-stream&lt;/span&gt;
          (&lt;span class="hljs-name"&gt;socket-connect&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;localhost&amp;quot;&lt;/span&gt; &lt;span class="hljs-number"&gt;10081&lt;/span&gt;
                          &lt;span class="hljs-symbol"&gt;:element-type&lt;/span&gt; &amp;#x27;(unsigned-byte &lt;span class="hljs-number"&gt;8&lt;/span&gt;)))))
  (&lt;span class="hljs-name"&gt;write-netstring&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;{\&amp;quot;method\&amp;quot;:\&amp;quot;ping\&amp;quot;,\&amp;quot;params\&amp;quot;:[],\&amp;quot;id\&amp;quot;:1}&amp;quot;&lt;/span&gt; s)
  (&lt;span class="hljs-name"&gt;finish-output&lt;/span&gt; s)
  (&lt;span class="hljs-name"&gt;princ&lt;/span&gt; (&lt;span class="hljs-name"&gt;read-netstring&lt;/span&gt; s))
  (&lt;span class="hljs-name"&gt;close&lt;/span&gt; s))
&lt;span class="hljs-comment"&gt;; { &amp;quot;result&amp;quot;: &amp;quot;pong&amp;quot; }&lt;/span&gt;
&lt;span class="hljs-comment"&gt;; --&amp;gt; T&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Yay! This is Common Lisp talking to a &lt;a href="http://json-rpc.org/"&gt;JSON-RPC&lt;/a&gt; server written in C. This means that I have now the foundations for rewriting Poliqarp on top of JSON-RPC (according to the &lt;a href="http://blog.danieljanus.pl/poliqarp-new-protocol.html"&gt;protocol spec&lt;/a&gt; I have recently posted) up and running, and all that remains is to fill the remainder.&lt;/p&gt;&lt;p&gt;Well, to be honest, this is not exactly JSON-RPC. First off, as you might have noticed, the above snippet of code sends JSON-RPC requests as &lt;a href="http://cr.yp.to/proto/netstrings.txt"&gt;netstrings&lt;/a&gt;. This is actually intentional, and the reasons for adopting this encoding have been described in detail in the spec (it basically boils down to the fact that it greatly simplifies reading from and writing to network, especially in C). I wrote some crude code to handle netstrings in CL — now it occurred to me that it might actually be worthwhile to polish it up a little, write some documentation and put on &lt;a href="http://www.cliki.net/"&gt;CLiki&lt;/a&gt; as an asdf-installable library. I’ll probably get on to this quite soon.&lt;/p&gt;&lt;p&gt;Second, the resulting JSON object does not have all the necessary stuff. It contains the result, but not the error or id (as mandated by the &lt;a href="http://json-rpc.org/wiki/specification"&gt;JSON-RPC spec&lt;/a&gt;). This is actually a deficiency of the &lt;a href="http://www.big-llc.com/software.jsp"&gt;JSON-RPC C library&lt;/a&gt; I’m currently using. It places the burden of constructing objects that are proper JSON-RPC responses on the programmer, instead of doing that itself. This will be easy to sort out, however, because the library is small and adheres to the &lt;a href="http://en.wikipedia.org/wiki/KISS_principle"&gt;KISS principle&lt;/a&gt;. More of a problem is that the licensing of that library is unclear; I emailed the maintainers to explain the status.&lt;/p&gt;&lt;/div&gt;</content>
  </entry>
  <entry>
    <id>tag:blog.danieljanus.pl,2008-04-22:post:eclm-2008</id>
    <title>ECLM 2008</title>
    <link href="http://blog.danieljanus.pl/eclm-2008/"/>
    <updated>2008-04-22T00:00:00Z</updated>
    <content type="html">&lt;div&gt;&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;What is there left for me to do in this life&lt;/em&gt;?&lt;br&gt; &lt;em&gt;Did I achieve what I had set in my sights?&lt;/em&gt;&lt;br&gt; &lt;em&gt;Am I a happy man, or is this sinking sand?&lt;/em&gt;&lt;br&gt; &lt;em&gt;Was it all worth it?—was it all worth it?&lt;/em&gt;&lt;br&gt;&lt;/p&gt;&lt;p style="text-align: right;"&gt;— Queen&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Gusts of moderate wind are blowing at my face through the open window of a train from Amsterdam to Warsaw as I write these words. The last buildings of Amsterdam have vanished a while ago, giving ground to the damp, low countryside of the Netherlands — not quite fascinating sight to be watching — so I decided to fire up my laptop and write down some impressions while they are sharp and vivid — impressions from the &lt;a href="http://www.weitz.de/eclm2008/"&gt;European Common Lisp Meeting&lt;/a&gt; that was held in Amsterdam yesterday.&lt;/p&gt;&lt;p&gt;I was there with &lt;a href="http://blog.pasternacki.net/"&gt;Maciek&lt;/a&gt; and &lt;a href="http://lisp.jogger.pl/"&gt;Richard&lt;/a&gt;. Amsterdam did not receive us warmly, pouring some mild yet cold rain on us, but our hosts — Lispniks from the Hague, Gabriele and Victor from &lt;a href="http://streamtech.nl/"&gt;Streamtech&lt;/a&gt; — turned out to be really nice guys. I’m not going to go into a very detailed description of the social aspects of our trip, instead focusing on the conference itself. And that is definitely a topic worth talking about for a long time.&lt;/p&gt;&lt;p&gt;The first man to speak was Jeremy Jones of &lt;a href="http://www.clozure.com/"&gt;Clozure Associates&lt;/a&gt;, talking about InspireData and how they did it in Lisp. Although they also seem to be the people behind Clozure CL the implementation of Common Lisp, InspireData, the product their presentation was about, seems to have been written in LispWorks. It is a quite interesting application for browsing datasets in many interesting ways and draw conclusions for them. Jeremy started off with a demonstration presenting the key ideas of InspireData and what it can do, and this almost instantly hooked the attention of most of the gathered Lispers; mine, at least, definitely. First off, it seems to be quite a nice success story of a real-world application of Lisp, well worth learning about and mentioning where it deserves a mention. Second, one of its great features shown by the demo is that one can copy HTML tables from a Web browser and paste them as InspireData datasets. Given that Poliqarp now has statistical features and can export its data to HTML, I wonder whether it is possible to couple it with InspireData to interactively explore linguistic material in an absorbing way. That’s certainly a topic worthy of further research.&lt;/p&gt;&lt;p&gt;And last but not least, Jeremy outlined the points they did wrong and those they got right. Among those latter were two letters that now constitute a huge part of my professional life: &lt;strong&gt;QA&lt;/strong&gt;. He just couldn’t emphasize enough how crucial the fact that they had a serious quality assurance process from the very beginning proved to yield the final quality of the product. That’s the lesson I’m now quickly learning. When I learned that InspireData was mostly tested by hand by a skilled QA team, I felt somewhat proud of being able to automate large parts of the process at Sentivision. I’m very curious where this path will lead me to. Let’s hope for the best!&lt;/p&gt;&lt;p&gt;The next speaker was Nicolas Neuss of University of Karlsruhe, talking about &lt;a href="http://www.femlisp.org/"&gt;Femlisp&lt;/a&gt;, a framework for solving partial differential equations in Common Lisp. I have little to say about this one, since I lack the mathematical background needed to fully comprehend and appreciate the topic; it’s just not my kettle of fish. Undoubtedly, though, Femlisp seems to be filling its niche in a neat way, as the demonstrations showed.&lt;/p&gt;&lt;p&gt;After a coffee break, Stefan Richter came up with the one presentation that I’ve been looking forward to the most; that of using Common Lisp for large, scalable Internet systems. After all the talks were over, Maciek dubbed it a “very nice anti-FUD presentation” and I could not agree more. I didn’t learn many new things from it, but the author clearly knows how to attempt to convince non-Lispers to try out Lisp. The talk started off with outlining the typical designs of Web apps and portals, starting with simple one-server scenarios that don’t scale well and progressing in the direction of more scalable and extensible ones. Stefan then pointed out that in some mainstream languages like Java there exists a mature and proven infrastructure for employing such designs. And then came the key point — &lt;em&gt;that this is the case also for Common Lisp!&lt;/em&gt; All the necessary tools are there, ready to use Right Now and free for the most part; they’re just not as mature in some cases. This is not much of a problem, though, given the incremental nature of Lisp development: any problem at hand is typically fixable much faster than in case of other languages. The only weird thing was that the author advocated using continuation-based Web frameworks (such as &lt;a href="http://common-lisp.net/project/cl-weblocks/"&gt;Weblocks&lt;/a&gt; or &lt;a href="http://common-lisp.net/project/ucw/"&gt;UnCommon Web&lt;/a&gt;) just a couple of minutes after discouraging using sticky sessions.&lt;/p&gt;&lt;p&gt;Next came Killian Sprotte with a speech about &lt;a href="http://www2.siba.fi/PWGL/"&gt;PWGL&lt;/a&gt;, the program for computer-aided music composing. I have very mixed feelings about it. Notice that I didn’t use the word “presentation” — there was no presentation at all. Yes, that’s right. The speaker was just talking and showing off various things in the musical engine. Now, having no presentation accompanying the talk is not necessarily a bad thing in itself; but without one, it’s a little harder to draw attention of the audience and a whole lot easier to deliver a chaotic talk instead of a cleanly-structured and well-organized one. Such was the case with this speech. Some features were shown, but with a fair amount of obscurity and boredom thrown in, leaving me with a rather low overall impression.&lt;/p&gt;&lt;p&gt;As for PWGL itself, some of the ideas employed in it seem a bit peculiar (for want of a better word) to me. As befits a Lisp program, it is an extensible utility that actually allows users to program music just as one programs software. But the way that programming is done… well, think of a graphical editor for Lisp programs. An editor in which to write, say, a factorial function, you right-click an initially blank sheet, select &lt;code&gt;defun&lt;/code&gt; from a pop-up with a complicated set of menus and submenus… and kaboom! up comes a box divided into several sub-boxes. They correspond to — what else they could? — the name of the function, list of arguments, and a body. You can draw boxes representing computations, drag them around and link them with arrows — this is supposed to build complicated expressions out of simpler ones. And there is a huge library of musical tools, all available for the convenience of a programmer. Or, should I say, a composer.&lt;/p&gt;&lt;p&gt;Sounds cool? Maybe — for a newbie. I can’t really say. As someone who has high experience with Lisp and programming in general, I can only speak for myself. And for me all this click-and-drag-programming seems to be an unnecessarily tedious, obscure and error-prone way of doing things. Stuff like score editors, chord editors or various transformations is admittedly cool, but for lower-level matters the kind of visualization PWGL offers (and it obviously has its rough edges) seems to get in the way rather than staying out of it. But perhaps that’s just me?&lt;/p&gt;&lt;p&gt;By the time the fourth talk ended, most Lispers were already hungry, so a lunch break followed. I talked to some guy (I don’t remember his name, alas) who’s working on porting Clozure CL to 64-bit Windows. This is great news — when the port’s complete, it has high chances of becoming the free Common Lisp implementation of choice for many Windows Lisp hackers.&lt;/p&gt;&lt;p&gt;Juan José García-Ripoll then &lt;a href="http://ecls.wiki.sourceforge.net/space/showimage/eclm2008.pdf"&gt;talked&lt;/a&gt; about &lt;a href="http://ecls.sourceforge.net/"&gt;ECL&lt;/a&gt;, another CL implementation that is characterized by a fairly small memory and disk footprint, while still managing to achieve decent performance (via compilation to C) and good standard compliance. It was good to see that ECL is still quite alive and getting better and better with each release. Just for the heck of it, I attempted in the evening to reproduce the problem I had with ECL a while ago on a fresh CVS checkout. I managed to reproduce it (for the curious, it was an issue with ECL failing to build after having been &lt;code&gt;configure&lt;/code&gt;d with the option &lt;code&gt;--disable-shared&lt;/code&gt;). So I reported the bug to Juan, and he promised to look into it within the next days. And I must say that reporting bugs IRL to open source projects’ maintainers is a very nice experience. :-)&lt;/p&gt;&lt;p&gt;And then came a really big surprise, and I mean a &lt;em&gt;nice&lt;/em&gt; surprise. It took the form of Kristofer Kvello of &lt;a href="http://www.selvaag.no/"&gt;Selvaag&lt;/a&gt;, a Norwegian-based house-building company, and his presentation on &lt;a href="http://www.selvaag.no/en/Companies/Selvaagbluethink/aboutus/Sider/default.aspx"&gt;House Designer&lt;/a&gt;, a Common Lisp program for aiding in designing residences, as the name suggests. Yet another example of a success story in an area CL can really excel at. Basically, what House Designer can do is that you give it a &lt;em&gt;sketch&lt;/em&gt;, containing a rough description of the shape of a flat or residence and layout of rooms, and out comes a very detailed project with all sorts of bells and whistles: the program automatically figures out what the number of windows should be and where they should be located, the number and location of electric outlets, the optimal types of walls, layout of water installation and what not. It’s transfixing when you think of the sheer amount of tedious labour it automates, taking into account all of the professional knowledge about designing houses accumulated over years, some parts of which a human can easily omit. And it’s been Lisp all the lifetime of this project, and it’s Lisp all the way down (except for the GUI in Java)! Very, very impressive!&lt;/p&gt;&lt;p&gt;Marc Battyani’s talk about &lt;a href="http://www.hpcplatform.com/"&gt;programming FPGAs in Lisp&lt;/a&gt; probably should not have been stacked so late in the programme. I mean, the topic seems to be quite interesting (though a bit low-level for my interests), but there was something about Marc’s way of talking and showing things that sent me off dozing almost instantaneously. I’d been a bit tired after the many hours of sitting and listening to speeches, especially after having woken up at six o’clock, and so I somewhat regret missing large parts of the talk. It’s nice to know, though, that it is possible to do such things with Lisp. Seems to have a high hack value, as in: “Why do it this way? Because we &lt;em&gt;can&lt;/em&gt;!”&lt;/p&gt;&lt;p&gt;And what better end of a conference could one ask for than a rant by Kenny Tilton? If you have only encountered Kenny on the Usenet (some of the crème de la crème of his postings is &lt;a href="http://www.pasternacki.net/en/ken-tilton-fortunes"&gt;meticulously collected by Maciek&lt;/a&gt;) and think he’s one heck of a freak, you definitely should listen to him live. Here was another talk without slides — just talking and demonstrating stuff — but this time, it was a totally different thing. Kenny sure knows how to attract the attention of the audience and how not to let it loose throughout an hour’s worth of talking. And he changes topics with mastery, using digressions to a great effect to avoid the boredom slipping in, caused by bragging about one thing all the time. There was &lt;a href="http://smuglispweeny.blogspot.com/2008/02/cells-manifesto.html"&gt;Cells&lt;/a&gt; in that talk, there was &lt;a href="http://www.theoryyalgebra.com/"&gt;teaching of algebra&lt;/a&gt;, and there was high-speed driving through the streets of New York. I only hope someone has recorded that to put it online.&lt;/p&gt;&lt;p&gt;So, this was it. There was much talk afterwards, there was much beer, there was much socializing, there was much rejoicing. I saw a real &lt;a href="http://laptop.org/"&gt;XO-1&lt;/a&gt; and played with it for a while, and boy, isn’t it cute! And then we all came back. And here I am, sitting at my desk in Warsaw (it’s the next day already; I really wish my laptop had a better battery), finishing up this longish blog entry and asking myself: was this 50 euro well spent?&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;Yes, it was a worthwhile experience, hahhahahahahhaaaa!&lt;/em&gt;&lt;br&gt; (evil chuckle à la Kenny)&lt;br&gt; &lt;em&gt;It was worth it!&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;</content>
  </entry>
</feed>
