Created
November 27, 2011 18:56
-
-
Save roman/1397984 to your computer and use it in GitHub Desktop.
Keep the important parameter first!
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
(ns main | |
(:require [clojure.string :as string])) | |
; this code is to solve the following algorithmic problem: | |
; http://www.spoj.pl/problems/ADDREV/ | |
(def sum (partial reduce + 0)) | |
(defn main [& args] | |
(let [n (Integer/parseInt (read-line))] | |
(repeatedly n reverse-sum))) | |
(defn flip [g] (fn [a b] (g b a))) | |
(defn reverse-sum [] | |
(let [rmap (flip map)] | |
(-> (read-line) | |
(string/split #"\s+") | |
(rmap string/reverse) | |
(rmap #(Integer/parseInt %)) | |
sum | |
str | |
string/reverse | |
(string/replace #"^0+" "") | |
(rmap println)))) |
Interesting, I was wondering why there wasn't a drop-while, take-while functions with Strings in Clojure (Whenever I tried to used them with Strings I got back a coll of characters rather than another String). It makes sense now given what you said that performance is the main issue.
The problem with minor annoyances is that they add up little by little. Hopefully this list of small annoyances won't grow to much while I'm getting deeper into the rabbit hole of Clojure :-).
Thanks for your feedback @swannodette.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's a minor annoyance. clojure.string does not treat Java strings as sequences for a practical reason - performance. So you're going to run into problems with the threading operators as you move between sequences and strings.