Fork me on GitHub
#beginners
<
2017-10-09
>
stvnmllr201:10:47

I'm guessing there is an easy way to escape [:span "&times;"] in cljs hiccup. Anyone know of one?

stvnmllr201:10:55

If not, I'll just use an image in this case. Thanks.

madstap01:10:21

@stvnmllr2 In reagent I've done the following [:span {"dangerouslySetInnerHTML" {"__html" "&times;"}}]

madstap01:10:33

I think that's a react thing, probably works in om, rum etc as well

seancorfield01:10:56

@stvnmllr2 You might want to look at 2.0.0-alpha1 -- I know safe HTML by default is planned for 2.0.

seancorfield01:10:23

(I assume the 2.0 Hiccup does both clj and cljs but didn't check)

kennytilton03:10:28

@tkjone I use loop when I want to juggle a little state while iterating. hth

athomasoriginal15:10:37

It does help! Do you have an example I could reference?

noisesmith17:10:51

if you are consuming a collection in order, reduce is simpler and performs better, FYI. It can only use one accumulator, but you can use a collection and hold multiple values in the accumulator.

kennytilton19:10:04

Sorry, @tkjone, just saw this. Hope my code-quoting works:

kennytilton19:10:12

(let [elts [1 2 3 5 7 4 6 8 5]]
    (loop [wall false
           [e & more] elts]
      (let [new-wall (if (even? e) (not wall) wall)]
        (when new-wall
          (println :wall-is-true new-wall e))
        (recur new-wall more))))

kennytilton19:10:01

Your requirements were trickier than I realized, btw, but that should give you an idea.

noisesmith20:10:57

the reduce version of that is

(reduce (fn [wall e]
          (let [w (if (even? e) (not wall) wall)]
            (when w
              (println :wall-is-true w e))
            w))
        false
        [1 2 3 5 7 4 6 8 5])

vinai08:10:55

Is there a "standard" approach to set up a rdbms schema (e.g. postgresql) the first time an app is run?

stvnmllr211:10:39

@vinai https://www.clojure-toolbox.com/ Database Migrations I've used migratus and Ragtime

vinai11:10:20

Thanks! Thats a nice reference

Kari Marttila13:10:10

I found today a really weird behaviour with insert-multi! and Redshift jdbc driver. If I give to insert-multi! two rows like [["id1" 1.1] ["id2" 2.2]], I get values 1.1 and 2.2 in Redshift. But if I give rows [["id1" 1] ["id2" 2.2]] then I get values 1 and 2 (not 1 and 2.2) in Redshift. I was wondering that this should probably not be the default behaviour (the first row is used to interpret the type of columns or something weird like that)?

rinaldi14:10:53

@vinai I don't know if I understand your question correctly but it seems you're looking for a nice pattern to keep db connection self contained. If that's the case I would recommend Stuart Sierra's component.

vinai15:10:18

@rinaldi Thanks, I am using component - I was just wondering about setting up the schema. At the moment I was simply calling a "create table fi not exists..." in the component start. It works okay, but somehow it felt ... off. Can't put my finger on why really.

vinai15:10:13

On that note - how do you prefer to serialize edn for storage (for example in a db table). JSON and keywordize-keys? prn-str and clojure.edn/read? transit?

sundarj16:10:00

i would use transit - it'll be the fastest, and most compact

noisesmith16:10:47

+1 on transit - it’s not recommended for long term storage but it round trips, and it’s extensible with local rather than global config (another huge bonus)

vinai17:10:05

What are the downsides of prn-str and clojure.edn/read-string though? Not trying to avoid transit here, only wondering...

vinai17:10:52

As a data format, edn is more readable and maybe even more stable.

vinai17:10:16

@U051SS2EU I've using transit to send data over the wire and it works great. But I don't understand the local vs. global config. Can you give me a pointer to local vs global extensibility config for transit?

noisesmith17:10:53

sure - with read-string, to define a data reader / writer for a new type that previously wasn’t readable as edn, you need to modify clojure’s global *data-readers* and the global clojure.core/print-method multimethod - you can’t define a printer / reader combo local to one context

noisesmith17:10:59

with transit, there’s an optional key called :handlers, and another called :default-handler and those control how things are read or written in local scope

vinai17:10:04

Thanks - digesting, but makes sense

rinaldi15:10:24

That might be naive but what came to mind is that delay might be helpful? From the docs: >Takes a body of expressions and yields a Delay object that *will invoke the body only the first time it is forced* (...)

vinai15:10:40

Hm, interesting, never used delay far.

athomasoriginal18:10:00

If I have an anonymous function with several when forms, is there a way to break? For example:

(fn [] 
    when condition 
        expression 1
        expression 2  ;; <-- break here - don't try to evaluate the next when
    when condition 
       expression 1)

sundarj18:10:29

no. Clojure is a functional language, and breaking is an imperative construct - you would have to wrap both whens in an appropriate if if you want to branch

vinai18:10:02

You could throw an exception cough cough

athomasoriginal18:10:29

Yikes. Imperative programming throwing me off the path again. Given this situation, here is what my code looks like. Is there a better way to handle this?

(reduce (fn [inBetween element]
                  (p "")
                  (p "break")
                  (when (or (identical? element this) (identical? (get @appState :lastChecked) element))
                    (p "1")
                    (set! (.. element -checked) true)
                    (not inBetween))

                  (when (true? inBetween)
                    (p "2")
                    (set! (.. element -checked) true)
                    true)

                  (when (false? inBetween)
                    (p "3")
                    false))
              false
              (get-checkboxes)))
I also wrote the above in cond etc but because I want to use side-effects, I figured the when would be more appropriate given that it is wrapped in a do

noisesmith18:10:39

if you only want one of the cases to run, cond with do is probably more appropriate

athomasoriginal18:10:37

Good point. @rinaldi would condp work in this scenario as my predicate is different for each case?

rinaldi18:10:01

I tend to use condp for situations where I want to react to specific values. Not sure if it suits your needs but came to mind when reading through your snippet.

athomasoriginal21:10:48

Okay, implemented a solution https://github.com/tkjone/clojurescript-30/tree/master/12-check-multiple-checkboxes If there are interested parties, this is a very small program and I would be very interested to learn if there are ways to improve this and make it as idiomatic as possible

sb18:10:38

Hello, what is the best test repository (like clj-webdriver.taxi /Selenium) now?

lepistane19:10:08

is this normal behavior?

(defn foo [profile]
  (let [name (str "foo-" (name profile))]
    (prn name)))

lepistane19:10:08

i get error

ClassCastException java.lang.String cannot be cast to clojure.lang.IFn  auto-service-jobs.core/create-healthcheck-job (form-init20863580730528680.clj:206)

lepistane19:10:23

profile is keyword

ghadi19:10:25

before you edited, you had a bug

ghadi19:10:39

you were shadowing name previously

lepistane19:10:06

what does that mean?

ghadi19:10:41

naming a let binding something that hides a global name

ghadi19:10:51

name is already a function in clojure core

ghadi19:10:09

you are binding it to a local let-binding named, name

lepistane19:10:20

this is why people say dont program when you are tired

lepistane19:10:31

that is so obvious. thank you very much

ghadi19:10:44

the way it is defined currently shouldn't throw, it was your pre-edit code

ghadi19:10:11

shadowing names is fine, not frowned upon. Just be conscious when you're doing it

lepistane22:10:37

how do you write tests when working with tokens so i have tokens.edn where i get all my tokens anyway i wanna test my function but it uses token from that file and i dont wanna put token in my test should i put token as parameter ?

seancorfield22:10:36

@lepistane Perhaps you want to mock the part of your function that reads from the file?