This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-03-07
Channels
- # announcements (3)
- # babashka (28)
- # bangalore-clj (6)
- # beginners (91)
- # calva (14)
- # cider (7)
- # clojars (6)
- # clojure (24)
- # clojure-europe (3)
- # clojure-nl (5)
- # clojure-uk (134)
- # clojuredesign-podcast (1)
- # clojurescript (16)
- # cryogen (3)
- # cursive (3)
- # data-science (1)
- # events (1)
- # fulcro (7)
- # jobs-discuss (1)
- # luminus (4)
- # malli (1)
- # re-frame (7)
- # shadow-cljs (74)
- # spacemacs (23)
- # sql (6)
- # tools-deps (2)
speaking of next.jdbc, in case of a “simple” select statement (`select from where`) would it be more “appropriate” to use sql/find-by-keys
? should also be more secure?
@tzafrirben I would recommend you stick to the main API in next.jdbc
and avoid next.jdbc.sql
-- that's really only meant as a migration step from clojure.java.jdbc
@seancorfield thanks for clearifiing - I’ve actually never used clojure.java.jdbc
before and jumped straight to next.jdbc
, and found this function in the https://github.com/seancorfield/next-jdbc/blob/master/doc/friendly-sql-functions.md. Make sense to me to stick to main API though
Yeah, the SQL functions are very helpful sometimes, for very simple get/find/insert/update/delete operations. Feel free to use them. They are not going away. But remember that the core API is the only way to do anything more powerful.
I believe this is the third time I've seen you say this:
Feel free to use them. They are not going away. But remember that the core API is the only way to do anything more powerful.
Does putting this (with different phrasing possibly) in the docstring of next.jdbc.sql make sense?
FYI this is what the docstring says today:
"Some utility functions that make common operations easier by
providing some syntactic sugar over `execute!`/`execute-one!`.
This is intended to provide a minimal level of parity with
`clojure.java.jdbc` (`insert!`, `insert-multi!`, `query`, `find-by-keys`,
`get-by-id`, `update!`, and `delete!`).
For anything more complex, use a library like HoneySQL
to generate SQL + parameters.
The following options are supported:
* `:table-fn` -- specify a function used to convert table names (strings)
to SQL entity names -- see the `next.jdbc.quoted` namespace for the
most common quoting strategy functions,
* `:column-fn` -- specify a function used to convert column names (strings)
to SQL entity names -- see the `next.jdbc.quoted` namespace for the
most common quoting strategy functions.
In addition, `find-by-keys` supports `:order-by` to add an `ORDER BY`
clause to the generated SQL."
@U883WCP5Z Not sure exactly what change you're suggesting? Feel free to submit a PR.
I just re-read all the docs, and found this section. Just wanted to confirm such a section existed.
In addition, convenience functions -- "syntactic sugar" -- are provided to insert rows, run queries, update rows, and delete rows, using the same names as in clojure.java.jdbc. These are in next.jdbc.sql since they involve SQL creation -- they are not considered part of the core API.
And sure enough, it already exists. No PR necessary. The docs are a delight to read. Thanks for all your hard work ❤️!
The new Readme lib works great as well.Hi all, I have started learning Clojure today : ) , to start with i wanted to loop over a sequence & print the index of an item if it matches a given value, I am doing two thing worng, 1. thinking in terms of imperative programming way 2. wrong syntax (lots of confusinon becuase of parens) How would this be done in a more functional way?
Hi! A couple things that I could suggest: 1. Make use of the map-indexed function. If you combine it with doseq you could achieve the desired result quite concisely 2. Take a look at the -> and ->>. 3. In your first let you have excessive vectors - you could skip wrapping each binding into its own vector and writing just (let [a 1 b 1] But to get a more detailed view at the problem, take a look at this SO question: https://stackoverflow.com/questions/4830900/how-do-i-find-the-index-of-an-item-in-a-vector (the top two answers + their comments for extra insights) And have a nice time diving into Clojure - you’d love it!
(let [[u -1] [idx -1]] ; u,idx = (-1,-1)
(doseq [x [1 2 3 4 5 6]] ; for x in [1,2,3,4,5,6]
((inc idx) (if (= x 5) (let [u idx]))) ; idx +=1; if x==5 : u=idx
(println u) ; print (u)
)
This returns a seq of indices at which 5 is present
(keep-indexed #(when (= %2 5) %1)
[1 5 3 4 5 5 10])
Check out keep-indexed
.thanks a ton 🙂
does %
here represents a modulo operation?
% is the function argument to an anonymous function #(...). If there are more than one, the arguments are named %1, %2 and so on.
@UV136PREU Here are the relevant docs: https://clojure.org/guides/learn/functions#_anonymous_function_syntax In any case, I recommend reading that entire page.
Hi! A couple things that I could suggest: 1. Make use of the map-indexed function. If you combine it with doseq you could achieve the desired result quite concisely 2. Take a look at the -> and ->>. 3. In your first let you have excessive vectors - you could skip wrapping each binding into its own vector and writing just (let [a 1 b 1] But to get a more detailed view at the problem, take a look at this SO question: https://stackoverflow.com/questions/4830900/how-do-i-find-the-index-of-an-item-in-a-vector (the top two answers + their comments for extra insights) And have a nice time diving into Clojure - you’d love it!
@chaot8 thanks a ton for responding back, I was trying to get it solved myself, I have come something like this, i am going to look into the link you posted 🙂 :
(loop [i 0 arr '(1 2 3 4 5)]
(if (< i 5)
; (if (= arr[i] 3)
; (println "yes"))
(recur (inc i) arr))
)
You’re welcome! But also definitely take a look at the answer by @U883WCP5Z - it’s more concise than what I suggested.
And one more: do explore the loop+recur, but keep in mind that it’s a very low level construct and normally you would avoid it unless there is no other option
I can't find an answer from @U883WCP5Z
It’s under your original question shown as reply
is there any function that tells shape of data. like [#{} [] {} ]
or something
I am not aware of any. But why do you need it? If you want to validate an input against an expected shape, there is clojure.spec
during the development I am having some nested data strcture - I would like to see it's shape instantly
it is for development only - for REPL
You could use print (any flavor that suits you best) or you could go with ‘type’, which would tell you the exact underlying data type (although it may be too precise for your task). There are also collection kind testing functions like ‘map?’
syntax quote resolves all symbols, so for qualified symbols like user/y
nothing changes, and for unqualified symbols the current namespace (`user` , in your case) is added
\`\`\`y . three `here becomes (clojure.core/seq (clojure.core/concat (clojure.core/list (quote quote)) (clojure.core/list (quote user/y))))
Hello. Is there Clojure-roadmap somewhere? Eg. something similar to this one: https://github.com/Alikhll/golang-developer-roadmap
I meant 'learning' roadmap like I've put in the example. No the language development roadmap.
Ah, gotcha. I didn't look at the link because the only context I've ever heard for "roadmap" around languages is how they evolve. Never heard of a "roadmap" for learning.
I'm still not comfortable with taking in user input. Is there a guide somewhere on read-line
and read
and various IO/input things? Is everything read as a string? I find myself assigning the input to a name using let
and then going from there. Is that idiomatic? The docstring for read-line
is "Reads the next line from stream that is the current value of in ." I am not familiar with streams
and what exactly is meant by in. read
's docstring brings in a lot more unfamiliar, java-ish terms.
The core team are planning the 1.11 release at the moment. But changes to Clojure are very conservative because the team values backward compatibility.
read-line
reads everything as a string. read
attempts to parse text input as Clojure data/code.
*in*
is roughly equivalent to standard input on Unix/Linux. *out*
is like standard output.
There is a Clojure cheat sheet with an IO section that groups IO-related Clojure functions there, also with some links to Java Reader, Writer, InputStream, and OutputStream classes, that would be good to at least be aware of, even if you don't dive into the details: https://clojure.org/api/cheatsheet
It also groups most other Clojure functions into related groups, too, not just IO
Many kinds of IO use 'buffered' readers and writers for efficiency. If you do some printing to a buffered output stream, and want to make sure the in-memory buffer contents are forced out, i.e. flushed, you may call flush
. There is usually a similarly-named function provided in most programming languages for the same purpose.
Useful for forcing debug output to appear in files, logging-destinations, etc. near times where you expect the program to crash, or if you need to make sure that everything you have printed to some destination is actually there, before proceeding to some next step that expects all file contents to be there.
Note that flush usually means only “flush to OS managed buffers” not actual storage- that’s what fsync is for
Thank you! Sadly I do know of those docs but reading it isn't clearing things up. All those Java things you mentioned are unfamiliar. It's a big fear in my Clojure development that my lack of any experience, but especially Java experience, is really going to hold me back despite others saying they don't use much Java and I can't find the learning materials within the Clojure ecosystem to fill in those gaps.
Depending upon what you are doing, you can often get by without knowing the Java things. e.g. with-open
to open a file for reading, using a file name given in a string, and then using calls to read-line
and similar functions inside of there, is commonly done.
When folks say they use Spec "at the boundaries" are they referring to taking in user input like this? Since these kinds of things are super side effect-y and non-pure right?
That does make sense. I'm probably being overdramatic. I just really like Clojure but always have this feeling I don't understand anything going on under the surface/nice abstractions that it provides.
It is useful to know the name of the Java class Reader
since you will come across that term when looking at docs. It is simply a "file handle" returned when opening a text file using one of several functions, one being http://clojure.java.io/reader, that you can then use to read that file as text. The Reader
class handles various kinds of text encoding for you.
I understand how disorienting it can feel when getting into a new programming language, or any kind of technology/art/science where most things are unfamiliar. This beginners channel can be a pretty useful place to ask basic questions when getting started.
Definitely. I love this slack. I miss it when exploring other languages. I've been dabbling with programming/Clojure for a while now though but don't seem to progress much. So mostly looking for scapegoats when I keep struggling. Hahaha. Thanks for the chat and advice!
If you are interested enough in Clojure to spring for the cost of a book, there are several good ones available. I'm not the best resource to pick the 'optimal' book for you, but ones like "Programming Clojure, 3rd ed", and "Getting Clojure" I have heard good things about, and would be worth checking reviews / samples on-line
I have those books and more! I've read through them. My real problem I think is I don't build my own projects. I keep flitting about tutorials. I'm basically everything Rich disagrees with here: https://gist.github.com/stijlist/bb932fb93e22fe6260b2
Ha! I just read this and feel totally called-out! Fellow "perpetual beginner" here. I do feel that my excessive "flitting about" is somewhat justified by my admittedly dense skull - I feel that I need to experience concepts from as many different angles as possible before I truly grok them.
Makes sense. Any application areas you are especially interested in when it comes to programming?
"Makes sense" meaning "understood", not "I recommend continuing with that approach" 🙂
I do want to build full stack web apps which seems right in the CLJ/CLJS wheelhouse but I convince myself I don't understand the underlying fundamentals of web development/java/js.
If you don't need JavaScript/cljs right away for the interactivity they provide in a web page, you can probably save yourself a lot of time by focusing on the 'back end' part first, since that is needed in pretty much any web dev. (Unless you really prefer JavaScript/front-end design/interactivity aspects of web dev).
I agree and that's what I'm going to explore now. I was told here a while back to just start with Ring, Compojure, and maybe hiccup or selmer and build a little app and I've been seeing now why.
It certainly sounds like good advice to me. Start with a small set of libraries that people have found are enough to get a lot of things done, and practice making something with those, asking for help on things if you get stuck. Save the "taking over the world new web site" for next year 🙂
https://github.com/seancorfield/usermanager-example is a reasonably small good place to start
Yup, that is who was giving me that great advice earlier. I need to start listening! Lol. Thank you.
... And despite what I just said, I'm currently recreating little temperature and weight converter command line tools for myself as I'm currently living down in South America. Hence the question on user input because I was struggling with it.
FYI, reading from *in*
can be a little strange (or impossible?) in some Clojure REPL environments, because Clojure and the REPL env 'fight' over who gets to read the input. Not all have problems, but some do. Reading from *in*
interactively is probably best done in a Clojure program that you start from the terminal, rather than in a REPL situation.
In a REPL situation, simply passing 'Fahrenheit temp to convert' as a parameter to a function is more reliable than reading from *in*
I've encountered that already. Yup! By "starting from a terminal" do you mean like using lein run
?
Gotcha. Yup, that's the little things I am finally learning from real world experience.
If you write a Clojure function that reads from a named file on the file system, then calling that from a REPL should not encounter that problem.
When writing programs that read from stdin, I create a file named stdin and bind *in*
to it. Typing the input everytime is a hassle anyways.
@chase-lambert I liked https://www.braveclojure.com/clojure-for-the-brave-and-true/ and https://pragprog.com/book/dswdcloj3/web-development-with-clojure-third-edition
Anyone here use Calva? I'm using Repl and it's like the clojure.repl namespace is not available, I can't doc functions
You need to require(causing the code to be loaded) a namespace before that kind of thing works
Yeah I had required it. I was starting to think I was going mad. Turns out the Repl was in some limbo state, just needed a good restart 🙂
Hello everyone. I'm getting this error I can't figure out while installing lein
Leiningen on my Mac: sudo mv lein /usr/local/bin/lein Password: mv: lein: No such file or directory sudo chmod a+x /usr/local/bin/lein lein version Downloading Leiningen to /Users/guillermoithier/.lein/self-installs/leiningen-2.9.2-standalone.jar now... % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 618 100 618 0 0 2414 0 --:--:-- --:--:-- --:--:-- 2414 100 13.9M 100 13.9M 0 0 6470k 0 0:00:02 0:00:02 --:--:-- 8713k shasum: /Users/guillermoithier/.lein/self-installs/leiningen-2.9.2-standalone.jar.pending.shasum: no properly formatted SHA1 checksum lines found Failed to properly download https://github.com/technomancy/leiningen/releases/download/2.9.2/leiningen-2.9.2-standalone.zip The checksum was mismatched. and we could not verify the downloaded file. We expected a sha256 of 09805bd809656794cfe7d9155fd4b8bea2646092318690bf68e3c379574a2d3c and actually had 13174ff1bd353dbe2bdc5f20093d6b2d4139fd35da10131c17bbe83f0328dd1c. I'm not sure where to go from here.
I did run this script before the other scripts:
curl > lein