3 ms·
Here's a translation of that to Clojure, since I'm learning that now: (use 'clojure.set) (reverse (sort (map int (intersection
by mcav 16y ago
Here's a translation of that to Clojure, since I'm learning that now:
(use 'clojure.set)
(reverse (sort (map int
(intersection (set "letters in this sentence")
(set "and this one")))))
Edit: Take two...
(use 'clojure.set)
(->> (intersection (set "letters in this sentence")
(set "and this one"))
(map int) sort reverse)
(experienced lispers, feel free to make this more idiomatic)
- mattrepl 16y agoHere's one using a list comprehension to make it more closely resemble the Python example. (require ['clojure.set :as 's]) (-> (for [c (s/intersection (set "letters in this sentence") (set "and this one"))] (int c)) sort reverse)
- sreque 16y agoNot sure why we are posting alternative language implementations, but here is the Scala version :). You could say that this was an exercise for me in learning 2.8, which I am unfamiliar with. A for comprehension isn't needed because Scala already treats Strings as a sequence of characters. (Set("letters in this sentence":_* ) & Set("and this one":_* )).toSeq.sorted.reverse Or you, can take advantage of Scala's rich choice of collections. import scala.collection.immutable.TreeSet import scala.math.Ordering._ TreeSet("letters in this sentence":_* )(Char.reverse) & Set("and this one":_* ) toSeq The machinery Scala has in place to make the second example work is quite impressive, and I was pleased to see that it worked. Even though the intersection method returns a brand new set, using implicit arguments it correctly creates a set of the correct type (TreeSet) with the correct ordering function (Char.reverse), without any duplication of code in the standard library (like overriding '&' in every sub-class).
- wycats 16y agoAn easier Ruby solution would be: ("letters in this sentence" + "and this one").chars.sort.reverse
- sreque 16y agoExcept you aren't using sets, so you get duplicate characters. I can just as easily write ("letters in this sentence" + "and this one").toSeq.sorted.reverse in scala. You could tag the end of your ruby statement with .uniq and you would be fine. Alternatively, you could wrap the above scala statement in TreeSet(). These solutions process things in a significantly different way than was done by the OP, though.
- wcarss 16y ago("letters in this sentence".split(//) & "and this one".split(//)).sort.reverse.join I'm just learning, but I think this performs it correctly with sets.
- AndyKelley 16y agoI find the python code much more readable.
- lelele 16y agoIMO, both suck at readability, since you have to read inner expressions first. An example with a "pipe" would be clearer.