Fork me on GitHub
#beginners
<
2017-03-27
>
danp05:03:26

Hi all, just looking for a bit of clarification on sequential collections. I'm reading the Joy of Clojure, and it states they're ordered sets of values, and this link also confirms: http://insideclojure.org/2015/01/02/sequences/

danp05:03:24

but when I enter `(sequential? [2 3 1)` it returns `true`.

danp05:03:44

The term ordered in this context, does it just mean that the order matters, whereas with something like a hashmap, the order doesn't matter?

Prakash06:03:39

yes. that is basically it.

danp06:03:49

Ace, thanks - thought so too, but would rather get confirmation than assume I've got it ðŸ™‚

jdeisenberg20:03:38

Is it possible to do something like this (I know syntax here is wrong, but you get the idea): `(def m {:x 3, :y 2, :z (+ :x :y)})`

greg_arcara20:03:01

Hello, if I have two arrays of integers and I want to take the greater of the two, but also have a condition say the max < 4 then to double the max, but only the first x times that max < 4, what would be the idiomatic way to express that? Do I have to use a recur or is there a way to accomplish that w/ map/reduce?

tbaldridge20:03:14

@jdeisenberg you can use a `let` there

jdeisenberg20:03:15

@tbaldridge The idea is I want to use a key that's already in the map to define something later on in the map. (Presume that instead of 2 and 3 I had some really long, complex expressions)

tbaldridge21:03:15

@greg_arcara you can do that in a reduce, although it gets a bit tricky. Basically have the accumulator of the reduce be a hashmap and update it as needed.

jdeisenberg21:03:17

@tbaldridge Ah, of course! Why didn't I think of that?!:thinking_face:

greg_arcara21:03:33

@tbaldridge can I do the reduction with two arrays?

tbaldridge21:03:45

ah you have two arrays....

greg_arcara21:03:50

yea, that's my issue

greg_arcara21:03:00

I can do it with one ðŸ™‚

tbaldridge21:03:25

`map` can take two seqs, so you can do: `(map vector a b)`

tbaldridge21:03:44

then either operate on the `first` or `second` of those elements

tbaldridge21:03:05

`(map vector a b)` is `zip` in Python (or some other languages you may know)

greg_arcara21:03:39

that's a good approach

jdeisenberg21:03:51

@tbaldridge Still not sure how the code would look to get that value for `:z` since I'd have to access the map currently being defined.

greg_arcara21:03:03

@tbaldridge thanks, I'll give this a go

tbaldridge21:03:36

@jdeisenberg you'd have to do this:

``````(def m (let [x 5 y 4]
{:x x :y y :z (+ x y)}))``````

jdeisenberg21:03:54

@tbaldrige Got it; thanks.

jdeisenberg21:03:09

(Sorry about the mistype on the name there)

greg_arcara23:03:34

@tbaldridge thanks so much for your help, I was able to make the solution work.