<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<rss version="2.0"><channel><title>Of Herbs and Stewed Rabbit</title><link>http://benedekfazekas.github.io</link><description>Mostly about lisps, functional programming, emacs and sometimes perhaps dragons or just stewed rabbit</description><item><title>Clean up a messy namespace with cider and clj-refactor</title><link>http://benedekfazekas.github.io/2015/12/06/clean-up-namespace-with-cider-cljrefactor/</link><pubDate>Sun, 6 Dec 2015 00:00:00 +0000</pubDate><description>&lt;p&gt;On &lt;a href=&quot;https://skillsmatter.com/conferences/6861-clojure-exchange-2015&quot;&gt;&lt;strong&gt;clojureX&lt;/strong&gt;&lt;/a&gt; this year we almost had a panel discussion on editors; it was cancelled at the last minute unfortunately. So I thought why not publish the longer version of the screencast I prepared demonstrating how to use some &lt;a href=&quot;https://github.com/clojure-emacs/clj-refactor.el/wiki&quot;&gt;&lt;strong&gt;clj-refactor&lt;/strong&gt;&lt;/a&gt; features in Emacs to clean up a messy namespace.&lt;/p&gt;&lt;p&gt;I highly recommend &lt;a href=&quot;http://www.parens-of-the-dead.com&quot;&gt;&lt;strong&gt;Parens of the dead&lt;/strong&gt;&lt;/a&gt; to everybody by the way. The series is real joy to watch and demonstrates many things, among them is how to use Emacs with &lt;a href=&quot;https://github.com/clojure-emacs/cider&quot;&gt;&lt;strong&gt;cider&lt;/strong&gt;&lt;/a&gt; and &lt;a href=&quot;https://github.com/clojure-emacs/clj-refactor.el/wiki&quot;&gt;&lt;strong&gt;clj-refactor&lt;/strong&gt;&lt;/a&gt; to work with clojure code in a very effective way. On the other hand Magnar uses a certain bunch of clj-refactor features possibly because he builds his project up from scratch. In my brief demo/screencast I'd like to rather demo some features which are very helpful in an other typical refactoring scenario: when you want to clean up an already existing (legacy?!) namespace.&lt;/p&gt;&lt;p&gt;Also both cider and clj-refactor has so many features now that discoverability is an issue. This screencast and the &lt;a href=&quot;https://github.com/clojure-emacs/clj-refactor.el/wiki/hydra-code&quot;&gt;&lt;strong&gt;demonstrated use&lt;/strong&gt;&lt;/a&gt; of &lt;a href=&quot;https://github.com/abo-abo/hydra&quot;&gt;&lt;strong&gt;hydras&lt;/strong&gt;&lt;/a&gt; tries to offer some remedies for that too.&lt;/p&gt;&lt;p&gt;&lt;iframe width=&quot;630&quot; height=&quot;473&quot; src=&quot;https://www.youtube.com/embed/mOSUE3czp9w?rel=0&amp;vq=large&quot; frameborder=&quot;0&quot; allowfullscreen&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;h4&gt;&lt;code&gt;ml&lt;/code&gt; move to let&lt;/h4&gt;&lt;p&gt;Move the current form to the closest &lt;code&gt;let&lt;/code&gt;. More details &lt;a href=&quot;https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-move-to-let&quot;&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt;. This is an elisp only feature meaning that you don't even need a running REPL to use it.&lt;/p&gt;&lt;h4&gt;&lt;code&gt;rs&lt;/code&gt; rename (local) symbol&lt;/h4&gt;&lt;p&gt;Rename all occurrences of a local symbol. More details &lt;a href=&quot;https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-rename-symbol#or-a-locally-defined-symbol&quot;&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt;. This is a feature which uses the analyzer to figure out the location of the local symbol therefore you need a running REPL with &lt;a href=&quot;https://github.com/clojure-emacs/refactor-nrepl&quot;&gt;&lt;strong&gt;refactor-nrepl&lt;/strong&gt;&lt;/a&gt; middleware added to it.&lt;/p&gt;&lt;h4&gt;&lt;code&gt;is&lt;/code&gt; inline symbol&lt;/h4&gt;&lt;p&gt;Inline all occurrences of the symbol at point. Can be used both globally and locally. See screencast gif &lt;a href=&quot;https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-inline-symbol&quot;&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt;. Again this is feature needing the analyzer to figure out the location of the occurrences of the symbol you want to inline.&lt;/p&gt;&lt;h4&gt;&lt;code&gt;pf&lt;/code&gt; promote function&lt;/h4&gt;&lt;p&gt;Can promote &lt;code&gt;#(foo %)&lt;/code&gt; style anonymus function to &lt;code&gt;(fn [arg1] (foo arg1))&lt;/code&gt; or all the way to first level &lt;code&gt;defn&lt;/code&gt; or a &lt;code&gt;(fn [arg1] (foo arg1))&lt;/code&gt; style anonymus function into a first level defn. See details &lt;a href=&quot;https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-promote-function#promote-anonymous-function-to-defn&quot;&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt;. Again this needs the analyzer.&lt;/p&gt;&lt;h4&gt;&lt;code&gt;ef&lt;/code&gt; extract function&lt;/h4&gt;&lt;p&gt;Extract the form at point, or the nearest enclosing form, into a toplevel defn. Newly created function is either private or public, depending on &lt;code&gt;cljr-favor-private-functions&lt;/code&gt;. See a screencast &lt;a href=&quot;https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-extract-function&quot;&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt;. This needs the analyzer as well to figure out which locally bound vars the s-expression uses that need to be turned into arguments in the new function.&lt;/p&gt;</description></item><item><title>Java8 stream API vs Clojure fizzbuzzed</title><link>http://benedekfazekas.github.io/2015/02/06/java8-fizzbuzzed/</link><pubDate>Fri, 6 Feb 2015 00:00:00 +0000</pubDate><description>&lt;p&gt;Lately I came across a vanilla java solution for the good old &lt;a href=&quot;http://en.wikipedia.org/wiki/Fizz_buzz&quot;&gt;&lt;strong&gt;fizzbuzz&lt;/strong&gt;&lt;/a&gt; problem. This gave me the idea to try (finally) the java8 stream API: fizzbuzz seemed a small but fitting problem. And while at it why not code up a solution in clojure too so I can compare the two. This post documents the one afternoon adventure.&lt;/p&gt;&lt;p&gt;Let's also spice fizzbuzz up a bit with adding two extra requirements:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;- if the number in question has 3 in it we print &amp;#39;bingo&amp;#39;
- at the end we print a kind of statistics where we show the number of occurrences of &amp;#39;Fizz&amp;#39;, &amp;#39;Buzz&amp;#39;, &amp;#39;FizzBuzz&amp;#39;, &amp;#39;Bingo&amp;#39; and not changed numbers
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Please note that although the first one is just an other condition it is also an other type of condition not related to any modulo of or any arithmetic operation with the given number. The second is interesting too because the solution needs say something about the transformed list. A bit of state might need to be maintened while doing the transformation, right? ;)&lt;/p&gt;&lt;p&gt;So first of all as a starting point let's see the vanilla solution in java:&lt;/p&gt;
&lt;!--?prettify lang=java linenums=true --&gt;
&lt;pre&gt;&lt;code&gt;public class FizzBuzz {

    public void fizzBuzz(int rangeStart, int rangeEnd, PrintStream out) {

        Map&amp;lt;String, AtomicInteger&amp;gt; statMap = new HashMap&amp;lt;String, AtomicInteger&amp;gt;();

        List&amp;lt;String&amp;gt; transformedNumbers = new ArrayList&amp;lt;String&amp;gt;();
        for(int i = rangeStart; i &amp;lt;= rangeEnd; i++){
            String elem = &amp;quot;Integer&amp;quot;;
            String iAsString = String.valueOf(i);
            if (iAsString.contains(&amp;quot;3&amp;quot;)) {
                elem = &amp;quot;Bingo&amp;quot;;
            } else if (i % 15 == 0){
                elem = &amp;quot;FizzBuzz&amp;quot;;
            } else if(i % 3 == 0){
                elem = &amp;quot;Fizz&amp;quot;;
            } else if(i % 5 == 0){
                elem = &amp;quot;Buzz&amp;quot;;
            }
            transformedNumbers.add((&amp;quot;Integer&amp;quot;.equals(elem))? iAsString : elem);
            statMap.computeIfAbsent(elem, k -&amp;gt; new AtomicInteger(0));
            statMap.get(elem).getAndIncrement();
        }

        out.println(transformedNumbers);
        out.println();
        out.println(statMap);
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Pretty naive approach but hey-ho we just started. Let's see the (kinda) equivalent in clojure:&lt;/p&gt;
&lt;!--?prettify lang=clojure linenums=true --&gt;
&lt;pre&gt;&lt;code&gt;(defn fizzbuzz [start end]
  (letfn [(calc-stats [cmap elem] (let [key (if (string? elem) (keyword elem) :integer)]
                                    (update-in cmap [key] #(if % (inc %) 1))))]
    (let [fizzbuzzed (-&amp;gt;&amp;gt; end
                          inc
                          (range start)
                          (map #(cond
                                 (.contains (str %) &amp;quot;3&amp;quot;) &amp;quot;bingo&amp;quot;
                                 (= (mod % 15) 0) &amp;quot;fizzbuzz&amp;quot;
                                 (= (mod % 3) 0) &amp;quot;fizz&amp;quot;
                                 (= (mod % 5) 0) &amp;quot;buzz&amp;quot;
                                 :default %)))
          stats (reduce calc-stats {} fizzbuzzed)]
      [fizzbuzzed stats])))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Just to point out the obvious differences in clojure you don't need to iterate through the list when fizzbuzzing it: just give a recipe in the anonymous function (line 7.) how to fizzbuzz a given element. In the java code you iterate through all the elements and update a bit of local state as a side effect to get the new list and the statistics. The usage of a REPL is obvious too when you look at the clojure code. You don't really need to worry about how to print your results while developing, playing with the code: the REPL will take care of it. My last point is types: in java you have to convert the original int to string while clojure being a dynamic language you are fine with a list with partly string and partly number elements.&lt;/p&gt;&lt;p&gt;In clojure after you had done the fizzbuzzing you can easily reduce the new list into the stats. That is a bit awkward though, you might want a solution which does the stats and the fizzbuzzing in one step. Also using the &lt;code&gt;cond&lt;/code&gt; is equivalent with the java &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else-if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; construct but not really nice either. So bit of work on these points:&lt;/p&gt;
&lt;!--?prettify lang=clojure linenums=true --&gt;
&lt;pre&gt;&lt;code&gt;(defn- fizzbuzz&amp;amp;stats [list&amp;amp;stats n]
  (let [mod-pred (fn [n word] #(when (= (mod % n) 0) word))
        safe-inc (fn [n] (if n (inc n) 1))]
    (if-let [s (some #(% n) [#(when (.contains (str %) &amp;quot;3&amp;quot;) &amp;quot;bingo&amp;quot;)
                             (mod-pred 15 &amp;quot;fizzbuzz&amp;quot;)
                             (mod-pred 3 &amp;quot;fizz&amp;quot;)
                             (mod-pred 5 &amp;quot;buzz&amp;quot;)])]
      (-&amp;gt; list&amp;amp;stats
          (assoc :list (concat (:list list&amp;amp;stats) (seq [s])))
          (update-in [(keyword s)] safe-inc))
      (-&amp;gt; list&amp;amp;stats
          (assoc :list (concat (:list list&amp;amp;stats) (seq [n])))
          (update-in [:integer] safe-inc)))))

(defn fizzbuzz-enhanced [start end]
  (-&amp;gt;&amp;gt; end
       inc
       (range start)
       (reduce fizzbuzz&amp;amp;stats {})))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The fizzbuzzed list and the stats is constructed in one reduce. Also eliminated the &lt;code&gt;cond&lt;/code&gt; and use &lt;code&gt;some&lt;/code&gt; with a vector of predicates: the first which applies wins and returns the fizzbuzz string for the element. If there is no winner stick with the original number. And of course either case add the element after fizzbuzzing to the result list inside the result hash. Reduce takes care of passing a fresh version of it to the next round.&lt;/p&gt;&lt;p&gt;Now it is time to get to the cake: code up something similar using the java8 stream API. I am sure there are other nice, very likely better solutions, don't hesitate to ping them to me on twitter ;)&lt;/p&gt;
&lt;!--?prettify lang=java linenums=true --&gt;
&lt;pre&gt;&lt;code&gt;public void fizzBuzz(int rangeStart, int rangeEnd, PrintStream out) {

    String[] fizzbuzzed = rangeClosed(rangeStart, rangeEnd).mapToObj(FizzBuzz::intToFizzBuzz).toArray(String[]::new);
    System.out.println(&amp;quot;start ---&amp;quot;);
    System.out.println(Arrays.toString(fizzbuzzed));
    System.out.println(&amp;quot;end   ---&amp;quot;);
    System.out.println(&amp;quot;stats start ---&amp;quot;);
    System.out.print(Arrays.stream(fizzbuzzed).collect(Stats::new, Stats::accept, Stats::combine));
    System.out.println(&amp;quot;stats end   ---&amp;quot;);
}

class Stats implements Consumer&amp;lt;String&amp;gt; {

    private int lucky, fizz, buzz, fizzbuzz, integer = 0;

    public void accept(String elem) {
        if (&amp;quot;lucky&amp;quot;.equals(elem)) lucky++;
        else if (&amp;quot;fizz&amp;quot;.equals(elem)) fizz++;
        else if (&amp;quot;buzz&amp;quot;.equals(elem)) buzz++;
        else if (&amp;quot;fizzbuzz&amp;quot;.equals(elem))
            fizzbuzz++;
        else
            integer++;
    }

    public void combine(Stats other) {
        lucky += other.lucky;
        fizz += other.fizz;
        buzz += other.buzz;
        fizzbuzz += other.fizzbuzz;
        integer += other.integer;
    }

    public String toString() {
        return &amp;quot;lucky: &amp;quot; + lucky +
            &amp;quot;\n fizz: &amp;quot; + fizz +
            &amp;quot;\n buzz: &amp;quot; + buzz +
            &amp;quot;\n fizzbuzz: &amp;quot; + fizzbuzz +
            &amp;quot;\n integer: &amp;quot; + integer;
    }
}

public static String intToFizzBuzz(int nr) {
    if (String.valueOf(nr).contains(&amp;quot;3&amp;quot;)) return &amp;quot;lucky&amp;quot;;
    if (nr &amp;gt; 0 &amp;amp;&amp;amp; nr % (3 * 5) == 0) return &amp;quot;fizzbuzz&amp;quot;;
    if (nr &amp;gt; 0 &amp;amp;&amp;amp; nr % 3 == 0) return &amp;quot;fizz&amp;quot;;
    if (nr &amp;gt; 0 &amp;amp;&amp;amp; nr % 5 == 0) return &amp;quot;buzz&amp;quot;;
    return String.valueOf(nr);
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Couple of notes. This solution is rather equivalent of the first iteration of clojure as it first creates the fizzbuzzed list and the reduces/consumes it to calculate the statistics. That is a bit awkwardly though as a stream which you run a terminal operation against you can't consume again. Although this makes sense it also means that the stream needs to be recreated for the reduction into the statistics because reducing it for printing is also a terminal operation. In Clojure there is no such problem as all the collections are persistent therefore you can easily reuse them. See the first clojure iteration code: the fizbuzzed list can be safely returned &lt;strong&gt;and&lt;/strong&gt; reduced into the statistics.&lt;/p&gt;&lt;p&gt;That said the stream API felt something I would not mind to work with on a more realistic problem. And mainly because of this API I tend to agree with Sean Corfield:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;I think I can safely say that I would no longer rather fall on a sword than program in Java – as long as it’s Java 8!&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;[from &lt;a href=&quot;http://seancorfield.github.io/blog/2014/06/20/some-thoughts-on-java-8/&quot;&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt;]&lt;/p&gt;&lt;p&gt;As wrapping up I mention two other fizzbuzz solutions. One I came across on the &lt;a href=&quot;http://rosettacode.org/wiki/FizzBuzz&quot;&gt;&lt;strong&gt;rosette code&lt;/strong&gt;&lt;/a&gt; and found it really elegant:&lt;/p&gt;
&lt;!--?prettify lang=clojure linenums=true --&gt;
&lt;pre&gt;&lt;code&gt;(map #(let [s (str %2 %3) ] (if (seq s) s (inc %)))
        (range)
        (cycle [&amp;quot;&amp;quot; &amp;quot;&amp;quot; &amp;quot;Fizz&amp;quot;])
        (cycle [&amp;quot;&amp;quot; &amp;quot;&amp;quot; &amp;quot;&amp;quot; &amp;quot;&amp;quot; &amp;quot;Buzz&amp;quot;]))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I think this is my favourite as it does not even use &lt;code&gt;mod&lt;/code&gt; just simply exploits positions in lists and the fact that &lt;code&gt;map&lt;/code&gt; can go through multiple collections. Also laziness shines through this example: just mind blowingly awesome.&lt;/p&gt;&lt;p&gt;The &lt;a href=&quot;https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition&quot;&gt;&lt;strong&gt;other solution&lt;/strong&gt;&lt;/a&gt; shows how much fizzbuzz is a rabbit hole and also well... funny? sad? humbling? ;)&lt;/p&gt;</description></item><item><title>Webapp regression testing for cheap</title><link>http://benedekfazekas.github.io/2015/01/16/cheap-webapp-regression-testing/</link><pubDate>Fri, 16 Jan 2015 00:00:00 +0000</pubDate><description>&lt;p&gt;An automated regression test suite is a very valuable asset, however most of the times it &lt;a href=&quot;http://www.pitheringabout.com/?p=995&amp;cpage=1#comment-21254&quot;&gt;&lt;strong&gt;does not come cheap&lt;/strong&gt;&lt;/a&gt;. You need to write your unit and/or functional tests and add them to the regression suite and then, and this is the real pain: maintain them. Almost every single refactoring means some work on the test code too. We've found a way around this at the MailOnline, and get a really valuable regression test suite for cheap. Perhaps it's worth to share the details.&lt;/p&gt;&lt;p&gt;Our regression test suite was not originally created as such. We aimed to solve a different testing problem. Since our project started off as a rewrite project, ie. rewriting the application producing the MailOnline website in clojure -- it was originally implemented in java and spring MVC --, the problem was how to make sure and prove that we are producing the same output. Mind you, even defining what is the same output here is not trivial as we did not care about whitespaces or other not really important, minor differences in the html output. Later we realised that the same idea can be reused to catch regressions and solve other testing problems too. More on this later.&lt;/p&gt;&lt;p&gt;The key idea of solving this was to use &lt;a href=&quot;https://github.com/cgrand/enlive&quot;&gt;&lt;strong&gt;enlive&lt;/strong&gt;&lt;/a&gt; for two crucial features:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;its ability to turn html into data structures we can work easily with in clojure,&lt;/li&gt;
  &lt;li&gt;its ability to write selectors to easily filter out parts of html we are interested in.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;With this at hand what needed to be done became quite straight forward:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;parse the html output for both the legacy and the new application&lt;/li&gt;
  &lt;li&gt;select some parts of the html from both the legacy's and the new application's output. We can even drill down to certain attributes&lt;/li&gt;
  &lt;li&gt;preprocess the selected data optionally. For example for an &lt;code&gt;img&lt;/code&gt; tag we are only interested in the &lt;code&gt;src&lt;/code&gt; and &lt;code&gt;class&lt;/code&gt; attributes: the other attributes are not crucial. Or can munge the data a bit, let's say because we have timestamps here and there: they won't be exactly the same ever.&lt;/li&gt;
  &lt;li&gt;compare the preselected and preprocessed data&lt;/li&gt;
  &lt;li&gt;produce some human or machine readable output of the comparison&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;In fact we came up with a protocol defining what we exactly need to do after the html is parsed already:&lt;/p&gt;
&lt;!--?prettify lang=clojure linenums=true --&gt;
&lt;pre&gt;&lt;code&gt;(ns fe-adm.ab-test.detective)

(defprotocol DetectiveProtocol
  &amp;quot;An investigator for A/B testing. Understands what parts of a page are
   interesting and how to compare those parts&amp;quot;
  (title [self]
    &amp;quot;Descriptive id&amp;quot;)

  (extract [self page-html]
    &amp;quot;Extract the interesting part of the page&amp;quot;)

  (keyfn [self]
    &amp;quot;Prime elements for use in sort-by&amp;quot;)

  (render [self a-html b-html]
    &amp;quot;Print HTML diff&amp;quot;)

  (render-unit [self a-html b-html]
    &amp;quot;Print unit compliant xml&amp;quot;)

  (diff [self a b]
    &amp;quot;Calculate missing and surplus&amp;quot;))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;One simple example of using this protocol for checking divs across the page:&lt;/p&gt;
&lt;!--?prettify lang=clojure linenums=true --&gt;
&lt;pre&gt;&lt;code&gt;(ns fe-adm.ab-test.div
  (:require [fe-adm.ab-test.detective :refer :all]
            [net.cgrand.enlive-html :as html]
            [fe-adm.ab-test.utils :as ab-utils]))

(defn- ignore-divs [div]
  (empty? (-&amp;gt; div :attrs)))

(defn- munge-divs [div]
  (-&amp;gt; div
      (ab-utils/trim-attrs-str :class)
      (ab-utils/replace-attrs-str :id #&amp;quot;^.*$&amp;quot; &amp;quot;{{some-id}}&amp;quot;)))

(defrecord Detective []
  DetectiveProtocol
  (title [self] :div)

  (extract [self html]
    (-&amp;gt;&amp;gt; (html/select html [:div])
         (remove ignore-divs)
         (map :attrs)
         (map munge-divs)
         set))

  (keyfn [self]
    (fn [attrs] (vec (sort (dissoc attrs :content)))))

  (render [self a-html b-html]
    (ab-utils/render-diff self a-html b-html))

  (render-unit [self a-html b-html]
    (ab-utils/render-unit-xml self a-html b-html))

  (diff [self a b]
    (clojure.set/difference a b)))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Note that the really interesting part happens in line 18. &lt;code&gt;extract&lt;/code&gt; function. All &lt;code&gt;div&lt;/code&gt; tags are selected although some are ignored (this part could be done by enhancing the selector actually). Then the value of the class attribute is trimmed: bit of clean up. And since div ids are uuid-s of some sort they are replaced with a constant so they won't fail the test needlessly.&lt;/p&gt;&lt;p&gt;As I said above after going live with the new application and retiring the legacy one we quickly realised that we can reuse the same idea and code for regression testing. We put together a box in production (not actually serving real traffic) getting a build for every single commit and compared its output with our production output. Presto, we have a regression test suite catching possible differences in our output for every single commit. If there is a difference we need to check if the change was intentional. If it was we use our CI tool's test muting feature to mark and silence it. Easy to do as you can see from the &lt;code&gt;render-unit&lt;/code&gt; function we are producing unit compliant output so our CI tool nicely listed our passed and failed tests. After a new production release all the test cases will pass naturally: that actually removes the mutes: clean sheet again.&lt;/p&gt;&lt;p&gt;Bigger pieces of work, refactorings frequently done on branches. With this approach it was easy to push a build on our prod test box from the branch and test the produced output against the production output to get some feedback of the state of the refactoring.&lt;/p&gt;&lt;p&gt;Another use case is when we decided to set up another cluster of our application in a new data center. We just run this test suite comparing the output of one of the new nodes against the output of a live node. Again presto: we were able to say when the new cluster was ready to server real traffic.&lt;/p&gt;&lt;p&gt;In retrospective my main take away from this story is how much value you can get for how less if you take the context of your application into consideration when you create your test suites.&lt;/p&gt;</description></item><item><title>Laziness driven changelogs</title><link>http://benedekfazekas.github.io/2015/01/05/laziness-driven-changelogs/</link><pubDate>Mon, 5 Jan 2015 00:00:00 +0000</pubDate><description>&lt;p&gt;At the MailOnline we have not been doing &lt;a href=&quot;https://twitter.com/javame/status/489338291494207488&quot;&gt;microservices&lt;/a&gt; exactly but something definitely far from big, monolithic applications. This resulted in quite a few applications across the board. Many written in clojure or in node.js, javascript for the browser, or some other language. We developers take care of releasing our software which is perhaps the right way to go most of the times, using some &lt;a href=&quot;http://martinfowler.com/articles/continuousIntegration.html&quot;&gt;CI&lt;/a&gt; tool and always trying to push for &lt;a href=&quot;http://martinfowler.com/bliki/ContinuousDelivery.html&quot;&gt;continuous delivery&lt;/a&gt;. Nevertheless the time had come when we were asked to provide some human readable information about the versions of the software we release. The request come from multiple sources: devs, devops and technical management all wanted this info. For a short time we could repel the requests by pointing people to our CI tool and/or gitlogs either in the command line or on github which we use to host almost all our projects -- rest is on bitbucket, just to make things even harder to track. On the top of that we use multiple issue tracking systems, yeah I know, don't ask: historical reasons. As these issues have very valuable information on what we are working on they should be referenced in these changelogs too.&lt;/p&gt;&lt;p&gt;When we realised that we can't really escape the changelog, release note request and to track things would actually help ourselves too we had practically to ways to go: either write these changelogs manually or write some code to generate them for us. Luckily almost from the beginning we were fairly good writing commit messages -- I read that some people actually use git hooks to validate commit messages and enforce certain rules: we are not that good but everybody in the group writes a meaningful one liner at least, referencing github or JIRA issues using tags like &lt;code&gt;[wip]&lt;/code&gt; or &lt;code&gt;[refactor]&lt;/code&gt;. That is fairly good base to build on.&lt;/p&gt;&lt;p&gt;On an other note a long annoyance of mine the release creation on github: why, oh why can't you automatically generate some changelog on github based on your commit messages? I am sure that would not work for everybody who wants to use the release facility on github but it should do it for many projects. So first round: let's do a bit of googling if the tool is already out there. Well... kinda... &lt;a href=&quot;https://pypi.python.org/pypi/gitchangelog&quot;&gt;gitchangelog&lt;/a&gt; looks like exactly what we need. But looking at its sample config file: not that sure anymore. So I basically figured writing something with a similar set of features with simpler config, less code and easy to use for any project but even easier for clojure projects would be fun.&lt;/p&gt;&lt;p&gt;Hence happened &lt;a href=&quot;https://github.com/MailOnline/gargamel&quot;&gt;gargamel&lt;/a&gt; which can be used as a leiningen plugin or with its CLI interface. It accepts any git refs to generate a changelog in between or can generate release notes between the two latest occurrence of some tags with a specific format (for example &lt;code&gt;v\d\.\d\.\d&lt;/code&gt;). It uses mustache templates to generate practically any output format (tested with html and markdown) and with simple &lt;a href=&quot;https://github.com/MailOnline/gargamel/blob/master/gargamel.edn&quot;&gt;configuration&lt;/a&gt; based on regexes it generates custom sections and munges certain objects (like creating links to github/jira issues from issue references) in the commit messages. On the top of that it is able to create changelogs of project dependencies in projects where those dependencies are managed in a &lt;a href=&quot;http://bower.io&quot;&gt;bower.json&lt;/a&gt; file.&lt;/p&gt;&lt;p&gt;So now we are able to plugin in change log and release note generation into our release process. We have two types of release note generation jobs on our CI tool: manual one where you can provide &lt;code&gt;from&lt;/code&gt; and &lt;code&gt;to&lt;/code&gt; git refs: this is used to generate changelog for release candidates and publish them in the appropriate chatrooms (we use &lt;a href=&quot;https://slack.com/&quot;&gt;slack&lt;/a&gt; heavily) for affected people to review. And we have an automated release note generation step which runs right after our production release. Integrating gargamel with CI and slack is the real win in this story. On the top of that on those open source projects I am involved in I can use gargamel now to create markdown changelogs for github releases. for example gargamel's own &lt;a href=&quot;https://github.com/MailOnline/gargamel/releases/tag/release-050-20150113_1225&quot;&gt;release notes&lt;/a&gt;.&lt;/p&gt;</description></item><item><title>Clean clojure files en masse continued</title><link>http://benedekfazekas.github.io/2014/03/24/clean-clj-project-en-masse-2/</link><pubDate>Mon, 24 Mar 2014 00:00:00 +0000</pubDate><description>&lt;p&gt;The project clean mentioned at the end of the previous post got integrated into &lt;a href=&quot;https://github.com/magnars/clj-refactor.el&quot;&gt;&lt;strong&gt;clj-refactor&lt;/strong&gt;&lt;/a&gt; and changed a bit during the process. Just a quicky about the code.&lt;/p&gt;
&lt;!--?prettify lang=emacs-lisp linenums=true --&gt;
&lt;pre&gt;&lt;code&gt;(defun cljr-project-clean ()
  (interactive)
  (when (or (not cljr-project-clean-prompt)
            (yes-or-no-p &amp;quot;Cleaning your project might change many of your clj files. Do you want to proceed?&amp;quot;))
    (dolist (filename (cljr--project-files))
      (when (s-ends-with? &amp;quot;clj&amp;quot; filename)
        (let ((buffer (get-file-buffer filename))
              find-file-p)
          (if buffer
              (set-buffer buffer)
            (setq find-file-p t)
            (find-file filename))
          (ignore-errors (-map &amp;#39;funcall cljr-project-clean-functions))
          (save-buffer)
          (when find-file-p
            (kill-buffer)))))))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;There are a few small changes basically just to make the this more configurable and more comfortable to use.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Line 3&lt;/strong&gt;: There is a &lt;code&gt;defcustom&lt;/code&gt; controlling if &lt;strong&gt;clj-refactor&lt;/strong&gt; should make sure that we want to proceed with a possible destructive change (en masse). Easy to get rid of this: &lt;code&gt;(setq cljr-project-clean-prompt nil)&lt;/code&gt; if we know what we are doing.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Line 7&lt;/strong&gt;: &lt;strong&gt;clj-refactor&lt;/strong&gt; nicely tries to find the buffer first. If it is found it uses it and then won't get killed when all the changes are done (see &lt;strong&gt;line 15&lt;/strong&gt;): there is a good guy. Otherwise we open the file with &lt;code&gt;find-file&lt;/code&gt; and kill it when done. Would be interesting to find a way to use &lt;code&gt;temp-buffer&lt;/code&gt; and/or &lt;code&gt;temp-file&lt;/code&gt; actually.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Line 13&lt;/strong&gt;: There is an other &lt;code&gt;defcustom&lt;/code&gt; to configure the list of functions to run on a given clojure file in the project. By default this is removing unusued requires and sort ns forms but easy to remove one of these or add others, even perhaps some project dependent cleaning functions implemented in the project itself.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Hope that really helps out there to fix some broken windows.&lt;/p&gt;</description></item><item><title>Clean clojure files en masse</title><link>http://benedekfazekas.github.io/2014/03/16/clean-clj-project-en-masse/</link><pubDate>Sun, 16 Mar 2014 00:00:00 +0000</pubDate><description>&lt;p&gt;&lt;a href=&quot;https://github.com/magnars/clj-refactor.el&quot;&gt;&lt;strong&gt;clj-refactor&lt;/strong&gt;&lt;/a&gt; is a very lightweight elisp library for Emacs to support every day refactorings for clojure. I've even read that it is an &lt;a href=&quot;https://twitter.com/cemerick/statuses/431608319552548864&quot;&gt;über-paredit&lt;/a&gt;: kinda fair. It definitely melds into the editing experience defined by the mix of cider, clojure-mode, paredit well. Adopting it you get loads of nice transformations which help you with your every day clojure coding. There are simpler ones like &lt;strong&gt;add require to namespace declaration&lt;/strong&gt; &lt;code&gt;ar&lt;/code&gt; and &lt;strong&gt;cycle collection type&lt;/strong&gt; &lt;code&gt;cc&lt;/code&gt; or &lt;strong&gt;cycle privacy&lt;/strong&gt; &lt;code&gt;cp&lt;/code&gt;. This latter sounds silly first: why an earth you would not just go to your &lt;code&gt;defn&lt;/code&gt; and add that '-' to make it private. But once your muscle memory has remembered &lt;code&gt;cp&lt;/code&gt; it is just natural to turn a function private with one easy key combo when you figured out it does not make sense to keep it public -- you don't even need to think about what to press. There are more arcane ones which give the wtf-just-happened-with-my-code experience first but then they start making absolute sense. The threading macro related ones for example: &lt;strong&gt;wrap in thread first&lt;/strong&gt; &lt;code&gt;tf&lt;/code&gt; and &lt;strong&gt;fully unwind threaded expression&lt;/strong&gt; &lt;code&gt;ua&lt;/code&gt; or the &lt;strong&gt;introduce let&lt;/strong&gt;, &lt;strong&gt;expand let&lt;/strong&gt;, &lt;strong&gt;move to let&lt;/strong&gt; triumvirate. Used in a correct way the latter one helps you to handle let expressions in a very clean way across your code. These are only examples of course, check out the full list on the project's really cleanly written github readme.&lt;/p&gt;
&lt;hr/&gt;
&lt;div class=&quot;sidenote&quot;&gt;

Just a side note on the shortcuts.  Well, that is the usual Emacs story.  People tend to freak out: 'not that many extra key combination I need to learn again'.  I find that my muscle memory kicks in very fast and what is even more helpful it is contextual.  I don't even try certain key combos when in the repl or when editing a text file.  So after a bit of practice you won't need to think how to move the form after the cursor to the already existing let expression.

&lt;/div&gt;
&lt;hr/&gt;&lt;p&gt;I also mentioned that &lt;strong&gt;clj-refactor&lt;/strong&gt; is lightweight. It is in the sense that it does not use any code analyzer library or such and it does not enforce you to use &lt;a href=&quot;https://github.com/clojure-emacs/cider&quot;&gt;&lt;strong&gt;cider&lt;/strong&gt;&lt;/a&gt; either. (Cider still can come in handy. For example you can use completion when adding something to the require section in your ns declaration.) It is an emacs-lisp library -- started out as tiny -- which uses some other elisp libraries like paredit, yasnippet, dash.el, s.el, multiple cursors (the good work of &lt;a href=&quot;https://github.com/magnars&quot;&gt;Magnar Sveen&lt;/a&gt; and others just like &lt;strong&gt;clj-refactor&lt;/strong&gt; itself). So &lt;strong&gt;clj-refactor&lt;/strong&gt; does not really 'understand' your code, but by using paredit it does understand the structure of your clojure files. This with some more elisp foo gives it enough power to be extremely handy: &lt;strong&gt;clj-refactor&lt;/strong&gt; is not so tiny anymore. There are plans to add some code analyzer to the mix but to be honest I would prefer at least to keep that if ever added as an optional feature even with the drawback of falling back to simpler behaviour. Lightweight means fast and speed is extremely important in order to avoid the productivity killer &lt;strong&gt;IDE&lt;/strong&gt; experience.&lt;/p&gt;&lt;p&gt;That leads me to the next group of offered refactorings, the ones which need to be performant because it makes sense to run them against a whole bunch of clojure files in one go. These are the 'clean up', house keeping features:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;replace &lt;code&gt;use&lt;/code&gt; with &lt;code&gt;:refer :all&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;sort use, require and import in your ns form&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;remove unused requires&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;When working on a sort of larger clojure project or multiple of those or as a member of a team of six, eight plus developers you might find running these against a whole project periodically a really nice thing as it helps fixing the &lt;a href=&quot;http://blog.codinghorror.com/the-broken-window-theory/&quot;&gt;broken window&lt;/a&gt; effect. I personally am not really a big fan of coding conventions, rules of how to write code or code style checkers but the number of namespaces which have unused requires is a good indicator of how much the code started to get disorganised. Better to fix those as fast as possible.&lt;/p&gt;&lt;p&gt;Here comes a small elisp function which does the trick:&lt;/p&gt;
&lt;!--?prettify lang=emacs-lisp--&gt;
&lt;pre&gt;&lt;code&gt;(defun cleanup-project-clj-files ()
  (interactive)
  (dolist (filename (cljr--project-files))
    (when (s-ends-with? &amp;quot;clj&amp;quot; filename)
      (find-file filename)
      (ignore-errors (cljr-remove-unused-requires))
      (save-buffer)
      (kill-buffer))))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Some quick points about the above code. It uses &lt;strong&gt;clj-refactor&lt;/strong&gt;'s own &lt;code&gt;cljr--project-files&lt;/code&gt; to find the clojure files. There are other ways but I thought the best is to use this as we are anyway using this library. One caveat that it will only work with leiningen managed projects for now. There are plans in the project as far as I know to eventually improve this. When that is done the above script will start working for any kind of projects. &lt;code&gt;ignore errors&lt;/code&gt; is needed because there is an error thrown if the given file does not have an ns declaration (as the project file itself does not). &lt;code&gt;save buffer&lt;/code&gt; might also trigger some additional clean up if you some hooks defined on save. For example my Emacs configuration turns all tabs into spaces when I save. Adding the above function to your Emacs is easy: either just copy it into your Emacs init file or save it in a separate file and make sure that file is loaded via your &lt;code&gt;init.el&lt;/code&gt;. If that is done you can just open any files in the project and &lt;code&gt;M-x cleanup-project-clj-files&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;I saw something very similar originally in &lt;a href=&quot;https://github.com/AlexBaranosky&quot;&gt;Alex Baranosky&lt;/a&gt;'s Emacs config repo on github and simplified it a bit. Also lots of information in the post comes from Alex and the small team around &lt;strong&gt;clj-refactor&lt;/strong&gt; while chatting with them about my pull requests. A very nice, fun team around a fun project.&lt;/p&gt;</description></item></channel></rss>