<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>lisp — jd:/dev/blog</title><description>Posts tagged &quot;lisp&quot; on jd:/dev/blog.</description><link>https://julien.danjou.info/</link><item><title>FOSDEM 2016, recap</title><link>https://julien.danjou.info/blog/fosdem-2016-recap/</link><guid isPermaLink="true">https://julien.danjou.info/blog/fosdem-2016-recap/</guid><description>Last week-end, I was in Brussels, Belgium for the FOSDEM, one of the greatest open source developer conference.</description><pubDate>Sat, 06 Feb 2016 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Last week-end, I was in Brussels, Belgium for the &lt;a href=&quot;http://fosdem.org&quot;&gt;FOSDEM&lt;/a&gt;, one of the greatest open source developer conference. I was not sure to go there this year (I already skipped it in 2015), but it turned out I was requested to do a talk in the shared &lt;a href=&quot;https://fosdem.org/2016/schedule/track/lua/&quot;&gt;Lua&lt;/a&gt; &amp;amp; &lt;a href=&quot;https://fosdem.org/2016/schedule/track/gnu_guile/&quot;&gt;GNU Guile&lt;/a&gt; devroom.&lt;/p&gt;
&lt;p&gt;As a long time &lt;a href=&quot;http://lua.org&quot;&gt;Lua&lt;/a&gt; user and developer, and a follower of &lt;a href=&quot;http://www.gnu.org/software/guile/&quot;&gt;GNU Guile&lt;/a&gt; for several years, the organizer asked me to run a talk that would be a link between the two languages.&lt;/p&gt;
&lt;p&gt;I&apos;ve entitled my talk &quot;How awesome ended up with Lua and not Guile&quot; and gave it to a room full of interested users of the awesome window manager 🙂.&lt;/p&gt;
&lt;p&gt;We continued with a panel discussion entitled &quot;&lt;a href=&quot;https://fosdem.org/2016/schedule/event/future_guile_lua/&quot;&gt;The future of small languages Experience of Lua and Guile&lt;/a&gt;&quot; composed of Andy Wingo, Christopher Webber, Ludovic Courtès, Etiene Dalcol, Hisham Muhammaad and myself. It was a pretty interesting discussion, where both language shared their views on the state of their languages.&lt;/p&gt;
&lt;p&gt;It was a bit awkward to talk about Lua &amp;amp; Guile whereas most of my knowledge was years old, but it turns out many things didn&apos;t change. I hope I was able to provide interesting hindsight to both community. Finally, it was a pretty interesting FOSDEM to me, and it was a long time I didn&apos;t give talk here, so I really enjoyed it. See you next year!&lt;/p&gt;
</content:encoded><category>talks</category><category>awesome</category><category>lua</category><category>lisp</category></item><item><title>Python 3.4 single dispatch, a step into generic functions</title><link>https://julien.danjou.info/blog/python-3-4-single-dispatch-generic-function/</link><guid isPermaLink="true">https://julien.danjou.info/blog/python-3-4-single-dispatch-generic-function/</guid><description>I love to say that Python is a nice subset of Lisp, and I discover that it&apos;s getting even more true as time passes.</description><pubDate>Tue, 17 Sep 2013 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I love to say that Python is a nice subset of Lisp, and I discover that it&apos;s getting even more true as time passes. Recently, I&apos;ve stumbled upon the &lt;a href=&quot;http://python.org/dev/peps/pep-0443/&quot;&gt;PEP 443&lt;/a&gt; that describes a way to dispatch generic functions, in a way that looks like what CLOS, the Common Lisp Object System, provides.&lt;/p&gt;
&lt;h2&gt;What are generic functions&lt;/h2&gt;
&lt;p&gt;If you come from the Lisp world, this won&apos;t be something new to you. The Lisp object system provides a really good way to define and handle method dispatching. It&apos;s a base of the Common Lisp object system. For my own pleasure to see Lisp code in a Python post, I&apos;ll show you how generic methods work in Lisp first.&lt;/p&gt;
&lt;p&gt;To begin, let&apos;s define a few very simple classes.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defclass snare-drum ()
  ())

(defclass cymbal ()
  ())

(defclass stick ()
  ())

(defclass brushes ()
  ())
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This defines a few classes: &lt;code&gt;snare-drum&lt;/code&gt;, &lt;code&gt;symbal&lt;/code&gt;, &lt;code&gt;stick&lt;/code&gt; and &lt;code&gt;brushes&lt;/code&gt;, without any parent class nor attribute. These classes compose a drum kit, and we can combine them to play sound. So we define a &lt;code&gt;play&lt;/code&gt; method that takes two arguments, and returns a sound (as a string).&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defgeneric play (instrument accessory)
  (:documentation &quot;Play sound with instrument and accessory.&quot;))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This only defines a generic method: it has no body, and cannot be called with any instance yet. At this stage, we only inform the object system that the method is generic and can be then implemented with various type of arguments. We&apos;ll start by implementing versions of this method that knows how to play with the snare-drum.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmethod play ((instrument snare-drum) (accessory stick))
  &quot;POC!&quot;)

(defmethod play ((instrument snare-drum) (accessory brushes))
  &quot;SHHHH!&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we just defined concrete methods with code. They also takes two arguments: &lt;code&gt;instrument&lt;/code&gt; which is an instance of &lt;code&gt;snare-drum&lt;/code&gt; and &lt;code&gt;accessory&lt;/code&gt; that is an instance of &lt;code&gt;stick&lt;/code&gt; or &lt;code&gt;brushes&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;At this stage, you should note the first difference with object system as built into language like Python: the method isn&apos;t tied to any class in particular. The methods are &lt;em&gt;generic&lt;/em&gt;, and any class can implement them, or not.&lt;/p&gt;
&lt;p&gt;Let&apos;s try it.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;* (play (make-instance &apos;snare-drum) (make-instance &apos;stick))
&quot;POC!&quot;

* (play (make-instance &apos;snare-drum) (make-instance &apos;brushes))
&quot;SHHHH!&quot;

* (play (make-instance &apos;cymbal) (make-instance &apos;stick))
debugger invoked on a SIMPLE-ERROR in thread
#&amp;lt;THREAD &quot;main thread&quot; RUNNING {1002ADAF23}&amp;gt;:
  There is no applicable method for the generic function
    #&amp;lt;STANDARD-GENERIC-FUNCTION PLAY (2)&amp;gt;
  when called with arguments
    (#&amp;lt;CYMBAL {1002B801D3}&amp;gt; #&amp;lt;STICK {1002B82763}&amp;gt;).

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [RETRY] Retry calling the generic function.
  1: [ABORT] Exit debugger, returning to top level.

((:METHOD NO-APPLICABLE-METHOD (T)) #&amp;lt;STANDARD-GENERIC-FUNCTION PLAY (2)&amp;gt; #&amp;lt;CYMBAL {1002B801D3}&amp;gt; #&amp;lt;STICK {1002B82763}&amp;gt;) [fast-method]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you see, the function called depends on the class of the arguments. The object systems &lt;strong&gt;dispatch&lt;/strong&gt; the function calls to the right function for us, depending on the arguments classes. If we call &lt;code&gt;play&lt;/code&gt; with instances that are not know to the object system, an error will be thrown.&lt;/p&gt;
&lt;p&gt;Inheritance is also supported and the equivalent (but more powerful and less error prone) equivalent of Python&apos;s &lt;code&gt;super()&lt;/code&gt; is available via &lt;code&gt;(call-next-method)&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defclass snare-drum () ())
(defclass cymbal () ())

(defclass accessory () ())
(defclass stick (accessory) ())
(defclass brushes (accessory) ())

(defmethod play ((c cymbal) (a accessory))
  &quot;BIIING!&quot;)

(defmethod play ((c cymbal) (b brushes))
  (concatenate &apos;string &quot;SSHHHH!&quot; (call-next-method)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, we define the &lt;code&gt;stick&lt;/code&gt; and &lt;code&gt;brushes&lt;/code&gt; classes as subclass of the &lt;code&gt;accessory&lt;/code&gt; class. The &lt;code&gt;play&lt;/code&gt; method defined will return the sound &lt;em&gt;BIIING!&lt;/em&gt; regardless of the accessory instance that is used to play the cymbal. Except in the case where it&apos;s a &lt;code&gt;brushes&lt;/code&gt; instance; only the most precise method is always called. The &lt;code&gt;(call-next-method)&lt;/code&gt; function is used to call the closest parent method, in this case that would be the method returning _&quot;BIIING!&quot;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;* (play (make-instance &apos;cymbal) (make-instance &apos;stick))
&quot;BIIING!&quot;

* (play (make-instance &apos;cymbal) (make-instance &apos;brushes))
&quot;SSHHHH!BIIING!&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that CLOS is also able to dispatch on object instances themself by using the &lt;code&gt;eql&lt;/code&gt; specializer.&lt;/p&gt;
&lt;p&gt;But if you&apos;re really curious about all features CLOS provides, I suggest you read the &lt;a href=&quot;http://www.aiai.ed.ac.uk/~jeff/clos-guide.html&quot;&gt;brief guide to CLOS by Jeff Dalton&lt;/a&gt; as a starter.&lt;/p&gt;
&lt;h2&gt;Python implementation&lt;/h2&gt;
&lt;p&gt;Python implements a simpler equivalence of this workflow with the &lt;code&gt;singledispatch&lt;/code&gt; function. It will be provided with Python 3.4 as part of the &lt;code&gt;functools&lt;/code&gt; module. Here&apos;s a rough equivalence of the above Lisp program.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import functools

class SnareDrum(object): pass
class Cymbal(object): pass
class Stick(object): pass
class Brushes(object): pass

@functools.singledispatch
def play(instrument, accessory):
    raise NotImplementedError(&quot;Cannot play these&quot;)

@play.register(SnareDrum)
def _(instrument, accessory):
    if isinstance(accessory, Stick):
        return &quot;POC!&quot;
    if isinstance(accessory, Brushes):
        return &quot;SHHHH!&quot;
    raise NotImplementedError(&quot;Cannot play these&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We define our four classes, and a base &lt;code&gt;play&lt;/code&gt; function that raises &lt;code&gt;NotImplementedError&lt;/code&gt;, indicating that by default we don&apos;t know what to do. We can then write specialized version of this function with a first instrument, the &lt;code&gt;SnareDrum&lt;/code&gt;. We then check for the accessory type that we get, and return the appropriate sound or raise &lt;code&gt;NotImplementedError&lt;/code&gt; again if we don&apos;t know what to do with it.&lt;/p&gt;
&lt;p&gt;If we run it, it works as expected:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; play(SnareDrum(), Stick())
&apos;POC!&apos;
&amp;gt;&amp;gt;&amp;gt; play(SnareDrum(), Brushes())
&apos;SHHHH!&apos;
&amp;gt;&amp;gt;&amp;gt; play(Cymbal(), Brushes())
Traceback (most recent call last):
  File &quot;&amp;lt;stdin&amp;gt;&quot;, line 1, in &amp;lt;module&amp;gt;
  File &quot;/home/jd/Source/cpython/Lib/functools.py&quot;, line 562, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File &quot;/home/jd/sd.py&quot;, line 10, in play
    raise NotImplementedError(&quot;Cannot play these&quot;)
NotImplementedError: Cannot play these
&amp;gt;&amp;gt;&amp;gt; play(SnareDrum(), Cymbal())
Traceback (most recent call last):
  File &quot;&amp;lt;stdin&amp;gt;&quot;, line 1, in &amp;lt;module&amp;gt;
  File &quot;/home/jd/Source/cpython/Lib/functools.py&quot;, line 562, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File &quot;/home/jd/sd.py&quot;, line 18, in _
    raise NotImplementedError(&quot;Cannot play these&quot;)
NotImplementedError: Cannot play these
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;singledispatch&lt;/code&gt; module looks through the classes of the first argument passed to the &lt;code&gt;play&lt;/code&gt; function, and calls the right version of it. The first defined version of the &lt;code&gt;play&lt;/code&gt; function is always run for the &lt;code&gt;object&lt;/code&gt; class, so if our instrument is a class that we did not register for, this base function will be called.&lt;/p&gt;
&lt;p&gt;For whose eager to try and use it, the &lt;code&gt;singledispatch&lt;/code&gt; function is &lt;a href=&quot;https://pypi.python.org/pypi/singledispatch/&quot;&gt;provided Python 2.6 to 3.3 through the Python Package Index&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Limitations&lt;/h2&gt;
&lt;p&gt;First, as you noticed in the Lisp version, CLOS provides a multiple dispatcher that can dispatch on the type of &lt;strong&gt;any of the argument&lt;/strong&gt; defined in the method prototype, not only the first one. Unfortunately, Python dispatcher is named &lt;em&gt;singledispatch&lt;/em&gt; for this good reason: it only knows to dispatch on the first argument. Guido van Rossum wrote a short article about the subject that he called &lt;a href=&quot;http://www.artima.com/weblogs/viewpost.jsp?thread=101605&quot;&gt;multimethod&lt;/a&gt; a few years ago.&lt;/p&gt;
&lt;p&gt;Then, there&apos;s no way to call the parent function directly. There&apos;s no equivalent of the &lt;code&gt;(call-next-method)&lt;/code&gt; from Lisp nor the &lt;code&gt;super()&lt;/code&gt; function that allows to do that in Python class system. This means you will have to use various trick to bypass this limitation.&lt;/p&gt;
&lt;p&gt;So while I am really glad that Python is going toward that direction, as it&apos;s a really powerful way to enhance an object system, it really lacks a lot of more advanced features that CLOS provides out of the box.&lt;/p&gt;
&lt;p&gt;Though, improving this could be an interesting challenge. Especially to bring more CLOS power to &lt;a href=&quot;http://hylang.org&quot;&gt;Hy&lt;/a&gt;. :-)&lt;/p&gt;
</content:encoded><category>python</category><category>lisp</category></item><item><title>OpenStack meets Lisp: cl-openstack-client</title><link>https://julien.danjou.info/blog/lisp-and-openstack-with-cl-openstack-client/</link><guid isPermaLink="true">https://julien.danjou.info/blog/lisp-and-openstack-with-cl-openstack-client/</guid><description>Building an OpenStack client library in Common Lisp, exploring what it takes to bring the OpenStack community beyond Python.</description><pubDate>Thu, 04 Jul 2013 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A month ago, a mail hit the &lt;a href=&quot;http://openstack.org&quot;&gt;OpenStack&lt;/a&gt; mailing list entitled &quot;&lt;a href=&quot;https://lists.launchpad.net/openstack/msg24349.html&quot;&gt;The OpenStack Community Welcomes Developers in All Programming Languages&lt;/a&gt;&quot;. You may know that OpenStack is essentially built using Python, and therefore it is the reference language for the client libraries implementations. As a Lisp and OpenStack practitioner, I used this excuse to build a challenge for myself: let&apos;s prove this point by bringing Lisp into OpenStack!&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://julien.danjou.info/content/images/03/cl-openstack-client-1.png&quot; alt=&quot;cl-openstack-client-1&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Welcome &lt;a href=&quot;https://github.com/stackforge/cl-openstack-client&quot;&gt;cl-openstack-client&lt;/a&gt;, the OpenStack client library for &lt;a href=&quot;http://common-lisp.net/&quot;&gt;Common Lisp&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;The project is hosted on the classic OpenStack infrastructure for third party project, &lt;a href=&quot;http://ci.openstack.org/stackforge.html&quot;&gt;StackForge&lt;/a&gt;. It provides the &lt;a href=&quot;https://jenkins.openstack.org/job/gate-cl-openstack-client-run-tests/&quot;&gt;continuous integration system based on Jenkins&lt;/a&gt; and the Gerrit infrastructure used to review contributions.&lt;/p&gt;
&lt;h2&gt;How the tests works&lt;/h2&gt;
&lt;p&gt;OpenStack projects ran a fabulous contribution workflow, &lt;a href=&quot;https://julien.danjou.info/blog/2013/rant-about-github-pull-request-workflow-implementation&quot;&gt;which I already talked about&lt;/a&gt;, based on tools like &lt;a href=&quot;http://gerrit.googlecode.com/&quot;&gt;Gerrit&lt;/a&gt; and &lt;a href=&quot;http://jenkins-ci.org/&quot;&gt;Jenkins&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;OpenStack Python projects are used to run &lt;a href=&quot;https://pypi.python.org/pypi/tox&quot;&gt;tox&lt;/a&gt;, to build a virtual environment and run test inside. We don&apos;t have such thing in Common Lisp as far as I know, so I had to build it myself.&lt;/p&gt;
&lt;p&gt;Fortunately, using &lt;a href=&quot;http://www.quicklisp.org/&quot;&gt;Quicklisp&lt;/a&gt;, the fabulous equivalent of Python&apos;s PyPI, it has been a breeze to set this up. &lt;em&gt;cl-openstack-client&lt;/em&gt; just includes a &lt;a href=&quot;https://github.com/stackforge/cl-openstack-client/blob/master/run-tests.sh&quot;&gt;basic shell script&lt;/a&gt; that does the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Download quicklisp.lisp&lt;/li&gt;
&lt;li&gt;Run a &lt;a href=&quot;https://github.com/stackforge/cl-openstack-client/blob/master/update-deps.lisp&quot;&gt;Lisp program to install the dependencies using Quicklisp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Run a &lt;a href=&quot;https://github.com/stackforge/cl-openstack-client/blob/master/run-tests.lisp&quot;&gt;Lisp program running the test suite&lt;/a&gt; using &lt;a href=&quot;http://common-lisp.net/project/fiveam/&quot;&gt;FiveAM&lt;/a&gt;, that exit with 0 or 1 based on the tests results.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I just run the test using &lt;a href=&quot;http://www.sbcl.org&quot;&gt;SBCL&lt;/a&gt;, though adding more compiler on the table would be a really good plan in the future, and should be straightforward. You can &lt;a href=&quot;https://jenkins.openstack.org/job/gate-cl-openstack-client-run-tests/4/console&quot;&gt;admire a log from a successful test&lt;/a&gt; run done when I proposed a patch via Gerrit, to check what it looks like.&lt;/p&gt;
&lt;h2&gt;Implementation status&lt;/h2&gt;
&lt;p&gt;For the curious, here&apos;s an example of how it works:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;* (require &apos;cl-openstack-client)
* (use-package &apos;cl-keystone-client)
* (defvar k (make-instance &apos;connection-v2 :username &quot;demo&quot; :password &quot;somepassword&quot; :tenant-name &quot;demo&quot; :url &quot;http://devstack:5000&quot;))

K

* (authenticate k)

((:ISSUED--AT . &quot;2013-07-04T05:59:55.454226&quot;)
 (:EXPIRES . &quot;2013-07-05T05:59:55Z&quot;)
 (:ID
  . &quot;wNFQwNzo1OTo1NS40NTQyMthisisaverylongtokenwNFQwNzo1OTo1NS40NTQyM&quot;)
 (:TENANT (:DESCRIPTION) (:ENABLED . T)
  (:ID . &quot;1774fd545df4400380eb2b4f4985b3be&quot;) (:NAME . &quot;demo&quot;)))

* (connection-token-id k)

&quot;wNFQwNzo1OTo1NS40NTQyMthisisaverylongtokenwNFQwNzo1OTo1NS40NTQyM&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Unfortunately, the implementation is far from being complete. It only implements for now the Keystone token retrieval.&lt;/p&gt;
&lt;p&gt;I&apos;ve actually started this project to build an already working starting point. With this, future potential contributors will be able to spend efforts on writing code, and not on setting up the basic continuous integration system or module infrastructure.&lt;/p&gt;
&lt;p&gt;If you wish to help me and contribute, just follow the &lt;a href=&quot;https://wiki.openstack.org/wiki/GerritWorkflow&quot;&gt;OpenStack Gerrit workflow howto&lt;/a&gt; or feel free to come by me and ask any question (I&apos;m hanging out on #lisp on Freenode too).&lt;/p&gt;
&lt;p&gt;See you soon, hopping to bring more Lisp into OpenStack!&lt;/p&gt;
</content:encoded><category>lisp</category><category>openstack</category></item><item><title>Hy, Lisp in Python</title><link>https://julien.danjou.info/blog/lisp-python-hy/</link><guid isPermaLink="true">https://julien.danjou.info/blog/lisp-python-hy/</guid><description>I&apos;ve meant to look at Hy since Paul Tagliamonte started to talk to me about it, but never took a chance until now.</description><pubDate>Wed, 03 Apr 2013 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I&apos;ve meant to look at &lt;a href=&quot;http://github.com/paultag/hy&quot;&gt;Hy&lt;/a&gt; since &lt;a href=&quot;http://blog.pault.ag/&quot;&gt;Paul Tagliamonte&lt;/a&gt; started to talk to me about it, but never took a chance until now. Yesterday, Paul indicated it was a good time for me to start looking at it, so I spent a few hours playing.&lt;/p&gt;
&lt;h2&gt;But what&apos;s Hy?&lt;/h2&gt;
&lt;p&gt;Python is very nice: it has a great community and a wide range of useful libraries. But let&apos;s face it, it misses a great language.&lt;/p&gt;
&lt;p&gt;Hy is an implementation of a &lt;a href=&quot;http://en.wikipedia.org/wiki/Lisp_(programming_language)&quot;&gt;Lisp&lt;/a&gt; on top of Python.&lt;/p&gt;
&lt;p&gt;Technically, Hy is built directly with a custom made parser (for now) which then translates expressions using the &lt;a href=&quot;http://docs.python.org/2/library/ast.html&quot;&gt;Python AST&lt;/a&gt; module to generate code, which is then run by Python. Therefore, it shares the same properties as Python, and is a Lisp-1 (i.e. with a single namespace for symbols and functions).&lt;/p&gt;
&lt;p&gt;If you&apos;re interested to listen Paul talking about Hy during last PyCon US, I recommend watching his lightning talk. As the name implies, it&apos;s only a few minutes long.&lt;/p&gt;
&lt;h2&gt;Does it work?&lt;/h2&gt;
&lt;p&gt;I&apos;ve been cloning the code and played around a bit with Hy. And to my greatest surprise and pleasure, it works quite well. You can imagine writing Python from there easily. Part of the syntax smells like &lt;a href=&quot;http://clojure.org&quot;&gt;Clojure&lt;/a&gt;&apos;s, which looks like a good thing since they&apos;re playing in the same area.&lt;/p&gt;
&lt;p&gt;You can try a &lt;a href=&quot;http://hy.pault.ag/&quot;&gt;Hy REPL&lt;/a&gt; in your Web browser if you want.&lt;/p&gt;
&lt;p&gt;Here&apos;s what some code look like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(import requests)

(setv req (requests.get &quot;http://hy.pault.ag&quot;))
(if (= req.status_code 200)
  (for (kv (.iteritems req.headers))
    (print kv))
  (throw (Exception &quot;Wrong status code&quot;)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This code would ouput:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(&apos;date&apos;, &apos;Wed, 03 Apr 2013 12:09:23 GMT&apos;)
(&apos;connection&apos;, &apos;keep-alive&apos;)
(&apos;content-encoding&apos;, &apos;gzip&apos;)
(&apos;transfer-encoding&apos;, &apos;chunked&apos;)
(&apos;content-type&apos;, &apos;text/html; charset=utf-8&apos;)
(&apos;server&apos;, &apos;nginx/1.2.6&apos;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see, it&apos;s really simple to write Lispy code that really uses Python idioms.&lt;/p&gt;
&lt;p&gt;There&apos;s obviously still a lots of missing features in Hy. The language if far from complete and many parts are moving, but it&apos;s really promising, and Paul&apos;s doing a great job implementing every idea.&lt;/p&gt;
&lt;p&gt;I actually started to hack a bit on Hy, and will try to continue to do so, since I&apos;m really eager to learn a bit more about both Lisp and Python internals in the process. I&apos;ve already send a few patches on small bugs I&apos;ve encountered, and proposed a few ideas. It&apos;s really exciting to be able to influence early a language design that I&apos;ll love to use! Being a recent fan of Common Lisp, I tend to grab the good stuff from it to add them into Hy.&lt;/p&gt;
</content:encoded><category>python</category><category>lisp</category><category>talks</category></item><item><title>Overriding cl-json object encoding</title><link>https://julien.danjou.info/blog/cl-postmodern-dao-json/</link><guid isPermaLink="true">https://julien.danjou.info/blog/cl-postmodern-dao-json/</guid><description>CL-JSON provides an encoder for Lisp data structures and objects to JSON format. Unfortunately, in some case, its default encoding mechanism for CLOS objects isn&apos;t exactly doing the right thing.</description><pubDate>Fri, 11 Jan 2013 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;a href=&quot;http://common-lisp.net/project/cl-json/&quot;&gt;CL-JSON&lt;/a&gt; provides an encoder for Lisp data structures and objects to JSON format. Unfortunately, in some case, its default encoding mechanism for CLOS objects isn&apos;t exactly doing the right thing. I&apos;ll show you how Common Lisp makes it easy to change that.&lt;/p&gt;
&lt;h2&gt;Identifying the problem&lt;/h2&gt;
&lt;h3&gt;CL-JSON &amp;amp; CLOS&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;CL-JSON&lt;/em&gt; mechanism encoding CLOS object is really neat. Let&apos;s see how it works for a simple case:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defclass kitten ()
  ((tail :initarg :tail)))

(json:encode-json-to-string (make-instance &apos;kitten :tail &apos;black))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;will produce:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{&quot;tail&quot;:&quot;black&quot;}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Still using CL-JSON, we can also decode the JSON object to a CLOS object:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(slot-value
 (json:with-decoder-simple-clos-semantics
   (json:decode-json-from-string &quot;{\&quot;tail\&quot;:\&quot;black\&quot;}&quot;))
 :tail)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That code will return &lt;em&gt;&quot;black&quot;&lt;/em&gt;. Note that it&apos;s also possible to specify which class should be used when decoding objects, but that&apos;s beyond the purpose of this article.&lt;/p&gt;
&lt;h3&gt;Postmodern&lt;/h3&gt;
&lt;p&gt;Now, let&apos;s introduce &lt;a href=&quot;http://marijnhaverbeke.nl/postmodern/&quot;&gt;Postmodern&lt;/a&gt;, a wonderful Common Lisp system providing access to the wonderful &lt;a href=&quot;http://postgresql.org&quot;&gt;PostgreSQL&lt;/a&gt; database. It also provides a simple system to map rows in a database to CLOS classes, called DAO for &lt;em&gt;Database access objects&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;With this, we can easily store our &lt;em&gt;kitten&lt;/em&gt; into a table.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defclass kitten ()
  ((tail :initarg :tail))
  (:metaclass postmodern:dao-class))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we try to encode this to JSON, it will produce the exact same result seen previously.&lt;/p&gt;
&lt;p&gt;The problem is what happens when one of our column has a &lt;em&gt;NULL&lt;/em&gt; value. Postmodern encodes this using the &lt;em&gt;:null&lt;/em&gt; symbol.&lt;/p&gt;
&lt;p&gt;So this code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defclass kitten ()
  ((tail :initarg :tail :col-type (or s-sql:db-null text)))
  (:metaclass postmodern:dao-class))

(postmodern:deftable kitten
  (postmodern:!dao-def))

(postmodern:connect-toplevel …)

(postmodern:create-table &apos;kitten)

(json:encode-json-to-string
  (postmodern:make-dao &apos;kitten))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;will return:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;{&quot;tail&quot;:&quot;null&quot;}&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Fail! The fact that the column is &lt;em&gt;NULL&lt;/em&gt; is represented by the &lt;em&gt;:null&lt;/em&gt; symbol. And CL-JSON encodes all symbols as string.&lt;/p&gt;
&lt;p&gt;This is not at all what we want here!&lt;/p&gt;
&lt;h2&gt;Overriding encode-json&lt;/h2&gt;
&lt;p&gt;CL-JSON provides and uses the &lt;em&gt;encode-json&lt;/em&gt; method to encode all kind of object. It is defined as a &lt;em&gt;generic function&lt;/em&gt;, and a lot of different methods are implemented to handle the different standard Common Lisp types. The one used for &lt;em&gt;standard-object&lt;/em&gt; is defined liked that:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmethod encode-json ((o standard-object)
                        &amp;amp;optional (stream *json-output*))
  &quot;Write the JSON representation (Object) of the CLOS object O to
STREAM (or to *JSON-OUTPUT*).&quot;
  (with-object (stream)
    (map-slots (stream-object-member-encoder stream) o)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All we need to do here, is to create a new method for our &lt;em&gt;kitten&lt;/em&gt; objects, that handles correctly the &lt;em&gt;:null&lt;/em&gt; case.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defclass kitten ()
  ((tail :initarg :tail :col-type (or s-sql:db-null text)))
  (:metaclass postmodern:dao-class))

(export &apos;kitten)

;; Switch package just to define the new method
(in-package :json)
(defmethod encode-json ((o cl-user:kitten)
                        &amp;amp;optional (stream json:*json-output*))
  &quot;Write the JSON representation (Object) of the postmodern DAO CLOS object
O to STREAM (or to *JSON-OUTPUT*).&quot;
  (with-object (stream)
    (map-slots (lambda (key value)
                 (as-object-member (key stream)
                   (encode-json (if (eq value :null) nil value) stream)))
               o)))

;; Go back into our package
(in-package :cl-user)

(postmodern:deftable kitten
  (postmodern:!dao-def))

(postmodern:connect-toplevel …)

(postmodern:create-table &apos;kitten)

(json:encode-json-to-string
  (postmodern:make-dao &apos;kitten))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With that new method, as soon as we encounter a &lt;em&gt;:null&lt;/em&gt; symbol as a value for an object&apos;s slot, we replace it by &lt;em&gt;nil&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Now if we try to encode another &lt;em&gt;kitten&lt;/em&gt;, we&apos;ll get:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{&quot;tail&quot;:null}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which is far better for our JavaScript data consumers!&lt;/p&gt;
&lt;p&gt;In the end, I think that this kind of trick is feasible that easily because of the way CLOS provides its generic method implementation.&lt;br /&gt;
The fact that methods don&apos;t belong to any class makes the extension of every program, library and class so much easier. Doing this in another language like Java would likely by impossible, and in Python it would unlikely be as clean as it is done in Common Lisp.&lt;/p&gt;
&lt;p&gt;The ability to teach &lt;em&gt;any&lt;/em&gt; library about how it should handle your class just by defining a new method is really handy!&lt;/p&gt;
</content:encoded><category>lisp</category><category>web</category><category>databases</category></item><item><title>Integrating cl-irc and cl-async</title><link>https://julien.danjou.info/blog/cl-irc-async/</link><guid isPermaLink="true">https://julien.danjou.info/blog/cl-irc-async/</guid><description>Recently, I&apos;ve started programming in Common Lisp.</description><pubDate>Fri, 04 Jan 2013 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Recently, I&apos;ve started programming in &lt;a href=&quot;http://common-lisp.net&quot;&gt;Common Lisp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;My idea here is to use &lt;a href=&quot;http://www.cliki.net/cl-irc&quot;&gt;cl-irc&lt;/a&gt;, an IRC library into an &lt;a href=&quot;http://en.wikipedia.org/wiki/Event_loop&quot;&gt;event loop&lt;/a&gt;. This can be really useful, for example to trigger action based on time, using timers.&lt;/p&gt;
&lt;h2&gt;Creating a connection&lt;/h2&gt;
&lt;p&gt;The first step is to create a basic &lt;em&gt;cl-irc:connection&lt;/em&gt; object on our own. This can be achieved easily with this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require :cl-irc)

(defun connect (server)
  (cl-irc:make-connection :connection-type &apos;cl-irc:connection
                                              :client-stream t
                                              :network-stream ?
                                              :server-name server))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will return a &lt;em&gt;cl-irc:connection&lt;/em&gt; object, logging to stdout (&lt;em&gt;:client-stream t&lt;/em&gt;) and having the server name &lt;em&gt;server&lt;/em&gt;. Note that the server name could be any string.&lt;/p&gt;
&lt;p&gt;You probably noticed the &lt;em&gt;?&lt;/em&gt; I used as :network-stream value. This is not a real and working value: this should be a stream established to the IRC server you want to chat with. This is where we&apos;ll need to use &lt;a href=&quot;http://orthecreedence.github.com/cl-async/tcp#tcp-connect&quot;&gt;&lt;code&gt;cl-async:tcp connect&lt;/code&gt;&lt;/a&gt; to establish a TCP connection.&lt;/p&gt;
&lt;p&gt;As you can read in this function&apos;s documentation, all we need to pass is the server address, two callbacks for read and general events, and the &lt;em&gt;:stream&lt;/em&gt; option to get a stream rather than a socket.&lt;/p&gt;
&lt;p&gt;So you would do something like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require :cl-irc)
(require :cl-async)

(defun connection-socket-read (socket stream)
  (format t &quot;We should read the IRC message from ~a ~%&quot; stream))

(defun connection-socket-event (ev)
  (format t &quot;Socket event: ~a~%&quot; ev))

(defun connect (server &amp;amp;optional (port 6667))
  (cl-irc:make-connection :connection-type &apos;cl-irc:connection
                          :client-stream t
                          :network-stream (as:tcp-connect server port
                                                          #&apos;connection-socket-read
                                                          #&apos;connection-socket-event
                                                          :stream t)
                          :server-name server))

(as:start-event-loop (lambda () (connect &quot;irc.oftc.net&quot;)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you run this program, it will connect to the OFTC IRC server, and then notice you each time the server is sending you a message.&lt;/p&gt;
&lt;p&gt;Therefore our problem here is how we you treat the message read from the stream in &lt;code&gt;connection-socket-read&lt;/code&gt; and handle them in the name of our connection object you used? We can&apos;t link both together at this point.&lt;/p&gt;
&lt;p&gt;We can&apos;t build a closure, because as the time we use &lt;em&gt;as:tcp-connect&lt;/em&gt; we don&apos;t have the &lt;em&gt;cl-irc:connection&lt;/em&gt; instance. Also we can&apos;t change easily the &lt;em&gt;read-cb&lt;/em&gt; parameter of our &lt;em&gt;network-stream&lt;/em&gt; established by &lt;em&gt;as:tcp-connect&lt;/em&gt;, simply because &lt;em&gt;cl-async&lt;/em&gt; doesn&apos;t use to do allow that.&lt;/p&gt;
&lt;h2&gt;Building a closure&lt;/h2&gt;
&lt;p&gt;So one solution here is to hack &lt;em&gt;cl-irc:make-connection&lt;/em&gt; so we can build an &lt;em&gt;cl-irc:connection&lt;/em&gt; instance without providing in advance the &lt;em&gt;network-stream&lt;/em&gt;, allowing us to build a closure including the &lt;em&gt;cl-irc:connection&lt;/em&gt; to read event for. This is what we&apos;re going to do in the &lt;code&gt;connect&lt;/code&gt; function.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require :cl-irc)
(require :cl-async)
(require :flexi-streams)

(defun connection-socket-read (connection)
  (loop for message = (cl-irc::read-irc-message connection)
        while message
        do (cl-irc:irc-message-event connection message)))

(defun connection-socket-event (ev)
  (format t &quot;Socket event: ~a~%&quot; ev))

(defun connect (server port nickname
                &amp;amp;key
                  (username nil)
                  (realname nil)
                  (password nil))
  ;; Build an instance of cl-irc:connection, without any network/output stream
  (let* ((connection (make-instance &apos;cl-irc:connection
                                    :user username
                                    :password password
                                    :server-name server
                                    :server-port port
                                    :client-stream t))
         ;; Use as:tcp-connect to build our network stream, and build a
         ;; closure calling `connection-socket-read&apos; with our `connection&apos;
         ;; as arguments
         (network-stream (as:tcp-connect server port
                                         (lambda (socket stream)
                                           (declare (ignore socket stream))
                                           (connection-socket-read connection))
                                         #&apos;connection-socket-event
                                         :stream t)))
    ;; Set the network stream on the connection
    (setf (cl-irc:network-stream connection) network-stream)
    ;; Set the output stream on the connection
    (setf (cl-irc:output-stream connection)
         ;; This is grabbed from cl-irc:make-connection
          (flexi-streams:make-flexi-stream
           network-stream
           :element-type &apos;character
           :external-format &apos;(:utf8 :eol-style :crlf)))

    ;; Now handle the IRC protocol authentication pass
    (unless (null password)
      (cl-irc:pass connection password))
    (cl-irc:nick connection nickname)
    (cl-irc:user- connection (or username nickname) 0 (or realname nickname))
    connection))

(as:start-event-loop (lambda () (connect &quot;irc.oftc.net&quot; 6667 &quot;jd-blog&quot;)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And here we are! If we run this, we&apos;re now using an event loop to run &lt;code&gt;cl-irc&lt;/code&gt;. Each time the socket has something to read, the function &lt;code&gt;connection-socket-read&lt;/code&gt; will be called on the non-blocking mode socket. If there&apos;s no message to be read, then the function will exit and the loop will continue to run.&lt;/p&gt;
&lt;h2&gt;Using timers&lt;/h2&gt;
&lt;p&gt;You can now modify the last line with this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defun say-hello (connection)
  (cl-irc:privmsg connection &quot;#jd-blog&quot; &quot;Hey I read your blog!&quot;)
  (as:delay (lambda () (say-hello connection)) :time 60))

(as:start-event-loop (lambda ()
                       (let ((connection (connect &quot;irc.oftc.net&quot; 6667 &quot;jd-blog&quot;)))
                         (cl-irc:join connection &quot;#jd-blog&quot;)
                         (say-hello connection))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will connect to the IRC server, join a channel and then say the same sentence every minute.&lt;/p&gt;
&lt;p&gt;Challenge accomplished!&lt;/p&gt;
&lt;p&gt;And I&apos;d like to thank &lt;a href=&quot;http://blog.killtheradio.net/&quot;&gt;Andrew Lyon&lt;/a&gt;, the author of &lt;a href=&quot;https://github.com/orthecreedence/cl-async&quot;&gt;cl-async&lt;/a&gt;, who has been incredibly helpful with my recent experimentations in this area.&lt;/p&gt;
</content:encoded><category>lisp</category></item></channel></rss>