This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # admin-announcements (20)
- # aws (24)
- # beginners (323)
- # boot (60)
- # business (1)
- # cider (23)
- # clara (7)
- # cljs-dev (38)
- # cljsrn (12)
- # clojure (302)
- # clojure-canada (5)
- # clojure-dev (26)
- # clojure-miami (1)
- # clojure-nl (13)
- # clojure-russia (64)
- # clojurecup (1)
- # clojurescript (202)
- # clojurex (4)
- # code-reviews (5)
- # core-async (23)
- # cursive (39)
- # datavis (26)
- # datomic (34)
- # devcards (5)
- # editors (19)
- # emacs (4)
- # events (6)
- # funcool (55)
- # hoplon (5)
- # ldnclj (3)
- # lein-figwheel (1)
- # luminus (15)
- # om (159)
- # omnext (7)
- # onyx (107)
- # slack-help (2)
- # testing (3)
Does anyone know why question marks are sometimes at the beginning, as in having a key in a hash-map like this
I understand a ? at the end is the same as the convention in Java of having
is at the beginning, it denotes a predicate. But what does it mean when the
? is at the beginning?
(?assoc m k v) which means conditional
assoc -> assoc value
v only if it's present, maybe it means optional data then?
There’s a convention in lisps of using a prefix question mark to indicate a meta-variable or something that will be unified or substituted.
Well, I did it here: https://github.com/maitria/avi/blob/master/src/avi/mode/normal.clj#L21
Oh… midje uses it in it’s table syntax: https://github.com/maitria/avi/blob/master/test/avi/t_nfa.clj#L26
In that example, ?nfa ?inputs and ?result get substituted once for each line in the table.
Its given me something to look out for, that I'll be able to 'complete' a bit later I think. Something like a default. 'substituted' is a funny word in an immutable language thou.
Most things that are mapped, get build out into something different, could have a ? at the beginning.
Prolly it means a little more than I understand right now. I know I'll get it when I see an example I need to understand. That's how I got destructuring.
The idea is introduced in Paul Graham’s “On Lisp” on page 238. Titled “Destructuring” ironically.
I've seen the
?var thing quite a bit in the context of... well, I thought logic programming... but now I'm having a hard time finding examples online.
I know they (Clojure/core) talk about Datomic in terms of Datalog queries but when I went looking for that, everything looked like Prolog instead...
Ah @eraserhd that's where I've seen it... I expected to see it in core.logic as well but...
I have to think how I can "store" the old value somehow and compare it against a new one
@seancorfield: first I thought to use set or distinct but no luck . Set does not do the job and distinct deletes to much
Since you want to go from a collection to a subset of it, you probably want to start with
Bear in mind you need to produce two things: an accumulated value (the collection with no duplicate elements) and the last seen value (so you can detect duplicates).
if last_element != former_element then add last_element to acc and former_element = last_element else take the next element
Also, consider destructuring which is a great way to pull apart a pair of things
4clojure had a lot fewer exercises when I did it... for a while I was actually keeping up with it at first... but it got away from me once it got to about 70 challenges!
Clojure is my primary work language these days. We first took Clojure to production in 2011.
oke, when Im much more familiair I think I will try to make a e-commerce site or a accounting site
Most of our low-level e-commerce integration is done with Clojure now (iTunes, Paymentwall, Braintree, SBW)
oke, I think about the front part, making invoices , adding and deleting products in clojure
In our app, the VC part of MVC is CFML, and some of our M (legacy code). More and more of our M is Clojure now.
But we've also moved nearly all of our HTML emails over to Clojure (using Selmer) now.
I'm hand-writing (in a notebook!!) the stuff I'd like to do with
async, mostly, since I am using
sente, this would be my
oke, I doubt between luminus and doing everything myself but as I said that is a problem of the future
I've avoided using framework (I'm not even sure I'd call Luminus a real framework. Even the main site calls it a 'micro-framework' which is probably more accurate) and instead rolled out my own handlers and stuff.
Incidentally, I've actually run into the most hiccups (heh) in Clojure with regards to destructuring
oke, then I have to find a good book/tutorial where things like compujure and ring are explained
@roelof: It is, if like me, you learn best by working through an example end to end. Did you say you came to Clojure from Ruby and Rails? If so it would probably suit you quite well.
@agile_geek: do you have a example of a end-to-end example. I though web development has one a picture upload site
The picture upload site is the main example that runs through most of the chapters in the book. There is a RESTful API example in the early chapters that was using the liberator library but I haven’t seen the second edition of Web Development with Clojure so it may have changed as there are a lot of new libraries in that field.
@roelof: nothing specific springs to mind although there are loads of blog articles on Clojure web development. The example in the book works quite well as it covers rendering pages in different templating libraries, using ClojureScript, database integration with JDBC and Korma, etc.
that is a difficult one but I saw on the clojure koans that you can also use protocols as a alternative
You can although Protocols can only dispatch on type whereas multimethods can dispatch on anything.
I wanted to understand
defmulti properly as I plan to manage my
sente event handlers through a
@seancorfield: where in your code is then the [  nil] part or do Ihave to add it myself
@adam_cameron: I've been tinkering for a while and I'm still very much a beginner. I just haven't had the consistency with it. It's totally different to anything else I've used
That's what I'm finding too (and I'm not really any further than "G'day world" still).
Other stuff keeps getting in the way. For example I am currently writing a CFML-oriented blog article. And have another one to do after that (which is a guest author one, so will be quick to do). But I only have another 35min left before I need to start my day job
@seancorfield: so it will be
(reduce (if (= last x) [acc x] [(conj acc x) x]) [ nil] coll)
I've got a question. Well, assuming I used
io/copy to write a file to the local filesystem, how would I go about to retrieve that same file in a different function or page to display it?
@andymyers: I’ve been on the Clojure / Lisp journey for close to three years but almost all outside the ‘day job’. It takes time but stick with it. If this is your first lisp and first functional language it’s a whole new paradigm and a completely alien syntax to learn and you go through ‘cycles’ of understanding.
It's a lot easier when you can integrate it into your work... because it takes repetition...
hmm, I think a parentheses problem : When I do this
(fn [[acc last] x] (reduce (if (= last x) [acc x] [(conj acc x) x]) [ nil]) list)) I see this output : => #object[clojure_exercises.core$roelof$fn__1456 0x5316c2d8 "[email protected]"]
thanks, it was a parentheses problem. I have now a output like this ; "[[\\L \\e \\r \\o \\y] \\y]" so I have to makeit look like a string
Hmm. Never mind. I was told the written files would be linked to some domain I could just refer to.
hmm, another nice challenge. When the input is a string , then the output must also be a string , if it's a list the output must also be a string
it working , Another one solved and i saw a very short solution : #(map first (partition-by identity %))
@roelof everyone gets tripped up by
contains?. Research what
contains? does when it operates on an indexed data structure (like vector) versus an associative data structure (like a map).
Am I right here : (contains? [1 1 1 1 1] 2 ) is right because there is a second number avaible
almost, vectors are zero indexed so it’s because there’s a third number available.
I still this community because everyone is willing to help and explain things to a newcomer/beginner
other , I tried other languages and there sometimes beginners questions are or ignored of just given a answer without explanation
here no answers but explanations and questions so the newcomer will find the right answer him/her self
Thanks folks. Might be time I revisit the 4clojure. I only got about 25% of it done last time round. @adam_cameron are you in this with me?
I was gonna look at Project Euler to start with, but yeah, why not? It'd be good to be able to compare notes. What I mostly need is time.
I only get about 45min in the morning and same @ lunchtime to do anything @ the moment. Evening spare time is all Fallout 4. And this will remain the case for... a while.
@andymyers: if you have questions about 4clojure , ask here, Here there are enough people that can and learn you good clojure ?
(off topic alert) @roelof: sporadically. I don't buy a lot of games, but I don't mind the occasional one. The previous game I bought was Skyrim (so back in 2011), to put it in perspective. I'm finding FO4 to be pretty engrossing currently though.
Hello all - I am using compojure to create a restful API using postgresql as my data store - I have a route
/ that invokes a call to my model to return all the rows in the database. The issue is that when I visit the page in a browser or do a
get call to the api endpoint the data being returned acts like a file download that comes up empty. No errors but no data either - I can provide more code if needed but any initial thoughts?
@roberto: do I need to use wrap-defaults at all? It looks like it provides some benefits but since I will be handling file uploads through the api. If I do need it then I am assuming I need to chain the middlewares via a
(-> (wrap-json-body) (wrap-defaults app-routes site-defaults))
Just using the wrap-json-body instead of wrap-defaults did allow the api to return the proper structure - Thanks! just curious about the defaults that were setup when I ran
lein new compojure <appname>
I’ve always had to use wrap-json-body, for some reason the order in which you apply the middlewares is significant, and I can never get it right.
why do I get this error message : CompilerException clojure.lang.ArityException: Wrong number of args (2) passed to: core/my-interpose on this code ; https://www.refheap.com/112242
btw, I was wrong yesterday about the
reduce fn. It only takes an accumulator and one iterator.
also, the idiomatic way to write vars or function names is to hyphenate them, instead of snake case.
Somehow I have to use a acc , the current item and the char who needs to be placed into the right place
@roberto: you are right. When I change my code to ' (reduce (fn [acc ch ] (conj acc ch "2")) [] list)' it does work
but somehow I need to use a acc, the current item and the char which needs to be imposed
You don't need ch in the reduce fn - it doesn't change and it's available in the whole my-interpose function
oke, I tried this
(reduce (fn [acc ch ] (conj acc ch )) [ch] list) but then the ch is only 1 inserted
ch is in scope within your function, and the reduce
fn can access the outer function’s scope.
Before you go too far with reduce: consider the shape of the solution... if you have four elements in your list you'll only have three copies of ch, between the elements
oke, I changed the function to this
(reduce (fn [acc x ] (conj acc ch x )) [ch] list) and now see this : [0 0 1 0 2 0 3]
oke, when I changed it to acc x ch I see this : [0 1 0 2 0 3 0] and it needs to be [ 0 1 0 2 0 3]
This is pseudo code, not clojure code:
(fn [acc it] (if (last of list) == (it) (conj acc it) (conj acc ch it)))
but remember if you have
[0 1 0 2 0 3] you can always call
rest to get
[1 0 2 0 3]
@seancorfield: this is not working : (rest(reduce (fn [acc x ] (conj acc ch x) ) [ch] list))
Since you are prepending
ch to each element, you’ll have two
ch at the beginning — you only want one.
oke, but when I do this
(rest(reduce (fn [acc x ] (conj acc ch x) ) ch list)) then I see this error message : CompilerException java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IPersistentCollection,
@seancorfield: thanks , this is working : `(rest(reduce (fn [acc x ] (conj acc ch x) )  list)) )`
It always helps to think about the base cases… empty list, single element list...
and I will not surprise me that repeat cares of that it also happens on the other places
My solution would be something like
#(rest (mapcat vector (repeat %1) %2)) — untested
looks almost like another solution I found :
fn [sep xs] (drop-last (mapcat list xs (repeat sep))))
Yup, in this case — with
mapcat — you could use either
rest are mirror images — so swapping the args to
mapcat would require swapping
(drop-last coll) ==
(drop-last 1 coll) — you can drop an arbitrary number of elements off the end
oke, as we say in the Netherlands, All roads leads to Rome. So there are more then 1 ways to solve a problem
conj uses the "fastest" way to add elements so for vectors it’s at the end, for lists it’s at the beginning (behaving like
cons). For maps and sets it’s just "whatever"