Fork me on GitHub
#clojure
<
2016-08-10
>
atroche00:08:41

as opposed to this working-but-verbose example:

(let [m #::my-ns {:a 1
                  :b 2
                  :c 3}
      {:keys [::my-ns/a ::my-ns/b ::my-ns/c]} m]
  [a b c])

porglezomp00:08:23

Wait, does (partition 0 [1 2 3]) really just hang?

porglezomp00:08:56

Or rather, I support forcing it hangs, because it produces an infinite seq of empty seqs?

porglezomp00:08:20

Well, that’s my first contribution to http://clojuredocs.org, I was really surprise by that, and accidentally hung my browser for a minute 2–3 times there.

scriptor01:08:35

@porglezomp: looks like, specifically, it hangs because this would always evaluate to true https://github.com/clojure/clojure/blob/clojure-1.7.0/src/clj/clojure/core.clj#L3065

scriptor01:08:52

(count (take 0 coll)) will always be 0

porglezomp01:08:15

Yeah, it makes sense just from a “consume all the pieces” point of view.

porglezomp01:08:26

If you take nothing off, you’ll never get all the way through the collection

gfredericks01:08:35

even (count (take 0 :not-a-coll)) is 0

scriptor01:08:29

well that's because (take 0 <anything here>) returns nil, and the count of nil is 0

lvh02:08:00

So, I accidentally became the maintainer of cloverage, and I’m trying to figure out how to add cljc/cljs support (hoping it won’t be quite as bad as it looks). It’s been around for a while, so I’m not sure if parts of it could be obviated. For example, is there something like ztellman/sleight (ideally managed as something part of clojure) that does “read, but modify before using” — in this case, for adding coverage instrumentation?

lvh02:08:18

cloverage is growing features like “try to read cljc if clj fails” — which feels like behavior cloverage shouldn’t implement itself, because it can only mimic clojure (best case: more code to maintain; worst case: it gets it wrong in a way that affects coverage results)

danielcompton02:08:44

I think tools.namespace will become your friend here

lvh02:08:01

cool, thanks!

lvh02:08:58

@danielcompton: Thanks! I’ll make sure to look at that more closely

herbm04:08:54

newbie question: (Math/pow 5 2) ; works but (fnil Math/pow) nil 2) ; gives unable to find pow in Math

herbm04:08:16

That extra ) wasn't in there though

herbm04:08:32

, (fnil Math/pow nil 2)

herbm04:08:51

I don't think it matters -- fnil doesn't do what I believed apparently

pre04:08:14

Can anyone comment on why function signatures look the opposite in core clojure functions, like map vs select-keys? In map (or other h-o fns), the first arg is typically another function, followed by a seq; in select-keys and other similar fns, the order is turned around. This still confuses me and I often have to look up the docs, as I can’t write ->> or -> fluidly. Any comments, appreciated.

Alex Miller (Clojure team)04:08:47

Because of this intentional split in arg ordering, data structure or collection functions work well when chained with -> and sequence functions work well with >>

Alex Miller (Clojure team)04:08:49

It's generally not common to be doing both in the same threading chain but if so, as-> can sometimes be helpful

Alex Miller (Clojure team)04:08:57

@atroche: you will be interested in the new namespaced key destructuring in Clojure 1.9 - see http://dev.clojure.org/jira/browse/CLJ-1919

atroche04:08:06

@alexmiller: perfect, thanks 🙂

pre04:08:28

thanks @alexmiller. I need to use more of as->

Alex Miller (Clojure team)04:08:02

@bhagny control over the namespace map syntax during printing is coming http://dev.clojure.org/jira/browse/CLJ-1993

lvh06:08:33

Someone has suggested a patch to cloverage that rewrites into, empty and walk because the original versions don’t preserve metadata. Is that the best way to get metadata to be preserved? https://github.com/lshift/cloverage/pull/72/files

hiredman08:08:57

Program transformation based on walk has always struck me as kind of, uh, hinky I guess is the word

hiredman08:08:22

ztellman has a library he wrote for program transformation

yonatanel08:08:08

@lvh: Not sure what you need those functions for but specter has a walker that preserves metadata: https://github.com/nathanmarz/specter/wiki/List-of-Navigators#codewalker

lvh08:08:16

@hiredman: hm, right. that lib already uses riddley so maybe we should just port it to that

sivakumargsk12:08:55

How to write this one in cljs ? "SivaKumar25689".match(/[A-Z][a-z]+|[0-9]+/g).join(" ") => "Siva Kumar 25689" or how to split a string at Capital letter in clojure script ?

gnejs12:08:53

way verbose.. but (->> "SivaKumar234" (re-seq (re-pattern "([A-Z][a-z]+)|([0-9]+)")) (map first) (clojure.string/join " ")) should work.

sivakumargsk12:08:39

@gnejs: thanks its working.

madstap12:08:11

@sivakumargsk: Somewhat less verbose version

(->> "SivaKumar234"
    (re-seq #"[A-Z][a-z]+|[0-9]+")
    (str/join " "))

gnejs12:08:55

ah, great - I knew there was a short-hand for regexs 🙂

gnejs12:08:21

(any my regex-fu is weak, it seems 🙂 )

gnejs12:08:14

(as is my spelling.. dang)

manishkumarmdb13:08:17

@sivakumargsk: (clojure.string/join " " (re-seq #"[A-Z][a-z]+|[0-9]+" "ManishKumar123"))

guy13:08:14

Hey folks, got a dumb question. Is it possible to print out the contents of a transient vector

guy13:08:37

and see what they are instead of a reference.

guy13:08:52

My guess is i would have to convert it to something else and print it, then convert it back?

akiva13:08:35

Most likely, yes. Printing is a side effect and transients aren’t meant for side effects.

akiva13:08:51

You can grab elements via get while transient so you might be able to use something like for to generate side effects but that would really circumvent what transients are there for.

guy14:08:23

Thanks @akiva that makes sense

akiva14:08:22

And no questions are dumb. Everyone here is learning all the time.

guy14:08:41

thanks 🙂

noobjure15:08:11

Thought I'd share this amazing blog (ton of interesting Clojure projects). It's mainly embedded stuff but though it was worth a share. https://nakkaya.com/tags/#clojure

peterromfeld15:08:42

@herbm im not sure how fnil works with more then one argument.. ((fnil name "") nil)

radon16:08:53

> (doc fnil)
-------------------------
clojure.core/fnil
([f x] [f x y] [f x y z])
  Takes a function f, and returns a function that calls f, replacing
  a nil first argument to f with the supplied value x. Higher arity
  versions can replace arguments in the second and third
  positions (y, z). Note that the function f can take any number of
  arguments, not just the one(s) being nil-patched.

timgilbert16:08:16

Hey all... I can't really see this anywhere but I thought I'd ask. I'm hoping to use clojure.tools.cli to parse some command-line arguments that are in the form --define key-name value but I don't see built-in support for it, am I missing something?

timgilbert16:08:19

I'll probably just wind up using a --define key=value syntax, just wanted to see if there's a good alternative before I write it

akiva16:08:54

@timgilbert, not sure about clojure.tools.cli but you could easily parse that yourself. If all of your arguments are in that format, use (partition 3 …). If you have other style arguments, just move through the argslist and check for the argument type you’re expecting and then take the next one or two arguments and process that little chunk.

jswart16:08:16

@timgilbert: depending on what you are doing, you should check out boot. It comes with a macro called defcli that handles cli arguments, defaults, and help text.

timgilbert16:08:26

Hmm... I'm using leiningen and I have a bunch of other stuff tied into tools.cli already, I think I'll probably just wind up parsing it myself with the :assoc-fn stuff. Thanks though!

jstew16:08:48

@jswart: Nice to know, I've been doing a lot of work with tools.cli lately and was looking for an alternative.

jswart17:08:30

Yeah I’m a huge boot fan. Project configuration as a program that you control is nice. It gives you a few abstractions and then gets out of the way.

jstew17:08:31

I'm using boot anyway so it's kind of a no-brainer.

hlship17:08:33

@yonatanel: I don’t think I ever considered the idea that you could apply a new configuration on top of an existing system. I would not do it … component and config work well enough that you should discard the entire stopped system and just create a new one. I don’t use the reloaded workflow since I use Cursive, which does a lot of the interesting parts for me.

hlship17:08:52

When I’ve been doing more active server-side development (web services with Rook) a key part of my arsenal was a special filter that, on every request, rebuilt the Rook dispatch table for the request, allowing changes to functions to be picked up.

hlship17:08:30

I hope to provide something similar, but for Pedestal, with Rook 2 (which is Rook’s meta-data driven namespaces and functions as a thin layer on top of Pedestal).

hlship17:08:43

I’ve found that code inside protocol methods is hard to debug (or even set breakpoints on) inside Cursive, so my protocol methods are usually a thin layer around a more-pure function.

abdullahibra17:08:09

what is the best way to get random number between range, i know there is rand-int, but i want to get float also?

abdullahibra17:08:26

rand work from 0 to limit you specify

abdullahibra17:08:35

what about from -1 to 1 ?

dg17:08:02

I would just scale the result from (rand) to the range you want

dg17:08:05

Something along the lines of

(defn rand-float [min max]
  (+ min (rand (- max min))))

abdullahibra17:08:14

(-> [(rand 1) (rand -1)] rand-nth)

abdullahibra17:08:25

dg what do you think about this?

abdullahibra17:08:15

(defn rand-float [min max] (-> [(rand max) (rand min)] rand-nth)

gfredericks17:08:15

(- (rand 2) 1)

gfredericks17:08:38

or generally (+ (rand (- max min)) min)

dg17:08:50

That would work, though I'm not sure if the probability distribution is truly the same. What I posted is a more general solution.

abdullahibra18:08:13

dg: thanks man

gfredericks18:08:31

@abdullahibra your happens to give a good distribution for (-1 1) but in general it could give you stuff out of range

gfredericks18:08:39

e.g. (rand-float 10 11)

chris19:08:25

does anyone know why defn is so complicated?

chris19:08:24

I just expected it to be some combination of def a name a fn and a body, but it’s huge

robert-stuttaford19:08:55

optiona metadata, doc strings, multiple arity parsing, and more

porglezomp19:08:40

All made worse by the imperative macro parsing.

robert-stuttaford19:08:42

a lot of it is just untangling the args so that it knows what you've given it, because many args are optional, and the args are positional

fellshard19:08:59

That let-rebinding is interesting to me. Makes sense as a way to break down transformations in sequence.

robert-stuttaford19:08:17

yeah. tbh never seen that before 🙂

oahner19:08:55

that's a neat structure alright

robert-stuttaford19:08:59

be interesting to see how using core.spec to parse args instead would differentiate this impl

gfredericks19:08:28

it'd be much easier

gfredericks19:08:53

but I doubt that cleanup will ever happen because clojure.spec needs defn to exist already :)

oahner19:08:17

not sure how this would affect performance either

oahner19:08:22

this is pretty lean

hiredman19:08:25

well, just write a spec compiler

chris19:08:43

also that’d make a hard dependency on spec where you don’t need one. it’d be an interesting experiment though to redef defn and see how it works

hiredman19:08:44

takes in a spec, spits out clojure code to parse it

fellshard19:08:45

That's what bootstrapping is for 😛

oahner19:08:06

isn't spec part of the next stdlib?

robert-stuttaford19:08:13

yeah. i don't think we'd ever get one, but it'd be nice to see what the difference would be

robert-stuttaford19:08:33

alot of macros are tough to read because of this positional parsing

robert-stuttaford19:08:46

uses up all your short term memory slots 😛

robert-stuttaford19:08:24

oahner: yes, part of 1.9

fellshard19:08:27

With great power comes... great obfuscation?

hiredman19:08:00

I swa this (http://wiki.call-cc.org/explicit-renaming-macros) the other day, which looks like an interesting way to bootstrap other macro systems

chris19:08:51

@oahner: it is part of standard lib, it’s just not required by default. I thought it was a contrib-esque lib like core.async

hiredman19:08:10

it comes with the clojure jar

oahner19:08:18

I imagined it'd be part of the core

oahner19:08:26

it does in cljs.spec anyway

chris19:08:32

yeah that’s what I was saying, I was wrong originally

robert-stuttaford19:08:32

it's part of the jar because of the integration with doc, and all the improvements to predicates and stuff

robert-stuttaford19:08:38

e.g. we finally have boolean?

robert-stuttaford19:08:15

less well known is the new map reading/printing syntax, for maps with namespaced keys: http://dev.clojure.org/jira/browse/CLJ-1910

hiredman19:08:32

anyway, if you really want to use spec to parse things, without having a runtime dependency on spec, write a spec compiler instead of an interpreter

oahner19:08:25

speaking of spec and namespaced keys, am I to understand that it's good practice to namespace all keywords that might be used as a key?

robert-stuttaford19:08:53

all keywords that'd have a spec, possibly

robert-stuttaford19:08:02

although there's req-un and opt-un

robert-stuttaford19:08:11

lots of existing code and data out there!

josh_tackett19:08:22

Anyone know how to hover over something using semperos?