This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-06-10
Channels
- # admin-announcements (2)
- # arachne (2)
- # beginners (53)
- # boot (52)
- # cider (7)
- # cljs-dev (61)
- # cljsrn (12)
- # clojure (61)
- # clojure-greece (22)
- # clojure-nl (16)
- # clojure-russia (103)
- # clojure-spec (84)
- # clojure-uk (15)
- # clojurescript (137)
- # community-development (14)
- # cursive (4)
- # datomic (14)
- # devcards (6)
- # euroclojure (3)
- # funcool (26)
- # hoplon (27)
- # jobs (4)
- # lambdaisland (1)
- # leiningen (1)
- # om (75)
- # onyx (77)
- # planck (15)
- # proton (2)
- # re-frame (23)
- # ring-swagger (9)
- # schema (1)
- # specter (95)
- # untangled (124)
- # yada (27)
@seancorfield: That's cool approach. Thanks for respond.
Hopefully a quick question. I'm trying to write a function that takes a list of integers and then needs to convert all the negative numbers in the list to their positive equivalents. I've got it working, but I'm sure there must be a more elegant way to do it. Here is my function:
(fn[lst]
(dotimes [x (count lst)]
(if (neg? (nth lst x))
(println (+ 1 (bit-not (nth lst x))))
(println (nth lst x)))))
And here is an example of the data going in:
2
-4
3
-1
23
-4
-54
Is there a way that I could do something along these lines (fn[lst] (apply (max x (- x)) lst))
where x
is a reference to the current integer in lst
that is being apply
'd to (if that makes sense)?
I've refined the initial solution to exclude the bit-not
operator:
(fn[lst]
(dotimes [x (count lst)]
(if (neg? (nth lst x))
(println (- (nth lst x)))
(println (nth lst x)))))
But I'd really like to find out if I could use the apply
approachHi @yogidevbear I would separate a) the process on the elements of the list and b) display all elements of a list
@st: I can separate out the max function as (defn abs [n] (max n (- n)))
Is that what you mean?
Is there a major difference doing (map abs l)
and (apply abs l)
?
yes, map
is generally used to apply (sorry for the confusion) a function on elements of a sequence
and apply
is used to pass arguments to function in a list instead of one by one
Ok cool, that makes sense
So if I want to run any particular functions / modifiers against items in a list/sequence, then I'd make use of map?
one general remark (if I may), try to avoid to loop elements of a sequence as in your first example. Prefer map
, filter
. reduce
… functions
Will try 🙂
> So if I want to run any particular functions / modifiers against items in a list/sequence, then I'd make use of map? yes
Haven't done too much practical coding in Clojure yet, but starting to push through the initial hump of learning
Thanks for the help
welcome in Clojure world 😉 , http://www.4clojure.com is an excellent place to practice
I tried it initially, but got a bit stuck with the initial questions so working through the FP section on hackerrank
Will definitely come back to it once I'm a little more familiar with the language
(require '[criterium.core :refer :all])
=> nil
(quick-bench (reduce + (range 1 1000000)))
Evaluation count : 72 in 6 samples of 12 calls.
Execution time mean : 8.611557 ms
Execution time std-deviation : 379.934603 µs
Execution time lower quantile : 8.279878 ms ( 2.5%)
Execution time upper quantile : 9.226505 ms (97.5%)
Overhead used : 1.381211 ns
Found 1 outliers in 6 samples (16.6667 %)
low-severe 1 (16.6667 %)
Variance from outliers : 13.8889 % Variance is moderately inflated by outliers
=> nil
(require '[clojure.core.reducers :as r])
=> nil
(quick-bench (r/fold + (range 1000000)))
Evaluation count : 54 in 6 samples of 9 calls.
Execution time mean : 11.730933 ms
Execution time std-deviation : 62.820550 µs
Execution time lower quantile : 11.656258 ms ( 2.5%)
Execution time upper quantile : 11.813079 ms (97.5%)
Overhead used : 1.381211 ns
Found 1 outliers in 6 samples (16.6667 %)
low-severe 1 (16.6667 %)
Variance from outliers : 13.8889 % Variance is moderately inflated by outliers
=> nil
(def numbers (range 1 1000000))
=> #'user/numbers
(quick-bench (reduce + numbers))
Evaluation count : 78 in 6 samples of 13 calls.
Execution time mean : 8.290750 ms
Execution time std-deviation : 217.324063 µs
Execution time lower quantile : 8.089783 ms ( 2.5%)
Execution time upper quantile : 8.635626 ms (97.5%)
Overhead used : 1.381211 ns
Found 1 outliers in 6 samples (16.6667 %)
low-severe 1 (16.6667 %)
Variance from outliers : 13.8889 % Variance is moderately inflated by outliers
=> nil
(quick-bench (r/fold + numbers))
Evaluation count : 60 in 6 samples of 10 calls.
Execution time mean : 10.575012 ms
Execution time std-deviation : 197.249105 µs
Execution time lower quantile : 10.435835 ms ( 2.5%)
Execution time upper quantile : 10.908811 ms (97.5%)
Overhead used : 1.381211 ns
Found 1 outliers in 6 samples (16.6667 %)
low-severe 1 (16.6667 %)
Variance from outliers : 13.8889 % Variance is moderately inflated by outliers
=> nil
Essential viewing before engaging in performance analysis: https://www.youtube.com/watch?v=0tUrbf6Uzu8
I’m looking at getting a Clojure book. I’m considering The Joy of Clojure or Living Clojure. Do ya’ll have a preference between those two or a different one you highly recommend? I’ve gone through most of Clojure for the Brave and True already.
I can't really say for definite as I'm still learning, but I've heard good things about The Joy of Clojure. Someone also recommended either Clojure Programming or Programming Clojure but I can't remember which of the two titles it was
I think it was Clojure Programming
Hm. One’s O’Reilly the other is Pragmatic, heh.
Joy of Clojure is great but it's the "why" of Clojure so folks often recommend it is not a "first book" when learning.
Living Clojure is better as an introductory book.
Hm. I’ve done some programming in it before. I’ve done an Alexa Skill in it and the Brave and True examples.
Then Living Clojure is the right level for you.
After that buy Joy of Clojure.
I did JoC "first" but I have a background in FP and quite a bit of Lisp (from decades ago).
Does Living Clojure have examples that you work through?
Oh yup. A seven-week training course.
Cool thanks for the feedback.
for example in my tests, I have some stuff in a catch block but I realized I just want the stuff to execute if is fails
Sounds like a strange test. Isn’t the is
result the whole point of the test?
@donaldball: took a listen - your point being that it's just an dark art?
Well, sure. More specifically that running criterium in a repl is okay for order-of-magnitude analyses probably but that’s about it