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)))) |
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.
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
@Nakort, in clojure it seems there is two kinds of functions (I say it seems, because I'm not 100% familiar with all clojure's core apis), the ones that receive the important parameter first, and the ones that receive the important parameter last.
clojure.string
functions receive important parameter first, meaning, the string you are going to be working with is received first, some examples:the collection functions receive parameters last:
There are two functions to compose functions together
->
and->>
, with->
you compose functions like the one inclojure.string
module, with->>
you compose functions like the ones in collections. I didn't see why they do this, given that if you follow a standard (important parameter first or important parameter last) all functions may be composable, and you would use just one->
or->>
.It seems you don't want to be able to compose some functions with others:
From
clojure.string
docsTweet from @swannodette (Main developer of core.logic, making him an authority IMO)
I found that quite frustrating and limiting, I imagine, given that clojure doesn't have a type system you can shoot yourself in the foot pretty easily; however there are cases where functions in the
clojure.string
module return collections (split for example), and I would like to be able to compose them. I found a way by implementing the basic flip function (line #13) which is not really difficult, this might not work in other scenarios though.Just observations and opinions, that's all.