clojure

How do I implement foreach loop in clojure?

℡╲_俬逩灬. 提交于 2019-12-21 03:27:42
问题 Implementing a for loop in clojure seems to be easy, but how can I implement a foreach statement that reads each element in the list(vector) and does something? like this... (foreach i list expression) Thanks in advance! 回答1: Just replace for with doseq and you're all set. Don't use map , which is just as lazy as for . 回答2: map is the functional equivalent of foreach , whereas doseq is for imperative programming with side-effects. map takes a function f and a seqable collection coll and

As a data container, what are the main differences between vector and list

社会主义新天地 提交于 2019-12-21 03:13:23
问题 Say we need a list of numbers, there are two definitions: (def vector1 [1 2 3]) (def list2 '(1 2 3)) So what are the main differences? 回答1: The [1 2 3] is a vector, whereas '(1 2 3) is a list. There are different performance characteristics of these two data structures. Vectors provide quick, indexed random access to its elements (v 34) returns element of vector v at index 34 in O(1) time. On the other hand it is generally more expensive to modify vectors. Lists are easy to modify at head and

How to filter vector of maps by multiple keys in Clojure

我与影子孤独终老i 提交于 2019-12-21 03:03:06
问题 Assume we have a datastructure like this one: (def data (atom [{:id 1 :first-name "John1" :last-name "Dow1" :age "14"} {:id 2 :first-name "John2" :last-name "Dow2" :age "54"} {:id 3 :first-name "John3" :last-name "Dow3" :age "34"} {:id 4 :first-name "John4" :last-name "Dow4" :age "12"} {:id 5 :first-name "John5" :last-name "Dow5" :age "24"}])) I have learned how to filter it by one key, for example: (defn my-filter [str-input] (filter #(re-find (->> (str str-input) (lower-case) (re-pattern))

retrying something 3 times before throwing an exception - in clojure

社会主义新天地 提交于 2019-12-21 02:52:14
问题 I don't know how to implement this piece of Python code in Clojure for i in range(3): try: ...... except e: if i == 2: raise e else: continue else: break I wonder why something so simple in Python is so hard in Clojure. I think the difficulty is because Clojure is a functional programming language and thus is not suitable for such an imperative task. This is my attempt: (first (remove #(instance? Exception %) (for [i (range 3)] (try (......) (catch Exception e (if (== i 2) (throw e) e)))))))

Sleeping a thread inside an ExecutorService (Java/Clojure)

亡梦爱人 提交于 2019-12-21 01:06:33
问题 I have a rather massive number of threads being created inside a clojure program: (import '(java.util.concurrent Executors)) (def *pool* (Executors/newCachedThreadPool)) (defn do-something [] ; work Thread/sleep 200 ; repeat) (dotimes [i 10000] (.submit *pool* do-something)) It's been a while between JVMs for me and I am basically wondering here if there is any argument against using sleep or yield inside the function that is being executed by the Executor? If I understand correctly, in this

How would the 'Model' in a Rails-type webapp be implemented in a functional programming language?

Deadly 提交于 2019-12-20 20:39:08
问题 In MVC web development frameworks such as Ruby on Rails, Django, and CakePHP, HTTP requests are routed to controllers, which fetch objects which are usually persisted to a backend database store. These objects represent things like users, blog posts, etc., and often contain logic within their methods for permissions, fetching and/or mutating other objects, validation, etc. These frameworks are all very much object oriented. I've been reading up recently on functional programming and it seems

How would the 'Model' in a Rails-type webapp be implemented in a functional programming language?

风格不统一 提交于 2019-12-20 20:38:28
问题 In MVC web development frameworks such as Ruby on Rails, Django, and CakePHP, HTTP requests are routed to controllers, which fetch objects which are usually persisted to a backend database store. These objects represent things like users, blog posts, etc., and often contain logic within their methods for permissions, fetching and/or mutating other objects, validation, etc. These frameworks are all very much object oriented. I've been reading up recently on functional programming and it seems

What is the correct term for the following functional programming pattern?

余生长醉 提交于 2019-12-20 19:42:09
问题 I've heard it referred to as a stream, as an infinite list, and sometimes even as a lazy sequence. What is the correct term for the following pattern? (Clojure code shown) (def first$ first) (defn second$ [str] (cond (empty? str) () true ((first (rest str))))) (defn stream-builder [next_ n] (cons n (cons (fn [] (stream-builder next_ (next_ n))) ()))) (defn stream [str n] (cond (= 0 n) () true (cons (first$ str) (stream (second$ str) (- n 1))))) (def odd (stream-builder (fn [n] (+ 2 n))1))

How do you make a binary search tree in Clojure?

好久不见. 提交于 2019-12-20 19:35:16
问题 In Scheme, I can use define-struct to make a binary search tree, but how do you do it in Clojure? 回答1: You can use structmaps. To define one: (defstruct bintree :left :right :key) To make an instance: (struct-map bintree :left nil :right nil :key 0) You can then access the values in the struct like this: (:left tree) etc. Or you can create new accessor functions: (def left-branch (accessor bintree :left)) and use it: (left-branch tree) 回答2: I don't know Clojure, but I bet it's the same way

Alternatives to java on android

时光总嘲笑我的痴心妄想 提交于 2019-12-20 18:32:17
问题 I just got myself an android phone and I'm dying to start coding on it ! However I'm not a big java fan, although I can live with that, I would like to know if there're reasonable alternatives for the android virtual machine. I've done a medium sized project using clojure, however from the reviews I read, it's very slow when running on android. How about scala ? I read that some people did experiments with it in android, is it "fast enough" ? How big is the learning curve ? Cheers, Ze Maria