🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of midjr

midjr's solution

to Anagram in the Clojure Track

Published at Jul 13 2018 · 2 comments
Instructions
Test suite
Solution

Given a word and a list of possible anagrams, select the correct sublist.

Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets".

Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

anagram_test.clj

(ns anagram-test
  (:require [clojure.test :refer [deftest is]]
            anagram))

(deftest no-matches
  (is (= []
         (anagram/anagrams-for "diaper" ["hello" "world" "zombies" "pants"]))))

(deftest detect-simple-anagram
  (is (= ["tan"] (anagram/anagrams-for "ant" ["tan" "stand" "at"]))))

(deftest does-not-confuse-different-duplicates
  (is (= [] (anagram/anagrams-for "galea" ["eagle"]))))

(deftest eliminate-anagram-subsets
  (is (= [] (anagram/anagrams-for "good" ["dog" "goody"]))))

(deftest detect-anagram
  (is (= ["inlets"]
         (let [coll ["enlists" "google" "inlets" "banana"]]
           (anagram/anagrams-for "listen" coll)))))

(deftest multiple-anagrams
  (is (= ["gallery" "regally" "largely"]
         (let [coll ["gallery" "ballerina" "regally"
                     "clergy"  "largely"   "leading"]]
           (anagram/anagrams-for "allergy" coll)))))

(deftest case-insensitive-anagrams
  (is (= ["Carthorse"]
         (let [coll ["cashregister" "Carthorse" "radishes"]]
           (anagram/anagrams-for "Orchestra" coll)))))

(deftest word-is-not-own-anagram
  (is (= [] (anagram/anagrams-for "banana" ["banana"]))))

(deftest capital-word-is-not-own-anagram
  (is (= [] (anagram/anagrams-for "BANANA" ["banana"]))))
(ns anagram)

(defn- same-characters? 
  [word word2]
    (= (sort (.toUpperCase word))
      (sort (.toUpperCase word2))))

(defn- anagram?
  [word word2]
  (and (not= (.toUpperCase word) (.toUpperCase word2))
       (same-characters? word word2)))

(defn anagrams-for 
  [word list]
  (filterv (partial anagram? word) list ))

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of midjr
midjr
Solution Author
commented over 5 years ago

I am wondering why line 15 ultimately ended up working. I created the partial function which became the predicate of the filterv function. This means list should be the collection that gets filtered on based on the partial.. but the entities of the list should be passed into the partial.. How is it being used twice?

Avatar of pgorczak

filter passes on elements from the collection for which the predicate evaluates to true. I guess filterv does about the same but instead of returning a lazy sequence, it creates a vector (non-lazily?).

Sticking to the community style guide, you should use clojure.string's upper-case here. I also liked frequencies as a possible alternative to sort.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?