Fork me on GitHub
#clojure
<
2017-08-21
>
qqq02:08:17

is there a version of merge where if the value is nil, it does an dissoc ?

qqq02:08:33

(this is with regards fo merging two hash maps)

spangler02:08:02

@qqq You probably canโ€™t do it with nil, but check out merge-with, it accepts a function that governs the merge

cddr07:08:57

Is there anything more fun than building interpreters in Lisp? Nope. I don't think there is https://github.com/cddr/ksml

leira07:08:42

Is there a idiomatic way to express:

leira07:08:01

(let result (somefn call)
  (if result
      result
      (error handling)))

leira07:08:53

(or (somefn call) (error handling))
?

sfalcon07:08:29

use try/catch?

leira08:08:31

what if somefn doesn't throw, but just return nil?

sfalcon08:08:52

(or (somefn call) (error handling)) this will do it then

leira08:08:56

what about

leira08:08:42

`(let result (somefn call) (if (empty? result) (error handling)

leira08:08:34

clojure
(let result (somefn call)
  (if (empty? result)
      (error handling)
      result))

sfalcon09:08:39

not sure of what you want to do but you might try seq in this case

sfalcon09:08:39

Returns a seq on the collection. If the collection is
  empty, returns nil.
So you could do (or (-> arg somefn seq) (error handling))

sfalcon09:08:58

what you wrote is still ok as it is though

yonatanel09:08:11

What permissive license is most suitable for clojure libs? Is it affected by clojure itself using EPL?

misha10:08:13

there is no reason in using reduce inside the loop, is there?

misha10:08:43

@leira might as well use if-let with a seq then:

(if-let [coll (->> [1 3] (filter even?) seq)]
  coll
  "error")
=> "error"
(if-let [coll (->> [1 2 3] (filter even?) seq)]
  coll
  "error")
=> (2)

misha10:08:25

but that requires not forgetting seq, which, when present, is not intuitively obvious why is there (unless you really used to it, or use it a lot). So I prefer:

(let [coll (->> [1 2 3] (filter even?))]
  (if (empty? coll)
    "error"
    coll))

misha10:08:12

although seq is idiomatic

sundarj11:08:08

you could use not-empty instead

misha12:08:54

the only difference from seq โ€“ is not-empty preserves collection type, but you still need to wrap it in if, etc (in original question context).

pesterhazy13:08:38

that's not the only difference

pesterhazy13:08:09

hm yes it is

mfikes14:08:05

If you are writing portable code, (if (seq [1]) 1 2) is currently slightly faster in ClojureScript than (if (not-empty [1]) 1 2), as it avoids a checked if.

hyankov15:08:14

@mfikes, what is > checked if

dpsutton16:08:29

mast-session.core> (source not-empty)
(defn not-empty
  "If coll is empty, returns nil, else coll"
  {:added "1.0"
   :static true}
  [coll] (when (seq coll) coll))
nil
there's a when statement in there which is doing the same check you're doing outside. It avoids doing this conditional twice in a row

dpsutton16:08:32

ah my apologies for an answer that missed the mark

mfikes16:08:19

In short, ClojureScript knows to omit a call to cljs.core.truth_ for constructs that produce seqs.

hyankov17:08:08

OK, I got it. Great post on type hints! Thanks @dpsutton and @mfikes

akiroz19:08:18

got a question regarding state management libraries like component and code structure. If you have web request handers that depend on a stateful thing like a DB connection do you define a component that depends on the DB component? The problem with that is that code reloading no longer works because the system will have an instance of my handers with the DB dependency injected. How do you guys usually use state management libraries?

rwilson19:08:14

Can't speak for "usually", but I've used ring middleware that injects the system into the request map, and then handlers can access whatever components they need from that. It's worked fine with code reloading, provided your systems sets up component dependencies like http-server <- app <- (db, etc) where "<-" is "depends on".

noisesmith19:08:02

@akiroz the right way to use component with a handler is to ensure that the http server itself is restarted, using the new components, when you do a restart

noisesmith19:08:45

that is, if I have an http server, and am using component, I start the server from inside a component

noisesmith19:08:57

(and shut it down when the component shuts down, of course)

akiroz19:08:44

Hmm... ok. With multiple handler fns I imagine I'd have a component that returns a map with 1 key per handler fn?

hiredman19:08:04

a lot of the structure will depend on if you are using compojure for routing

akiroz19:08:08

what's special about compojure?

hiredman19:08:32

compojure routes and handlers are basically the same thing, and you can't route without also handling

hiredman19:08:20

it isn't as flexible as if you have routing as distinct thing from handling

akiroz19:08:42

Ahh, alright. I'm not using compojure but I plan on having all my routes on one place anyway so I guess it's ok

hiredman19:08:22

I like bidi as a routing library with component

hiredman19:08:14

I can use it to map uris to keywords, and then have the http component lookup this keywords in itself, and have component assoc the right handlers under the right keywords in the http component

akiroz19:08:04

Oh thanks for the tip, I'm using yada so this will come in handy ๐Ÿ™‚

gadeva19:08:30

I am trying to use funcool/jdbc, to create server side cursors with oracle database, throws this error: SQLSyntaxErrorException ORA-00933: SQL command not properly ended

gadeva19:08:18

anyone have done this before?

hiredman19:08:52

do you have a ';' at the end of your sql string?

hiredman19:08:11

(if you do, remove it)

gadeva20:08:37

yes, it worked after removing ';'. Thanks.

adamfrey20:08:07

can I get some help remembering the name of a clojure library out there that does pretty printing of clojure source code? Not fipp, not clj-fmt is there another one?

adamfrey20:08:07

or actually, does anyone know of a clojure/cljs pretty printer that can align keys and values in maps with so that every value in the map starts on the same column?

pesterhazy21:08:07

to the first question, not sure about the second

adamfrey21:08:45

That's the one, thanks!

pwrflx22:08:23

how to test if a variable is class clojure.lang.Var$Unbound ?

pwrflx22:08:11

I'm accessing a dynamic binding, which is uninitialized. i'd like to test if it is uninitialized or not..

pwrflx22:08:37

@tbaldridge that's where I've started, but it throws an exaception

pwrflx22:08:51

`(bound? ring.middleware.anti-forgery/anti-forgery-token) `ClassCastException clojure.lang.Var$Unbound cannot be cast to clojure.lang.Var clojure.core/bound?/fn--5496 (core.clj:5307)

tbaldridge22:08:29

you have to call it with a var, if you call it that way it will try to deref the var

tbaldridge22:08:37

(bound? #'myvar)

pwrflx22:08:27

@tbaldridge ah damn you are right! thanks ๐Ÿ™‚

pwrflx22:08:40

did not think about this.