Fork me on GitHub
#clojure
<
2017-01-07
>
qqq01:01:06

are namespaces first class objects in clojure? can I pass a namespace to a function, then have the function lookup a function in the namespace? (I realize this is probably bad convention, but I'm doing some macro magic funkiness)

hiredman01:01:02

you can dynamically fiddle with namespaces and do all kinds of stuff, but the clojure runtime is pretty static in how it works, it does all its lookups, etc, at compile time, which can be at odds with what people expect if they can poke at namespaces

qqq01:01:58

(for [[k v] some-magica-list]
  (context k [] #'v/app-routes))
^^ I'm trying to do something like the above, where k = string, and v = a namespace, so I take a list of (string, namespace) pairs, and I generate the compojure routes from that -- is this something reasonable, or is this something that will just be causing me lots of headaches if I attempt it?

bfabry01:01:14

generating compojure routes, so, this could all happen at app startup right?

qqq01:01:11

you want to hear the really crazy part?

hiredman01:01:18

I dunno that you need a macro for that

qqq01:01:20

this is in a *.cljc, file, compiled twice

qqq01:01:28

under clj compilation, it generates compojure routes

qqq01:01:35

under cljs compilation, it generates reframe views

hiredman01:01:36

https://github.com/hiredman/graft/blob/master/src/graft/core.clj might be something to look at (but I don't really recommend using it)

qqq01:01:47

so in the same codebase, I'm looking at reframe + the server rest api

hiredman01:01:25

reframe views, you'll need a macro

qqq01:01:29

@hiredman: graft = handler already defined, and you're adding new routes to it at compile time?

qqq01:01:51

oh, "translate urls to functions for ring"

qqq01:01:01

though I have no idea what that means

wei01:01:13

what’s a good way to remove nils from a nested collection of maps and lists? (remove-nils {:a [1 2 nil [4 nil 1]]}) => {:a [1 2 [4 1]]}

rgorrepati01:01:49

(remove nil? coll)

rgorrepati01:01:41

I think you should try some version of postwalk

adambros01:01:04

i would also question why you need to remove them

adambros01:01:12

or even why they are in your collection

adambros01:01:20

but if you are writing a helper/library function then ignore that

wei02:01:39

writing a view template parser where the template is a map. some sections have conditionals which return nil when the section is supposed to be hidden

qqq05:01:37

anyone here with experience replacing compojure (macro driven routing) with bidi/silk (data driven routing)?

domkm05:01:11

@qqq Yes, though I wrote the latter. 😉 Feel free to @mention or DM me with any issues you run into.

martinklepsch06:01:17

I have a huge map and want to find all kv-pairs that match a certain predicate. I think clojure.walk is what I’m after but I don’t know how to use it. Can anyone provide me a pointer maybe?

qqq06:01:49

(filter f m) ?

qqq06:01:59

it'd treat the m as a list of kv pairs

martinklepsch06:01:02

@qqq but it doesn’t traverse the map in depth. That said, tree-seq might be what I’m after 🙂

qqq06:01:30

clearly I am misunderstanding your question; do you have sample data ?

qqq06:01:36

(and the correct response to the sample data)

martinklepsch06:01:54

It’s a deeply nested map with a key like _projects123123 somewhere, I want to find all kv-pairs with this key. I’ll try tree-seq and if that doesn’t cut it I’ll whip up an example

martinklepsch07:01:23

Ok tree-seq didn’t yet lead me anywhere

martinklepsch07:01:19

here’s some example data https://gist.github.com/martinklepsch/69da07de5d8b3d6e9502b1df000f163c What I want to get out of it is all the values of the various _showtimes1iJdI0 keys in the map

martinklepsch07:01:26

@mikethompson flatten-keys gets a bit hairy when you also have seqs etc

martinklepsch07:01:49

@mikethompson I’ll take a look at specter, meant to since ever basically anyways

martinklepsch07:01:54

found some solution, doesn’t feel pretty but well, … it works 🙂

(->> input-data
     (tree-seq #(or (map? %) (vector? %))
               #(cond (map? %) (vals %) (vector? %) %))
     (filter map?)
     (map #(get % "_showtimes1iJdI0"))
     (keep identity))

tolitius08:01:06

@martinklepsch, in case you can use specter

$ boot repl
boot.user=> (set-env! :dependencies '[[com.rpl/specter "0.13.2"]])
boot.user=> (require '[com.rpl.specter :as s])

boot.user=> (def m {:a 34 :b 28 :c {:bingo 40 :d 23 :e {:bingo 41}} :bingo 42})
#'boot.user/m

boot.user=> (s/select [(s/walker #(and (coll? %)
                                  (= :bingo (first %))))
                       (s/view second)] m)
[40 41 42]

martinklepsch08:01:41

@tolitius interesting, I’ll give that a shot if I run into perf issues

qqq09:01:48

anyone have a minimal example of clojure + silk routes for ring?

qqq09:01:04

I get how silk works for match/unmatch, but I do not yet see how to create, in silk, routes that I can use with ring

zilti14:01:06

qqq: Ring doesn't care about routes. You'll just have to take the http path header and throw it at silk to match it.

zilti14:01:55

Is there a boot equivalent to lein's :java-source-path?

agile_geek15:01:06

@rb1719 in your example x is a map with one key-value pair where the key is :a and the value of that key is :b. You can create a new map with :a set to 1 but :b was the original value of :a so that will not exist in the new map. {:a 1 :b} is not a legal map as it's one key-value pair and a key on it's own. All maps must have an even number of forms i.e. key-value together.

rb171915:01:01

@agile_geek if I have (def x {:a 1 😛 4}) is there a way to change the value of 1 to 2 for example?

rb171915:01:48

I don't know why that smily face is there haha

agile_geek15:01:54

but it always creates a new map

agile_geek15:01:17

:b if not in backticks is an emoji

rb171915:01:04

Which function would you have to use to change the 1 to 2? Is it assoc?

rb171915:01:48

Oh I didn't see that. Thanks for the help. Still new to Clojure so a lot to learn

agile_geek15:01:50

BTW it might be better to ask this kind of question in the #beginners channel

rb171915:01:22

I didn't know there was a beginners channel. I will ask there in the future. Thanks again

agile_geek15:01:33

Be aware that assoc produces a new map, as all data structures in Clojure are, by default, immutable it will not change the value of the var x

pupeno16:01:39

How do I disable clojar signing on release? It says add :sign-releases false to the relevant :deploy-repositories entry. but I don't have any :deploy-repositories

andy.fingerhut17:01:31

@tbaldridge: I feel there must be some significant comic effect that can come from the use of 'impudence miss-match' 🙂

pandeiro18:01:57

is it possible to create a quartzite job without assigning it a toplevel var? (e.g., without using defjob or defrecord)

pandeiro18:01:18

i'm trying to create a function that given data, generates job specs

qqq19:01:34

(def api-routes
  (silk/routes {:api-data [["api"]
                           {"limit" (silk/? (silk/int :limit) {:limit 100})
                            "offset" (silk/? (silk/int :offset) {:offset 0})}
                           (serve/POST)]}))
(silk/match api-routes {:path ["api"] :request-method :post})

why does this return nil?} according to docs, it should return something like: ;=> {:limit 100, :offset 0, :domkm.silk/name :api-data, ...}

martinklepsch19:01:25

Is there a pretty printer focused on printing data in a predictable, diff-friendly way? I tried clj-fmt and clojure.pprint but both don’t really help much it seems

pandeiro19:01:22

@martinklepsch Can you not use clojure.data/diff prior to printing and just pprint the result?

pandeiro19:01:09

Alternately I think you can adjust the line-length on clojure.pprint so that it's so huge that there aren't arbitrary line breaks

martinklepsch19:01:14

I don’t want to print the diff. I want to print the entire data but in a way that does not break tools like git diff horribly

qqq19:01:37

does diff have a ignore whitespace option?

martinklepsch19:01:12

An important aspect for that is to maintain the order of keys in maps

tbaldridge19:01:29

But maps are unordered....

martinklepsch19:01:42

I can live with weird line breaks every now and then but shuffled keys just wrecking everyting

agile_geek19:01:44

well hash maps are

pandeiro19:01:45

convert to sorted map before print?

martinklepsch19:01:03

@tbaldridge I know but that doesn’t mean they can’t be printed in an ordered way 🙂

tbaldridge19:01:41

I don't know of any pretty printer that does this, but fipp might be a bit more extensible than the built in pprint

martinklepsch19:01:30

I’ll give it a try 🙂

Jeremie Pelletier19:01:42

is there a simple way to express arbitrary maps in clojure.spec? say a string->number or a number->::user map?

tbaldridge19:01:04

but it sounds like what you want is a pprint that defaults to one key per line

tbaldridge19:01:14

@lambdacoder yes...is it map-of?

Jeremie Pelletier19:01:03

ahh, thanks! I was trying to google for it and the about/spec page doesnt mention it 🙂

Jeremie Pelletier19:01:21

there it is! that guide could be linked to at the bottom of the about page, seems to be a natural follow up read

Jeremie Pelletier20:01:25

oh wait, its linked to a bit before the end, my bad

leov20:01:31

quick question - is there a better way to figure out whether current date is in the past in clojure? all I got now is (neg? (compare (clj-time.coerce/from-date some-java-util-date-clojure-instant) (clj-time.core/now)))

qqq20:01:32

I must be missing something trivial. How do I use silk (instead of compojure) with ring? I understand that ring gets a map encoing the request, but I don't see how to hook this up to silk/match

notanon20:01:58

@qqq unlike compojure and defroutes, you will use silk to match the incoming url+params and use that dispatch a ring handler yourself. no?

notanon20:01:54

i'd imagine you need a ring handler to respond to all requests, which inspects the url and then uses the silk returned match to call ring handlers which you're presumably storing in your own map which maps results from silk queries to ring handlers

notanon20:01:15

i find it hard to believe someone doesnt have an example out there on the interwebz

qqq20:01:02

@notanon: I have not yet found it in the first three pages of google

tolitius20:01:16

@leov:

boot.user=> (require '[clj-time.core :as t])

boot.user=> (t/before? (t/date-time 1986 10) (t/now))
true

notanon20:01:19

that's a bad sign for usage of silk 🙂

qqq20:01:29

either I'm supposed to manually match on :headers :uri

qqq20:01:34

or there is some builtin function I'm not seeing

notanon20:01:35

i'd imagine this is the first use case anyone would try on the server

notanon20:01:52

once you get it working you should throw it up on github so no one else needs to reinvent this particular wheel

domkm20:01:06

@qqq Here's a third-party example I bookmarked a while ago https://gist.github.com/Deraen/426285564376266c675b though it's frontend

domkm20:01:41

@qqq You can use the ring-handler function https://github.com/domkm/silk/blob/master/src/domkm/silk/serve.cljx if you want to write a little less

qqq20:01:04

@domkm: ah, that's what I was missing -- thanks!

leov20:01:04

@tolitius, thanks. somehow missed it. also I thank you very much for making wonderful mount library. it is so much simpler to use than system

ddeaguiar20:01:04

@leov if you're using java 8, you can write some interop code around java.time. I think there are a few clojure libs which wrap it as well.

leov20:01:32

unfortunately, I'm using java-on-android via lein-droid

qqq20:01:39

@domkm: I saw the bidi vs silk resources; but I couldn't figure out how to get a ring handler out of either of them

qqq20:01:53

It's possible this is due to me not understanding any of ring at all.

tolitius20:01:30

@leov: thanks for the feedback, very welcome 🙂

domkm20:01:15

@qqq Not a problem. Check out the Ring spec https://github.com/ring-clojure/ring/blob/master/SPEC . It's only 140 lines and very readable.

qqq20:01:12

@domkm: ah, thanks; I need to understand this

domkm20:01:27

sure thing