Fork me on GitHub
#beginners
<
2020-08-11
>
hiredman00:08:47

hiccup expects vectors to start with tags

hiredman00:08:09

you are passing in a vector [(html ... that is a tag and a body

nando00:08:30

So what should I do instead?

hiredman00:08:38

don't do that

hiredman00:08:38

you likely can get rid of both the wrapping vector and the extra call to html

nando00:08:50

(defn home
  [request]
  (response
   (layout[[:div.container
                  [:h1.display3 "Hello there!"]]
                         ])))

nando00:08:19

The above produces this error "[:div.container [:h1.display3 "Hello there!"]] is not a valid element name."

nando00:08:43

(defn home
  [request]
  (response
   (layout[:div.container
                  [:h1.display3 "Hello there!"]]
                         )))

nando00:08:57

Ah ha! The above works

Rowan Barnard02:08:27

Hi all, I've been learning from the book Getting Clojure the past few weeks and I am just running a code example but not getting the results as expected from the text. Here is the code: (​defn​ chatty-vector [] (println ​"Here we go!"​) [1 2 3])

Rowan Barnard02:08:10

Followed by: (​def​ s (lazy-seq (chatty-vector))) and (first s)

Rowan Barnard02:08:59

That last is meant to trigger the println expression to be executed according to the book but it doesn't for me - I tried both in Atom+Chlorine as well as on the PowerShell commandline REPL with clj tool

Rowan Barnard02:08:34

Is this some behaviour that is changed in Clojure since the book came out or perhaps an error in the book?

Noah Bogart02:08:27

how do i import or require a protocol from a different namespace?

dpsutton02:08:09

just require the namespace in which they are defined

Noah Bogart02:08:52

ope, okay. got myself confused with records and importing. thanks

Ty05:08:32

Does anyone know the book Rich is talking about in Are we there yet?

seancorfield06:08:07

(here's the transcript of Rich's talk https://github.com/matthiasn/talk-transcripts/blob/master/Hickey_Rich/AreWeThereYet-mostly-text.md -- that site has transcripts of several other talks by him and others)

andrewzhurov11:08:22

Is there a way to select a shape? Maybe with EQL or clojure.spec

;; if shape is described with clojure.spec
(s/def ::meaningful ...)

(defn save-meaningful! [org]
  (-> org
      (clojure.spec/select ::meaningful) ;; desired feature, ensures only speced shape is getting through {:meaningful-key "meaningful value"}
      (transact-org!)
      ))

(save-org! {:meaningful-key "meaningful value"
            :trash-key "trash value"})
(select-keys ...) works for this simple case, but let's imagine it is a deep map

Jelle Licht12:08:01

how can I express the following (Java) method annotation @Plan(trigger=@Trigger(factchanged="time")) in Clojure? So how do I express the 'nested-ness' of these annotations?

chrisblom14:08:15

can you explain what these annotations do?

Alex Miller (Clojure team)14:08:43

annotations are expressed in Java on code - are you using gen-class or deftype or something? there is (limited) support for annotations in interop

Noah Bogart15:08:21

do protocol interfaces support variable argument lists yet? or is this (https://clojuredocs.org/clojure.core/defprotocol#example-542692d2c026201cdc326f80) still the best work around?

Noah Bogart15:08:48

reasoning: i have a macro that creates an if branch, where it either adds or doesn't add a specific call in the parameter list. this works in "normal" function calls because parameter length isn't checked unless execute. but in a protocol method, the parameter length is checked at compile. I can add an extra method signature that has a plz-dont-use parameter, but that feels like it's opening me up to bugs in the future.

Noah Bogart15:08:39

code example, from after macro expansion:

(if use-eid#
  (handler (first costs) state side new-eid# card actions)
  (handler (first costs) state side new-eid# (make-eid state eid) card actions))

Noah Bogart15:08:14

handler here is the protocol method

Alex Miller (Clojure team)15:08:39

no, they do not support varargs

Scott Starkey19:08:50

Hi folks - I’m working on a game. I have a sequence of 16 vectors - 4 of one set of keys, 4 of another set of keys:

([:a :w] [:a :x] [:a :y] [:a :z] [:b :w] ... [:d :z])
(Note: The data won’t be neatly ordered like this.) I’d like to think of this data as 4 rows x 4 columns. I’ve figured out how to evaluate the rows for a test condition, but I’m having problems evaluating the columns. Maybe it would be easier to turn the grid 90° and then just use the same tests for the rows. Any thoughts on how to tackle this problem?

Noah Bogart19:08:55

a row is (partition 4 coll), right?

Noah Bogart20:08:45

so a column within a row would be (nth row index)

Noah Bogart20:08:15

where index is a number between 0 and 3

Noah Bogart20:08:49

oh, you're asking about the entire column i bet

Scott Starkey20:08:02

yes, the whole column

Noah Bogart20:08:58

row * number-of-columns + column is the formula for each cell in the column

Noah Bogart20:08:56

forgot to mention, lol, number-of-columns is 1-indexed and column is 0-indexed

Noah Bogart20:08:42

(1-indexed isn't quite correct, it's just the natural means of counting)

Scott Starkey20:08:02

Maybe a couple of nested for statements?

Noah Bogart20:08:07

that could work!

noisesmith20:08:04

if you don't have substructure, you want a single for comprehension with multiple clauses

👆 3
noisesmith20:08:03

in C / Java "for" is a statement that makes a loop, in Clojure it's a comprehension that generates a lazy sequence

noisesmith20:08:26

we don't have statements

Noah Bogart20:08:04

and if you're selecting a single column, you only need a single clause: (for [r (range 4)] (nth coll (+ 3 (* r 4))))

noisesmith20:08:31

@scotto if you store the data in a more structured way, the form you present here is easy to derive - is there a reason it needs to be spread out like this?

Scott Starkey20:08:08

No, @noisesmith I could structure it differently if that would help things.

Noah Bogart20:08:40

the non-math way would be (->> data (partition 4) (map #(nth % 3)))

👍 3
Noah Bogart20:08:10

when it's only 16 items long, seems like not much to be gained by keeping it as a 1-d array

Noah Bogart20:08:56

forgot to mention, lol, number-of-columns is 1-indexed and column is 0-indexed

noisesmith20:08:27

consider for example {:a {:w {} :x {} :y {} :z {}} :b {:w {} :x ... ...} ... ... :d {... ... :z {}}

noisesmith20:08:13

depending on what you are using the data for - this structure is friendly for get-in for finding data at a specific nested index (and update via update-in or assoc-in)

Scott Starkey20:08:03

Here’s the thing - I want to make a test to see if there are 3 in a row of one of the keys, vertical or horizontal. I am wanting to filter/remove those from the possible permutations. There are a metric-butt-load of permutations, effectively a lazy sequence.

(combo/count-permutations tiles)
  ; => 20922789888000

noisesmith20:08:32

clearly I didn't understand what you were doing here

Scott Starkey20:08:41

sorry! My fault.

noisesmith20:08:18

you can use (partition 3 1 ...) and map a function that detects repeats of column or row

noisesmith20:08:15

that might not be straightforward to apply at the filtering step though

Scott Starkey20:08:37

Yeah, I already have the function working that detects if 3 of a sequence of 4 is a match of either key.

Scott Starkey20:08:57

But only for rows.

Scott Starkey20:08:57

Hell, it might be easier to make a rotate 90 degrees function that takes the sequence [%1 %2 %3 %4 %5…. %16] and converts it to [%1 %5 %9 %13 %2 … %16]. I can hand-code that but it seems icky.

Scott Starkey20:08:06

OK, I managed to rotate it using the for statements mentioned above. Onward!

(defn rot90 [s]
  "Takes a sequence of 16 vector pairs and rotates it 90 degrees."
  (into [] (for [x (range 4)] (into [] (for [y (range 4)] (nth s (+ x (* y 4))))))))

noisesmith20:08:45

@scotto does the same thing, more idiomatic:

(defn rot-90 [s] (apply mapv vector (partition 4 s)))

noisesmith20:08:27

also it's immediately obvious how to parameterize it if your size ever changes from 4

Scott Starkey20:08:32

(not familiar with the mapv function… Looking that bad-boy up.)

sova-soars-the-sora21:08:14

map, apply, reduce, lifeblood of the sequentialist

phronmophobic21:08:34

don't you mean the cons-equentialist?

😄 3