Fork me on GitHub

so I'm working through some Hackerrank problems, the one I am working on is to compute e^x for the first 10 terms with a given value x The following is my solution, it works just fine, but I am curious if someone can let me know how this can be simplified. In addition, as per the problem description, def/defn are not allowed (for some stupid reason).


reduce is necessary when later iterations depend upon values calculated in earlier ones, but here it looks like there is no dependency, so map would also work.


I don't see e mentioned anywhere in that code, so not sure how it could be working


it is calculating the first 10 terms in the series expansion for e^x


I'm not quite clear how to use map here, can you explain?


reduce could be taken advantage of to reduce the number of arithmetic operations required, I believe, since the N-th term appears to be equal to the (N-1)-th term divided by N, or something similar to that.


(map (fn [v]
       (/ (Math/pow x v)
          (reduce * (range 1 (inc v)))))
     (range 0 10))


I believe the reduce part of your code is equivalent to the map expression above. Note that your res arg is not used anywhere.


except to conj onto the end of a vector


hmmm right.. my thinking was it was building a vector which is again reduced with addition. But I see your point.. trying to figure out it now.. basically that reduce is superfluous


Changing that to map makes the code smaller and for many people, simpler to understand its intent.


As mentioned above, if you find a way to calculate the (N+1)-th term from the N-th term with fewer arithmetic operations, then reduce could be taken advantage of to implement that.


using map there is quite a bit cleaner. Thank you! This is why I wanted feedback 🙂


where is the proper place/folder to put your data spec in leiningen?


I don't think there is only one proper place -- it can be put in any source file you want.


alright, thanks


@cybersapiens97 At work we tend to put our data specs in their own namespaces. If there are any functions in with them, they tend to be just predicates used in the specs and perhaps some utilities that operate on the data. But that's just our "tendency" and we break those rules if it makes sense 🙂

Lennart Buit06:10:28

Hey there, I am trying to spec methods of protocols, but that doesn’t seem to be supported. Now this:!topic/clojure/f068WTgakpk is suggesting to wrap protocol functions in ordinary functions and spec those. What I am wondering tho, the thread seems to suggest that is better anyway to wrap protocol functions, but those functions will never be more than a simple facade right? Why is this an accepted practice?


Hello, can anyone explain to me how clj performs module management?


I'm wondering specifically if it's possible for a namespace to not have a folder component (or what I think is a folder component)


Do you mean: Is it possible to create a Clojure namespace, but not create a file in a correspondingly named directory and file name in the file system that corresponds to it?


If so, the answer is yes, but not sure if you are asking that question.


@andy.fingerhut More along the lines of that (ns implies a folder structure of foo/bar.cljs, so is it possible to have a (ns foo) with the file foo.cljs.


though with some fiddling I've figured out that early errors that caused this confusion came from a typo elsewhere


and that it's possible


I am working through clojurekoans however am stumped at this problem (= 25 ( (fn [n] (* n n))))

Lennart Buit09:10:32

You can format code by using backticks (` )

Lennart Buit09:10:01

Like so: (fn [a] (+ a 6))


I have an answer but I cannot get head around solution


being fn [n f] (f n)) 5


It does not make sense. as the solution does not have sufficient arguments in the first place never mind the fact it responds by calling the second function with an argument.


Any ideas? I have 2 days on this problem and cannot find and explanation anywhere


...and in English....I have spent 2 days....


The complaint is the lack of arguments so it fails


Why does this work?


apologies noobie

Space Guy09:10:49

OK, a solution I'm getting is:

;; at the top, next to 'square'
(defn use-5-on-this [f]
  (f 5))
"Higher-order functions take function arguments"
(= 25 ( use-5-on-this
   (fn [n] (* n n))))
Did either of your revious solutions work? I get an exception when I try those


the solution at the bottom of the snippet works


the real states true

Space Guy09:10:19

OK, didn't see the rest of that snippet. Will check


but why would it work - if there is only 1 argument?


It seems that it is cheating and getting a partial result?

Space Guy09:10:08

The second argument is the (fn [n] (* n n)) (the newline doesn't matter)


yep that's correct


(zxc 5 nil) doesn't return a funciton, rather it is a direct call

Space Guy09:10:15

((fn [a b] (b a)) 5 (fn [n] (* n n)))

( (fn [n] (* n n)) 5 )

(* 5 5)



ok - got it - the second function is an argument!!!


You can make it even more concise using the reader syntax, like so: (= 25 (#(% 5) (fn [n] (* n n))))


Of course - me being blinkered and all!

Michael Fiano10:10:26

If I have a byte array, what is a good way to get the sequence of bytes from the start and up to but not including a given other sequence of bytes?

Michael Fiano10:10:12

I won't know the length, and I'd like to do this without interop


@mfiano how about (let [idx (clojure.string/index-of s p)] (if (nil? idx) s (subs s 0 idx))) Of course the string s and pattern p would have to be strings. Are you looking for efficiency?


Hi all. Do you create intermediate collections when using the threading macros in clojurescript? -> ->>


yea it just turns threaded code into ‘normal’ code, (->> xs (map #(* 3 %)) (filter odd?)) becomes (filter odd? (map #(* 3 %) xs)), you can look at this with macroexpand. If the ‘normal’ code uses intermediate collections then yes it will use intermediate collections, the answer to your question is most of the time yes.


You could also use transducers in the above case to avoid intermediate aggregates.


Thanks for the answers, yes I'm considering using transducers but just wasn't sure if the threaded variant was skipping intermediate collections already.


if you use tranducers then the answer is no, most the time I use threading I dont use transducers. When in doubt macroexpand (if your in the repl you can just paste you code with ' in front (macroexpand '(->> xs (map #(* 3 %)) (filter odd?)))) and then reason about the code.

👍 1

I'm seeing namespaced keywords printing out like this at my REPL. Is that a "feature" or is something configured wrong? #:x{:foo 1} when I type {:x/foo 1}


Oh, it's some kind of "extract the common namespace" feature by the looks. {:x/foo 1 :y/foo 2} doesn't do the same thing.


This is intentional new behavior in Clojure 1.9


Any ideas appreciated


((println .... is pretty much always a bug


(a b c) is calling a with the arguments b and c. ((println 1 2) 3) is calling the result of println (which is always nil) on the argument 3


OK, so I removed at the println and still get the same result being ....NullPointerException user$zxc.invokeStatic (:2)


you like still left the outer parens


It wasn't the println alone that was the problem, it was that you had an unnecessary set of parens around the body of your defn


without that unnecessary set of parens, the println would be ok


but I have removed the printlns and still get the same error


unnecessary is too mild a term -- harmfully unnecessary extra parens.


parens is short for parentheses ()


user=> ((println) (loop []))

NullPointerException   user/eval11 (NO_SOURCE_FILE:2)
user=> ((loop []))
NullPointerException   user/eval149 (NO_SOURCE_FILE:3)


You had something like this (defn zxc [a b] ((println a b "*****") ...)). We are recommending you change that to (defn zxc [a b] (println a b "******") ...)


Please read my previous message very carefully


you are now trying to call the result of the loop as a function


parentheses are significant in Clojure and other Lisp languages. You can't remove them when they are needed, and you can't add the when they are unneeded.


(...) means function application so ((loop [...] ...)) means evaluate the loop then invoke the result as a function, and your loop evaluates to nil


In many programming languages, and mathematics, you can add redundant parentheses without changing the meaning. That isn't true in Clojure.


ok so the problem is thew loop eventually resolves to a nit as there is nothing to process, whereas you infer it should present the answer as the result? Correct?


the problem is the loop returns nil, and you are invoking the result of the loop as a function


so you are trying to call nil as a function


(nil), or (any-expression-here-that-returns-nil), causes an exception


nil is fine. (nil) is an error


when it is code at least


you had (a b) where a was the println expression and b was the loop expression, because println returns nil, you had (nil b) which is an error, you removed the println and left yourself with (b) where b is still the loop expression, and that loop expression (just like the println) returns nil, so you have (nil) which is still an error


whats the best way to create a memoized or cached variable dependent on 2 arguments? I want to make a client thats dependent on 2 values passed in from a config and i want to keep referencing it later on. should i use:

(def client
    (fn [client-config]
but at the same time the fconfig will not change for this during system run time so instead cant i just cache the value? but it seems overkill to use cache. defonce pass in a function?


can comment in thread so we dont clutter the channel


Are you saying that if you memoize it, then the cache will never contain more than 1 set of arguments, because those are the only arguments the function will ever be called with during a run of your program?


delay makes sense as well


to cache the results of this function at the only place that calls it.. aka client start-up


Yeah, given your answer of "yes" to my question, delay make sense.


memoize should be able to handle functions with any number of arguments, but they must be arguments for memoize to "know" about them.


and must be values that return true when you call clojure.core/= to compare them, if you want the advantages of memoization, so mutable Java objects are a bit out of scope for memoization.

👍 1

if you know js you have something like function () {return null;}()()


(not exactly that because not everything in js is an expression)


Excuse my noobieness ....loop [x a y b] - I understood that the use of a b here is to assign values to X and Y so I am confused here


So on the recur x and y would pick up the values


sorry, my a b were meant as meta syntactic variables and not related to the a and b in your program


you had an expression like ((println ...) (loop ...)) which is a list containing two items (println ...) and (loop ...)


a list means function application


so you were apply the result of (println ...) to the result of (loop ...)


the result of (println ...) is always nil, and that is an error


you removed (println ...) from the expression and were left with ((loop ...))


((loop ...)) is a list containing a single item (loop ...)


so you are calling the result of (loop ...) as a function with no arguments


I get the loop is a "single" function


no, you don't


loop is not a single function


line 3 has a harmfully unnecessary left parentheses that should be deleted, as well as the one it matches.


It changes the meaning of what your program does in a way you do not want.


(W) means invoke W as a function, whatever it is, for you, in your program, W is the expression that looks like (loop ...)


so the loop runs, and the result of the loop is nil, and then you invoke that nil


your ifs also are not correct


I'd suggest using an editor that at least highlights matching parens, and even better one that supports some kind of structural editing (like paredit)


I am using rainbow paren and paredit in ms code


(the incorrect ifs appear to have been introduced when deleting the println)


the second if isn't actually nested in the first (as I assume you meant it to be)


with paredit in emacs, either of two structural editing commands would have removed the issue with the first version of the program, raise-sexp on the loop expression would have deleted the surrounding ((println ...) ....), and splice-sexp would have removed just the surrounding function call


Hi not using emacs ms VScode, nevertheless I will have to think about what you have stated and come back with a better solution 😉


to both of you


no worries. As at least one alternative, I have successfully edited a lot of Clojure code without paredit, too, and it isn't everyone's cup of tea. You don't need them to be effective in a Lisp. You do need to know where the parens are needed, and where they are not.

Sy Borg23:10:20

sorry for a stupid question - how to run leiningen 2.8.1 with clojure 1.9.0? it still uses 1.8.0


change the version in your project.clj

Sy Borg23:10:52

I mean repl


the repl’s classpath and clojure version are based on the dependencies in your project.clj

Sy Borg23:10:20

I created project.clj in lein home dir, it worked. before there was only profile.clj. thanks, Alex


i don't get, why the default version is 1.8.0 ?


because if you are doing something where you get the default version you are doing it wrong


Because the Leiningen developers have not chosen (yet) to release another version of Leiningen that defaults to Clojure 1.9. Perhaps there have not been significant changes to Leiningen since then to justify a new release.


lein is a build tool, but because clojure didn't ship with a very friendly launcher people pushed to shoe horn in the ability to run lein repl without a project.clj, but it was always a silly feature, lein is a build tool and you should run it in some project context with a project.clj that defines the version of clojure you use


The latest version of Leiningen is 2.8.1 -- when that was released, Clojure was at clojure-1.9.0-beta3 so Leiningen defaults to 1.8.0 when used outside a project. I am a bit shocked there hasn't been a release of Leiningen since Clojure 1.9 dropped on December 8, 2017!