This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-11-25
Channels
- # bangalore-clj (5)
- # beginners (225)
- # boot (36)
- # cider (1)
- # clara (2)
- # cljsjs (1)
- # clojure (76)
- # clojure-belgium (1)
- # clojure-conj (1)
- # clojure-india (4)
- # clojure-italy (5)
- # clojure-korea (1)
- # clojure-russia (22)
- # clojure-spec (35)
- # clojure-uk (52)
- # clojurescript (67)
- # community-development (17)
- # core-logic (2)
- # cursive (2)
- # datascript (28)
- # datomic (44)
- # emacs (1)
- # funcool (3)
- # hoplon (14)
- # lein-figwheel (2)
- # leiningen (2)
- # luminus (3)
- # midje (3)
- # mount (2)
- # nyc (2)
- # om (54)
- # om-next (1)
- # onyx (30)
- # re-frame (57)
- # reagent (19)
- # ring-swagger (23)
- # slack-help (10)
- # spacemacs (2)
- # specter (1)
- # vim (23)
hey question I note that you can avoid problems with order of functions via (declare), is there any reason clojure doesn't just do this for you?
it's because a clojure file should sort of represent an interaction you have with a REPL, having top level declarations automatically would require the whole file to be parsed once just for that purpose (JavaScript compilers usually does this).
thanks that sounds reasonable
although sometimes I find I want to use functions in other functions its not TOO cumbersome to write (declare foo) just annoying
emacs + cider it initially works if I forget to put them in order it just fails when I reconnect later
my project per se is a big disordered pile of functions designed not towards any real end but to learn
going through clojure for the brave and true and the little schemer
@michaelmrose and do you like the first book. I liked the book but found the exercises to hard
I just read that declare
is discouraged
somewhere in the docs
It helps in learning what a namespace does if you know you can start reading it from the bottom up. You lose that if you use declare. Granted, sometimes there's situations where using declare does make your namespace more readable
declare
is fine, not discouraged
but generally the recommendation is order your fns to minimize the need for it
Clojure requires this because it’s a single-pass compiler, which has huge benefits in simplicity and performance
thanks, but does it makes any difference according to repl : (= '( 1 2 ) [ 1 2])
are the same
The code I have is this :
(defn my_nth_drop [s n]
(loop [ s1 s
acc [] ]
(if (empty? s1 )
(flatten acc)
(recur (drop n s1 ) (conj acc (take ( - n 1) s1 ))))
))
you can't do it with a map, because map does something for every thing in a collection, thus not changing the size. you could do it with a reduce.
@roelofw: Clojure is not weakly typed. What you're encountering is that all sequentials (list, vector, seqs) are compared in the same equality partition. That is, they only check whether they contain the same elements in the same order (and not the type of the collection). This is a pragmatic choice that helps in blurring the line between collections and seqs.
@alexmiller oke, thanks
Anyone a example where reduce takes more then 1 argument like my function does with take n ?
If you want to use a reduce, you can def. do that. Just keep conj'ing and skip the conj when the count
is n
. @roelofw
oke, and set the counter to zero because let's say I have ( 1 2 3 4 5 6) and I want to delete all the 3th this schould be the answer ( 1 2 4 5 )
The docs for carmine (a redis client https://github.com/ptaoussanis/carmine) recommend setting up a connection pool and access macro like this:
(def server1-conn {:pool {<opts>} :spec {<opts>}})
(defmacro wcar* [& body] `(car/wcar server1-conn ~@body))
What is the advantage of using a macro here instead of something like
(def wcar* (partial car/wcar server1-conn))
Maybe I misunderstand what you’re saying, I’m asking why not define wcar*
(I should change the name) as a partial instead of as a macro.
Well, it also wouldn't work, you need it to be a macro since you can't evaluate your (car/ping)
(etc) which would happen if you passed it to a function
You asked "can I do the same with for example a map or reduce function", I said "If you want to use reduce..."
out of my head , I would do something like this : (fn [s count] (reduce (fn [acc, n] (if ( == ??? count) (acc) ((conj acc (take ( - n 1))) s ))
"Keep adding to the list (reduce conj [] xs)
, but skipping if the list is at n
elements (if .... ..... ......)
"
the function with a loop :
(defn my_nth_drop [s n]
(loop [ s1 s
acc [] ]
(if (empty? s1 )
(flatten acc)
(recur (drop n s1 ) (conj acc (take ( - n 1) s1 ))))
))
the only thing I have read that loop schould be avoided and map or reduce can be better be used
so if n = 3 then I take 3 characters out of the given seq and then I can drop the next one
in your reducing function, decide if you care about the particular item (conj it) or dicard it
oke, but then I have to know if it's a first , second or thirth item in the list , so I have to count , right ?
we cannot use reduce as far as I know because reduce uses 2 variables acc for the output and n for the current item
Besides [[acc output] n]
would let you have three values in two arguments. That’s a pattern I use quite a bit.
if our counter has reached the top, don't conj, otherwise conj and increases the counter?
(fn [s n] (reduce (fn [[ acc output] x] (if (= acc n ) [ 0 output] [ (+ acc 1) (conj output x)] ))))
(you’re missing an argument to reduce
)
nope, I see this error message :
CompilerException java.lang.RuntimeException: Unable to resolve symbol: in this context, compiling:(C:\Users\rwobb\AppData\Local\Temp\form-init842216484282399039.clj:1:714)
I have now this :
(fn [s n] (reduce (fn [[ acc output] x] (if (= acc n ) [ 0 output] [ (+ acc 1) (conj output x)] )) s ))
you're also gonna need to figure out if we should use 0 based counting or 1 based counting
This seems to be compiling : '(fn [s n] (reduce (fn [[ acc output] x] (if (= acc n ) [ 0 output] [ (+ acc 1) (conj output x)] )) [] s )) `
we gave it the more complicated first parameter that it is destructuring. its also gonna look at the acc thing first.
no, I mean I see compile errors when doing this :
(fn [s n] (reduce (fn [[ acc output] x] (if (= acc n ) [ 0 output] [ (+ acc 1) (conj output x)] )) [ 0 [] ] s ))
When I change the zero as initial value with 1 , it's looking better , I see then [3 [1 2 4 5 6]]
Nope , when I do ( (fn [s n] (last(reduce (fn [[ acc output] x] (if (= acc n ) [ output] [ (+ acc 1) (conj output x)] )) [ 0 [] ] s))) '( 1 2 3 4 5 6 ) 3)
. I see this error : ClassCastException clojure.lang.PersistentVector cannot be cast to java.lang.Number clojure.lang.Numbers.add (Numbers.java:128)