Fork me on GitHub
#beginners
<
2016-06-10
>
andrewzhurov05:06:27

@seancorfield: That's cool approach. Thanks for respond.

yogidevbear10:06:09

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``````

yogidevbear10:06:52

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)?

yogidevbear10:06:43

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` approach

st10:06:40

Hi @yogidevbear I would separate a) the process on the elements of the list and b) display all elements of a list

yogidevbear10:06:25

@st: I can separate out the max function as `(defn abs [n] (max n (- n)))`

yogidevbear10:06:39

Is that what you mean?

st10:06:40

``````(def l [2 -4 3 -1 23 -4 -54])
(run! println (map abs l))
``````

st10:06:05

let’s decompose

st10:06:44

`(map abs l)` will return the list of absolute values of your input

st10:06:05

and `run! println` will display each element

yogidevbear10:06:38

Is there a major difference doing `(map abs l)` and `(apply abs l)`?

st10:06:06

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

yogidevbear10:06:08

Ok cool, that makes sense

yogidevbear10:06:59

So if I want to run any particular functions / modifiers against items in a list/sequence, then I'd make use of map?

st10:06:20

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

yogidevbear10:06:49

Will try 🙂

st10:06:53

> So if I want to run any particular functions / modifiers against items in a list/sequence, then I'd make use of map? yes

yogidevbear10:06:17

Haven't done too much practical coding in Clojure yet, but starting to push through the initial hump of learning

yogidevbear10:06:32

Thanks for the help

st10:06:30

welcome in Clojure world 😉 , http://www.4clojure.com is an excellent place to practice

yogidevbear10:06:03

I tried it initially, but got a bit stuck with the initial questions so working through the FP section on hackerrank

yogidevbear10:06:22

Will definitely come back to it once I'm a little more familiar with the language

senya2215:06:14

I'm seeing slower results in reducers/fold that with reduce, wondering why:

senya2215:06:28

``````(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``````

senya2215:06:58

Similarly here:

senya2215:06:03

``````(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``````

senya2215:06:19

wonder if criterium adds its own overhead

donaldball15:06:51

Essential viewing before engaging in performance analysis: https://www.youtube.com/watch?v=0tUrbf6Uzu8

jeff.engebretsen15:06:34

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.

yogidevbear15:06:53

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

yogidevbear15:06:02

I think it was Clojure Programming

jeff.engebretsen15:06:06

Hm. One’s O’Reilly the other is Pragmatic, heh.

seancorfield15:06:18

Joy of Clojure is great but it's the "why" of Clojure so folks often recommend it is not a "first book" when learning.

seancorfield15:06:52

Living Clojure is better as an introductory book.

jeff.engebretsen15:06:27

Hm. I’ve done some programming in it before. I’ve done an Alexa Skill in it and the Brave and True examples.

seancorfield15:06:58

Then Living Clojure is the right level for you.

seancorfield15:06:13

After that buy Joy of Clojure.

seancorfield15:06:51

I did JoC "first" but I have a background in FP and quite a bit of Lisp (from decades ago).

jeff.engebretsen15:06:59

Does Living Clojure have examples that you work through?

jeff.engebretsen15:06:32

Oh yup. A seven-week training course.

jeff.engebretsen15:06:33

Cool thanks for the feedback.

Tim16:06:41

hello, is there a way I can before an action if `is` gives a failure?

Tim16:06:50

kind of like a try/catch block

Tim16:06:30

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

Tim16:06:49

I suppose I could write my own `is` macro

Tim16:06:01

but is there an easier way?

jeff.engebretsen16:06:13

Sounds like a strange test. Isn’t the `is` result the whole point of the test?

senya2219:06:40

@donaldball: took a listen - your point being that it's just an dark art?

donaldball19:06:07

Well, sure. More specifically that running criterium in a repl is okay for order-of-magnitude analyses probably but that’s about it

senya2220:06:51

I guess parallelizing in this case is just not efficient