Fork me on GitHub
#clojure
<
2017-02-15
>
hiredman01:02:05

I would likely use group-by, which is more general

hiredman01:02:44

It accounts for multiple entries under the same key

hiredman01:02:35

You may also want to look at clojure.set/index

tolitius01:02:38

@timgilbert how do plan to use the result? because a simple group-by could suffice (especially if you have duplicate keys):

=> (def xs [{:a 42 :b 4 :c 5} {:a 32 :b 1 :d 5} {:a 12 :b 23 :c 76 :g 5} {:a 34 :b 4 :c 6}])

=> (group-by :a xs)
{42 [{:a 42, :b 4, :c 5}], 32 [{:a 32, :b 1, :d 5}], 12 [{:a 12, :b 23, :c 76, :g 5}], 34 [{:a 34, :b 4, :c 6}]}

Drew Verlee02:02:16

Anyone have a software conference or talk they would recommend? Preferable something i could watch on youtube. It doesn’t have to be clojure related 🙂

tbaldridge02:02:39

@drewverlee I recommend this one, probably the best talk I've ever seen: https://www.youtube.com/watch?v=l3XwpSKqNZw

Drew Verlee03:02:28

Oh boy, "to the moon" was not an ideal watch before bed. Im all sorts of reved up.

Drew Verlee03:02:45

Great talk! Thanks for the suggestion

alexbaranosky05:02:41

@timgilbert does this do what you want?

(zipmap (map :product-manufacturer/slug manufacturers)
        manufacturers)

timgilbert05:02:06

Yes, that’s perfect! Thanks @alexbaranosky (and everyone else who offered suggestions). The (group-by) example is almost perfect too, but I’d rather have {1 {:x 1}} than {1 [{:x 1}]}

timgilbert05:02:59

FWIW, the keys I want to use for the top-level map (`:x` in the above example) are unique in the seq of maps I’m transforming

alexbaranosky05:02:58

yeah my solution assumed :product-manufacturer/slug is a unique identifier

alexbaranosky05:02:07

otherwise you'd lose entries

stardiviner05:02:11

How to enable this team's IRC gateway?

qqq06:02:04

What would be the right channel to discuss "what is the ideal way to setup auto completion in clojure? using static methods [analyzing source code] or dynamic methods [querying a repl]?"

pastafari06:02:02

@qqq: you could ask folks in #cider how auto completion works in emacs.

dominicm07:02:18

@qqq check out the cider nrepl project, there's a complete "op" that it adds. I believe it's backed by a library called completion.

residentsummer11:02:54

does anyone know the difference between using reader macro ^ to assign metadata vs with-meta? I’m using clojure 1.8

residentsummer11:02:29

that’s what I’m observing:

(meta (:foo {:foo (with-meta str {:optional true})})) => {:optional true}
(meta (:foo {:foo ^{:optional true} str})) => nil
(meta (:foo {:foo ^:optional str})) => nil

residentsummer11:02:00

I’ve read the doc (https://clojure.org/reference/metadata), but didn’t find anything regarding this behavior...

moxaj11:02:42

@residentsummer I'd guess the reader macro assigns the meta to the symbol itself, not the value it'd resolve to

moxaj11:02:24

(defmacro x [y]
  (println (meta y))
  `(println (meta ~y)))

(let [asdf 10]
  (x ^:optional asdf))
;; => {:optional true}
;; => nil

residentsummer11:02:17

IMHO this is seriously limit the usefulness of metadata...

moxaj11:02:47

how come?

residentsummer11:02:07

I was trying to implement a somewhat unified http params parsing in my request handlers with something like this

(-> handler
    (wrap-parse-params
     {:start_date my-date-parser
      :end_date ^:optional my-date-parser}))

residentsummer11:02:44

and if metadata is applied to symbols, I have go some other route...

moxaj11:02:04

if wrap-parse-params is a macro, you can see the meta

moxaj11:02:40

otherwise, you can use with-meta, or just express the 'optional' part as pure data

residentsummer11:02:47

Yeah, I’ve initially went with the data way (well, kind of). Just tried to add some visual appeal and remove a ton of (:require [my.middleware :refer [optional default]]). Now it looks like {:end (optional my-date-parser)}

moxaj11:02:09

you could do [:optional my-date-parser]

moxaj11:02:25

or, if optional is the only meta, something like

(wrap-parse-params
  :required {...}
  :optional {...})

residentsummer11:02:16

Well, I can do it with macro… But the last time I touched them, I woke up in the macro hell 😄 Vector variant looks promising, though. Thank you for suggestions!

moxaj11:02:50

yeah, pure data is almost always preferable imo

baptiste-from-paris12:02:06

@tbaldridge really inspiring talk, thx for sharing

velebak14:02:51

@tbaldridge holy moly, what a speech. thanks.

jjttjj16:02:19

is there a clean way to convert (System/getProperty "file.separator") into a regex? I can't do (re-pattern (System/getProperty "file.separator")) because I guess you need four backslashes to escape it properly for re-pattern?, like (re-pattern "\\\\")

jjttjj16:02:15

I could always just do a conditional check for windows or the backslash but at that point I might as well just hard each slash instead of even using file.separator

jjttjj16:02:01

Ok I think (re-pattern (java.util.regex.Pattern/quote (System/getProperty "file.separator"))) is what I wanted

featalion16:02:53

hi all. I have a problem with buffer loading / evaluation and multimethods. For example, I have

(defmulti my-multi (fn [switcher _] switcher))

(defmethod my-multi :first
  [_ arg1]
  (do-something-first arg1))

(defmethod my-multy :second
  [_ arg1]
  (do-something-second arg1))
I update it to
(defmulti my-multi (fn [switcher _ _] switcher))

(defmethod my-multi :first
  [_ arg1 arg2]
  (do-something-first arg1 arg2))

(defmethod my-multy :second
  [_ arg1 arg2]
  (do-something-second arg1 arg2))
then I load / eval buffer with C-c C-k (`cider-load-buffer`), or with cider-eval-buffer. But when method is called, I get clojure.lang.ArityException. The code lays in one file (and namespace), but is called from another. I tried to load / evaluate both buffers, but it doesn't help. Also, code is called from core.async if it matters. it is pretty uncomfortable to restart Clojure REPL each time I change arity of a multimethod...

tbaldridge16:02:38

@featalion there's a long rationale as to why, but the gist is the function in the defmulti is set once on the initial eval

tbaldridge16:02:08

2 ways to fix this: Put the function in a defn, then do (defmulti my-multi #'my-fn)

dpsutton16:02:10

we were just making sure that this was a clojure limitation and not a CIDER defect

tbaldridge16:02:37

The other is to put (def my-multi nil) before the multimethod

tbaldridge16:02:46

here's the problem, if you put defmethods in other namespaces you'd have to re-eval those namespaces whenever the defmulti namespace is redefined. So in order to keep any of that from happening, re-evaluating a defmulti is a no-op

tbaldridge16:02:09

Granted, I would have preferred the other behavior, but many people disagree with me.

mobileink17:02:37

did something happen to namespace functions in 1.9.0? ns-map no longer tells me what I :required. I have (ns foo (:require [bar.baz])) and I need a way to discover that bar.baz was required.

scknkkrer17:02:23

You have to look at the source on git.

featalion17:02:31

@tbaldridge thanks, I will try it! It seems acceptable during development to do so.

kah0ona17:02:33

ugh. is there, with compojure-api, a special case for the "/" route? I used to have a index.html in /resources, which worked fine, but now I want to migrate that index.html to hiccup, and it doesn't match the '/' route

kah0ona17:02:12

what am i doing wrong? Because if I change the "/" into "/something-else", and I turn my browser to localhost:3449/something-else, it DOES work.

kah0ona17:02:17

so it seems the "/" is some special case

kah0ona17:02:31

is this the case?

pesterhazy17:02:59

have you tried ""?

pesterhazy17:02:54

personally I prefer bidi because it's a bit easier to grok (IMO), but obviously this should work with compojure too

juhoteperi17:02:55

"/" should work, that's what I have in all the apps using compojure-api

hiredman17:02:00

my guess is you didn't completely restart your process, and didn't start the webserver in a way that allowed for live re-definition, so the server is still serving the original routes you defined

kah0ona20:02:52

hiredman: i actually did restart the repl to be sure... haven't tried "" though...

pesterhazy17:02:18

hiredman may be on to something here

byron-woodfork18:02:42

Question for current or previous Clojure beginners: What was the most difficult part(s) about getting into Clojure for you? The setup? Learning the actual language itself? Finding help? Figuring out how functional programming works? Or something else?.. I'm looking to put together a learning resource focuses on some of the pain points of getting started with Clojure to make things as seamless as possible for newbies. All help would be appreciated 😃 If you can, please use the 'start a thread' button on this post so we don't make clutter and keep things easy to find Thanks! 😃

tanzoniteblack18:02:44

lukecage: I've been programming in Clojure for a few years and so only have unreliable memories of being a beginner. But at my last job I coached a number of new people learning clojure when coming into our code base. The biggest problem that was common among all of these people was mutability

tanzoniteblack18:02:40

the idea of using map and reduce to build a new collection instead of doing some kind of manipulation of the existing sequence constantly had me telling me to stop reaching for atoms so they could do mutations

donaldball18:02:31

lukecage: It’s been a while for me, but when I was starting out, my primary problems were 1. using an editor without paredit 2. learning good architectural design patterns 3. debugging errors when realizing lazy seqs

tbaldridge18:02:06

For me it was finding a good editor, getting a good dev env setup, and figuring out how the JVM dependencies work. Most of that was solved for me via Cursive. I didn't have a emacs background so learning emacs made it hurt even worse. And I started on a Windows machine back when lein's support for windows was really bad.

tbaldridge18:02:03

As in any language though, I think a big bump is that part when a beginner says "so I finished Brave Clojure and done some 4clojure stuf....how do I write something real?"

sveri19:02:04

Coming from java I remember reading my first clojure book. And unpatient as I am I was waiting for the chapter that explained how classes can be used in clojure. Well, it took a few hours and some ruthless searching through the book until I understood that clojure has no concept of classes, but instead uses vecs / seqs and maps. After that I was pretty fine. The next bumper for me were understanding component from a concept point of view and getting a dev setup working that hot reloads every code. Especially the #'var part was very hard for me to understand as it is not easily searchable.

mobileink19:02:18

lazy stuff. still bites me in the butt occassionally. just the other day i spent an hour or two on a bug before realizing all i needed was doall.

tbaldridge20:02:06

I hear that @U0LGCREMU and it kindof-sortof-not-really gets better. I still have problems with that from time to time.

mobileink20:02:13

@tbaldridge i consider laziness a good feature: it reminds me every now and then that i'm not so smart after all. ;)

tbaldridge20:02:47

Makes me wonder sometimes if it wouldn't be better to teach beginners transducers first

mobileink20:02:03

the humility feature. every language should have one.

mobileink20:02:13

believe it or not i've still not gotten around to *ducers. having too much fun with metaprogramming.

mobileink20:02:32

dunno about first, but early. or were you being ironic?

tbaldridge20:02:18

nope, I was being serious. I've wondered if lazy seqs seem so familiar because we learned them first.

mobileink20:02:29

speculation: the distinction between eval strategy, and semantics, is a,major stumbling block for noobs.

mobileink20:02:20

laziness does not affect reasoning about meaning, but it does affect reasoning about comoutation. in languages without laziness this distinction never even comes up.

mobileink20:02:02

@U08PR8LJZ : the conceptual shift. we can all, dog-like, learn to perform tricks in a new language without really understanding much.

tolitius20:02:43

@U08PR8LJZ: the tallest mountain for me was mapping the real world projects, that I know, to Clojure: i.e. how do I do AOP in Clojure? how do I inject a datasource.. and where? how do I orchestrate the whole application? What it comes down to be is the difference between OOP and Clojure thinking: * OOP: Objects, delegation + message passing vs. * Clojure: data from A needs to transform and get to B It did not happen overnight, but once I started to think in data => pipeline => data => pipeline all the real world problems that I could not map to Clojure before suddenly mapped, almost like a thin thread orthogonal to all the OOP application layers with just data and functions.

mobileink20:02:59

for me, enlightenment came (not in a flash, gradually) when i realized that clojure was making me reconceptualize what the entire enterprise is all about.

Drew Verlee21:02:59

I dont think its possible together a “learn clojure path” that works for everyone. Everyone has a different starting point. How I would approach someone with 20 years of java experience is way different then someone new to software. Most of the struggle comes from helping people understand the tools their already using. Why do we mutate state? Why do we have classes? Why do we not share objects over the wire? I think “Clojure for the Brave” is fantastic for new comers. Perennially, as someone who hasn’t built a large system, i’m taking most of my notes from “Clojure Applied” Which seems to build an argument for various tools in the ecosystem which i had a hard time understanding before like protocals. It wasn’t that i didn’t understand what they did, but rather i didn’t understand why I would use them over a Map. I believe the answer is, to communicate intent to other developers. I think “Clojure Applied” gives a lot of insight into how to build a system, which for me, was the next big question after understanding the basics of clojure.

arnaud_bos23:02:27

For me: "Figuring out how functional programming works?", definitely

arnaud_bos23:02:22

It felt like my brain was rewiring itself

Drew Verlee05:02:58

I think many new programmers, including myself would actual have a harder time answering the question "Why use a OO language?” I found this talk by Bob Very helpful towards answering that question: https://youtu.be/TMuno5RZNeE?t=12m23s the summery is that OO languages let you respond to change better through polymorphism, which lets you invert the flow of control of a program so that high level modules dont have to know about the implementation details of low level modules. I mention this because i want to re-enforce the idea that I think it would help a lot of programmers see the value in FP and clojure if they understood better the tools their currently using.

fingertoe16:02:42

I am a year in and would still consider myself fairly “Beginner” Clojure is pretty easy, its figuring out the best way to do things that is hard — because it is quickly evolving and new libraries are constantly added, often the new way is the best way to do stuff- but the best documented stuff is older stuff. As a new learner looking for example code, I am going to find a lot more 2012 examples than 2016 examples,

fingertoe16:02:35

On the clojurescript side, I feel that it “assumes” a pretty good understanding of the JavaScript host language. Interop is cool, but if it is necessary, then I need to learn 2 languages instead of one, and then learn how to twist the javascript, and that isn’t a comfortable learning curve.

fingertoe16:02:06

I am probably fairly unusual in that I came to Clojure first, with only mild dabbling experience in other langages

byron-woodfork18:02:54

Thank you everyone who replied. Lots of good info here!

baptiste-from-paris18:02:26

hello guys, I bet this question has already been asked but my datomic console when launched with bin/console -p 8080 mbrainz datomic: (after install script) throw this error =>

Exception in thread "main" java.lang.IncompatibleClassChangeError: Implementing class...

jeff.terrell21:02:26

baptiste-from-paris: Have you tried the #datomic channel? :-)

mobileink19:02:26

@scknkkrer : took a look, did not see anything obvious. i wonder if there is any way other than ns-map to discover what has been required/loaded in a namespace.

spieden23:02:50

anyone remember the name of that lib that enables running lein tasks from the repl?

spieden23:02:00

ahh alembic