3 ms·
1) (map #(* % 2) (range 1 10)) 2) (reduce + (range 1 1000)) 3) (def string "This is an example tweet talking about scala and sbt.") (some
by SomeIdiot 15y ago
1)
(map #(* % 2) (range 1 10))
2)
(reduce + (range 1 1000))
3)
(def string "This is an example tweet talking about scala and sbt.")
(some #(.contains string %) ["scala", "akka", "play framework", "sbt", "typesafe"])
4)
As a string:
(slurp "filename")
As a list you could just do:
(.split (slurp "filename") "\n")
Or if you want a lazy sequence:
(line-seq (BufferedReader. (InputStreamReader. (FileInputStream. file-path) "UTF-8")))
There are also contrib libraries for this that do it nicer.
5)
(apply prn (map #(str "Happy birthday" (if (= % 4) " dear NAME" " to you")) (range 1 5)))
6) I'm going to leave this blank for now. It's an easy enough problem, but I can't think of a way of doing it in a nifty one-liner.
Edit: Ahh thought of it now. group-by does it nicely:
(group-by #(> % 60) [49, 58, 76, 82, 88, 90])
which returns a map with the elements keyed to the result of the anonymous function. In this case:
{false [49 58], true [76 82 88 90]}
7) Something like
(xml-seq (clojure.xml/parse the-xml))
probably.
8)
(apply max [14, 35, -7, 46, 98])
(apply min [14, 35, -7, 46, 98])
9)
Clojure has pmap, which is used just like map is, work is just done in parallel. Also Clojure is known for its concurrency features, which I won't go over here.
10) I'm sure this can be done in a similar amount of code in Clojure, though I am not going to try.
Any comments/corrections are welcome.