r/Clojure • • Mar 30 '17

Fourteen months with Clojure

https://blog.skyliner.io/fourteen-months-with-clojure-beb8b3e4bf00
70 Upvotes

16 comments sorted by

17

u/weavejester Mar 30 '17

Incidentally:

(every? #(= % "success") coll)

Can also be written as:

(every? #{"success"} coll)

3

u/Radiationwnderdefeat Mar 30 '17

Sets as functions are amazing. Another one of the small and simple things that makes clojure amazing.

2

u/6086555 Mar 30 '17 edited Mar 31 '17

The best part it that it's possible to extend other types as well to make strings, regex, numbers as functions. Incredibly powerful but maybe a bit risky

Edit: clojurescript only

3

u/weavejester Mar 31 '17

Iiiis it? You can't add clojure.lang.IFn to an existing Java class, especially not a final one like java.lang.String.

2

u/6086555 Mar 31 '17

Hum my bad, maybe it's a clojurescript only thing

1

u/marxama Apr 01 '17

IIRC, you can use extend, extend-type etc to have existing types implement protocols. But in Clojure, IFn is a Java interface, not a protocol (whereas in ClojureScript, it indeed is a protocol)

4

u/[deleted] Mar 31 '17

The post mentions nesting. To deal with nesting and because some-> didn't quite go far enough I wrote a short circuited thread first operator.

(short-> (foo)
         (bar x)
         (goo x y)
         
         :on boolean?)

The need to short circuit a set of threaded steps seems pretty common and might be nice to see something like this in core.

3

u/Psetmaj Mar 30 '17

Very insightful/enjoyable, this helps ease the apprehension I've been having about a soon-to-be-big project I just started in Clojure


On the procedural errors bit towards the end, you could go with a more data-driven solution with reduce.

Here's a very-contrived/constrained example, I could definitely be misunderstanding your exact pain

(defn foo [x]
  (if (> x 3)
    x))
 
(defn bar [x]
  (if (> x 5)
    x))
 
(defn baz [x]
  (if (> x 7)
    x))
 

;;Backwards on functions, I know
(defn err-comp [fns arg]
  (reduce
   #(if-let [result (and %1 (%2 %1))]
       result;;pass result to next function
       false);;log and return false
   arg fns))

(err-comp [foo bar baz] 3);=> false
(err-comp [foo bar baz] 4);=> false
(err-comp [foo bar baz] 8);=> 8

You could probably write a macro or function to do the if/logging wrapping as well.

8

u/coda_hale Mar 30 '17

(Co-worker of the author here.)

That's essentially the Maybe monad. The Either monad combines the short-circuit of the Maybe monad (i.e. neither Nothing nor Left bind with anything) with the ability to specify an actual return value. There are a lot of ways to implement it, and most Clojure code involves some form of monadic composition. It just doesn't make a lot of noise about it, unlike other functional languages.

The upside of using monadic composition is sugaring stuff like Haskell's do-notation or the mlet macro from Cats, which allow you to very naturally thread monadic values together.

2

u/poliphilo Mar 30 '17

I believe what you're trying to do in that logging example essentially is monadic, so any proper Clojure solution is going to be 'essentially' some monad with some sugar.

I'd think the difference between a good and a bad solution boils down to succinctness and clarity (to a sophisticated reader of the code). The Either solution avoids the nesting, but it strikes me as a step down from even the if-let solution on those criteria. A logging-some-> macro could be another approach.

2

u/ws-ilazki Mar 31 '17

Like /u/coda_hale said, that's basically a Maybe monad and they wanted an Either. You could build off your example to get Either-like behaviour pretty easily, though:

(defn either [fs x default]
  (let [r (err-comp fs x)]
    (if (false? r) default r)))

(either [foo bar baz] 3 "foo")  ;=> "foo"
(either [foo bar baz] 4 "foo")  ;=> "foo"
(either [foo bar baz] 8 "foo")  ;=> 8

I actually did something like your err-comp a while back for personal use, though I reversed the argument order for the maybe:

(defn maybe [m & fs]
  (let [f (first fs)
        fs (rest fs)]
    (cond (nil? m) m
          (nil? f) m
          :else (recur (f m) fs))))

(maybe 3 foo bar baz) ;=> nil
(maybe 4 foo bar baz) ;=> nil
(maybe 8 foo bar baz) ;=> 8

Doing it that way let me put the initial value at the start, which let me use &rest for the function list instead of having to wrap it up myself. Plus that way it reads entirely left-to-right, which seemed cleaner.

To add the either-like behaviour from above in top of this maybe, one could do this:

(defn either [m default & fs]
  (let [r (apply maybe m fs)]
    (if (nil? r) default r)))

(either 3 "foo" foo bar baz) ;=> "foo"
(either 4 "foo" foo bar baz) ;=> "foo"
(either 8 "foo" foo bar baz) ;=> 8

1

u/lgstein Mar 30 '17

Unless you really don't know the fns at compile time you should use some-> and some->>. They are macros and generate code without collection looping overhead.

1

u/Psetmaj Mar 31 '17

Will look into those, in this particular case, I assumed the functions weren't always known ahead of time. In most of my professional projects where I am able to justifiably demand Clojure, little is known at compile time.

2

u/dotemacs Mar 30 '17

Clojure has no facility like return

You can get letr from https://github.com/aphyr/dom-top/blob/master/README.md which might step in to fill that need.

2

u/CurtainDog Mar 31 '17

Types of any kind are rare to a degree that astonish me

Types are everywhere, they're just far more useful than the ones developers have become accustomed to.

1

u/mac Apr 03 '17

This inspired me to write two new macros:

(either-> {:x 1}
           :y :a
           inc :b)
;; => :a

(as-either-> {:x 1} x
            (:x x) :a
            (get x []) "error out at step 2"
            (inc x) :b)
;; => "error out at step 2"

Generally useful?