clojure

Clojure into-array iterate over array [closed]

时光总嘲笑我的痴心妄想 提交于 2019-12-13 09:26:59
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . Im trying to make the change to Clojure from a primarily Java only experience and i am trying to solve a problem in Clojure. I can do it in Java and it shouldnt be too much harder to convert to Clojure but i just cannot see how it would work.... What i want to do is iterate over an array of strings

Empty Maps in Clojure

隐身守侯 提交于 2019-12-13 09:12:08
问题 I keep getting empty maps when i am passing data into two hash maps in clojure, i know that the data is being sent through the functions as i have used print statements that show it correct data, its just when i assoc the data to the map it doesnt appear to do anything and leaves me with {} Can anyone see what i am doing wrong?? (defn sort-string [y] (apply str (sort y))) (defn get-unique [y] (let [x (sort-string (str/lower-case y)) hashmap1 (hash-map) hashmap2 (hash-map)] (if-not (contains?

how can you interleave two vectors of differing lengths in clojure

限于喜欢 提交于 2019-12-13 08:05:35
问题 What is the simplest way to interleave two vectors with n+1 and n members? (def a [:a :c :e]) (def b [:b :d]) (interleave a b ); truncates to shortest list [:a :b :c :d] ;what I would like. (interleave-until-nil a b) [:a :b :c :d :e] 回答1: Cons the first, interleave the rest with arguments reversed. (cons (first a) (interleave b (rest a))) ;=> (:a :b :c :d :e) 回答2: Conj nil to the second, interleave colls get all butlast (butlast (interleave a (conj b nil))) ;=> (:a :b :c :d :e) 回答3: (defn

Raising elements in a vector to a power

谁都会走 提交于 2019-12-13 07:27:24
问题 I am trying to input a vector and parameter p, which in turn should raise each element of the vector to the power p. So far I have tried mapping the numeric tower function power, but that has proved unsuccessful. What would be the easiest way to raise each element of a vector to a power p? (defn p' [x p] (map power x p)) 回答1: You need something like: (defn compute [exp numbers] (map #(power exp %) numbers)) For more information, type the following in your REPL: (doc map) 回答2: To expand on

How to print each elements of a hash map list using map function in clojure?

匆匆过客 提交于 2019-12-13 06:45:48
问题 I am constructing a list of hash maps which is then passed to another function. When I try to print each hash maps from the list using map it is not working. I am able to print the full list or get the first element etc. (defn m [a] (println a) (map #(println %) a)) The following works from the repl only. (m (map #(hash-map :a %) [1 2 3])) But from the program that I load using load-file it is not working. I am seeing the a but not its individual elements. What's wrong? 回答1: In Clojure

Clojure list inside list declaration

自作多情 提交于 2019-12-13 06:25:07
问题 I was trying to declare a list inside list in Clojure. Expected behavior: `(`()) => (()) Actual behavior: `(`()) => ((clojure.core/list)) What does that output mean? Also, I would like to understand how the behavior below is consistent. `() => () `("hi") => ("hi") `(`()) => ((clojure.core/list)) Unrelated to my question, here's a code snippet which actually returns (()) : (conj `() `()) 回答1: Basically, don't nest quotes. I'm going to use the basic quote special form here, but the same

Use of ^ in clojure function parameter definition

早过忘川 提交于 2019-12-13 05:39:02
问题 (defn lines "Given an open reader, return a lazy sequence of lines" [^java.io.BufferedReader reader] (take-while identity (repeatedly #(.readLine reader)))) what does this line mean? -> [^java.io.BufferedReader reader] also I know this is a dumb question. can you show me the documentation where I could read this myself? So that I don't have to ask it here :) 回答1: You can find documentation here: https://clojure.org/reference/java_interop#typehints Clojure supports the use of type hints to

How to use list as parameters

微笑、不失礼 提交于 2019-12-13 05:18:38
问题 I am new to Clojure so this question may be trivial: Suppose you have a list (\h \e \l \l \o) and you want to use it as the actual parameters when calling a function, for example, str in order to get the same result as: (str \h \e \l \l \o) I came to a solution using eval: (def paramlist '(\h \e \l \l \o)) (eval (conj paramlist str)) But I understand that is quite dirty and over killing the problem. What is the best solution to do this? 回答1: Use apply user=> (apply str '(1 2 3 4)) "1234" 来源:

Friend authentication empty param

北城以北 提交于 2019-12-13 03:36:49
问题 I am trying to implement friend authentication on my web app but when i try to login i get this */login?&login_failed=Y&username=*...my param is empty and i cant login,what am i doing wrong? these are my routes... (defroutes routes (GET "/" [] (index)) (GET "/indexMessage" [] (indexMessage)) (GET "/login" req (index)) (POST "/insert-user" {params :params} (let [firstname (get params "firstname") lastname (get params "lastname") email (get params "email") password (get params "password") sex

Importing a namespace into the lein repl and refer to it

只愿长相守 提交于 2019-12-13 02:58:16
问题 In a file I can do this: (:require [clojurewerkz.neocons.rest :as nr]) how can I import this into the repl and still be able to refer to it by 'nr'? thanks 回答1: Lee's answer is right, of course, but why do you need to quote? The vector [...] is evaluated and the values inside too, and here both clojurewerkz.neocons.rest and nr are treated as variables, which are unbound (you do have an error message, don't you?). You can also choose to quote the symbols: (require ['clojurewerkz.neocons.rest