Fork me on GitHub
#beginners
<
2021-11-19
>
Little Mose00:11:08

Hi, any suggestion of open source project for a beginner with some entry level Clojure learner to start?

👍 2
Shawn14:11:55

Are you looking for a public project to contribute to, or are you generally looking to code more Clojure to improve your skills? If the latter, I would recommend 4clojure and Advent of Code.

pavlosmelissinos14:11:47

Not sure about those to be honest. They work for some people but others (myself included) aren't into solving arbitrary puzzles. They can be challenging but I think they're too detached from real-life applications. If you ask me, real-world projects are usually more meaningful if they solve real-world problems, so I'd start from that. What's a problem in your life that you'd like to solve? This is great because part of the design process is to read other people's code and you're less likely to lose interest in it because, once it's finished, you'll be getting some value out of it. Games are a fun choice as well and they make you think about state in Clojure, e.g. tic-tac-toe is a popular first project.

pavlosmelissinos15:11:46

Oh and in the beginning try to avoid projects that have a lot of moving parts, e.g. an eshop is not a good first project imo. On the other hand, working with public datasets is simple and can be a lot of fun. Interact with one of the countless public APIs out there and do stuff with their data.

Little Mose01:11:07

Thanks for reply! Very helpful. Yes, I was addicted to programming contest, but after many years of programming, I don't have the time and desire for puzzle-style problem. I spent some time in https://exercism.org/tracks/clojure to get the sense of programming in Clojure, and then I would like to find the differences between Clojure and other languages in real projects.

Little Mose01:11:49

Currently, I'm trying to implement a rss-reader client, which I have implemented in other languags, I think it's a nice start point.

Benjamin10:11:04

what is the closest to "`condlet` " ? I'm looking for a mix of cond and when-let . Like

(condlet
 [thing (re-find ...)] (do-stuff1 thing)
 [other-thing (re-find ...)] (other ...)
 default)

Ben Sless10:11:42

What's exactly the expected behavior?

Ben Sless10:11:37

Seems like you can roll your own with if-let

Benjamin10:11:04

don't I have to nest if-let then?

thumbnail10:11:24

You can use a custom macro to do the nesting for you.

Benjamin10:11:58

lulling to the docstring. Did Rich mention that he doesn't like this one once what?

thumbnail10:11:02

yeah first time I noticed the comment tbh; but probably it's a complexity thing 😛

👍 1
Ben Sless10:11:24

Just don't mix the two, keep cond-let just a series of nested if-lets

Benjamin10:11:44

seems to work

(defmacro condlet
  "Try to bind each binding form via if-let.
  Evaluate the first form with the binding-form bound.
  Else evaluate to the last dangling form, nil if ommitted. "
  [& forms]
  (when forms
    (let [[binding form & more-forms] forms]
      (if (and binding form)
        `(if-let
             ~binding ~form
             (condlet ~@more-forms))
        binding))))
tried better doc string

Ben Sless12:11:29

This is good, and simpler than the sugar-ed version

pithyless22:11:34

(defmacro <<- [& forms]
  `(->> ~@(reverse forms)))

(<<-
  (if-some [thing (re-find ...)] (do-stuff1 thing))
  (if-some [other-thing (re-find ...)] (other ...))
 default)
<<- brought to you by this tweet: https://twitter.com/cgrand/status/1281527501387440128 @U02CV2P4J6S

clojure-spin 1
Muhammad Hamza Chippa14:11:02

how i can compare the value of vector with its previous values for example if I have vector [5 6 4 8] how can I compare the 6 with 5 , 7 with 6 I wanted something like this if value is greater than previous value return red otherwise green like [green green red green] ?

raspasov14:11:18

(->> 
 [5 6 4 8] 
 (partition 2 1)
 (map #(apply < %))
 (map #(if % :green :red)))

manutter5114:11:33

Maybe this will help you?

(partition 2 1 [5 6 4 8])
=> ((5 6) (6 4) (4 8))

Akiz15:11:19

Hi, lets say that i get a max values like this

(map 
  (fn [lst] (apply max-key :price lst)) 
  (partition 5  
    [{:name 1 :price 10} 
     {:name 2 :price 10} 
     {:name 3 :price 20} 
     {:name 4 :price 50} 
     {:name 5 :price 40} 
     {:name 6 :price 70} 
     {:name 7 :price 15} 
     {:name 8 :price 20} 
     {:name 9 :price 14} 
     {:name 10 :price 24}])))
and the result is
({:name 4, :price 50} {:name 6, :price 70})
I would like to learn what is the nice & idiomatic way to distribute this result to the original maps like this fe:
({:name 1 :price 10 :max 50} {:name 2 :price 10 :max 50} {:name 3 :price 20 :max 50} {:name 4 :price 50 :max 50} {:name 5 :price 40 :max 50}
{:name 6 :price 50 :max 70}  {:name 7 :price 15 :max 70} {:name 8 :price 20 :max 70} {:name 9 :price 14 :max 70} {:name 10 :price 24 :max 70}
thx

Ed16:11:07

(->> [{:name 1 :price 10} 
        {:name 2 :price 10} 
        {:name 3 :price 20} 
        {:name 4 :price 50} 
        {:name 5 :price 40} 
        {:name 6 :price 70} 
        {:name 7 :price 15} 
        {:name 8 :price 20} 
        {:name 9 :price 14} 
        {:name 10 :price 24}]
       (partition 5)
       (map (juxt (fn [lst] (apply max (map :price lst))) identity))
       (mapcat (fn [[mx lst]] (map #(assoc % :max mx) lst))))
maybe something like that???

👍 1
Akiz16:11:42

This is nice

raspasov08:11:33

In this case, I would take a step back and ask “Is there a way to structure the output data so I don’t have to distribute the result to every map?”

seancorfield16:11:50

My first thought is something like this @zikajk

dev=> (into [] 
 #_=>       (comp (partition-all 5)
 #_=>             (mapcat #(let [max-price (:price (apply max-key :price %))]
 #_=>                        (map merge % (repeat {:max max-price}))))) 
 #_=>       data)
[{:name 1, :price 10, :max 50} {:name 2, :price 10, :max 50} {:name 3, :price 20, :max 50} {:name 4, :price 50, :max 50} {:name 5, :price 40, :max 50} {:name 6, :price 70, :max 70} {:name 7, :price 15, :max 70} {:name 8, :price 20, :max 70} {:name 9, :price 14, :max 70} {:name 10, :price 24, :max 70}]

Akiz16:11:25

Thanks, i wrote something similar. So i believe this is a not so bad solution coming from you :-)

seancorfield16:11:26

But I hope someone will suggest something nicer for that nested max/map expression...

👍 1
pavlosmelissinos17:11:24

How often would you say with-precision is really the answer to Non-terminating decimal expansion; no exact representable decimal result. ? I'm wondering whether the exception is just a symptom of bad design sometimes. Edit: "bad design" as in mixing bigdecimals and doubles when you only need doubles, or something. Sorry if the question doesn't make a lot of sense, I don't understand the issue very well but I've started seeing it more often lately (in code that didn't use to need the with-precision workaround) and that made me wonder.

West18:11:12

What's wrong with ratios?

mg18:11:38

Depends on the domain too. When modeling money, for instance, generally you want to stay in bigdecimal land

mg18:11:56

and in that situation you can have operations where you can't guarantee exact decimal representations. Exchange rates, for instance.

👍 1
pavlosmelissinos18:11:57

> What's wrong with ratios? How do you mean that?

West19:11:36

(/ 2 3)
;; => 2/3

(/ 2.0 3.0)
;; => 0.6666666666666666
I guess if you’re passing in floating point numbers you’ll get more out, but integers can be turned into ratios so there’s no need to deal with floating point precision in such cases.

andy.fingerhut20:11:55

Ratios are exact for + - * /, but note that the memory required to maintain that exactness can grow linearly over a sequence of N dependent math operations, if the denominators are different enough.

andy.fingerhut20:11:08

e.g. do not have small lowest-common-denominators with the intermediate numerator values.

andy.fingerhut20:11:52

And thus the run time for doing those exact + - * / operations can also grow at least linearly, or more, for many dependent math ops.

andy.fingerhut20:11:33

BigDecimal does not have these issues, because while it is exact for + - operations, it is NOT exact for all * / operations -- you must specify for each such operation how it will do rounding or truncation.

Noah Bogart19:11:34

is there a way to tell if a swap! changed the atom?

Alex Miller (Clojure team)19:11:07

use swap-vals! instead - compare the old/new that are returned

👍 1
🎯 1
Noah Bogart19:11:00

forgot about that one, thanks!

hiredman20:11:53

or compare-and-set!

sheluchin20:11:45

A little specific, but something weird happened and I don't understand.. I've been working with https://github.com/clj-jgit/clj-jgit for a few days, interacting with it from my REPL and such. All good. Suddenly I've started getting very heavy log output like this when making calls to it:

2021-11-19T20:14:05.103Z x DEBUG [org.eclipse.jgit.internal.storage.file.FileSnapshot:239] - file=/home/sheluchin/repos/foo/.git/config, create new FileSnapshot: lastRead=2021-11-19 15:14:05.099201000, lastModified=2021-11-18 14:52:20.617262000, size=92, fileKey=(dev=3c,ino=3903529)
2021-11-19T20:14:05.120Z x DEBUG [org.eclipse.jgit.internal.storage.file.FileSnapshot:548] - file=null, size changed from -1 to 124 bytes
2021-11-19T20:14:05.125Z x DEBUG [org.eclipse.jgit.util.SystemReader:389] - loading config FileBasedConfig[/home/sheluchin/.config/jgit/config]
I know it's a bit specific, but is there something I should check for? My repo doesn't seem to have any related changes.

hiredman20:11:42

Very likely you don't have any logging setup in your project, so the log output you get is at the whims of whatever logging setup your dependencies bring in

hiredman20:11:35

Which can be a tangle, likely you added a new dependency (not logging related) that did this

hiredman20:11:36

If you don't proactively setup logging then things like the order of different dependencies in the classpath can alter logging behavior

sheluchin20:11:30

I don't think so. I didn't add any new deps lately and my project is based on the fulcro-rad-demo, so it has this: https://github.com/fulcrologic/fulcro-rad-demo/blob/develop/src/shared/com/example/lib/logging.cljc Do I need some specific subkeys here maybe? https://github.com/fulcrologic/fulcro-rad-demo/blob/develop/src/shared/config/defaults.edn#L71 Still not sure why it suddenly started happening though :thinking_face:

hiredman20:11:23

It isn't going to be any of that

hiredman20:11:55

What your are seeing is the default output from an unconfigured java.util.logging

hiredman20:11:44

And timbre is a whole other thing

sheluchin20:11:15

Oh, so I should look at how to set up config for java.util.logging?

hiredman20:11:34

That would be one way

hiredman20:11:41

My preference for complex projects these days is to setup up log4j2 and redirect all the other java logging to it

hiredman20:11:18

If you haven't encountered the complexities of java logging before, you can sort of split logging into a top half and bottom half. The top half is the API that programs call with log messages, the bottom half actually outputs the log messages somewhere.

hiredman20:11:21

A lot of logging libraries provide both a top and bottom half, and some libraries, like the clojure specific tools.logging is just the top half

hiredman20:11:20

And most logging libraries provide shims that act like the top half of other libraries, but direct messages to their own bottom half

hiredman20:11:13

Timbre is this weird kind of attempt to paper over the complexities of logging, while also doing some clojure specific stuff, and I don't really care for it, and avoid using it, so I can't speak too much to how it is effecting all of that

hiredman20:11:46

The logging stuff there appears to be trying to pipe the top half of a lot of java logging libraries to the log4j bottom half

hiredman20:11:17

My mistake, it is piping it to slf4j bottom half

hiredman20:11:45

Mmm, that output may not actually be from java.util.logging, but from slf4j, so configuring slf4j, possibly through timbre may be the thing

sheluchin21:11:27

Heh, this sounds like a job I shouldn't be doing last thing on a Friday 🙂

sheluchin21:11:08

Thanks @hiredman, that gives me a lot to look into. I'll leave it to the next morning to tackle this. A little odd that is just randomly started happening, but there must have been some event in my workflow that triggered the change and I just don't remember.

sheluchin21:11:48

Ah, figured it out. Totally my fault. The function which starts the HTTP server and populates the DB also inits the config, and I wrongly assumed since I was only interacting with git/local filesystem, I didn't need any of that... on this particular run. I attribute it to fatigue 😛 But I appreciate your explanation very much nonetheless. I'm not that familiar with java logging complexities, so this top/bottom split gives me something to look into before I inevitably do encounter it.