read


read
or learn more

Monkeying with Clojure’s comp Function

Aug 18, 2010

A slight modification to Clojure’s comp function gives me more power:

I added a function body to comp+ handling the case where it’s given no function, returning the identity function. This let’s me use comp+ in more interesting ways without requiring that I handle the case of no arguments explicitly and without exploding when I don’t.

Just one of those little things I guess.

:f

2 Comments, Comment or Ping

  1. Alf

    I’m a newbie to Clojure, but wanted to test the code. Couldn’t quite make it work though. Modified the test to the following:

    (require ‘clojure.contrib.string) (for [f (map #(apply comp+ %) [[keyword name] [] [clojure.contrib.string/upper-case name]]) e ‘[foo bar]] (f e)))

    The change was really just to require clojure.contrib string, and reversing the final vectors values from [name clojure.string/upper-case] to [clojure.contrib.string/upper-case name].

    This seems to give the expected result.

    Also wondering why you are not basing your comp+ function on the existing comp function. The following has the same result. Is this less efficient than copying the code?

    (defn comp+ ([] identity) ([f & fs] (apply comp f fs)))

  2. Thanks for the call on require — fixed.

    All of my “monkeying” posts work against the original source.

Reply to “Monkeying with Clojure’s comp Function”