Hot answers tagged

3

For quote-word-count, there's a few things to note. I think this is a good use of ->>. Personally, I would have started the threading with (range quote-count) instead of splitting that though. From my experience, needlessly elongating the thread call just hurts readability. I also recommend maintaining the same type of object being threaded all the ...


3

I don't have any performance related suggestions unfortunately. I do have a couple cleanup suggestions though. The bounds checking function can be cleaned up a bit by making use of "comparison chaining" similar to what you'd use in Pytbon: (defn within-bounds? [[row col] width height] (and (< -1 row width) (< -1 col height))) In the ...


3

You are right to want to avoid creating the nested structure to begin with. I imagine you read my answer in the question you linked (the second one). As there, here the solution is to use mapcat instead of map. And as I also say in the comments there, while mapcat-indexed does not exist, you can just pass an extra (range) argument to get numbering. (defn ...


3

Initial comments for only the dfs function: Variable names start makes sense for the initial point. is-goal? is- is not necessary, the question marks already indicates that it's a predicate function, so name it goal?. get-successors is a pure function that will return the same answer given the same point, pure functions don't need a verb. Verbs are to ...


2

Looks pretty good to me. There are a few shortcuts you could add if you don't mind using the Tupelo Clojure library. One of them is vals->map and with-map-vals. Called with a list of symbols like (vals->map a b c) returns a map like {:a a :b b :c c}: (let [a 1 b 2 c 3] (vals->map a b c)) ;=> {:a 1 :b 2 :c 3} } Given a map like ...


2

I'd never thought of using reduce for this. Neat! However, you can simplify mk-balanced? a little. str->chars is redundant: reduce and the other sequence functions accept strings as such. You can use reduced to short circuit a reduce. The reduced can return any non-empty sequence: no need for [:false]. The simplified version is ... (defn mk-balanced? [...


2

We want to exclude from the vector (more generally a sequence) the maps of which any map in the blacklist is a sub-map. Let's define a function that tests whether its first argument is a sub-map of its second argument: (defn submap? [a b] (every? (fn [[k v]] (= v (b k))) a)) For instance, => (map #(submap? {1 2} %) [{} {1 1} {1 2}]) (false false ...


2

Depending on your final goal, you may be interested in the Tupelo Forest library. Here is a sample of what you can do (data converted to the "tree" format): (dotest-focus (with-forest (new-forest) (let [data {:tag "program" :state "here" ::tf/kids [{:topic "Books" ...


2

All that the lazily-find-prime-factors function needs is the (primes) sequence. You just keep trying each in turn. Your smallest-factor-of drops the failed factors, but forgets what it has done, so you have to do it again for the recursive call. And I'd rename your is-factor-of? function: => (is-factor-of? 4 2) true => (is-factor-of? 2 4) false ... ...


2

Three suggestions. Carciginate repaired the syntax of your gen-primes algorithm. It can, though, be significantly simplified: (defn gen-primes [] (letfn [(primes-step [table d] (letfn [(reinsert [table prime] (update table (+ prime d) conj prime))] (if-let [factors (get table d)] (recur (...


2

First, working top-down, your triggers at the top should be in sets (#{}), not vectors (you'll see why a little farther down): (def sure-triggers #{"Does this cryogenic chamber make me look fat?" "4?"}) (def whatever-triggers #{"Tom-ay-to, tom-aaaah-to." "Let's go make out behind the gym!" "It's OK if you don't want to go to the DMV." "...


2

As you have already noticed, this can be viewed as a case of mutual recursion. To demonstrate this I give the following mutually recursive definitions: (defn sort-parts [work] (lazy-seq (sp-loop work))) (defn sp-loop [[part & parts]] (if-let [[pivot & xs] (seq part)] (sp-loop ;or recur for tco (let [smaller? #(< % pivot)] (...


2

We are far from from beta-reduction here. The "reduction" can only be understood using explicit instruction flow semantics. No. You have chosen to interpret loop and recur in this way, but that is not how Clojure understands them. recur is simply a flag for a tail-recursive call, functional as you like. loop is a gloss on defining and applying a ...


2

Unfortunately, I've never used spec, reframe or clojurescript before, but I can note general Clojure things that can be fixed up. filter-people has far too much nesting. Having code like you do affects readability (you need to read it bottom-up instead of top-down like you would most code), and makes it harder to add to (as you'll need to need adjust ...


1

This is nice code for someone just learning Clojure. You haven't made any of the common pitfalls like trying to use def to create local variables. I'm going to just jump around and mention things as I see them. Your parse-int function is much slower (and arguably more manual) than it needs to be. If you run lein check on your project, you'll see the ...


1

This code is quite good for someone learning the language. You haven't made any of the common errors such as using def inside of function instead of let. I don't see anything really outright wrong with the code, so I'll just be commenting on style and things that I think can be improved. I'll just go top to bottom after addressing some things you mentioned. ...


1

I've played around with your code to try to understand it, frankly. After a long sequence of changes, I ended up with the following: (defn primes [n] (let [mark (fn [i di v] (reduce (fn [w i] (assoc w i di)) v (range i (count v) di))) [answer &_] (reduce (...


1

I don't really see anything major. I also don't know Cljs (only Clj), so I apologize if a suggestion of mine doesn't apply to Cljs. Just a few small things: Personally, I don't like having function definitions all on one line. Increasingly, I'm even splitting def definitions over two lines. I find it generally helps readability. I'd change your definitions ...


1

Easy-peasy. Just imagine walking around the rectangle starting at the origin in a counter-clockwise direction, and keeping track of the length traversed: (defn random-edge-coord [width height] (let [len-1 width len-2 (+ width height) len-3 (+ (* 2 width) height) len-4 (* 2 (+ width height)) pos (rand len-4)] (cond ...


1

I like what you have done and much enjoyed getting to grips with it. Let's take a look. The Clojure reader provides a syntax tree of sequences, which your algorithm traverses vertically, by recursive descent and horizontally by double shunting an operator stack and a value stack. Let's look at the role of next-op - the last binding in the loop. It's a ...


1

First, yes, this is functional, and yes, this is how to use loop properly. . . . using recur on every loop iteration instead of mutating variables. This is the intent of loop. Instead of mutating a variable, you just pass the "altered data" to the next iteration via recur. You also aren't abusing side effects by misusing def or atoms, so that's good. ...


1

First, your calls to .ba aren't being resolved for other, so that's forcing use of reflection. If you run lein check, you'll see: Reflection warning, thread_test.clj:22:22 - reference to field ba on java.lang.Object can't be resolved. This has the potential to slow the method down, although it only happens once per call, so the effect wouldn't be huge. ...


1

For some reason I can't open the link to the question site. But "Returns [X] if given phrase is one of the following inputs", to me, does not mean it only returns that for that inputs. It also doesn't say you should return nil otherwise. If we take the listed inputs to be test cases; then baking them in the implementation is not good. I understand the ...


Only top voted, non community-wiki answers of a minimum length are eligible