This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-09-27
Channels
- # beginners (86)
- # calva (1)
- # cider (21)
- # clj-kondo (2)
- # clojure (31)
- # clojure-europe (3)
- # clojure-italy (7)
- # clojure-nl (7)
- # clojure-spec (15)
- # clojure-uk (70)
- # clojurescript (4)
- # clojutre (31)
- # code-reviews (6)
- # cursive (10)
- # datomic (8)
- # duct (3)
- # emacs (2)
- # fulcro (34)
- # funcool (3)
- # jackdaw (2)
- # jobs (10)
- # jvm (2)
- # kaocha (1)
- # off-topic (21)
- # pathom (11)
- # re-frame (10)
- # reagent (4)
- # schema (1)
- # shadow-cljs (72)
- # sql (1)
- # tools-deps (3)
- # vim (9)
- # xtdb (4)
oh, I wondered what that offered and it just calls the constructor
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/MapEntry.java#L19
i don't believe so. what problem are you trying to solve? how does laziness impact you?
found something like this: user> (instance? clojure.lang.LazySeq (lazy-seq (map inc))) ;; => true
@papachan There are a lot of lazy sequences that are different types...
clojure.lang.LongRange
user=> (type (range))
clojure.lang.Iterate
user=>
(but I haven't checked ancestors yet)Neither of those are LazySeq
, for example:
user=> (instance? clojure.lang.LazySeq (range 10))
false
user=> (chunked-seq? (range 10))
true
user=> (instance? clojure.lang.LazySeq (range))
false
user=> (chunked-seq? (range))
false
user=>
what problem are you trying to solve?
(that was my next question 🙂 )
sequences are inherently maybe lazy. asking whether it is implies that you care if it isn't, and that's usually a sign that you're doing something fragile or wrong
realized?
is another predicate in this area but it's fraught with similar perils and is also something I recommend not using
Hello! I'm looking for an interactive tutorial of clojure, like something that guides you through the basics. Does anyone know of such a thing? Thanks!
If I may suggest this: https://clojurebridgelondon.github.io/
The when condition is about the map
Well I’d say the left hand side, which is fundamentally about a map (and also about its structure)
Read it as “when there is a map to destructure, destructure it’s bindings and invoke the body”
If you want to guard against missing keys in a map m
, you could do something like
(when-let [{:keys [a b c]} (and
(every? (fn [k] (some? (get m k)))[:a :b :c])
m)]
(prn a b c))
Or, if you want to allow explicit nil
you could use (contains? m k)
instead of (some? (get m k))
Wouldn’t just (and a b c)
be easier?
Definitely an option, but it would be false for something like {:a false :b "foo" :c true}
. You could still do (let [...] (when (every? some? [a b c]) ...))
(or (and (some? a) (some? b) (some? c))
) so that the binding isn't so big.
Definitely an option, but it would be false for something like {:a false :b "foo" :c true}
. You could still do (let [...] (when (every? some? [a b c]) ...))
(or (and (some? a) (some? b) (some? c))
) so that the binding isn't so big.
Just been looking at http://chouser.n01se.net/apply-concat/ What would you recommend if you had a sequence of either maps or sequences of maps, and you wanted a sequence of maps?
e.g. [{:a 1} [{:a 2} {:a 3}] {:a 4}] and we want ({:a 1} {:a 2} {:a 3} {:a 4}) Flatten seems to be the only correct one here, apply concat and friends all turn the un-nested maps into vectors, ([:a 1] {:a 2} {:a 3} [:a 4])
Usually there’s a good way to ensure you’re only getting sequences of maps. Is that possible in your case?
(defn splice-when
[pred]
(fn [rf]
(fn
([] (rf))
([result] (rf result))
([result input]
(if (pred input)
(reduce rf result input)
(rf result input))))))
(sequence (splice-when vector?) [{:a 1} [{:b 1} {:c 1}] {:d 1}])
A funny way using a transducer
I've got a bunch of functions that each return a thing (mostly) but sometimes multiple things and sometimes no things, and I want to collect those things.
I think if you sometimes return multiple things, it would make most sense to always return multiple things even if it’s a sequence of one thing (or nil if theres no things)
At the moment they return a map or vector of maps or nil. I suppose I could get them to always return a vector of maps (maybe empty) but the commonest case is one map so seems a bit wasteful
@danieleneal yeah, you may be right
It would make for a cleaner api imo
then you can just (sequence cat xs)
or (mapcat identity xs)
away without a care in the world
I'll look into it!
Does a function like this have a name in functional programming? And does such a function exist in clojure.core and I just haven’t found it?
(defn apply-if
"Returns a function that applies f if pred returns true for the specified value"
[pred f]
(fn [v]
(if (pred v)
(f v)
v)))
In clojure.core
Hi guys, I have a question , so in OOP world , we have the repository pattern that is responsible to query and save the domain objects , it can be either single object or a graph of them, usually under a ORM to deal with the object relational mismatch or manual mapping. What is the approach in Clojure? I asume that there is nothing like the respository pattern, would then it be , for query load the tables and transform to domain model as clojure maps? and for persistence , transform the domain model map back to sql insert/update using some statement builder ? , that seems to be a lot of manual work.
@vachichng I would look at next-jdbc (https://github.com/seancorfield/next-jdbc). In my code, I've just treat things like maps and get and store.
An in terms of "work" - I've found it to be refreshingly easier/simplier and more comprehensible than working with an ORM
Even in my non-clojure work (Kotlin), I use jooq which is a very simple layer over sql
@vachichng if you want to dig deeper there are #sql and #honeysql channels
But the Clojure way is to use plain data (hash maps) for domain objects / entities and simply read / write them from / to a database. You won't find much support for "relations" for example.
is there a best way for this same result:
(apply str (remove #((set "[]") %) "[abc]"))
for one thing, (set "[]")
is idiomatically #{\[ \]}
imho
but using seq operations to go from one string to another is usually a bad idea
also #(s %)
for some set s, is just s
for all #(f %)
you should replace it with f
, with the exception of enforcing only one arity being possible via runtime error (which...)
what purpose is the use of (delay ...)
serving here?
https://github.com/http-kit/http-kit/blob/master/src/org/httpkit/client.clj#L97
@csd it's helpful to remember that all def forms are evaluated when clojure loads a file, there's no "compile only mode" available
so a delay is a simple way to ensure you can load up the code without firing the missiles
for example, without the delay, doing aot compilation while making a jar would create a new http client