Thursday, 15 May 2014

Clojure practice

Recently I have dabbled into Clojure. I've been planning a post for a while, but since now Kaloz has posted a Scala functional programming demonstration, the time has come to reply in kind. Please, read his post to get the problem, here I take the lazy way and simply show a Clojure solution.

(ns clojure-study.garbage
  (:require [clojure.core.match :as m]))

(def num->chars { 
                  \2  "ABC"
                  \3  "DEF"
                  \4  "GHI"
                  \5  "JKL"
                  \6  "MNO"
                  \7  "PQRS"
                  \8  "TUV"
                  \9  "WXYZ"})

(defn- red-fn [resolvedStrings number]
  (m/match (num->chars number)
    nil resolvedStrings
    item (for [c item
               i resolvedStrings]
            (str i c))))

(defn combinations [number]
  (reduce red-fn [""] number))

(println (combinations "77501998489"))

No comments :

Post a Comment