Fork me on GitHub
#beginners
<
2016-09-14
>
olegakbarov10:09:27

how do i get the index of current item in for while iterating the vector of maps?

val_waeselynck11:09:54

@olegakbarov I don't think for has this feature, you may want to use map-indexed instead

olegakbarov11:09:32

@val_waeselynck thanks, looks you’re totally right

olegakbarov11:09:46

one more thing: does this looks like idiomatic way to rearrange items in vector?

(defn rearrange [arr a b]
  "a and b are both indexes and arr is vector"
  (let [item-a (nth arr a)
        item-b (nth arr b)
        b4-a (subvec arr 0 a)
        btwn (subvec arr (+ a 1) b)
        aftr-b (subvec arr (+ b 1))]
     (concat b4-a btwn [item-b] [item-a] aftr-b)))

val_waeselynck12:09:17

@olegakbarov seems ok, you should consider that it does not return a vector, and AFAICT it will still hold a reference to the old vector

olegakbarov12:09:08

if i wrap it into into [] does it looses reference ? is it better in terms of performance?

val_waeselynck12:09:59

@olegakbarov into is eager so yes, it will loose reference. It's not generally better in terms of performance, it depends on the use case. Using concat and subvec makes current implementation return a (lazy) sequence very fast, but it will not support random access reads, and traversing it may be slower than traversing a vector.

val_waeselynck12:09:29

I usually make an implementation that works and avoid worrying about the performance until necessary 🙂

olegakbarov12:09:50

that makes sense, thanks!

olegakbarov12:09:46

btw, my solution works only for a < b

sveri12:09:51

sounds like a good usecase for spec

olegakbarov12:09:41

@sveri could you please go in more details?

sveri12:09:49

@olegakbarov You could use the :fn part to make sure the invariant a < b is enforced by spec: http://clojure.org/guides/spec#_spec_ing_functions

olegakbarov13:09:03

thanks, i’ll take a look!

pawel.kapala13:09:41

I have a map that I thread through a number of functions like this:

(-> my-map
    (foo params)
    (bar params)
    (baz (repeatedly 10 some-fn))) ;; I’d like to use apply on baz here
As a next step I'd like to use apply to add a number of params to baz: (defn baz [mymap & args]). What is the best way to achieve that? Thanks!

akiva14:09:27

I’d go with as-> instead of ->.

akiva14:09:33

(as-> my-map $
    (foo $ params)…)
etc.

pawel.kapala14:09:10

@akiva, thanks! I looked up docs for https://clojuredocs.org/clojure.core/as-&gt; and I found example I think I was looking for, and for reference I’m gonna go with:

(-> my-map
    (foo params)
    (bar params)
    (as-> xs (apply (partial baz xs) (repeatedly 10 some-fn)))
I learned as-> today! Big thanks!

seancorfield16:09:47

@pawel.kapala Good to see you here on Slack!

marciol18:09:31

Hey plp, I have very basic notions of clojure, but I want to focus more on clojurescript. What would be the best path given these premises.

marciol18:09:56

I need to master clojure itself before jump straight on clojurescript?

pawel.kapala18:09:44

@marciol clojure differs a bit from clojurescript, but they intersect, AFAIK 😉

pawel.kapala18:09:57

personally I really liked using reframe as a starting point to clojurescript (but at the time I knew some clojure TBH)

sveri18:09:06

@marciol You dont need to know clojure to learn clojurescript, just like you dont need to know Java to learn Javascript

sveri18:09:22

the difference is, if you know clojure or clojurescript you know like 95% of the other language

marciol18:09:09

hmm @sveri , good to know, so the best source to learn currently would be clojurescript unraveled?

sveri18:09:53

@marciol I dont know what the best source for learning clojurescript is. I started both at the same time and learning clojure I was able to write cljs anyway

sveri18:09:04

The hardest part for me was javascript interop

pawel.kapala18:09:56

@sveri Ha! because you need to actually know javascript 🙂 Had same thing: I’m no javascript ninja 😆

marciol18:09:06

@pawel.kapala, @sveri I know a bit more about javascript

sveri18:09:33

@pawel.kapala It depends on your usecase. I needed javascript interop for existing libraries like charts or so. If you dont need that, you can get along with pure clojurescript

sveri18:09:00

Also I did know javascript long before, but had trouble with the interop and how to do that correctly

marciol18:09:35

great, I will focus more on clojurescript side, given that my backend development currently is focused on RoR. Eventually I will use clojure in backend side too, but for while the clojurescript is the current target.

sveri18:09:30

good luck