Fork me on GitHub
#beginners
<
2021-10-18
>
Muhammad Hamza Chippa11:10:55

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 ?

Johan Thorén11:10:13

(map #(= 35 %) '(35 36 37))
;; => (true false false)

Johan Thorén11:10:42

But what's the end goal?

Johan Thorén11:10:12

This might not be the proper way to do it, depending on what you plan on doing with the returned sequence.

Muhammad Hamza Chippa12:10:21

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)))

Johan Thorén12:10:08

So you want to perform an action when x is found in a sequence?

Muhammad Hamza Chippa12:10:51

I want to display a content on client side (hiccups) when I am hard coding it like it is working

Johan Thorén12:10:07

Perhaps something like this?

(when (some #(= 35 %) '(35 26 17)) (do something...))

🙌 1
Muhammad Hamza Chippa12:10:50

it may work let me try

sova-soars-the-sora14:10:19

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

Jamison Dance13:10:41

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?

emccue13:10:24

So you want to create mock file objects?

emccue13:10:20

you could make a protocol for the parts of File you use

emccue13:10:10

(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")))

emccue13:10:41

if the scope of this is small and you aren’t recreating all of File, I think its a decent approach

emccue13:10:48

but it could easily break if you pass the File to anything you don’t control that actually expects a File

Jamison Dance14:10:50

Neato, I haven’t dug in to protocols yet.

Jamison Dance14:10:08

Is the * at the end of the protocol name just a convention?

noisesmith15:10:55

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?

emccue22:10:05

@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

DenisMc14:10:54

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.

Apple14:10:56

reaches zero?

Ed14:10:24

I'd suggest looking at reductions

DenisMc14:10:39

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.

Ed14:10:40

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))))

DenisMc14:10:21

Should read ‘subtract 10 from 6’

Ed14:10:58

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?

DenisMc14:10:12

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.

👍 1
Apple14:10:25

so you have 10, and 6-5-3, and you want result 6-5

DenisMc14:10:05

Not quite, I have 10, {:balance 6}{:balance 5}{:balance 3}, and want {:balance 0}{balance 1}{:balance 3}

DenisMc14:10:48

..and obviously I want to know about it if the sum of all balances isn’t enough to clear the original amount.

Apple14:10:21

(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 names

DenisMc15:10:02

Thanks a lot for your help, I think that will work for me.

sova-soars-the-sora20:10:52

Hi, 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 😮

Ed20:10:12

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"???

sova-soars-the-sora21:10:16

weird, I don't think that page is making any requests