Fork me on GitHub
#clojure
<
2015-08-19
>
timgilbert04:08:36

Anyone have a more aesthetically-pleasing way to conditionally assoc items into a map than this? (merge {:a :b} (when condition {:foo :bar}) (when other-cond {:spam :eggs}))

timgilbert04:08:23

(I’m not worried about merge conflicts, the keys are unique)

tel04:08:50

When working with a reloaded workflow, how do you keep your global system variable from getting clobbered?

underplank04:08:41

@tel: how do you mean clobbered?

tel04:08:14

I have (defonce system nil) at the top of the file where things like start and reset are defined

tel04:08:41

so when ctn.refresh comes back through it replaces system

tel04:08:55

so I can’t just start the old one again

underplank04:08:10

hmm.. do you have a code snippet?

underplank04:08:27

(btw im just learning about the whole reloaded workflow)

underplank04:08:55

I think i've been using the system library. which gives you some helper methods (go) and (refresh) that sort this out for you.

tel04:08:11

(defonce system nil)

(defn init! [] (alter-var-root #'system (constantly (lc/start (core/init)))))
(defn start! [] (alter-var-root #'system lc/start))
(defn stop! [] (alter-var-root #'system #(when % (lc/stop %))))

(defn refresh! []
  (stop!)
  (repl/refresh :after 'reify.fisher.backend.system/start!))

tel04:08:33

I’ll see what system is doing

tel04:08:04

it looks like it comes from reloaded.repl

underplank04:08:31

yeah. I think given this snippet here /code (reloaded.repl/set-init! dev-system)

tel04:08:34

which it solves by just init’ing before starting, ok

underplank04:08:02

(reloaded.repl/set-init! dev-system)

underplank04:08:22

I think basically does your defonce for your, kind

tel04:08:40

nah, the defonce was a hack I knew wouldn’t work

tel04:08:50

the ctn.repl docs say so specifically simple_smile

underplank04:08:03

well we all have our windmills simple_smile

tel04:08:22

suppose so

afhammad10:08:33

Are there any good articles/books/talks on how to architect clojure code. Specifically around namespaces, writing modular & DRY code? I understand Component is one approach, anything on the topic would be appreciated.

mpenet10:08:47

Clojure Applied covers some possible approaches

tcrayford10:08:48

@afhammad: there was a talk by walmart labs last year at clojure conj that goes over component pretty well (like, how it feels to actually use it)

greywolve11:08:37

@afhammad: there's also an approach similar to component, using Prismatic's graph library: http://www.infoq.com/presentations/Graph-Clojure-Prismatic

afhammad11:08:12

Thanks guys, will check them out. Example repo recommendations welcome too. @greywolve @tcrayford @mpenet

mitchelkuijpers12:08:53

Anyone knows how you can get leiningen to copy dotfiles from the resources folder?

mitchelkuijpers13:08:10

No one knows how to get a dotfile in my jar 😞

sdegutis14:08:58

What's a stupefying technique for getting the first element in an infinite sequence that satisfies a condition and stopping at that point, returning the satisfying element?

exupero14:08:06

You mean other than (first (filter pred coll))?

tcrayford14:08:16

you have to unchunk the seq as well, if it is chunked

teslanick14:08:19

(first (drop-while (complement pred) coll)) ?

sdegutis14:08:02

@exupero: Ha, so simple yet it works perfectly!

sdegutis14:08:17

Is there a way to run Clojure code in here?

rauh14:08:09

@sdegutis: also check out (some pred coll)

hlship14:08:01

@timgilbert: (cond-> {:a :b} condition (assoc :foo :bar) other-cond (assoc :baz :biff))

sdegutis14:08:22

@rauh: even better! if it works, of course simple_smile

sdegutis14:08:00

@rauh: No that only returns the result of the function, not the satisfying element.

rauh14:08:05

Well make sure your pred returns the item itself and not just true

sdegutis14:08:19

Probably only works for (some identity).

rauh14:08:26

or wrap it #(when (pred %) %)

sdegutis14:08:36

Augh, that's an anti-pattern.

sdegutis15:08:13

#(when (pred %) %)

sdegutis15:08:30

I've seen it recommended a lot and it's just not good.

jonas15:08:06

@sdegutis: interesting. Why is that?

sdegutis15:08:40

I can't explain why, I just know it's bad practice to write anything redundant and especially in a custom function like this.

swizzard15:08:07

you’d get something weird if you accidentally passed it 2 args, for one, right?

sdegutis15:08:12

I have practically no #()-style functions in all my code, because usually it means I'm not using some built-in function.

sdegutis15:08:56

In this case, (some #(when (f %) %) coll) should really be (->> coll (filter f) (first))

sdegutis15:08:15

@swizzard: I'm not sure about that. Maybe.

swizzard15:08:51

and the answer there is no

swizzard15:08:30

(i don’t have a clojure repl on my work computer)

noisesmith16:08:58

cljs is not strict about argument count the way clj is

swizzard16:08:31

i always thought multiple non-numbered %s in anonymous funcs would get assigned to additional args

swizzard16:08:43

(but again—no repl at work :()

swizzard17:08:58

is that clj or cljs?

swizzard17:08:36

and i’m still wrong!

swizzard17:08:38

good to know, though simple_smile

greywolve18:08:15

what's the best way to remove all nils from a nested data structure of maps and vectors?

Joe R. Smith18:08:01

@greywolve: : postwalk + filter / reduce for things that are map? or vector?

greywolve19:08:21

@solussd: i've tried this, which works for my use case, but blows up if a map has a value that is nil (luckily i shouldn't have any nil values there), ie [{:a 1 :x nil}] blows up , but [{:a 1 x: [nil]] is fine.

greywolve19:08:26

(walk/postwalk #(if (coll? %) + (into (empty %) + (keep identity %)) %) coll)

Joe R. Smith19:08:16

do you want to remove mapentries where the value is nil?

greywolve19:08:01

or keep them, doesn't matter

greywolve19:08:22

though ideally, yes, remove them simple_smile

Joe R. Smith19:08:35

ah, you’ll need to prewalk, so you can identify mapentries

Joe R. Smith19:08:48

that would return {:a 3, :b [1 2 {:c 5}]}

greywolve19:08:28

@solussd awesome! thanks!

Joe R. Smith19:08:55

wish there was a less verbose way to do it, but you’re welcome simple_smile

curtosis20:08:11

I asked this over in #C053AK3F9, but didn't get any bites... is there a simple library for handling tcp client duties? I was looking at aleph, but I don't need codecs. Just send and receive a string, hopefully dealing with chunking behind the scenes.

cr821:08:23

you can set the codec w/ aleph to :string

cr821:08:34

and probably get what you're after

cr821:08:09

if you give it nothing it should just default to handing you byte arrays as soon as they're available

cr821:08:27

curtosis: actually it looks like that's the only thing you can do now, aleph doesn't directly force use of gloss on you in the latest version, so you just get back the channels that accept/receive byte arrays

curtosis21:08:02

hmm, ok. that sounds like it should work nicely

jeffmk23:08:56

Anyone doing backend APIs for webapps/smartphones: What server platforms do you prefer? (e.g., immutant, tomcat, nginx, etc.)