Fork me on GitHub
#beginners
<
2017-10-15
>
mikeyford12:10:29

Having a bit of trouble with my answer to problem 22 (http://www.4clojure.com/problem/22) on 4clojure.. my answer

#(reduce + (for [i %] (let [x 1] x)))
seems to trip the alarm for count although I don't explicitly call it. Is that because count is being used in the for source perhaps?

Lucas Barbosa13:10:12

Yes, for uses count. Here’s the source: https://github.com/clojure/clojure/blob/clojure-1.9.0-alpha14/src/clj/clojure/core.clj#L4576 What about #(reduce (fn [c _] (inc c)) 0 %)?

mikeyford13:10:57

Ah yes, that's nice and introduces me to the underscore to learn about 🙂 thanks

noisesmith15:10:18

there's not much to learn about _ - it's just our preferred name for things we don't use, but as far as clojure is concerned it's just a name

noisesmith15:10:56

(let [_ 2] (+ _ _)) => 4 - don't write code like that,it just demonstrates that _ is a name convention, not a syntax

mikeyford15:10:11

Thanks for the clarification.

ErhardtMundt13:10:47

I'm using enlive to parse some html page and I came to the point I have this problem:

<div id="whoa"><span class="blah">garbage</span>interesting stuff</div>

ErhardtMundt13:10:56

I want to extract only interesting stuff

ErhardtMundt13:10:05

and ignore garbage

ErhardtMundt13:10:53

I tried with [:#whoa html/text-node] but I get garbageinteresting stuff

cgrand13:10:18

@erhardt.mundt [:#whoa (html/left :.blah)] should do

ErhardtMundt13:10:40

thanks @cgrand!

ErhardtMundt14:10:53

is it possible to use the -> macro with forms that don't expect the chained result as first parameter?

sundarj14:10:34

@erhardt.mundt

(-> [1 2 3] (as-> coll (map inc coll)))
(2 3 4)

sundarj14:10:48

(-> [1 2 3] (->> (map inc)) first)
2

ErhardtMundt14:10:57

thanks @sundarj

sundarj14:10:09

no prob 🙂

mseddon16:10:42

is there a reason why map-invert is in clojure.set? I find it a bit confusing

noisesmith16:10:09

inverting relations is a set operation, right?

noisesmith16:10:24

see also project, rename-keys etc.

hmaurer16:10:45

a map is a function where the domain are keys are the codomain are values (both sets). map-invert inverts that function, swapping the domain and the codomain?

hmaurer16:10:02

wild guess

noisesmith16:10:05

exactly that

mseddon17:10:33

fair enough 🙂

yogidevbear20:10:15

I have a question about Clojure immutability and def. Am I right in assuming that Clojure is immutable in the sense that I cannot modify data inside an existing Clojure collection (as opposed to a Java collection which can be mutated), but this has nothing to do with defining data using def? def can be used to redefine a data collection which has nothing to do with Clojure being immutable? Here is an example of what I'm referring to with mutating an existing collection:

(def initial-data (long-array (range 5)))
(seq initial-data) ;; (0 1 2 3 4)
(def test-j (seq initial-data))
(def test-c (seq (into-array Long/TYPE initial-data)))
(seq test-j) ;; (0 1 2 3 4)
(seq test-c) ;; (0 1 2 3 4)
(aset initial-data 0 99)
(seq initial-data) ;; (99 1 2 3 4)
(seq test-j) ;; (99 1 2 3 4)
(seq test-c) ;; (0 1 2 3 4)

hmaurer20:10:15

@yogidevbear you are right

noisesmith20:10:20

def creates a var, a var is a mutable container

yogidevbear20:10:39

Perfect, thanks for the clearer definition 🙂

yogidevbear20:10:00

I thought that was the case so glad I'm not getting it completely wrong

noisesmith20:10:08

we have many ways of holding immutable data in a mutable container - var, atom, ref, agent

hmaurer20:10:48

@yogidevbear Clojure takes the pragmatic approach of letting you use mutability in places where it is convenient, and provides you the tools to deal with it. I don’t think you would ever want to mutate a var at runtime (@noisesmith?), but Clojure has “atoms”, “refs” and “agents” which all provide some way to mutate data at runtime in a controlled manner

hmaurer20:10:57

(e.g. ensuring atomic updates, etc)

yogidevbear20:10:26

That all makes sense :+1:

noisesmith20:10:33

@hmaurer well, the repl is a kind of runtime of sorts, but yes, you want to hold something in another container if you intend to mutate it programmatically

hmaurer20:10:59

@noisesmith oh right, but the repl is a special-case in my mind

hmaurer20:10:10

since you want to iteratively develop by re-defining things

drewverlee21:10:17

Anyone know how to convert a time instance into a datetime?

drewverlee21:10:52

i feel like this should be something like (clj-time.core/from-inst %)

drewverlee22:10:23

FML i didn't see the coerce lib, there goes an hour of my life.

drewverlee22:10:14

also, a more fudenemtal question "how does the #inst "..." syntax even work?

jumar08:10:02

@drewverlee here's some info: https://clojure.org/guides/weird_characters#tagged_literals Here are the default-data-readers defined in core.clj: https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj#L7681 And here's the DispatchReader defined for "#" in LispReader.java https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LispReader.java#L106 TaggedLiteral-s are then read in LispReader's readTagged method: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LispReader.java#L1418

yogidevbear22:10:58

Hi again 🙂 I was wondering about shorthand function syntax #(). Is it possible to pass in multiple arguments to the shorthand function and then refer to them using something like the % syntax from within the shorthand function. Like (filter #(true? (:some-key %)) some-collection), but for multiple arguments (not just one argument of some-collection)

drewverlee22:10:12

I think for the short hand anymous function you can only use the index of the argument 1 2 3 4... so %1 %2 %3...

hmaurer22:10:43

@drewverlee and %&

yogidevbear22:10:00

Thanks @drewverlee. I tried that, but it didn't work the first time which is why I was asking here. I must have had a silly typo somewhere

drewverlee22:10:40

@yogidevbear can you post what you have?

yogidevbear22:10:09

(reduce #(+ %1 (:keyword %2)) 0 coll)

yogidevbear22:10:50

☝️ this is working

debamitro22:10:53

Thanks, trying that