Fork me on GitHub
#clojure
<
2016-04-26
>
j-po00:04:20

Is there a reason there’s no cond-let? (It seems like there used to be in contrib, but after contrib was deprecated, cond-let doesn’t seem to have ended up anywhere. This suggests to me that maybe people realized it was a bad idea for some reason.)

seancorfield01:04:42

I can imagine if-let and when-let but what would cond-let even look like?

kenny01:04:54

Is it possible to have multimethods dispatch on a function of the dispatch-val and the val? The use case I am thinking of is dispatching to a multimethod when a object is an instance of a class. e.g.:

(defmulti my-method (fn [val dispatch-val]
                                      (instance? dispath-val val)))
(defmethod my-method IPersistentVector
  [val])

j-po01:04:48

Multimethods already dispatch on type/class, using isa?

kenny01:04:35

Rightt.. Thanks

kgzm01:04:17

I have a question of aesthetics mostly. I made a state monad contraption and I want the binds to look ah.. nicer.

(defn move [thing location]
  (>> (>>= (query [thing :location])
           (fn [cur-location]
             (guard (exists? cur-location)
                    (dispose cur-location thing))))
      (acquire location thing)
      (occupy thing location)))

  ;;Example code that doesn't look so nice.
  (defn find-two-guys [guy1 guy2]
    (>>= (query [guy1 :location])
         (fn [loc1]
           (>>= (query [guy2 :location])
                (fn [loc2]
                  (return [loc1 loc2]))))))
Full source of the implementation is available here: https://gist.github.com/KGZM/2977192a27c5c28ae72c077a189e4c3b

kgzm01:04:24

Lispy prefix notation makes combinators a little weird and the nesting to get names from binds runs away fast. I like the combinators being variadic rather than binary.. but I'm wondering if there isn't a different way to think about this that might look a bit nicer.

danielcompton01:04:00

@leov, it’s kind of a historical thing to sign libs. It’s done on Maven central and other places in Java land. In Clojars, there used to be two repos, one for anything people uploaded, and one for signed libs that matched the GPG key added to a users profile. That was too confusing, so there is only one repo now with no restrictions on signing or not. You could make a good argument that lein deploy :clojars shouldn’t no longer try to sign libs by default. In theory, we could build a Clojure web of trust, where we verify that every lib we use is signed by people we trust, and we can be sure we’re using the right code. In practice, I don’t think any of the Clojure tools verify this, so it may be security theatre? I’m not 100% sure on this yet.

risto01:04:29

I think I remember seeing a macro for bind that was just like the threading macro

risto01:04:53

Basically like kliesli composition, but I can't find it right now

kgzm01:04:23

@risto: Hmm. That stuff is interesting. Thanks, gives me an idea for a macro that should clean things up and hopefully not cause too many problems.

lmergen06:04:39

euh, is it correct that i cannot define ad-hoc / lambda functions inside a threading macro? i'm getting errors about "first argument to rename-keys should be a vector"

lmergen06:04:15

ah wait, i just need an extra pair of parens, which makes sense

manalipankaj08:04:59

can anyone guide me how to make a simple email client in clojure

mpenet08:04:19

there are a few open sources libs/examples out there, start with these

manalipankaj09:04:32

can u suggest me any one

manalipankaj09:04:40

I am new to clojure

mpenet09:04:05

manalipankaj: check what you can find on http://www.clojure-toolbox.com

mpenet09:04:37

postal and mailer are probably somewhat decent choices

slipset11:04:32

playing around with zippers, and parsing xml I found http://blog.korny.info/2014/03/08/xml-for-fun-and-profit.html

slipset11:04:51

But I’m stumbeling on something strange:

slipset11:04:09

> (def myxml "<foo><bar>baz</bar></foo>")
;; => #'manntall-creator.core/myxml
> (def xzip (zip/xml-zip (xml/parse-str myxml) ))
;; => #'manntall-creator.core/xzip
> (xzip/xml-> xzip :foo)
;; => ()

slipset11:04:23

I cannot see why this should return the empt list?

lmergen11:04:40

ok, so, I'm trying to figure out the most elegant way to solve a problem. my problem: i need a conditional update-in, that only updates values when the keys are actually found. by default, update-in will create new entries if the keys are not found, which is not acceptable in my case. what is the best approach to this ?

c0rrzin11:04:13

@lmergen: I’d just try to get the value first, if absent, return the map, otherwise, apply the update

lmergen11:04:27

hmmm, right

lmergen11:04:30

so just keeping it simple

slipset11:04:50

(some-> (fetch-val) (update-in [:foo :bar :baz] f))

c0rrzin11:04:33

you want nil when value isn’t found? or the old map?

lmergen11:04:36

@slipset: how does that prevent update-in from calling when (:foo (fetch-val)) doesn't exist?

slipset11:04:04

it doesn’t, I misunderstood your question simple_smile

lmergen11:04:41

basically, I have an (update [xs]) function, which conditionally updates the database fields for an object. one key is password, which, if present, needs to be replaced with the hashed version. but if not present, shouldn't be in the result map.

c0rrzin12:04:31

@lmergen: is this what you want?

(defn update-in-if [m ks f & args]
  (if (get-in m ks)
    (apply (partial update-in m ks f) args)
    m))

lmergen12:04:15

seems exactly it yes!

lmergen12:04:41

how do people manage these kind of functions in their projects, btw... do you just have a 'toolset.clj' file where you dump all these ?

lmergen12:04:49

do you have a separate library for this?

xcthulhu12:04:19

You can make a private function if it's a one-time thing

c0rrzin12:04:29

it’d be something like a clojure core extension, utils.clj, misc.clj or something

xcthulhu12:04:32

(defn- update-in-if ...)

lmergen12:04:45

yeah i know, but this is a pattern that repeats quite often in my code

lmergen12:04:59

and i wanted to solve it once and for all now

xcthulhu12:04:07

I always make a utils.clj or utils.cljc for this sort of thing

lmergen12:04:17

ok cool, i have that as well

lmergen12:04:29

guess i should live with that until it becomes unmanageable

xcthulhu12:04:13

Well, once it becomes really big if its nice you can always make it open source

xcthulhu12:04:27

That's Zach Tellman's utils which are really popular

lmergen12:04:39

i'm not that confident of myself yet

xcthulhu12:04:11

If you say so, it's not a big deal.

xcthulhu12:04:10

The biggest PITA for releasing an open source package is Clojars, I've been using jitpack lately:

xcthulhu12:04:31

This is becoming #C03RZGPG3 ... sorry

mpenet12:04:47

oO it's super easy with clojars, just 1 lein command

mpenet12:04:22

@lmergen: use contains? instead of get-in, it should be faster

lmergen12:04:10

mpenet: yeah, i don't think the function works properly, since it calls update-in with all the keys, even if just a limited subset is actually available in the container

lmergen12:04:35

so i probably need to use select-keys to get the proper subset, and then call update-in

mpenet12:04:47

well contains? wont work with nested stuff tho

xcthulhu12:04:25

@mpenet Mostly its the PGP key management, setting up your account, etc

mpenet12:04:57

I could be wrong but I dont think that's mandatory (the pgp part). Even then it's a one time process for both.

xcthulhu13:04:21

Like I said, this is #C03RZGPG3 , we can move this discussion over there if you really want to argue about this, okay?

thug.nasty17:04:44

(.start (Thread.) function arg1 arg2) is the syntax, no?

thug.nasty17:04:04

or is it (.start (Thread. function arg1 arg2))

thug.nasty17:04:16

nvm, figured it out

thug.nasty17:04:28

(.start (Thread. #(function args*)))

bvulpes19:04:09

is it a very very bad idea to assoc onto records in the defrecord implementation?

Lambda/Sierra19:04:52

You mean (defrecord ... (some-method [this] (assoc this ...)) ? That's fine.

seancorfield19:04:52

But don’t dissoc the base fields out of a record — that turns the record into a regular map. Michal Marczyk mentioned that in his Clojure/West talk and I just lost a couple of hours this morning due to exactly that issue until I remembered that comment in his presentation!

sveri20:04:02

When doing \(defrecord ...)` clojure generates (clojure.core/defrecord). Is there a way to omit that clojure.core namespace?

jr20:04:08

Use the symbol ’defrecord instead (~‘defrecord ..)

sveri20:04:13

@jr Thanks, exactly what I need simple_smile