This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-07-19
Channels
- # announcements (4)
- # aws (9)
- # babashka (1)
- # beginners (41)
- # cider (13)
- # clojure (45)
- # clojure-europe (16)
- # clojure-hungary (3)
- # clojure-norway (6)
- # clojure-uk (6)
- # clojurescript (14)
- # clr (3)
- # css (1)
- # datalevin (2)
- # datomic (2)
- # fulcro (8)
- # introduce-yourself (6)
- # jobs (3)
- # malli (2)
- # nbb (49)
- # off-topic (3)
- # reitit (23)
- # shadow-cljs (36)
- # tools-deps (2)
Hey. I'm trying to find how to specify type hint for my Object
array and I can't find it here:
https://clojure.org/reference/java_interop#typehints
https://clojure.org/reference/metadata#_metadata_reader_macros
Are there better places to look in?
I used ^java.util.ArrayList
. I'll try that too.
An array list and an array are two very different things - make sure you to use the right one.
I know. I only wanted something that I wouldn't need to use atom
. I have a dependency injection framework and it returns less data that I want so I'll make a hack to mutably get something I want from initialization.
ArrayList
and array
are very different things and I wanted it to spend little time on initialization. Obviously I was trying to create already pre-allocated ArrayList (even though it's only one item).
But anyway. It wasn't a big gain. I lost too much performance by being forced to use DI framework as it's hardcoded into the initialization of a third-party lib.
I gained more performance by removing logging than doing this hack :rolling_on_the_floor_laughing: (20ms per call)
What’s the opposite operation of not-any?
? This one comes up many times, I want true
if any object in coll fits predicate.
(not-any? #(= (:id %) 1) coll)
vs
(some #(when (= (:id %) 1) true) coll)
is way more wordy and redundant, also first one returns true/false, second one returns true/nilsome always kinda stood out to me…
any? is already defined in clojure.core
also putting boolean
around it doesn’t work really… you need to also change predicate to return nil
if you return false
in some
predicate it will keep going
that’s why it’s so clunky and you have to use when
some
returns first non-nil result of the predicate given
oh wait… I am wrong, other *-some
operations do that… my bad
sorry about that I thought it used the same rules as if-some
when-some
etc
is there a good way to reuse core.match
patterns to do different things on the same match in different places?
I’m wishing I could do something like
(def match-pattern '[a b])
(defn subtract [x]
(clojure.core/match x
match-pattern
(- a b)
_ x))
(defn add [x]
(clojure.core/match x
match-pattern
(+ a b)
_ x))
This is part of why Rich doesn't like pattern matching, it's brittle and doesn't handle code reuse. If you want to do this it must be done with your own wrapper macro.
yeah. currently i’m resorting to producing data from the match, like
(defn match-ab [x]
(clojure.core.match/match x
[a b] {:a a :b b}
_ nil))
and then operating upon that. it works fine, just wondering if perhaps there’s a better way. what’s the alternative approach that is not based on pattern matching at all?could just make a predicate that returns if it's a two-tuple and nil otherwise and do
(defn match-ab [x]
(when (two-tuple? x)
(zip-with [:a :b] x)))
yeah, the actual case is with nontrivial nested vectors, so the pattern match is very convenient for extracting the data
sure, then make your validation also extract the data into a more convenient format.
it doesn't just have to return a boolean
when-some
is your friend
right, and I do. I was just saying that it’s nice to be able to use clojure.match for that validation, and return the data as opposed to operating upon the data. it’s my way of attempting to get the best of both worlds: convenience of pattern matching, but with some resusability
sure, that makes sense, but unfortunately core.match doesn't really support this, and I think for good reason, as the example you posted breaks hygeine in an unpredictable way that can cause issues.
Is there a cool way for map
to refer to the previous item in a sequence, or should I just reduce
it
(I’m adding a running balance to a sequence of transactions)
map
cannot do that without an extra step where you create an extra shifted sequence and combine it into a sequence of pairs with the original sequence.
But reduce
or loop
can do that.
Chances are you can also use partition
and then map over its result
Or if it is something like a vector, you could map over the indexes instead of the elements
map
can operate over multiple sequences at once, so something like below might also work.
user> (let [values (range 10)]
(map (fn [prev curr] [prev curr])
values
(rest values)))
([0 1] [1 2] [2 3] [3 4] [4 5] [5 6] [6 7] [7 8] [8 9])
A common idiom is:
(map (fn [prev next]
(my-combine prev next)
coll
(rest coll))
I think it's pretty cool 😎 .Thanks guys! I like the different methods. Since I needed a running sum, I found reductions
and went with this:
(def txns [{:amount 10}
{:amount 20}
{:amount 30}])
(def bals (reductions + (map :amount txns)))
;; => (10 30 60)
(mapv #(assoc %1 :balance %2) txns bals)
;; =>
;; [{:amount 10, :balance 10}
;; {:amount 20, :balance 30}
;; {:amount 30, :balance 60}]
Like was mentioned about partition before, you could use (map your-fn (partition 2 1 coll))
but yeah, reductions
is like scan in other functional languages and does this well, yeah
yeah I realized I actually need more than the previous item, needed an accumulated sum as of the previous item