<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/assets/atom.xsl" type="text/xsl"?><feed
	xmlns="http://www.w3.org/2005/Atom"
	xmlns:thr="http://purl.org/syndication/thread/1.0"
	xml:lang="en-US"
	><title>Sacha Chua - tag - clojure</title>
	<subtitle>Emacs, sketches, and life</subtitle>
	<link rel="self" type="application/atom+xml" href="https://sachachua.com/blog/tag/clojure/feed/atom/index.xml" />
  <link rel="alternate" type="text/html" href="https://sachachua.com/blog/tag/clojure" />
  <id>https://sachachua.com/blog/tag/clojure/feed/atom/index.xml</id>
  <generator uri="https://11ty.dev">11ty</generator>
	<updated>2014-05-29T01:50:23Z</updated>
<entry>
		<title type="html">Playing around with Clojure, Cider, and 4Clojure</title>
		<link rel="alternate" type="text/html" href="https://sachachua.com/blog/2014/05/playing-around-clojure-cider-4clojure/"/>
		<author><name><![CDATA[Sacha Chua]]></name></author>
		<updated>2014-05-29T05:52:23Z</updated>
    <published>2014-05-29T01:50:23Z</published>
    <category term="emacs" />
<category term="org" />
		<id>https://sachachua.com/blog/?p=27258</id>
		<content type="html"><![CDATA[<p><a href="http://4clojure.com">4Clojure</a> has a lovely series of exercises to help you practice Clojure. I don't know much Clojure yet. I've basically been taking what I know of Emacs Lisp and trying to cram it into Clojure syntax. (compose is pretty cool!) I should probably read through a Clojure tutorial and some kind of syntax reference. (<a href="http://hyperpolyglot.org/lisp">Hyperpolyglot</a> is neat!) But hey, I've gotten through 21 problems so far.</p>
<p>Tom Marble and I were chatting about Clojure, Emacs, and Org Babel. As it turns out, there are lots of ways to interact with 4clojure problems from within Emacs. Tom told me about the <a href="https://github.com/losingkeys/4clojure.el">4clojure package</a> by Joshua Hoff, which is probably slightly improved with the following code:</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span class="org-keyword">require</span> '<span class="org-constant">clojure-mode</span>)
(<span class="org-keyword">defun</span> <span class="org-function-name">my/4clojure-check-and-proceed</span> ()
  <span class="org-doc">"Check the answer and show the next question if it worked."</span>
  (interactive)
  (<span class="org-keyword">let</span> ((result (4clojure-check-answers)))
    (<span class="org-keyword">unless</span> (string-match <span class="org-string">"failed."</span> result)
       (4clojure-next-question))))
(define-key clojure-mode-map (kbd <span class="org-string">"C-c C-c"</span>) 'my/4clojure-check-and-proceed)
</pre>
</div>
<p>That one doesn't track your progress on the website, though, so you'll still want to copy and paste the solution yourself.</p>
<p>I like working within Org Mode so that I can easily take notes along the way. Here are the notes I took while figuring out how to get Clojure and Org to work together. <a href="http://www.braveclojure.com/basic-emacs/">http://www.braveclojure.com/basic-emacs/</a> is nice. <a href="http://bzg.fr/emacs-org-babel-overtone-intro.html">http://bzg.fr/emacs-org-babel-overtone-intro.html</a> has a good introduction. Here's what I used from those:</p>
<div id="outline-container-sec-1" class="outline-1">
<h3 id="sec-1">Install Java (at least version 6), Clojure and Leiningen.</h3>
<div id="text-1" class="outline-text-1">
<ul class="org-ul">
<li><a href="http://clojure.org">http://clojure.org</a></li>
<li><a href="http://leiningen.org/#install">http://leiningen.org/#install</a></li>
</ul>
</div>
</div>
<div id="outline-container-sec-2" class="outline-1">
<h3 id="sec-2">Install the clojure-mode and cider Emacs packages</h3>
<div id="text-2" class="outline-text-1">
<p>Evaluate this by moving the point to the <code>#+begin_src</code> line and running <code>C-c C-c</code></p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(add-to-list 'package-archives '(<span class="org-string">"melpa"</span> . <span class="org-string">"http://melpa.milkbox.net/packages/"</span>) t)
(package-refresh-contents)
(package-install 'clojure-mode)
(package-install 'cider)
</pre>
</div>
<p>And then evaluate this afterwards:</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(add-to-list 'org-babel-load-languages '(emacs-lisp . t))
(add-to-list 'org-babel-load-languages '(clojure . t))
(org-babel-do-load-languages 'org-babel-load-languages org-babel-load-languages)
(setq nrepl-hide-special-buffers t
      cider-repl-pop-to-buffer-on-connect nil
      cider-popup-stacktraces nil
      cider-repl-popup-stacktraces t)
(cider-jack-in)
</pre>
</div>
</div>
</div>
<p>That should let you evaluate this:</p>
<div class="org-src-container">
<pre class="src src-clojure">(<span class="org-builtin">list?</span> '(1 2 3 4))
</pre>
</div>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br>
And that let me do stuff like this for <a href="http://www.4clojure.com/problem/27">#27: Palindrome Detector</a>:</p>
<div class="org-src-container">
<pre class="src src-clojure">(<span class="org-keyword">defn</span> <span class="org-function-name">__</span> [x] (<span class="org-builtin">=</span> (<span class="org-builtin">seq</span> x) (<span class="org-builtin">reverse</span> x)))
(<span class="org-builtin">list</span>
  (<span class="org-builtin">false?</span> (__ '(1 2 3 4 5)))
  (<span class="org-builtin">true?</span> (__ <span class="org-string">"racecar"</span>))
  (<span class="org-builtin">true?</span> (__ [<span class="org-constant">:foo</span> <span class="org-constant">:bar</span> <span class="org-constant">:foo</span>]))
  (<span class="org-builtin">true?</span> (__ '(1 1 3 3 1 1)))
  (<span class="org-builtin">false?</span> (__ '(<span class="org-constant">:a</span> <span class="org-constant">:b</span> <span class="org-constant">:c</span>))))
</pre>
</div>
<table border="2" frame="hsides" rules="groups" cellspacing="0" cellpadding="6">
<colgroup>
<col class="left">
<col class="left">
<col class="left">
<col class="left">
<col class="left"> </colgroup>
<tbody>
<tr>
<td class="left">true</td>
<td class="left">true</td>
<td class="left">true</td>
<td class="left">true</td>
<td class="left">true</td>
</tr>
</tbody>
</table>
<p>If all the results are true, then I've passed. Yay! In the web interface, __ is where your answers go. Fortunately, it's also a valid Lisp name, so I can defn a function to replace it when testing locally. The proper answer would probably be something like <code>(fn [x] (= (seq x) (reverse x)))</code> when submitted through the web interface, which is close enough.</p>
<p>it would be great to have something like 4clojure for Emacs Lisp &#8211; a site where you can practise solving small, well-defined problems. =) Has someone already written one?</p>
<p>You can <a href="mailto:sacha@sachachua.com?subject=Comment%20on%20https%3A%2F%2Fsachachua.com%2Fblog%2F2014%2F05%2Fplaying-around-clojure-cider-4clojure%2F&body=Name%20you%20want%20to%20be%20credited%20by%20(if%20any)%3A%20%0AMessage%3A%20%0ACan%20I%20share%20your%20comment%20so%20other%20people%20can%20learn%20from%20it%3F%20Yes%2FNo%0A">e-mail me at sacha@sachachua.com</a>.</p>]]></content>
		</entry>
</feed>