This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-12-01
Channels
- # 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)
@andreortiz: try #C053K90BR
Does anyone know why question marks are sometimes at the beginning, as in having a key in a hash-map like this {:?data nil}
?
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?
I've seen (?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.
@seancorfield: It’s in core.unify.
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...
@trancehime: thanks, maybe this can be something for a project I have in min
No problem
I am new too, it's all a learning experience for me
Hey @roelof ! Good morning !
What's today's project?
Which one?
Ah yes... that's an interesting one...
and hopefully this one : http://www.4clojure.com/problem/47
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 reduce
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).
Then you can just extract the accumulated value.
Think about how you do this in a non-FP language...
and then fold that into a reduce
as a pair of values.
if last_element != former_element then add last_element to acc and former_element = last_element else take the next element
Start with nil
?
Also, consider destructuring which is a great way to pull apart a pair of things
(fn [[acc last] x] ...)
for example as your reduce
function...
"assign" bad
"bind"
You bind a value to a symbol.
And loop / recur
will re-bind a (new) value to a symbol.
That way you'll get into the FP mindset.
Heh, I do this in a bare repl... I find IDEs overwhelming...
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!
oke, and do you do now other challenges or do you use clojure for work @seancorfield
Clojure is my primary work language these days. We first took Clojure to production in 2011.
It only became our primary language late last year
Internet dating platform.
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 event-msg-handler :ev-id
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.
Helps me to learn the language while also accomplishing tasks
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
Couldn't tell you; I didn't really have time to read books
@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.
I just read around libraries and built my own little examples.
oke, thanks for the info @agile_geek
@seancorfield: is this what you mean as a solution : https://www.refheap.com/112225
I had to spend a lot of note-taking to wrap my head properly around defmulti
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 defmulti
@roelof: without the ( )
around acc
but yes.
and you need an initial value which would be... [[] nil]
perhaps?
I think you want [acc x]
for that case BTW
so (if (= last x) [acc x] [(conj acc x) x])
... something like that?
So, yeah, I think you're very close...
@seancorfield: where in your code is then the [ [] nil] part or do Ihave to add it myself
It's the initial value for reduce
...
(reduce func init coll)
@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)
@roelof: something like that, yeah...
@andymyers: any questions where we can help you
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?
@roelof: you need a fn
in there
(fn [[acc last] x] ...)
@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...
wax on, wax off... over and over again...
Yes, do exercises every day. Very important.
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 "clojure_exercises.core$roelof$fn__1456@5316c2d8"]
reduce
takes a function...
Your reduce
has an if
instead of a function
(reduce (fn ...) init coll)
@seancorfield: yes, somehow I have to check if last is equal to x
(reduce (fn [[acc last] x] (if ...)) [[] nil] list)
-- like that?
Remember it's always (reduce function init coll)
@roelof: the way I think of reduce is that it’s calling a function over every element of a sequence while maintaining state in [acc last]
in @seancorfield’s example.
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
The 4clojure test does apply str
You're overthinking
(reduce (fn [[acc last] x] (if ...)) [[] nil] list)
-- is the template
When the input is A string you'll get a sequence of characters
The 4clojure test does apply str on that so you don't have to worry
But are you actually testing the right thing?
On strings you will get a vector of characters. That's correct.
it working , Another one solved and i saw a very short solution : #(map first (partition-by identity %))
@seancorfield: thanks, now a break and maybe this afternoon or evening the next challenge : http://www.4clojure.com/problem/47
@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.e. (contains? [1 1] 2)
would return false
I still this community because everyone is willing to help and explain things to a newcomer/beginner
We were all beginners sometime… and we are all still learning.
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
I know I'm still learning
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.
@adam_cameron: game addict ??
@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 am going to go ahead and pass out. Also order matters.
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
@roelof: nice... I always forget about partition-by
!
@seancorfield: I did it know it because I not needed it before
Where are you on the problem now?
(I just woke up and I'm on my phone - not yet out of bed! :) )
I had this : https://www.refheap.com/112242
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
That leaves acc and x - two args
oke, I tried this (reduce (fn [acc ch ] (conj acc ch )) [ch] list)
but then the ch is only 1 inserted
the 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]
You are starting with [ch]
And then prepending it to every element
So it's in there twice at the beginning.
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]
Think about what the accumulator should start with
Your reduce prepends ch to every element so...?
Also think about what (my-interpose 0 [])
should return
No, don't think of it like that
Easier to do something different at the beginning than the end
Correct
What about (my-interpose 0 [1])
Correct
(Stepping away for 15)
This is pseudo code, not clojure code:
(fn [acc it]
(if (last of list) == (it)
(conj acc it)
(conj acc ch it)))
@roberto: No need to special case it and easier to do something different at the start of the list, rather than the end /cc @roelof
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))
As I said above, you’re starting with [ch]
which is not correct
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,
Because ch
is not a vector.
Think about the types.
What is acc
?
right… so what’s the initial value?
correct
so what goes in the reduce
?
(reduce func init coll)
so the init
expression should be… ?
Correct!
@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...
@seancorfield: found a shorter one : #(drop-last (interleave %2 (repeat %1))
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 vector
or list
interchangeably
and drop-last
and rest
are mirror images — so swapping the args to mapcat
would require swapping drop-last
/ rest
I’d probably use butlast
instead of the 1-arity drop-last
tho’...
(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
Many, many more than one
tomorrow work on this one :http://www.4clojure.com/problem/31
now my task for tommorrow : http://www.4clojure.com/problem/41
conj can take any number of elements to ‚add' to the collection
the location where it’s added depends on the collection type
@tmtwd: 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"
Well, it’s consistent but it’s just not obvious...