This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-12-17
Channels
- # adventofcode (76)
- # announcements (6)
- # beginners (103)
- # boot (28)
- # calva (128)
- # cider (48)
- # cljs-dev (40)
- # clojure (268)
- # clojure-austin (2)
- # clojure-dev (2)
- # clojure-europe (47)
- # clojure-italy (10)
- # clojure-nl (17)
- # clojure-spec (2)
- # clojure-uk (15)
- # clojurescript (45)
- # code-reviews (14)
- # cursive (5)
- # data-science (2)
- # datascript (1)
- # datomic (52)
- # duct (4)
- # emacs (2)
- # figwheel (1)
- # figwheel-main (4)
- # fulcro (13)
- # hyperfiddle (51)
- # leiningen (19)
- # nrepl (40)
- # off-topic (45)
- # pathom (3)
- # pedestal (28)
- # portkey (7)
- # re-frame (25)
- # reagent (76)
- # reitit (7)
- # shadow-cljs (92)
- # slack-help (3)
- # specter (5)
- # timbre (2)
- # tools-deps (39)
- # unrepl (1)
- # vim (13)
@jaihindh.reddy Check out this library that does this for all the java time types https://github.com/magnars/java-time-literals Also, if you print it as #inst and read it back in it'll change type, and lose the offset information.
hi, i was hoping this snippet would access one key or use another if it is missing
but the (or) bit doesn’t ever seem to return if the first value is missing
the second entry always comes out as blank
i think it’s to do with how i’m accessing the key values inside the map function?
the expression (or :login :id)
always returns :login
So your line 10 is equivalent to (map :login users)
You need to turn (or :login :id)
into an actual function (e.g. anonymous), so that its outcome is dependent on some input
@christopher.paul do you know how to do that? (hint: you're close)
sorry was in dm with sundar
he helped me
as the key reference always returns true as it is a function
Oh TIL. That's cleaner than what I had in mind, thanks!
For transparency:
(map (some-fn :login :id) [{:id "1"}])
-> ("1")
vs
(map #(or (:login %) (:id %)) [{:id "1"}])
-> ("1")
i ended up writing my own ropey version of some-fn
hey all, I am off work from Wednesday till the new year, do you recon there is a project I can do in that time (because I have 0 life)?
I have a pattern in Clojure that keeps popping up and I'm not sure the best "clojure-esque" way to do it. Say, for example, I have the following:
user=> (println my-map)
{12 3, 10 4, 8 1, 7 1, 6 1, 4 1}
This is a map of keys (i.e. 12, 10, 8, …) with associated values (3, 4, 1, …)
I'm interested in finding the INDEX of the item in the map with the max value. So in this case [10 4] is the one I'm interested in and I want its index which is 1.well it does… from a key to a value :)
You can use https://github.com/clj-commons/ordered if you want ordered maps though
Ok, then how do I get it "ordered" so that I can get the one I'm interested in?
well, you might also consider sorted-maps, not sure if they would satisfy
What specifically do you want the order for?
FWIW @joseph.hance if you're actually using maps as if they were ordered, it's not just a conceptual issue, the insertion order actually does get shuffled around randomly
user=> {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9}
{7 7, 1 1, 4 4, 6 6, 3 3, 2 2, 9 9, 5 5, 8 8}
Are you sure the shuffling is random? I'm getting the exact same result as you
the hashing function that my repl is the same that your uses so the results are consistent
But it's still unpredictable in practice
Got it, thanks 😉
Let me take a step back, I have:
user=> (println my-map)
{12 3, 10 4, 8 1, 7 1, 6 1, 4 1}
And I want to find [10 4] because 4 is the max value in this data structure.Note: beginner talking here.
What if you just take “10” as the “index” you want? Will that meet your needs?
lol, this is the first I’ve heard that there was a max-key
fn in clojure core.
Yeah I can see where that would be annoying.
it's the way it is because it's an "enhanced" max
, but it feels closer to a filter
-like function to me
@joseph.hance can you give a bigger-picture example of a case where you run into this pattern? I have a feeling we may be missing the forest for the trees and it sounds like you may have an interesting question behind your question.
...looking up max-key. Thanks.
hey guys
how can I do something like this:
(rename-keys (function-gives-map arg1) {:key1 :keey})
to a thread macro like this:
(-> arg1 function-gives-map ...)
user=> (macroexpand-1 '(-> arg1 function-gives-map (rename-keys {:key1 :keey})))
(rename-keys (function-gives-map arg1) {:key1 :keey})
there's no problem with arity ordering?
yeah, but rename-keys takes map and another map of keys that I rename]
if I wanted to put in the second arg of rename-keys I would use ->> ?
thanks 😃
play with macroexpand-1
as I've shown above to get a feel if you're not sure what a macro does
thanks!
Hey! People who are learning clojure and doing Advent of Code! Last week I did a code review on stream, and I found it to be a fun and interesting exercise. https://www.twitch.tv/videos/349004355?t=55m30s If anyone wants me to do the same for their Advent of Code code, please let me know! I won’t do one every day (and I won’t do one today), but I’d like to do one a couple times a week if enough submissions roll in.
It was awesome, thank you so much for the initiative. Unfortunately I couldn't attend as I expected, but I believe workflow sharing (so that others can understand REPL usage, interesting tools and dynamics) is the best approach forward if we want people to understand the niceties of the language and how the parentheses disappear. 🙂 Thanks!
Is there a better way to write this utility function?
(defn read-int-from-envvar-with-fallback [env_var_name default_val]
(min
(try
(. Integer parseInt (System/getenv env_var_name))
(catch Exception e default_val))
default_val))
(if-some [txt (System/getenv env_var_name)]
(min
(Long/parseLong env_var_name 10) ;; NOTE you should pass the radix explicitly
default-val)
default-val)
@nilesh.trMaybe something like this? (merge my-map {:add-key-if-new 7, :another-key-but-only-if-new 23})
merge
will overwrite existing keys right?
oh, but probably the other way around on the args there. sorry
and that isn't necessarily the fastest way to achieve what you asked, for a single key.
I can't think of anything off hand that is already there, like that.
@jstaab maybe update
? (update m k #(or % v))
Also, these functions will not work if nil
or false
are valid values in the map. You could support those by changing from (get m k)
to (contains? m k)
Hey guys, I'm wondering if you can help me out. I am trying to play around with luminus and get that running. I can create a new app from template lein new luminus firsttest +postgres
and I have a postgres url string set as the :database-url "
I can start the app and run the default user migrations, but
user=> (firsttest.db.core/create-user! { :id "fun1" :first_name "me" :last_name "me" :pass "secret" :email "" })
IllegalArgumentException db-spec mount.core.DerefableState@548f4400 is missing a required parameter clojure.java.jdbc/get-connection (jdbc.clj:409)
Any idea where I could start digging in to where this error is coming from?
PS -> the project clj file https://gist.github.com/brimatteng/f8925474224c5c8b2a3badac7af1955dHey @U9CPW8J1Y, did you figure this out? I can take a look, but I want to check that you haven’t figured it out already 🙂
Hey I haven’t gotten much further than this. It might be a problem with the luminous template for Postgres
@U9CPW8J1Y Can you paste your queries.sql please?
@U9CPW8J1Y and what do you get if you run lein test
from the root dir?
@U8WFYMFRU here’s the queries.sql created by that template:
-- :name create-user! :! :n
-- :doc creates a new user record
INSERT INTO users
(id, first_name, last_name, email, pass)
VALUES (:id, :first_name, :last_name, :email, :pass)
-- :name update-user! :! :n
-- :doc updates an existing user record
UPDATE users
SET first_name = :first_name, last_name = :last_name, email = :email
WHERE id = :id
-- :name get-user :? :1
-- :doc retrieves a user record given the id
SELECT * FROM users
WHERE id = :id
-- :name delete-user! :! :n
-- :doc deletes a user record given the id
DELETE FROM users
WHERE id = :id
@U9CPW8J1Y I may be too rusty on postgres to help with this one. Thanks @U8WFYMFRU!
I did just launch a postgres luminus app a couple of weeks ago so it’s a bit more fresh.
@U9CPW8J1Y The queries looks good, what about the test file?
In my test, db was accessed differently:
(ns firsttest.test.db.core
(:require [firsttest.db.core :refer [*db*] :as db]
(ns firsttest.test.db.core
(:require [firsttest.db.core :refer [*db*] :as db]
[luminus-migrations.core :as migrations]
[clojure.test :refer :all]
[clojure.java.jdbc :as jdbc]
[firsttest.config :refer [env]]
[mount.core :as mount]))
(use-fixtures
:once
(fn [f]
(mount/start
#'firsttest.config/env
#'firsttest.db.core/*db*)
(migrations/migrate ["migrate"] (select-keys env [:database-url]))
(f)))
(deftest test-save-user
(testing "Save user to db"
(jdbc/with-db-transaction [t-conn *db*]
(jdbc/db-set-rollback-only! t-conn)
(is (db/create-user! t-conn
{:id "fun1"
:first_name "me"
:last_name "me"
:pass "secret"
:email ""})))))