This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-10-18
Channels
- # announcements (8)
- # aws (5)
- # babashka (69)
- # beginners (37)
- # calva (222)
- # cider (10)
- # clj-commons (8)
- # clj-kondo (69)
- # cljdoc (5)
- # clojure (62)
- # clojure-dev (23)
- # clojure-europe (37)
- # clojure-italy (2)
- # clojure-nl (6)
- # clojure-sg (6)
- # clojure-uk (5)
- # clojurescript (25)
- # clojureverse-ops (12)
- # conjure (1)
- # cursive (1)
- # fulcro (9)
- # gorilla (1)
- # graalvm (6)
- # graphql (1)
- # gratitude (1)
- # honeysql (7)
- # introduce-yourself (4)
- # jobs (1)
- # kaocha (9)
- # keyboards (4)
- # leiningen (8)
- # lsp (21)
- # malli (9)
- # music (3)
- # nextjournal (17)
- # nrepl (6)
- # off-topic (10)
- # pathom (12)
- # portal (25)
- # reagent (8)
- # releases (1)
- # specter (1)
- # xtdb (1)
I want to compare a number 35 with every item of a sequence (35 36 38) and in return gives me (true false false) . How can I do that ?
(map #(= 35 %) '(35 36 37))
;; => (true false false)
But what's the end goal?
This might not be the proper way to do it, depending on what you plan on doing with the returned sequence.
I have a list of numbers (35 36 48) and I want to create conditions like this in when block (when (or (= x 35)(= x 36)(= x 37)))
So you want to perform an action when x is found in a sequence?
I want to display a content on client side (hiccups) when I am hard coding it like it is working
Perhaps something like this?
(when (some #(= 35 %) '(35 26 17)) (do something...))
it may work let me try
if you wanted just a list of true false results you could use keep
but it looks like (when (some ...))
is doing the trick you need 😃
https://clojuredocs.org/clojure.core/keep
Hello friends. I’m writing some code that deals with Java File objects, but really only uses a few methods on them. What is the best way to create something that responds to .isDirectory
, .getName
, and a few other methods that exist on the File
object for testing?
(defprotocol FileOperations*
(is-directory [_])
(get-name [_]))
(extend-type File
FileOperations*
(is-directory [this]
(.isDirectory this))
(get-name [this]
(.getName this)))
(defn your-code [file]
(println (get-name file)))
;; In testing
(your-code
(reify FileOperations*
(is-directory [_] false)
(get-name [_] "bob")))
if the scope of this is small and you aren’t recreating all of File, I think its a decent approach
but it could easily break if you pass the File to anything you don’t control that actually expects a File
Neato, I haven’t dug in to protocols yet.
Is the *
at the end of the protocol name just a convention?
there's an idiom in clojure to name "implementation details" with a *
suffix - it's a signal that the thing is only defined as a building block. eg. let
is written in terms of let*
and fn
is written in terms of fn*
(`let*` and fn*
don't do destructuring). that said, I'm not sure if this example fits, as there's no FileOperations
that you'd use instead?
@U051SS2EU I put the * because I was thinking “okay, maybe the external contract of the function is that it takes a File, but when you are internal to the function you know that narrows to a FileOperations”, so other code outside of this namespace and test file doesn’t try to make “creative” FileOperations objects but i could go either way on that. Probably not a nuance to have thrown in beginners in hindsight
Hi, I have a seq of maps like this: [{:id “foo” :balance 50.9M} {:id “bar” :balance 12M} I also have a total amount `total-amount` I need to iterate through my seq of maps, and successively subtract the :balance in each map from my `total-amount` until `total-amount` reaches zero. I also need to record the subtracted amount from each record in the original seq of maps. I’ve been messing around with map on this, but I can’t figure out how to keep track of the running value for the remainder as I subtract each `:balance` from the `:total-amount` . Any help gratefully appreciated.
reaches zero as in I have an amount (let’s say 10), and I may have (let’s say) 3 records in my seq of maps with :balances (let’s say 6, 5 and 3 respectively). As I iterate through those records, I first subtract 6 from 10 (answer 0 remainder 4), then subtract 4 from 5 (remainder -1), and then know that I can ignore the last record because I have ‘reached zero’ hope that makes sense.
maybe something like this?
(->> [{:id "foo" :balance 50M} {:id "bar" :balance 12M} {:id "baz" :balance 99M}]
(reductions (fn [{:keys [total-amount]} {:keys [balance]}] {:total-amount (- total-amount balance)}) {:total-amount 60M})
(take-while #(-> % :total-amount (> 0M))))
reductions
is like reduce, but will return a lazy seq of the intermediate steps that go into producing the final result. Then you can use something like take-while
to provide a terminating condition. Does that make sense?
It does. I’ll try your suggestion, and thanks for the quick response. A small bit of help can save hours of messing with this stuff.
Not quite, I have 10, {:balance 6}{:balance 5}{:balance 3}, and want {:balance 0}{balance 1}{:balance 3}
..and obviously I want to know about it if the sum of all balances isn’t enough to clear the original amount.
(let [total-amount 20
x [{:id "foo" :balance 6} {:id "bar" :balance 5} {:id "bar" :balance 3}]
y (reductions + (map :balance x))
z (map #(if (< total-amount %) total-amount %) y)
t (last y)]
[(if (< total-amount t) 0 (- total-amount t))
(map #(cond (<= %2 %3)
(assoc % :balance 0)
(< (:balance %) (- %2 %3))
%
:else
(assoc % :balance (- %2 %3)))
x y z)])
you can come up with better variable namesHi, I have never seen this error before
2021-10-18 11:52:54.507:WARN:oejs.HttpChannel:qtp1642716671-2292: handleException /le-page com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'no': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: (StringReader); line: 1, column: 3]
Popped up in my server console 3 times today 😮Looks to me like a web request returned some invalid JSON that you're trying to parse using Jackson (via cheshire?) ... Maybe the web service is saying something like "no data"???
weird, I don't think that page is making any requests
o_O hmm