Skip to content

Instantly share code, notes, and snippets.

@seeflanigan
Forked from rjmcdonald83/anagram.rb
Last active December 22, 2015 16:29
Show Gist options
  • Save seeflanigan/6500072 to your computer and use it in GitHub Desktop.
Save seeflanigan/6500072 to your computer and use it in GitHub Desktop.
Anagram Solutions
(ns anagram
(:require [clojure.string :refer [lower-case split]]))
(defn- duplicate? [a b]
(= (lower-case a) (lower-case b)))
(defn- unique-candidates [source candidates]
(remove #(duplicate? % source) candidates))
(defn- canonicalize [word]
(frequencies (lower-case word)))
(defn- is_anagram? [canonical_source candidate]
(= canonical_source (canonicalize candidate)))
(defn anagrams-for [source, candidates]
(->>
(unique-candidates source candidates)
(filter #(is_anagram? (canonicalize source) %))))
defmodule Anagram do
defp duplicate?(a, b) do
String.downcase(a) == String.downcase(b)
end
defp unique_words(source, candidates) do
Enum.reject(candidates, duplicate?(&1, source))
end
defp canonical(string) do
String.downcase(string) |> String.graphemes |> Enum.sort
end
defp is_anagram?(canonical_source, candidate) do
canonical_source == canonical(candidate)
end
def match(source, candidates) do
unique_words(source, candidates)
|> Enum.filter(is_anagram?(canonical(source), &1))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment