Created
September 25, 2012 18:58
-
-
Save devnoo/3783758 to your computer and use it in GitHub Desktop.
seven languages in seven weeks: clojure day2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn ceasarEncodeChar [char, shift] (nth infAlphabetSeq (+ shift (indexInAlphabet char) ))) | |
(defrecord CeasarCipher [shift] Cipher | |
(encode [c, message] (reduce str (map #(ceasarEncodeChar %1 shift) message ))) | |
) | |
(def ceasar1 (CeasarCipher. 3)) | |
(println (.encode ceasar1 "message")) | |
(println (encode ceasar1 alphabet )) | |
(def ceasar2 (CeasarCipher. 15)) | |
(println (.encode ceasar2 "message")) | |
(println (encode ceasar2 alphabet )) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'clojure.string ) | |
(use '[clojure.string :only [lower-case]]) | |
(defprotocol Cipher (encode [c, message]) ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def alphabet "abcdefghijklmnopqrstuvwxyz") | |
(def infAlphabetSeq (cycle (seq alphabet))) | |
(defn indexInKey[key, letter] (.indexOf key, (lower-case letter))) | |
(defn indexInAlphabet [letter] (indexInKey alphabet, letter )) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defrecord SubstitutionCipher [key] Cipher | |
(encode [c, message] (reduce str (map #(nth (distinct (concat key alphabet )) (indexInAlphabet %1)) message ))) | |
) | |
(def c (SubstitutionCipher. "zebra")) | |
(println (.encode c "message")) | |
(println (encode c alphabet )) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro unless [test body elsebody] (list 'if-not test body elsebody)) | |
(unless false (println "if") (println "else")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment