beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
Lemonski 2020-11-08T07:37:50.338700Z

@valtteri The Project itself is a Colordatabase for painters with features like create your own palette and download a pdf of it and if possible current prices in the most known shops. Maybe Clojure is overkill for such a small need?

Lemonski 2020-11-08T07:40:03.338900Z

@sova Since i basically would start from almost 0 prior programming (besides elisp) i would prefer some leads and templates, luminus looking promising, Got both "frameworks" saved for later when i know a little more clojure

Daniel Östling 2020-11-08T10:03:31.343Z

Is there a commonly accepted way to structure ”large” programs with multiple namespaces etc? Is there documentation/guides? :)

valerauko 2020-11-08T10:41:34.343100Z

I don't think there's a generally applicable way, no. I have my own pattern for web apps and try to split namespaces/folders by areas of responsibility in general. Here some examples: • a smallish library https://github.com/valerauko/vuk • a smallish website (actually the first clojure one i ever made) https://github.com/valerauko/books • a way bigger service https://github.com/valerauko/kitsune I hope you can use these for reference. (All are using leiningen so source code lives in src.) If you have questions ask away!

Daniel Östling 2020-11-08T11:13:14.344400Z

Okay, thanks :)

Lemonski 2020-11-08T16:48:14.351200Z

A weekend of Clojure and i feel very overwhelmed still dont know how to create a working programm even if its only say hello and work as a very unnecessary calculator 😄 I am reading the "brave Clojure" book. are there more practical examples out there?

Lemonski 2020-11-09T08:55:30.390600Z

@dorab i have leiningen installed and working. Lein repl and cider repl in emacs also working. I am basically trough chapter2 of brave. And was able to create simple stuff like enigma decription in repl etc. Rly only simple stuff. But still not sure where to go from there

Lemonski 2020-11-09T08:57:02.390800Z

@vincenz.chianese thank you for your advise i have the feeling brave is more like a reference book which is also good to have in the beginning, but yah i will take a look into the video.

dorab 2020-11-09T17:16:57.424700Z

@thomasfuston So I probably misunderstood your question. I thought you were having problems writing something simple. So, I presumed it was because your setup was not complete. It now sounds like you have the setup just fine, and are looking for what to do next. In that vein, what @vincenz.chianese says is good advice. If you're not already experienced in functional programming, then writing functions that can be composed together is a good next step. Previous expose to OOP can be detrimental since it causes you to un-learn some habits in order to do good FP. Most of my programming is just functions which I just call from the REPL. The only time I ever make a stand-alone command-line program out of the function is when I need the function to be run by someone else who is not familiar with a REPL and does not want to learn about the REPL. If you have not already, you could try reading/watching articles/videos about REPL-driven-development. You can start at https://clojure.org/guides/repl/introduction and https://clojure.org/guides/repl/annex_community_resources for other resources. FP/REPL are sometimes harder to learn because of previous exposure to OOP. But, once you persevere and get to the ah-ha moment, you will be changed! If I'm telling you things you already know, you can ignore me 🙂

dorab 2020-11-08T17:27:11.351300Z

Hi @thomasfuston do you have a REPL working? Since you say you are using Brave Clojure, I presume you have lein installed? If so, does lein repl give you a usable REPL? Just trying to figure out where you are in your journey.

seancorfield 2020-11-08T18:51:13.352Z

At work, we have over 100K lines of Clojure, stretching back across a decade. We were all learning in the beginning so we hadn't really figured structure. Over time, our code organization has changed -- which means, unfortunately, the various apps in our code base are not very consistent, and as we continue to add functionality, we are constantly refactoring, and trying to improve the structure.

2020-11-08T18:58:39.353100Z

Is there an integer division function in Clojure that truncates towards negative infinity (rather than towards zero, as quot does)?

2020-11-08T19:01:41.353200Z

I found a StackOverflow question via a quick Google search that links to this Java method that may be of use to you: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorDiv-int-int-

2020-11-08T19:02:04.353400Z

Google search terms I tried: java division round to negative infinity

2020-11-08T19:02:25.353600Z

StackOverflow question found early in search results: https://stackoverflow.com/questions/37308248/java-integer-division-doesnt-give-floor-for-negative-numbers

2020-11-08T19:02:47.354Z

I am not aware of anything built into Clojure that does what you wish.

2020-11-08T19:03:26.354200Z

Awesome thanks, I’ll take a look… I didn’t think to look at Java 🙂

2020-11-08T19:04:01.354400Z

I believe that Java's integer division also rounds towards 0, which might be the reason that Clojure's quot does so.

👍 1
seancorfield 2020-11-08T19:04:17.354600Z

Even in Java, it's a fair bit of work, depending on whether you need it to work with long as well as int.

seancorfield 2020-11-08T19:04:49.354800Z

(based on that same SO answer that Andy found)

2020-11-08T19:06:08.355100Z

There is also a variant of the floorDiv method that takes primitive long args instead of int: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorDiv-long-long-

2020-11-08T19:06:19.355300Z

Well the best I could come up with prior to asking here was (int (Math/floor (/ x y)))floorDiv feels marginally less painful 🙂

2020-11-08T19:06:24.355500Z

I would guess there are versions for float and double args, too.

2020-11-08T19:07:14.355700Z

🙂

2020-11-08T19:07:15.355900Z

The Java math libs are pretty extensive, even the stuff that comes with every JDK without requiring a separate Java lib. If you go for 3rd party Java libs, it gets even more huge.

👍 1
2020-11-08T19:10:57.356200Z

Many thanks for the help… I really appreciate it 🙂

Daniel Östling 2020-11-08T19:21:20.356400Z

Thanks @seancorfield 🙂 Yeah, I’ve been doing “hexagonal design”/DDD in other languages, I guess that could work in Clojure as well.

vncz 2020-11-08T22:47:55.356800Z

@thomasfuston I had the same problems initially, and it turns out that my initial idea "I'll learn the basic Clojure stuff and jump directly in trying to create a CRUD App" did not work at all. It takes some time to learn the language itself and the best baby step after figuring out the basics is to implement "data" only programs (such as algorithms and stuff) to get acquainted with the collection library. Once you're there, you'll likely understand what more and be able to write a web app with Pedestal for example. Ultimately, although the Clojure for the brave is a good book, it did not help me that much. What really helped me was this video: https://www.youtube.com/watch?v=P76Vbsk_3J0 I hope that's gonna help in your journey! 🙂

2020-11-08T23:01:30.358100Z

I'm coming to Clojure from Django where I've learned to love the ORM. What is the recommended approach for using SQL in Clojure? Is something like Toucan the right place to start? https://github.com/metabase/toucan

seancorfield 2020-11-08T23:09:16.358700Z

@randumbo Since we don't have objects, we tend not to use any sort of ORM in Clojure.

seancorfield 2020-11-08T23:11:18.360900Z

https://github.com/seancorfield/next-jdbc is the best JDBC wrapper to start with. You'll see https://github.com/clojure/java.jdbc in a lot of books/tutorials (which I also maintain), but that isn't getting updates any more.

seancorfield 2020-11-08T23:12:59.362800Z

There's a #sql channel where you can ask SQL/JDBC-related questions. If you want to work with SQL in .sql files, take a look at HugSQL (which also works with next.jdbc and clojure.java.jdbc). If you want a DSL to programmatically build SQL from data structures, take a look at HoneySQL. There are #hugsql and #honeysql channels for those too.

practicalli-john 2020-11-08T23:13:19.362900Z

I don't believe there is a single approach (no sliver bullet... and why would there be?) https://github.com/seancorfield/next-jdbc - if you want to use SQL code https://github.com/seancorfield/honeysql - if you want to write Clojure data structures rather than SQL https://www.hugsql.org/ - aims to make a clean separation between Clojure and SQL code. An toucan is already mentioned in your question. which as is says is as close as it gets to ORM without the O

2020-11-08T23:13:42.363400Z

Which approach would you recommend, of those two?

seancorfield 2020-11-08T23:13:47.363500Z

@jr0cket You shouldn't really recommend clojure.java.jdbc these days. I'm not updating it any more.

practicalli-john 2020-11-08T23:14:22.364100Z

pasted the wrong link...

1
seancorfield 2020-11-08T23:14:50.364700Z

At work, we use HoneySQL very heavily because we have a lot of complex queries that are built programmatically.

seancorfield 2020-11-08T23:17:07.366500Z

Some people like to keep all their SQL in separate .sql files which is why I mentioned HugSQL.

2020-11-08T23:18:05.367600Z

Do those tools have protections against low-level stuff like injection attacks?

seancorfield 2020-11-08T23:18:21.367800Z

Yes.

2020-11-08T23:19:40.369400Z

What are your thoughts on Toucan? It says it's built on HoneySQL, although still seems to use clojure.java.jdbc.

seancorfield 2020-11-08T23:20:21.369900Z

I think it adds a layer of unnecessary complexity, personally.

2020-11-08T23:21:05.370400Z

Understood. I get the sense that it's against the Clojure way.

seancorfield 2020-11-08T23:21:47.371700Z

I'd definitely recommend starting with basic libraries so you understand what's going on and then composing the libraries you like best. Choose simple over easy.

2020-11-08T23:21:56.372Z

Looking at HugSQL, it's a bit concerning that it doesn't look like it's actively maintained. Last commit was Sept. 30, 2019.

seancorfield 2020-11-08T23:22:26.372700Z

I've worked with a lot of ORMs in other languages -- dating back to full-on object databases in the '90s -- and I much prefer Clojure's approach.

2020-11-08T23:22:33.372900Z

Thanks @jr0cket.

seancorfield 2020-11-08T23:23:30.373900Z

@randumbo You'll get used to a lot of Clojure libraries seeming to be "unmaintained" -- Clojure favors small, composable libraries, that tackle one problem and therefore can often be "done".

seancorfield 2020-11-08T23:24:23.374800Z

Backward compatibility is also very important in Clojure, so don't be put off by pre-1.0 versions, or things seeming to stay alpha for a long time 🙂

2020-11-08T23:24:49.375200Z

heh, I've read that in many places, but I still find it kinda hard to believe.

Daniel Östling 2020-11-09T08:26:45.390400Z

It’s true for many Common Lisp libraries as well.

seancorfield 2020-11-08T23:25:48.376700Z

At work, we started with Clojure 1.3 and went to production on alpha 7 or 8. We've run pretty much every release of Clojure in production in alpha versions. Stability is important in the Clojure world.

2020-11-08T23:26:02.376900Z

If I don't need complex queries for now, would HugSQL be a better choice for the time being?

seancorfield 2020-11-08T23:27:14.377500Z

(we're running Clojure 1.10.2-alpha2 in production right now with 1.10.2-alpha3 in QA and 1.10.2-alpha4 in dev)

seancorfield 2020-11-08T23:27:37.378Z

Start with just next.jdbc and see how you get on.

seancorfield 2020-11-08T23:28:00.378500Z

If you decide you prefer your SQL in external files, then take a look at HugSQL.

seancorfield 2020-11-08T23:28:22.379Z

It's not an approach I like, so it's not really my place to recommend you do or don't use it.

2020-11-08T23:29:22.379500Z

Going to have to brush up on my SQL a bit. Been a while since I've written raw SQL.

seancorfield 2020-11-08T23:30:07.380400Z

For simple CRUD, you won't need it: next.jdbc (and clojure.java.jdbc) can save hash maps to tables without SQL and read them back.

2020-11-08T23:30:28.380900Z

I'll do so anyway, but that's good to hear.

seancorfield 2020-11-08T23:30:54.381400Z

(next.jdbc.sql/insert! ds :table {:name "Sean" :country "USA"}) for example

seancorfield 2020-11-08T23:31:28.382Z

(next.jdbc.sql/get-by-id ds :table 123) (defaults to using :id but you can override that) -- returns a single hash map

seancorfield 2020-11-08T23:32:15.383Z

(next.jdbc.sql/find-by-keys ds :table {:country "USA"}) returns a vector of hash maps for everyone WHERE country = ? and "USA" as that parameter.

2020-11-08T23:33:30.383900Z

Looks pretty straight forward indeed. Thanks @seancorfield, I'm going to work on integrating that into my Fulcro project on the backend.