Fork me on GitHub
#clojure
<
2020-05-04
>
Ramon Rios10:05:59

Am i able to merge in a nested map?

(let [col-specs (:col-specs table-specs)
        extra-specs (:extra-specs table-specs)
        col-w-id (merge {:id (default-id-column)} col-specs)]
  (create-table name (hash-map :col-specs col-w-id :extra-specs extra-specs)))
My attempt now resumes in separate stuff and merge them again.

xsc11:05:35

How about update? (Or update-in for deeper nesting levels.)

(update table-specs :col-specs #(merge {:id ...} %))

Kevin11:05:40

In this case, I think an assoc-in would be enough (if I’m reading this correctly)? (assoc-in table-specs [:col-specs :id] (default-id-column))

xsc11:05:54

You could also do the following but that might get tedious when there are more default values. It would, however, avoid calling default-id-column if not necessary.

(update-in table-spec [:col-specs :id] #(or % (default-id-column)))

Kevin11:05:28

Oh no, it’s a default id

Kevin11:05:48

Meta merge is also an option, but I think that would be overkill if you’re only using it for this. https://github.com/weavejester/meta-merge I’d rather go with xsc’s solution

Ramon Rios11:05:15

Thank you all

Ramon Rios11:05:42

i'm adding a new key into existent data in col-specs

manutter5113:05:53

Today’s Monday Madness™. Here’s project.clj:

:dependencies [...
               [clojure.java-time "0.3.2"]
               ...]
and deps.edn:
:deps
 {...
  clojure.java-time {:mvn/version "0.3.2"},
  ...}
and REPL:
(require '[clojure.java-time :as cjt])
Execution error (FileNotFoundException) at user/eval36178 (form-init10994301252492343597.clj:1).
Could not locate clojure/java_time__init.class, clojure/java_time.clj or clojure/java_time.cljc on classpath. Please check that namespaces with dashes use underscores in the Clojure file name.
But I can see that the jar file is listed in my IDE deps.

dpsutton13:05:43

ns java-time

dpsutton13:05:24

there is no namespace clojure.java-time

manutter5113:05:52

Oh wow, thanks. Never even occurred to me to check for that.

jmckitrick14:05:19

How difficult would it be for a small clojure project to import and use a locally built scala library?

noisesmith15:05:21

that really depends on how the scala library exposes itself - if it limits itself to java compatibility and doesn't require inheriting a class, it will be easy

noisesmith15:05:38

if it exposes scala features in its public api, not easy

noisesmith15:05:29

it's directly analogous to using c++ from another language - if it sticks to the substrate it's easy, if it exposes things requiring its own features, not easy

eudis14:05:08

Is there any intended use case for (constantly ['x]) vs (partial identity ['x]) ? (ie constantly vs identity) I'm trying to understand what is threshold for Clojure developers to include fundamental constructs into the base language. The more of these almost overlapping functions I encounter the more I'm asking these kinds of questions.

jsn14:05:54

what's overlapping here? You partial throws an exception when there are args while constantly doesn't.

eudis14:05:08

:thumbsup:

noisesmith15:05:41

also, as an aside, style wise I'd go with '[x] over ['x]

eudis15:05:17

Sounds good thanks for the recommendation

noah14:05:27

I've been googling this one a bit and turning up a blank. I have a function I'd like to perform differently when I'm in the development environment (or test) than in production code. Is there some way to def or call it that can accomplish this? Say (defn foo [x] doSomething) (defn ^:dev foo [x] doSomethingElse) or some such?

mgrbyte14:05:52

I do this kind of thing by using with-redefs, including the code that redefines the function in a namespace that is only included in development (:dev profile in lein or alias with clj tools)

👍 4
noah14:05:49

Ah ok, so just throw a with-redefs in my user.clj or some such eh? Sounds good. I'll give that a try. Thanks!

noah14:05:50

Hmm this doesn't seem to do quite what I'm after. I've used with-redefs for mocking in tests, etc. But what I'm looking for is to have, for instance, some fn that gets called in my normal code behave differently when I'm in a repl. Example: I have project.core/exit which prints a message then calls a fn that calls System/exit with a status code. I want that system-exit fn to not really call System/exit if it gets called from a dev environment.

noah14:05:49

If I understand correctly, I can only get a temp redefinition with with-redefs, so this wouldn't change things whenever things get called throughout the code outside of the with-redefs scope. Am I missing something?

noah15:05:09

Ah, nevermind. Figured out an acceptable way to use with-redefs here. Thanks again!

noisesmith15:05:48

beware that with-redefs is not thread safe

noisesmith15:05:34

if you always want the replacement, without any explcit scope, you can use alter-var-root which is actually safer

noisesmith15:05:42

(though much blunter a tool)

noah01:05:13

Thanks 🙂 It seems that either works for my limited needs, but I'll keep the non-thread-safeness of with-redefs in mind.

Brian14:05:09

How should my ci/cd pipeline call my Clojure repository's tests? From the command line I can do

Clojure -A:test
=> (require test :as t)
=> (t/-main)
but my experience with automation is that it can be tricky when you are responding to a programs prompts (like waiting for the repl to start, waiting for the require to load). Is there a one-liner that can do all of this? If not, what is the standard way people have their ci/cd call their tests?

dpsutton14:05:40

from that output seems like you would do clojure -A:test -m test

dpsutton14:05:42

from man clojure

main-opts
       -m, --main ns-name
              Call the -main function from namespace w/args

Brian14:05:17

Ah thank you @dpsutton this is exactly what I was looking for!

eudis15:05:42

Do aliases handlers defined in project.clj (leningen) require their own deps.edn? I'm running a custom alias command ({aliases {"mycommand" ["run" "-m" "myproject.scripts.core"]}) and I cannot require libraries defined in project.clj

eudis15:05:04

nevermind the above, it was a typo in my config ^

Pavel Klavík16:05:21

Hi, we have build a small framework for building static websites in Clojure with hot-code reloading called Volcano: https://github.com/OrgPad-com/volcano. Feedback is welcomed!

👏 8
Franklin17:05:34

What are some reasons for using fully qualified keywords as map keys?

bfabry17:05:28

spec, more precise automated refactoring, ability to add new information to a map without worrying about clashing key names

Franklin17:05:36

cool, thanks

Franklin17:05:28

what kind of tools are used for the automated refactoring you are talking about here?

bfabry17:05:44

I was thinking of cursive. but fully qualified keys will also make things like sed and grep more accurate

noisesmith17:05:04

and aliases make them less accurate again :D

bfabry17:05:15

eh, cursive or probably clj-refactor.el will know how to deal with aliases. sed/grep it's still more accurate than non-qualified, and aliases are usually used quite consistently

Franklin17:05:11

I have been tring to write clojure for less than 5 months now, I just bumped into this idea of namespaced keys while reading clojure.spec docs... Seems a little weird to me to be honest. Do you use it or the regular personally? @U051SS2EU @U050MP39D? Do you guys think this is something used a lot "out there"?

noisesmith17:05:22

> aliases are usually used quite consistently you hiring ? :D

bfabry17:05:19

it's not used a lot in existing codebases but it's gaining traction. it's kinda a thing people realised later on "hey this is actually a really good idea we should do it more"

noisesmith17:05:36

I used namespaced keywords before spec, I found them randomly while reading documentation and found that they helped with the dog/bark tree/bark problem - different domains use the same key to mean very disparate things and I wanted to know the difference

☝️ 8
😀 4
Franklin17:05:09

aaaah... @U051SS2EU that makes sense

noisesmith17:05:16

which is exactly why spec uses them - letting different domains use their own meaning for bark, by namespacing bark

noisesmith17:05:11

when you don't have namespacing, you get conflicting conventions (see every C lib foo, that defines each function as foo_x foo_y etc. etc...)

Franklin17:05:44

Coo, Thanks guys. Don't you think it's wonderful that I can talk to you like this? 😆

bfabry17:05:59

the internet is a wonderful thing

seancorfield17:05:48

@USWTQB9RU If you use the next.jdbc library for accessing a database, you'll find it returns hash maps with qualified keywords, so you can tell :person/id from :address/id in query results, for example.

eudis19:05:49

Anyone know how to get the request headers from a compojure.api GET request?

(context "/base" []
    :tags ["base"]
    (GET "/me" [& req]
      (let [response (me req)]
        (println req)))) ; // req is empty

lukasz20:05:52

Can you try (GET "/me" req ..... - I don't think you need to wrap the request in a vector

eudis20:05:58

wowzers. that did it

eudis20:05:00

thanks man!

eudis20:05:11

I dont know how I didn't think of trying that 🤯

eudis20:05:26

Thats great thanks. I've been persuing the compojure.api docs all day, but completely overlooked the base compojure (non .api) docs

eudis20:05:39

Thanks for the help

👍 4
Shima20:05:46

Hi everyone I have a few years experience with Clojure/Clojurescript and now I passionate about contributing to an open-source project. I like to be a free help for the community but I don't know which project is the best fit for me and besides that, I don't know Where to begin and who to email and ... I become grateful if you give me your guide Thanks in advance Shima

jjttjj21:05:31

What are the clojure projects and tools you like to use? Maybe you could start by looking at the issues in those github repos

seancorfield21:05:23

@UGR3UQULA cross-posted to #beginners as well and I asked the same question there (and made the same suggestion).

seancorfield21:05:00

We prefer folks not to cross-post because of duplicated questions and responses.

Shima21:05:58

I use clojure and clojurescript for implementing an algorithmic trading in stock market I mostly like debugging complex bugs specially because those are related to logical bugs(not concurrency like other languages) I used timbre, Clojure core libraries, Component, onyx, Aero, reframe, shadow-cljs, ... @U064UGEUQ

Shima21:05:56

You're right I'm sorry and wouldn't do that again:pray: @U04V70XH6

seancorfield21:05:26

NP. I deleted the question/thread/response from #beginners so we can continue here instead.

👍 4
seancorfield21:05:53

I'm not very familiar with most of those projects (beyond Clojure core/contrib which have a different contribution process to many of the community libraries)... I used to follow Onyx early on but ended up not using it so I stopped following it.

seancorfield21:05:22

It has 80 open issues and some of those look like very interesting bugs to try to reproduce and solve...

Shima21:05:51

Do you mean the bugs list in Github? Or does they use backlog somewhere? @U04V70XH6 It would be my first contribution, sorry if my questions are very trivial

seancorfield21:05:42

But take a look at any of the OSS projects you've used and see what open issues they have in their GitHub repo. If you're not familiar yet with contributing to GH projects in general, some projects have a CONTRIBUTING doc that provides additional guidelines for that specific project, but you'll want to have a read through the (excellent) GitHub help docs which walk you through the whole cloning/forking/pull request process as well.

seancorfield21:05:14

Some projects actually mark issues with some sort of label to indicate suitability for first time contributors.

seancorfield22:05:43

If you ran into things you felt were usability or documentation issues with any of the projects you used, that's also a good place to get started by creating a new issue to describe the problems you encountered along with suggestions for improvements and see how maintainers respond.

seancorfield22:05:36

(and on the Pull Request workflow mentioned above, there are projects out there which may not consider a PR if there hasn't already been an issue created to discuss the problem and various possible solutions)

Shima22:05:28

Very helpful Thank you @U04V70XH6

gerred01:05:09

Contributions aren't just code as well.

👍 4
seancorfield03:05:42

@U05509S91 Yup, I said that in the main channel -- as a project maintainer, contributions to documentation are particularly valuable!

λraulain21:05:08

I am not an expert at this but maybe this could help http://open-source.braveclojure.com/

👍 4
λraulain21:05:55

Just a list of some opensource clojure(script) projects. You might be interested in one.

👍 4
seancorfield21:05:48

(I'll repeat my response from #beginners for the benefit of others here) It's often easier to start contributing to projects you've already used and are familiar with. Also worth bearing in mind that "contributing" takes many forms and a lot of projects desperately need help with either documentation or testing -- I know as an open source maintainer myself, I am always really happy when someone contributes improvements to the documentation (that make it easier for more people to use my projects).

👍 28