This post is inspired by a promissing chapter from Grokking Functional Programming, namely page #12 of this, which illustrates functional approach in Scala. Here is an equivalent of it in Clojure:

(require '[clojure.string :as s])

(defn abbr [name]                       ;; in Scala
  (let [names (s/split name #" ")       ;; var names = name.split(" ")
        initial (first (first names))]  ;; var initial = names(0).take(1)
    (str initial "." (second names))))  ;; var abbreviation = intial + “.” + names(1)

(abbr "Alonzo Churh")                   ;; => "A.Churh"

Besides of pleasure of taking first-name-string as character’s sequence and calling first twice (though there is a shortcut for this ffirst) there is a pleasant feeling after seeing this code - I always tried to structure a method’s body in a Ruby class like this - some assignments & following action, which takes use of them:

def abbr(name)
  names = name.split(" ")
  initial = names[0][0]

  initial + "." + names[1]
end

Same logic here, but less parens (yeah.. haters), threating string as array (actually only since 1.9.x) If you’d like to see some human-readable method names - require "active_support/core_ext/array/access".

But let’s execute some edge cases:

(abbr "AlonzoChurch")  ;; "A."
(abbr "Alonzo ")       ;; "A."
(abbr ".Church")       ;; ".Church"

Ok, let’s look at Ruby:

2.1.2 (main):0 > abbr "AlonzoChurch"
TypeError: no implicit conversion of nil into String

2.1.2 (main):0 > abbr "Alonzo "
TypeError: no implicit conversion of nil into String

2.1.2 (main):0 > abbr " Church"
TypeError: no implicit conversion of nil into String

You might think some #to_s will recover the code:

def abbr(name)
  names = name.split(" ")
  initial = names[0][0]

  initial.to_s + "." + names[1].to_s
end

Nope, it’s incorrent:

2.1.2 (main):0 > abbr " Church"
=> "C."

The shortest magic is to use a regex:

def abbr(name)
  names = name.split(/ /) # <<- here
  initial = names[0][0]

  initial.to_s + "." + names[1].to_s
end

2.1.2 (main):0 > abbr "AlonzoChurch"  # => "A."
2.1.2 (main):0 > abbr "Alonzo "       # => "A."
2.1.2 (main):0 > abbr " Church"       # => ".Church"

Ok, I cheated above - Clojure used a regex, too. But you cannot split a string using a plain string anyway. There was the only way.

I would’n say it’s the favorite fearure of Clojure, but, such resistance against edge cases makes me think little less about details & testing edge cases, as some of them simply don’t count as edge.