I am a total beginner to programming so please excuse if this question is very rudimentary. I am beginning to learn Clojure using the book Clojure for the brave and true. I have successfully installed Java, Clojure, and Leiningen. The first step is to run lein new app clojure-noob When I do so I get the following response from my terminal Java HotSpot(TM) 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release. Generating a project called clojure-noob based on the 'app' template. Could not create directory /Users/Pascal/clojure-noob. Maybe it already exists? See also :force or --force according to the book I should be getting a response as follows | .gitignore | doc | intro.md | project.clj | README.md | resources | src | clojure_noob | | | core.clj | test | | clojure_noob | | | core_test.clj Please any help would be appreciated. I am really excited to begin my coding journey.
You can ignore the "were deprecated" warning line, I think. Should be harmless.
The "Could not create directory ..." message I can only reproduce if I do the command twice -- the first time it succeeds, the second time it gives that error because the directory was already created by the first time I ran it.
I would suggest checking to see if there is a clojure-noob directory, and if there is, delete it. After you have confirmed you have deleted it, try the lein new app clojure-noob
command again.
Thank you, not sure if that is because even try to create a file with a different name it says could not create a directory maybe it already exists
Perhaps it is a permissions issue and for some reason you don't have permissions (in your home directory?) to create new files. Are you on macOS or Linux? And what version of macOS? (they've been making it increasingly unfriendly to developers lately, IMO)
welcome to Clojurians, @abane288! 🙂
judging by "/Users/Pascal/clojure-noob", you must be on macOS. in your home directory (which is "/Users/Pascal"), execute the following command and report back what you get in the output:
lein new app clojure-noob --force && find clojure-noob
you should hopefully get an output that looks like the following and if you do, congratulations, you did it!
Generating a project called clojure-noob based on the 'app' template.
clojure-noob
clojure-noob/project.clj
clojure-noob/README.md
clojure-noob/doc
clojure-noob/doc/intro.md
clojure-noob/.gitignore
clojure-noob/.hgignore
clojure-noob/src
clojure-noob/src/clojure_noob
clojure-noob/src/clojure_noob/core.clj
clojure-noob/test
clojure-noob/test/clojure_noob
clojure-noob/test/clojure_noob/core_test.clj
clojure-noob/LICENSE
clojure-noob/CHANGELOG.md
clojure-noob/resources
@diego559 If you have more questions, you can also consult #adventofcode
Is there a shorter way to write this? I know there is the & operator that gives all the rest of a list, but I cannot get it to work. Also google is really bad for the search of a single character
(->> (number-formatter "1234567890")
(apply #(format "(%s) %s-%s" %1 %2 %3))) ;; &% instead of %1 %2 %3?
Hi, shouldn't you replace the #() form with a (fn [& args) (apply format "(%s) %s-%s args)
I'm not sure to know what is number-formatter
custom funtion, that returns a list with three elements
%&
should do it
I never think of the fn and always do #()
I'll try this
(defn number-formatter[str] (list 1 2 3))
(->> (number-formatter "1234567890") (apply (fn [& args] (apply format "(%s) %s-%s" args))))
it works here: "(1) 2-3"
Hm. Not sure if this is prettier:
(apply #(format "(%s) %s-%s" %1 %2 %3))
or
(apply (fn [& args] (apply format "(%s) %s-%s" args)
I must confess I share your point
another way is destructuring
I'm surprised there is no &% that I can just use to throw everything in that migth come.
This works:
(apply format "(%s) %s-%s")
Now it's pretty. Thanks for the input, it made me think about what apply does
ah ok, I was wondering why you have that first level of apply
I understand now
What the hell does the brave book wants me to do here :
You used (comp :intelligence :attributes) to create a function that returns a character's intelligence. Create a new function, attr, that you can call like (attr :intelligence) and that does the same thing.
this is the example
(def character
{:name "Smooches McCutes"
:attributes {:intelligence 10
:strength 4
:dexterity 5}})
(def c-int (comp :intelligence :attributes))
(def c-str (comp :strength :attributes))
(def c-dex (comp :dexterity :attributes))
(c-int character)
; => 10
(c-str character)
; => 4
(c-dex character)
; => 5
so it looks to me I have to write a function that takes out a attribute and prints it Am I right ?
"Create a new function, attr, (...)" means that it wants you to create a new función with the name attr
My best guess is that's is the problem
so you have (c-int character)
that takes a character and gives you that character’s intelligence
now it’s asking for you to make a function attr
that takes the keyword of the attribute :intelligence
and returns that number
(attr :intelligence)
=> 10
@roelof: perhaps it wants you to parameterise one of the elements in the comp function (without giving the answer away)…
is compojure, reitit and pedestal same sort of thing?
compojure and reitit are routers, pedestal I’m not sure of, I think it has some relation to interceptors
I guess pedestal is more like a framework, a bigger thing
@stebokas All three can be used to make web services. Pedestal has more stuff out of the box, but otherwise they’re quite comparable and a simple hello world example can be made in just a few lines of code in each of them. • Compojure uses the Ring handler model (functions wrapping functions) and a more traditionally Lispy way of defining routes using macros called GET, PUT, POST, etc. • Pedestal is a data-oriented library for making web services made by Cognitect (the company maintaining Clojure). It uses Clojure data for everything: defining routes, data for setting up servers, and data for defining its alternative to Ring handlers (interceptors). I like it a lot. • Reitit is sort of Metosin’s clone of the routing portion of Pedestal (with Metosin’s Sieppari being a clone of the interceptor chain). It is more actively maintained than Pedestal currently is and according to Metosin it’s slightly faster too, but currently not as feature complete - and not aiming to be - but unlike Pedestal it can be used in the frontend too (i.e. in ClojureScript). Obviously, there are other things to say about it too, but I haven’t used it myself.
@schpaencoder Shhhh! We don’t use the F-word around here 😛
thx @simongray, that kind of synthesis is not easy to find
fu-ction :face_with_hand_over_mouth: or is it f-mework 😄
what about fulcro?
are there any performance or idiomatic concerns that would dictate choosing between using 'alter-var-root' or using atoms ?
Not a direct answer, but related to your question: https://gist.github.com/reborg/dc8b0c96c397a56668905e2767fd697f#when-should-i-use-atomsrefsagents
The taboo is actually so strong that even fulcro doesn’t dare call itself a framework on its Github page. The F-word is used on the Fulcro website, but hidden away in a testimonial at the bottom of the page 😉
@stebokas it’s of course not function :)
Hi folks. I've been slowly trying to put together a set-up for ClojureScript, and recently got a recommendation to go with the 'Clojure CLI tools'. I just got out of the long prerequisite step of installing WSL2 and Ubuntu and getting it to actually work, but now I'm having a hard time finding a guide on what to do next. None of the resources I've found on Clojure CLI tool installation mention anything about WSL2. What should I do next?
I think you will find better leverage by installing directly on windows as oppose to going through wsl
For a bit of background, I went through the quick start guide a year or two ago and made an uber-simple webpage that used ClojureScript. I'm trying to get a more fleshed-out set-up ('toolchain'? what should I call it?), but am having a really hard time getting all the pieces together. After about 3 weeks of spare time spent on this I'm still not at the point of entering a single line of code and it's demoralizing.
What setup are you looking towards having? Like what editor are you planning to use?
Oof. I had used Leiningen before, but then got steered in the direction of WSL2 and ClojureCLI. Just manually updated Windows (no idea why my computer said it was 'up to date' when it was two whole Windows version behind!), and got WSL2 with Ubuntu working. Are you saying I don't need any of it?
Actually, where do Clojure CLI tools get installed? Windows, or the Linux VM?
(and if the VM, then would it have to keep getting re-installed every time you run the VM??)
I'm abit unsure about clojurescript - I just remember my friend was able to get things working better through windows
actually ....
What are you looking to do with ClojureScript exactly?
@factorhengineering I think you might have been beating on the wrong bush. I think for set-up, what you should either be looking at is https://figwheel.org/ or https://github.com/thheller/shadow-cljs
https://github.com/clojure/tools.deps.alpha/wiki/clj-on-Windows may be helpful re the CLJ tools on windows
but if you're doing WSL2, then just the normal linux instructions on https://clojure.org/guides/getting_started are probably the right answer
I did take a stab at using shadow-cljs in my second-latest attempt to into CLJS again a few months ago, but it went off the rails. The guide just started to make less sense. 😕
By the way, at that figwheel page you linked to, @zackteo, it mentions Clojure CLI tools on an Apple machine only.
No mention of WSL(2) I can see at any of the links actually.
I don't know how I ended up with this to-do list: 1. Install WSL2 (which requires a huge Windows update) 2. Install Clojure CLI tools.
@seancorfield I think the recommendation came from you. What am I spacing out on?
Thanks for the help so far by the way, all.
I guess I might try again with Leiningen and/or shadow-cljs
What's your IDE?
Emacs
I see the benefit using VS Code on the windows host and have all the code run on the wsl
but I know nothing about emacs, sorry
> (and if the VM, then would it have to keep getting re-installed every time you run the VM??) No, the data persists. Do you lose your data when you turn off your machine? It's the same thing. WSL is your best bet imho, for the sole reason that it offers a proper bash experience. Just fire up WSL and follow the clojure cli tools installation instruction for linux, like @alexmiller said.
Neither do I. 😉 ...but with the many, many decisions to be made about the toolchain (is there a better term to use?) I've already settled on Doom Emacs + CIDER.
@pavlos Great to know (about the persistence). Thanks.
The thing about wsl is, that you have a command line linux right in your windows. You can even access all the files. With wsl2 you can even use the gpu for calculations
Here is a guide to use emacs on windows that runs from the wsl: https://emacsredux.com/blog/2020/09/23/using-emacs-on-windows-with-wsl2/
you should probably use atoms for most things where you might ask this question
@hobosarefriends @schpaencoder I have no clue that is why I asked here. Bummer that most of the challenges in learning book are so vague..
how to get the sub string of a string in clojure, ex- hello@word
, i want string before @
expected output is hello ?
There are regex matching functions that can pull apart pieces of a string if you are familiar with regex matching, e.g. re-find
and re-match
.
@christiaaan Thanks. I will take a look. I will have to find something that discusses Clojure at some point too. I did not even know I had to do separate reading on just getting Emacs and WSL2 to play nicely together, though!
By the way, I keep getting pulled away from the computer, but I will try to check in on this thread throughout the day. In any case I should be on more consistently this evening.
If in this case you simply want to find the first @
character and get everything before that, there is the Java .indexOf
method for looking for the first index of a paraticular character in a string, and the subs
Clojure function for getting a substring of a string based on first index and number of characters.
http://ClojureDocs.org has some examples of using those functions (except .indexOf
, since it is a Java method, which you can do Google search for the API)
there’s also clojure.string/split
if you want both the before and after of the @
I hope I can stick with vs code and don't have to switch to emacs for some reason. learning a language is hard enough, I wouldn't want to learn a dwarf-fortress-y IDE on the side.#
(clojure.string/split "hello@word" #"@")
=> [“hello” “word”]
If you do try it, we can talk. I'm a noob at that too. org-mode is amazing though. It alone is worth the the price of admission to some people, I hear.
I had just heard Emacs+CIDER was the best for Clojure.
After trying Emacs I didn't love the heavy use of key chording (with Shift/Ctrl/Alt) and could imagine RSI from it, so I looked a bit further and found Doom.
I also tried Vim and found it kind of mind-melting, but I'm game for short-term pain for long-term gain.
I’m certain it’s asking for a function named attr
that takes in a keyword :intelligence
and returns the attribute for that key.
(defn attr [k] ~your code here~)
(attr :intelligence)
=> 10
@factorhengineering You can always run emacs in terminal mode from within wsl (`emacs -nw`) if you don't need the GUI. You can also ask at the #emacs channel, I'm sure others before you have faced similar issues. @christiaaan switching from an IDE to emacs was definitely one of the best decisions I've made in recent years, for various reasons, but you're right. Trying to juggle learning emacs AND clojure would definitely have been hard.
will try 🙂
which channel can I use the best for web development questions ?
I’m not aware of a channel for web development existing specifically, there are however channels for most of the libraries you’d use. I’d recommend asking about general web stuff here or maybe even #clojure, and once you decide on what libraries/approach you’re going to take you can ask more specific questions in the channels for those libraries.
for a tutorial of a web related things, I believe I’ve seen people to point to https://practicalli.github.io/clojure-webapps/content-plan.html and for an example https://github.com/seancorfield/usermanager-example springs to mind.
To be honest I haven’t looked much at either, maybe someone else has suggestions with more experience behind them.
but this taboo is ridiculous because for example ring calls users code :thinking_face:
https://github.com/gothinkster/realworld Also has some Clojure and ClojureScript example/real-world web projects
@pavlos I have not tried the non-GUI Emacs yet, but Doom strips away the menus and even some of the info bar stuff (which I want to figure out how to get back!), so maybe terminal Emacs wouldn't be that bad.
Why exactly was the switch worthwhile to you? I'm committed to trying to learn it all at once, but it would still be good to know (and @christiaaan might want to know too. 😉
to be clear there are no operators in clojure, &
is a destructuring syntax, and %&
is a magic symbol inside #()
that is translated into a destructured binding
(I call it magic because % tokens inside #() don't even follow normal parsing rules)
I've usually seen alter-var-root done for monkey-patching, eg. for debugging / diagnostic purposes - agreed that atoms are the go-to for mutation
@denis.baudinot is that the right link? I don't see alter-var-root mentioned at all
@noisesmith yes, but it seems to be a good primer or inspiration for atom/ref etc. usage. I didn’t even know alter-var-root existed until now. I guess it is used in very specific cases, while atoms (and the other constructs) are more typical.
Programming Clojure also has a nice section about these constructs if I remember correctly btw.
I'm all for learning it all at once but don't overdo it or you might get frustrated enough to quit both 😛 > Why exactly was the switch worthwhile to you? built-in ergonomics: • distraction-free environment/complete lack of annoying menus and sidebars • the keyboard is a first class citizen • much more lightweight than probably every fully-fledged IDE out there (advanced static analysis can be helpful but having it enabled all the time has its drawbacks; I was having memory issues with intellij/cursive on a machine with 16GB with just a couple of repls open and a browser) • extreme customizability (in lisp!) • version control your emacs.d directory and you have your emacs setup everywhere you go org-mode, magit, dired I could go on... I'm a new user too (~2 months in) but I don't think I could ever go back edit: multicursor has also been really useful so far! edit2: CIDER/clojure-mode too, obviously! ---------- Having a uniform environment is definitely a big deal. Everything is a buffer and interactive: magit status/diff, source/text files, directories. One major issue beginners (including me!) have with emacs is that you have to like learn 10 combos by heart to be even remotely productive but in the long term that's a good thing. Because the experience is consistent!
was more thinking of tutorials to learn to make websites and choose my own parts https://codepen.io/rwobben/full/xxVepxG
and if I then make such a layout with some reactive parts like going to the next page or showing some info
you already found a better solution, but fwiw there's such a thing, just spelled backwards:
user=> (#(apply + %&) 1 2 3)
6
@denis.baudinot thanks, that's a great read
anyways, i'm considering transactions now
in practical usage transactions / refs / agents are special case tools and not what's commonly needed
the normal approach is an atom holding a hash map with all the related state
refs end up being an optimization when the contention of one atom is a bottleneck (rare)
Why doesn't this work?
(map (take 2) [[1 2 3] [1 2 3]])
i'm using an internal database of atoms that are vectors that hold data, it works slick and cleans up spaghetti code by separating formatting and report generation from data acquisition which is difficult if i try to be immutably iodiomatic, i'd really like to be a clojure functional guru... but alas my roots are imperative from my C upbringing
I know the right way is
(map #(take 2 %) [[1 2 3] [1 2 3]])
but (take 2)
evaluates to a function
it returns a transducer, not a partial application of take
thank you!
the normal way to do this would be a single atom holding a hash map from some key to a vector
or better yet, to use a pipeline from data acquisition to report generation rather than storing the data in a place as program logic
hmm.. i use a hash map of keys to the database atom s
in my example index-of helped , Thanks 🙂
atoms inside hash-maps is a code smell, a single atom holding that map is almost always better
(not to say there are no exceptions, but those are rare)
sure, i can try it differently, i might like it better that way
functions like update
/ update-in
when combined with swap!
make this straightforward
(it's definitely clumsy without them)
Team I have a json structure , How can i fetch the data between { and }, Can that be possible in clojure regular expression ?
#json structure 1
[json structure 2
{json structure 3
}
]
#
yes, the database gets re-populated often as it just holds information from external programs that are executed, so i actually have a loop construct that resets all the atoms, if have there is just one atom to reset that is far less clumsy, i always start out with a huge mess of code that i perfect when i finally realize what i am doing after someone tells me what i should do 🙂
output is json structure 3
if the json is restricted enough it's possible with regex, but the normal thing would be to use a json parsing library
clojure or cljs?
clojure, i want to use clojure regex, i referred http://makble.com/clojure-regular-expression-extract-text-between-two-strings
but how can give prefix and suffix to { and }
a json library eg. clojure.data.json is simpler and less fragile than a regex
Parse the json to edn then acces the subtree you want via get-in
Regex generally not a good idea for this stuff.
but to literally answer the question, you would need "\\{"
and "\\}"
to escape the special meaning {} have in regex
@popeyepwr but consider what happens when structure 3 has another }
inside it
which is why we have actual parsers to construct real data structures, like data.json
yeah i understand that we should use data.json, but i get the text of file where i need to fetch those words
@noisesmith thank you, your insight is really impressive
i'm not sure i follow that objection. data.json will parse text to return data structures
shoulders of giants, years of suffering from my own mistakes, and all that 🍻
@factorhengineering people have suggested WSL2/Ubuntu because then all of the tutorials for macOS/Linux will apply -- I am using VS Code (on Windows, with the Remote-WSL2 extension) and everything Clojure-related runs on WSL2/Ubuntu, so I don't have to think about Windows at all: I can follow any tutorials out there. I'm just getting back to ClojureScript after a long absence, and I'm using the Clojure CLI and Figwheel Main (on WSL2) and just following the docs on https://figwheel.org/ and it all "just works".
I used Emacs on and off for years but switched to Atom about four years ago and then to VS Code in the last few months.
(I got tired of spending so much time caring and tending for my Emacs configuration and wanted something that "just worked" and kept itself up to date!)
@seancorfield do you have some tutorials or pieces I can study so I can learn web development when I finish the brave book
Maybe start with James Reeves's talks listed here https://www.booleanknot.com/
@roelof I don't know what to recommend. I suspect the Web Development with Clojure book will be too advanced for you right now -- but that's normally what I recommend for people who've mastered the basics. I would suggest playing with https://github.com/seancorfield/usermanager-example and reading the code and making sure you understand how that all works before you buy that book.
thanks
Web Development with Clojure is ok book but ignore the first chapter since it introduces everything and explains it in subsequent chapters. There are a few difficulties you will have to tackle. Understanding web development in general (HTTP, HTML, CSS, JS, using DB). Understanding Clojure specific libraries. And learning how to put everything together. In my case, I just started with frontend, playing with React, Reagent and Re-frame. I added backend several months later, following Web Development book. I would recommend by building a small app and learning everything along the way.
which is the best way to iterate on vector? I am trying with loop-recur, with 2 value,, so when i call (next v) it fail at the last
☝️:skin-tone-2: Definitely this: start small (with perhaps just Ring if you're building a back end server-side rendered app, or just Re-frame if you're building a front end only app) and make sure you understand that before adding more things.
Web dev with Clojure is "simple" (in the Clojure sense) but it is definitely not "easy".
@popeyepwr It depends what you're trying to do. What problem are you solving?
There is a lot of fragmentation and paths and variants and minutiae advantages to libs, it takes a long time to make a mental map of the landscape
first project I had in mind was to make a front and back-end app where I display some data from a external api
and I it is no problem that It takes a long time
and im willing to start very simple if needed
assuming you already grok http, my preferred path would be: start with https://github.com/ring-clojure/ring & https://github.com/ring-clojure/ring-defaults (get familiar with each middleware in ring-defaults), then https://github.com/duct-framework/duct, then maybe http://pedestal.io/ and https://github.com/metosin/reitit - this be just the backend (api or classic html generation) for frontend / spa: https://github.com/reagent-project/reagent & http://day8.github.io/re-frame/re-frame/ are established, then maybe http://fulcro.fulcrologic.com/
If you start with frontend, you can just mock-up some data and work on their visualization, maybe making it interactive. Later you can work on downloading data. Or you can flip it around and just write static HTML generator on backend using hiccup library.
One rarely needs to use loop recur, instead using higher order functions such as map, filter, reduce, makes things more understandable.
for getting data from places: https://github.com/seancorfield/next-jdbc is great, https://github.com/dakrone/clj-http for api calls is common
If you want to do some computation with all pairs of values, you can first apply (partition 2 1 vect)
to get all pairs.
no, luminus or another framework ?
I will begin with the ring readme
and play with that
for now GN
@popeyepwr (first) and (rest) not helpful?
I think the next weeks/months I will studie and try to understand the path that @claudius.nicolae pointed out
but for now more chapters from the brave book on the menu
Yeah I used some other way to find out my issue
🙂
(contains? (set (str/split "select abc" #" ")) #{"select"} ) is returning false, which function can be used to make sure values present in set is present in string..?
You're checking whether the set of words contains the singleton set #{"select"}
. This will work:
(contains?
(set (str/split "select abc" #" "))
"select")
Although, perhaps https://clojuredocs.org/clojure.string/includes_q might be what you want:
(str/includes? "select abc" "select")
Bear in mind that it doesn't do the exact same thing. For example, the both vary with the string "selecting abc"
.evaluate just the (set (str/split ...))
and see what you get. and then consider if #{select"}
is a member of that set
ya doing same? set can have multiple values #{"select" "insert"}
yes. and that set doesn't contain any sets. so (contains? #{"select" "insert"} #{"select"})
will be false. it has two elements "select"
and "insert"
, not a set containing "select"
I have a bit of a weird question. Are people using logic programming to compose/render/validate interactive html forms? I’ve been doing web-dev for 8y but I’ve never encountered a way to handle them that wasn’t in some way painful or unsatisfactory at least. What’s important to me is not just validation, but also guiding the user as well as possible (UX) on one hand, but also having a declarative way to model them AKA a way that is closer to a specification that you can talk about with a domain expert / customer etc. than to “code”. Forms are painful by default already. I’d like to be more productive, easily correct and clear when doing them. Forms with sufficient amounts of complexity/rules have always bothered me. Simple tools like json-schema are not powerful enough to model them in a data-driven. declarative way. More powerful things like clojure.spec have caught my eye, but I’m wary of parsing spec forms (clojure expressions) or creating them dynamically (based on stored data etc.). So typically I write some ad-hoc recursive logic. Typically it goes like this: You make an effort to pin down the rules and constraints with a client, expert or based on an API etc. Then there is a huge gap. And then there is your form model, fields, validation, special cases, ui updates etc. pp When I was reading a bit about core.logic I immediately thought: Hey this is much closer to the initial specification of a (complex) form! Can it do this? Clojure has libraries for html data-structures (hiccup etc.) So my intuition is that there might be some way to unify (sic!) logic programming and modelling forms.
I’ll dive a bit more into core.logic and try to find a good book on the subject out of sheer interest anyway. I guess my question is: Is my intuition completely wrong?