2019-02-27 Update: Web Apps and Multimethods

I have started looking at the tutorial for Luminus, the Clojure web framework. I am thinking about making a web app that sends data to Clara, the Clojure rules engine.

I have started looking at the tutorial for Luminus, the Clojure web framework. I am thinking about making a web app that sends data to Clara, the Clojure rules engine.

I have also started a github sub-project simply-clojure, which is a port of Simply Scheme to Clojure. I used multimethods, based on the use seen in Clojure For the Brave and True.

One of the functions that comes with Simply Scheme is appearances, which “returns the number of times its first argument appears as a member of its second argument”. It works with “words” and “sentences”. A “word” in SS is any type of data with a single member; it could be a number or a string with one word. A “sentence” is basically a list.

(appearances 'star '(star trek star wars star gate))
3
(appearances 'a 'stargate)
2
(appearances 1 123145167)
2
(appearances 1 '(1 2 1 2 3 4 1))
2

It can handle various datatypes. For Clojure, I decided to use multimethods. I think they can only take one argument, so I cheated and used a map.

(defmulti my-appearances (fn [the-map] (:type the-map)))

(defmethod my-appearances :sentence
  [the-map]
  (count (filter #(= (:part the-map) %1) (helper/split-string-to-words (:whole the-map)))))

(defmethod my-appearances :word [the-map]
  (count (filter #(= (:part the-map) %1) (helper/split-word-to-letters (:whole the-map)))))

(defmethod my-appearances :vector [the-map]
  (count (filter #(= (:part the-map) %1) (:whole the-map)))

Here are the tests:

(test/deftest test-my-appearances
  (test/testing "Testing my-appearances"
    (test/is (= 2 (my-appearances {:type :word, :part "e" :whole "feelings"})))
    (test/is (= 2 (my-appearances {:type :sentence :part "hello" :whole "hello goodbye hello again goodbye again nothing again" })))
    (test/is (= 3 (my-appearances {:type :vector :part 2 :whole [1 2 3 2 4 2 5]})))))

Code highlighted at http://hilite.me/.

You’re welcome.

Image from León Bible of 920, a 10th century Bible manuscript housed in the Leon Cathedral; image from Wikimedia, assumed allowed under Fair Use.

There are 311 words in this article.