Quantcast
Channel: dave yarwood
Viewing all articles
Browse latest Browse all 64

_why's (Poignant) Guide to Ruby in Clojure: Part 7

$
0
0

Parts 1 through 6 of this series can be found here, here, here, here, here and here.

Alright, so I kind of just remembered that there was still one chapter left to go – so here it is! Until _why decides to come out of hiding and write an 8th chapter (whenever that happens, if ever…), this will be the final installment of my translation of _why’s (poignant) guide to ruby into Clojure.

This is kind of a weird chapter in that the whole thing is hand-drawn (by one of the foxes, presumably), making the actual code examples a little hard to find. I feel like I’ve hit all the important bits that would constitute “examples.” This is kind of a short chapter in terms of code content, but it’s a fun one nonetheless. We’ve got a very brief example of how you might build a text adventure game using classes and metaprogramming, a function that takes a string and turns it upside-down, an example of overloading an arithmetic operator to work on arrays, and an interesting encryption algorithm.

Chapter 7

; ex. 1:
(defprotocolActions(look[room])(stab[room])(sleep[room]))(defrecordBanquetRoom[]Actions(look[_]"Red with mirrors")(stab[_]"The room screams!")(sleep[_]"Ahh, you slept on food"))(print"Banquet Room. Do what?")(let[what-to-do(clojure.string/trim(read-line))](eval`(~(symbolwhat-to-do)(BanquetRoom.)))); ex. 2:
(defnumop-apisdn[txt](let[in"ahbmfnjpdrutwqye"out(reversein)in-out(zipmapinout)](applystr(reverse(map#(in-out%%)txt))))); ex. 3:
(defdivide/)(defmulti/(fn[arg1arg2&more](and(coll?arg1)(number?arg2))))(defmethod/false[&args](applydivideargs))(defmethod/true[colln](partitionnnnilcoll))(/[:mad:bone:and:his:buried:head]3)(/(range111)5)

I noticed in the “Decody” example that _why made a small, but confusing mistake when commenting his code: In the lines of code referring to 2 letters on the same row, he commented “on the same column,” and vice versa. I have corrected this for the sake of clarity (and to avoid confusing myself!).

; ex. 4:
(defprotocolEncoding(locate[decodyletter])(at[decodyrowcol])(encode[decodytxt]))(defrecordDeCody[key]Encoding(locate[_letter](let[i(.indexOfkey(strletter))][(int(/i5))(remi5)]))(at[_rowcol](getkey(+(*row5)col)))(encode[decodytxt](let[pairs(->>(clojure.string/replacetxt#"[^a-z]""")(#(clojure.string/replace%"j""i"))(partition22nil)(mapcat(fn[[ltr1ltr2]](cond(nil?ltr2)[[ltr1\x]](=ltr1ltr2)(repeat2[ltr1\q]):else[[ltr1ltr2]]))))](applystr(mapcat(fn[pair](let[[l1l2:asls](map#(locatedecody%)pair)](cond(=(firstl1)(firstl2)); on the same row
(letfn[(move-right[[rowcol]][row(rem(inccol)5)])](map#(applyatdecody(move-right%))ls))(=(lastl1)(lastl2)); on the same column
(letfn[(move-down[[rowcol]][(rem(incrow)5)col])](map#(applyatdecody(move-down%))ls)):else; normal swap
[(atdecody(firstl1)(lastl2))(atdecody(firstl2)(lastl1))])))pairs)))))(defndecody[key]"Takes a string with arbitrary whitespace and removes all whitespace
   and other non-letter characters, returning a DeCody instance with
   the resulting string as a key (25 letters arranged in a 5x5 grid)."(DeCody.(clojure.string/replacekey#"[^a-z]""")))(defdc(decody"k x z p w
                 i g t f a
                 h o s e n
                 y m b r c
                 u l q d v")); example usage:
(encodedc"message to be encoded")

Viewing all articles
Browse latest Browse all 64

Trending Articles