This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-02-20
Channels
- # architecture (25)
- # beginners (68)
- # cider (10)
- # clara (3)
- # cljs-dev (90)
- # cljsrn (16)
- # clojure (132)
- # clojure-austin (7)
- # clojure-berlin (3)
- # clojure-czech (1)
- # clojure-dusseldorf (1)
- # clojure-greece (5)
- # clojure-italy (39)
- # clojure-spec (5)
- # clojure-uk (78)
- # clojured (2)
- # clojurescript (92)
- # community-development (6)
- # cursive (7)
- # data-science (1)
- # datascript (14)
- # datomic (32)
- # duct (8)
- # emacs (5)
- # figwheel (3)
- # fulcro (47)
- # hoplon (12)
- # jobs (10)
- # luminus (16)
- # lumo (5)
- # off-topic (1)
- # onyx (2)
- # parinfer (47)
- # pedestal (6)
- # re-frame (10)
- # reagent (2)
- # reitit (61)
- # ring (8)
- # ring-swagger (16)
- # shadow-cljs (116)
- # sql (17)
- # utah-clojurians (2)
- # vim (1)
@lee.justin.m Sorry for not being clear. I meant when using the library https://github.com/tolitius/mount to manage state, we can use the provided macro defstate
to manage states. Apparently I have a problem where some of the states started not in order and some of them started multiple times when used in cljs.
can I use an atom to store a string and then swap it like so?
(def some-string (atom ""))
(swap! some-string "Some new String")
Otherwise, if you wanted to use swap!
you could do something like (swap! some-string (constantly "foo"))
swap!
is for altering the value by applying a function. reset!
is just swap! (constantly v)
The important bit is that swap!
takes an update fn while reset!
dispenses with that and takes a value
Hi everyone, I want to make sure, is there going to be a significant performance hit if I have a function that structured like this?
(defn retrieve-entity
[id & [{:keys [include-detail?]}]]
(let [main-entity (retrieve-from-db id)
hydrate-details #(assoc % :minor-entity (retrieve-by-parent (:minor-id main-entity)))]
(cond-> main-entity
include-detail? hydrate-details)))
I haven't done any measurements on that recently, but am not aware of any reason why having #(...) or (fn [...] ...) inside of one function is slower, vs. using defn at the top level earlier in the file.
There are multiple places in Clojure core library code that use functions defined inside of other functions like that, including ones considered performance-sensitive.
I see, glad to know that I don't have to refactor it, thank you @andy.fingerhut
Hi everyone, how can I use map
only to a hash-map values? For example if I have
{:first [1 2 3]
:second [4 5 6]}
and I map the values with function inc
, I want the result to be
{:first [2 3 4]
:second [5 6 7]}
another approach, just for fun:
user=> (into {} (map (juxt key (comp str val)) {:first [1 2 3]}))
{:first "[1 2 3]"}
So there are multiple variations of a function called "map-vals" that you can copy and use (or use a library like 'medley' that defines it): https://github.com/weavejester/medley
Once you have map-vals, you can call it like this to achieve the effect you ask for: (map-vals #(mapv inc %) d1)
(mapv inc [1 2 3])
gives you [2 3 4]
You can find some approaches here : https://stackoverflow.com/questions/1676891/mapping-a-function-on-the-values-of-a-map-in-clojure
(let [foo {:first [1 2 3]
:second [1 2 3]}]
(into {} (for [[k v] foo] [k (mapv inc v)])))
I hope map-vals is added to the core : https://dev.clojure.org/jira/browse/CLJ-1959@hawari.rahman17 also have a look at reduce-kv
https://clojuredocs.org/clojure.core/reduce-kv#example-57d1e9dae4b0709b524f04eb
if this is something you find yourself doing frequently, I recommend Specter: https://github.com/nathanmarz/specter
I've looked into medley before, think I'll just use that, as it's not something that I'll do frequently
(def x [{:foo 2 :bar 11}
{:bar 99 :foo 1}
{:bar 55 :foo 2}
{:foo 1 :bar 77}])
;sort by :foo, and where :foo is equal, sort by :bar
(sort-by (juxt :foo :bar) x)
;=>({:foo 1, :bar 77} {:bar 99, :foo 1} {:foo 2, :bar 11} {:bar 55, :foo 2})
how to sort high to low on :foo? with juxt or comp? I can't figure out how to slip the comparator in?
(sort-by (juxt (partial :foo >) :bar) x)
I get a npe.(->> m
(sort-by (comp (partial :totalAudienceSize >) :catch-all)
))
doesn't order correctlyI get it eventually - thanks. (apply brain coffee)
I am having this issue as well with env vars that getting loaded but are not available when referencing env in another namespace. Anyone knows what's the problem? Thanks!
If I have a map like this {:a 1 :c 2 :d 3 :e 4}
with 10 more key value pairs , Is there a simpler way that takes less code to return a map with all of the keys besides one other than select-keys?
also, it can take multiple keys if you need to dissoc a few
(dissoc {:a 1 :b 2 :c 3 :d 4} :a :b) => {:c 3, :d 4}
Thank you, the reason I need to convert is so I can insert my map straight into the database rather than having to create another map with each column
@josmith2016 this might just be common sense, but I find that the rules for data conversions that work best are: 1) don’t convert unless forced to 2) convert at a boundary between systems when forced to
if it’s coming from java, sadly the “boundary” might be fuzzy, since interop is so trivial - so maybe the database layer boundary is a better place for the conversion
Does anybody know how to get an sql timestamp
from a string using the clojure.java-time
library?
I’m getting this error Text '2018-02-19 18:20:25.355' could not be parsed at index 10
when trying to convert it with this piece of code (jt/sql-timestamp (:date receipt))
Where (:date receipt)
= 2018-02-19 18:20:25.355
@josmith2016 That's because the string you are trying to parse is not in the default ISO format that Java Time expects. You'll need to construct an appropriate formatter and do the parsing directly (and then have clojure.java-time
convert it to sql-timestamp
).
Actually, I think a simpler solution will be to replace that space with T
and then it will parse automatically (jt/sql-timestamp (clojure.string/replace (:date receipt) " " "T"))
That format -- with a T
separating date and time instead of a space -- is a standard ISO format @josmith2016
Awesome thanks @seancorfield
Say I don't have my string in that format. Would I want to create the formatter using clojure objects and methods or using Java classes and methods @seancorfield
You can create a formatter (using jt/formatter
) and then jt/parse
your string using it. You probably want to read the Java Time documentation for details of the formatter patterns https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html @josmith2016