clojure

Zip two lists in clojure into list of concatenated strings

被刻印的时光 ゝ 提交于 2019-12-19 18:49:40
问题 Instead of zip-mapping two lists to get: (zipmap ["a","b","c"] ["c","d","e"]) = {"c" "e", "b" "d", "a" "c"} I want to concatenate the first element of the first list with the first element of the second list and so on to get: ("ce","bd","ac") or in the reverse order. 回答1: You can do that with map . map can take multiple collections, it takes the next element from each collection and passes them into the function passed as the first argument (stopping when one of the collections runs out). So

Clojure macro to create a synonym for a function

夙愿已清 提交于 2019-12-19 18:23:07
问题 Probably an easy one for anyone who actually knows how to write macros in any Lisp. I want to be able to define synonyms for function names. I've been copy-and-paste hacking core.clj to do this, but I don't want to be such a dunce forever! It seems obvious a macro that rewrites the call to a synoym-function into a call to the original function is the right way to do it. 回答1: If I understand your question, there's an easier way: def the new symbol to the old function. user=> (def foo +) #'user

How to cast a character to int in Clojure?

僤鯓⒐⒋嵵緔 提交于 2019-12-19 15:26:12
问题 How to cast a character to int in Clojure? I am trying to write a rot 13 in clojure, so I need to have something to cast my char to int. I found something called (int), so I put: (int a) Get: CompilerException java.lang.RuntimeException: Unable to resolve symbol: a in this context, compiling:(NO_SOURCE_PATH:13:1) Then I put: (int 'a) Get: ClassCastException clojure.lang.Symbol cannot be cast to `java.lang.Character clojure.lang.RT.intCast (RT.java:1087) Then: (rot13 ''a') Get:

How to cast a character to int in Clojure?

微笑、不失礼 提交于 2019-12-19 15:26:07
问题 How to cast a character to int in Clojure? I am trying to write a rot 13 in clojure, so I need to have something to cast my char to int. I found something called (int), so I put: (int a) Get: CompilerException java.lang.RuntimeException: Unable to resolve symbol: a in this context, compiling:(NO_SOURCE_PATH:13:1) Then I put: (int 'a) Get: ClassCastException clojure.lang.Symbol cannot be cast to `java.lang.Character clojure.lang.RT.intCast (RT.java:1087) Then: (rot13 ''a') Get:

Applying a map to a function's rest argument

只愿长相守 提交于 2019-12-19 13:40:42
问题 In Clojure, if I have a function f , (defn f [& r] ... ) and I have a seq args with the arguments I want to call f with, I can easily use apply : (apply f args) Now, say I have another function g , which is designed to take any of a number of optional, named arguments - that is, where the rest argument is destructured as a map: (defn g [& {:keys [a b] :as m}] ... ) I'd normally call g by doing something like (g :a 1 :b 2) but if I happen to have a map my-map with the value {:a 1 :b 2}, and I

Standard version or idiomatic use of (fn [f & args] (apply f args))

╄→гoц情女王★ 提交于 2019-12-19 13:31:16
问题 Every so often I find myself wanting to apply a collection of functions on several collections of parameters. It's easy to do with map and a very simple function. (map (fn invoke [f & args] (apply f args)) [- + *] [1 2 3] [1 2 3] [1 2 3]) (-1 6 27) Searching the web turns up quite a few libraries that define a similar function, often called funcall or invoke. Because of Clojure's penchant for variadic functions, I cannot help but think there should already be a default version of this

Standard version or idiomatic use of (fn [f & args] (apply f args))

狂风中的少年 提交于 2019-12-19 13:31:06
问题 Every so often I find myself wanting to apply a collection of functions on several collections of parameters. It's easy to do with map and a very simple function. (map (fn invoke [f & args] (apply f args)) [- + *] [1 2 3] [1 2 3] [1 2 3]) (-1 6 27) Searching the web turns up quite a few libraries that define a similar function, often called funcall or invoke. Because of Clojure's penchant for variadic functions, I cannot help but think there should already be a default version of this

What happens when I pass arguments to a Clojure symbol?

♀尐吖头ヾ 提交于 2019-12-19 13:06:52
问题 If I do this: ('a 'b 'c) I get this: c Why? 回答1: The link Hauleth posted is a good overview to symbols but the answer to your question is that calling a symbol as a function is equivalent to looking that symbol up in the first argument. ('a 'b) is equivalent to (get 'b 'a) The documentation for get shows that you can pass an optional third argument as the default. In your example 'c is treated as the default and returned since 'b is not a map and 'a can't be found. 来源: https://stackoverflow

In clojure how to map over overlapping pairs?

喜你入骨 提交于 2019-12-19 11:46:49
问题 Say I have the sequence: [1 2 3 4 5] And I want to map over them in the pairs: [(1, 2), (2, 3), (3, 4), (4, 5)] I have tried: (map f (partition 2 [1 2 3 4])) But this results in the sequence of pairs: [(1, 2), (3, 4)] How can I get the desired functionality? 回答1: By default partiton returns non-overlapping partitions, but you can supply a step argument to provide the offset at which partitions are created: clojure.core/partition ([n coll] [n step coll] [n step pad coll]) Returns a lazy

Append to an attribute in Enlive

纵然是瞬间 提交于 2019-12-19 09:46:53
问题 Is it possible to append a value to an attribute using enlive? example: I have this <a href="/item/edit/">edit</a> and would like this <a href="/item/edit/123">edit</a> I am currently doing this: (html/defsnippet foo "views/foo.html" [:#main] [ctxt] [:a] (html/set-attr :href (str "/item/edit/" (ctxt :id)))) But I would prefer not to embed the URL into my code, by just appending the id to the existing URL (html/defsnippet foo "views/foo.html" [:#main] [ctxt] [:a@href] (html/append (ctxt :id)))