This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-11-30
Channels
- # admin-announcements (13)
- # announcements (2)
- # avi (1)
- # aws (10)
- # beginners (427)
- # boot (3)
- # cider (4)
- # clara (26)
- # cljs-dev (21)
- # cljsrn (24)
- # clojure (205)
- # clojure-dev (32)
- # clojure-india (26)
- # clojure-japan (1)
- # clojure-russia (256)
- # clojurescript (41)
- # clojurex (1)
- # cursive (38)
- # datavis (99)
- # datomic (15)
- # emacs (19)
- # events (2)
- # funcool (5)
- # immutant (45)
- # ldnclj (3)
- # om (60)
- # omnext (4)
- # onyx (383)
- # overtone (7)
- # parinfer (1)
- # re-frame (3)
- # reagent (7)
- # ring (1)
- # testing (5)
@revivek: the first feedback I could give is: get rid of dynamic variables they're not really needed there; unlike in OOP (arguably), Clojure discourages you to have any state - in your example it doesn't change much but in the larger project it can cause unnecessary complexity
if you’re doing lein repl
in a terminal is there a way to clear the screen/buffer so you’re not constantly typing at the bottom of the screen?
⌘k works in iTerm, but when you start typing it reverts to the old bottom of screen position 😞
anyone have thoughts on http://purelyfunctional.tv for a clojure newbie? i work with JS daily, and never really used much in the way of a functional language, just for funsies
@andrut: thanks! Those declarations were inspired by a tutorial online and I was suspicious about why it was done that way with dynamic variables. Glad to have more context
What are thoughts on appropriate :pre :post usage? It's neat. Curious about particularly useful or frivolous usage patterns
We haven't found them to be particularly useful in (30kloc) production code.
We've tried pre/post, Prismatic Schema, and core.typed on and off at various times.
We found pre/post didn't really give useful enough error messages and the code looks a bit cryptic.
Having explicit tests (on inputs and outputs) made us think more deeply about what we were testing and whether the conditions actually mattered at all.
but still this error : IllegalArgumentException Don't know how to create ISeq from: java.lang.Long clojure.lang.RT.seqFrom (RT.java:528)
hellow
@roelof: you're so close. Instead of identity
you need recursion.
@seancorfield: thanks, I will think again how to make a recursive function there
@roelof: I’m suprised that you’re getting that error. Your code looks like it wouldn’t error it just might not do what you want. Maybe the :gen-class
is causing something to be messed up? The error you are getting I would expect you to get if you called testing
as in your example but left off the quote. Maybe try again after running lein clean
?
Just make the function you need at 'identity' the same as the function you need to write.
@noonian: no, he's getting it because mapcat
tries to concatenate non-sequences.
@cjmurphy is saying the same as me :)
Recursion is totally confusing. Sorry but it is supposed to be hard. But then slowly you get better with lots of practise, I hope...
No your function is almost correct as is.
@cjmurphy: I also hope it. Otherwise I get very hard days when I want to use clojure on daily basis with some projects
Just change identity
for another function.
To make testing
recursive.
Right at the deepest there is no list, yet mapcat needs a function that returns a list.
Somehow testing
needs to cause itself to be called recursively (to deal with nested sequences)...
Well it's a "simple" one and if you understand recursion then it's "easy".
Just change one word to make testing
recursive... That close!
@roelof don't change mapcat
just change identity
That's the only thing you need to change.
Yes. Now testing
is recursive.
mapcat
thanks, I have now this : https://www.refheap.com/112200 and see this error : CompilerException java.lang.IllegalArgumentException: Don't know how to create ISeq from: java.lang.Long, compiling:(C:/Users/rwobb/IdeaProjects/clojure-exercises/src/clojure_exercises/core.clj:13:1)
Why did you add that mapcat on the outside?
What you posted before was almost correct. You only needs to change identity to testing. Nothing else.
No other calls needed adding.
oke, but then I see this output : (testing '((1 2) 3 [4 [5 6]]) ) => (1 2 3 [4 [5 6]])
Use () instead of [] like 4clojure does
(Just to test that)
@seancorfield: good call
Because () is a seq but [] is not (it's a vector)
That 4clojure exercise doesn't use vectors as I recall.
(or (seq? list) (vector? list))
would make it work for [] too btw.
Ok so use the expanded condition I provided
(seq? [1 2 3]) ;=> false
, (sequential? [1 2 3]) ;=> true
, (seq? (seq [1 2 3])) ;=> true
Ah maybe I used sequential? when I solved it. I'm doing all this from memory since I'm on my phone (it's bedtime here).
Even with input lists it won’t work because in the base case you create a vector when the element is not a list.
= compares values not types
But your logic is entirely correct @roelof, its just seq?
returns false
for vectors which may be counter-intuitive.
So (= [1 2 3] '(1 2 3))
is true
@seancorfield: have a good night sleep
Woohoo!!
Ok I can sleep soundly now :)
Night
Goodnight @trancehime
i'm not going to bed!
And you might want to flatten a set so coll?
is the better thing to use? In this case and prolly generally as well??
I have a question
Assuming I have some vector like this: [{:data data} {:data data} {:data data}]
i would like to merge some additional data :data2 0 :data3 3
to each of the items inside that vector.
would i do a doseq
Yeah, I would use merge
or assoc
to put in the data
but like
how would I iterate that for each item in the vector
without exploding anything by accident
so map
is OK?
I think you want to think about transforming the sequence. So you want to return a new sequence so for
or map
makes sense. doseq
is only used for side effects and returns nil
.
cause I have to get a sequence of data from a database and then add extra stuff to each item in the sequence before I put it into a ratom
and then for updating you could use assoc
or merge
or update
depending on what you need to do
yeah. I have used map
before for simpler task
for
has the same syntax as doseq
except that it returns a sequence of the body instead of just executing it for side effects.
Sounds perfect for map. Keep same size of a coll but change the things it is made of.
OK, so map
it is
@seancorfield: when we were talking about unit testing before (this is a month or so ago now), was it clojure.test you recommended?
which... um... by the sounds of it is built-in anyhow?
@adam_cameron: he might be answering you tomorrow morning his time.
hmm... defonce
.
Cheers @cjmurphy, I know Sean pretty well and know when he's likely to pay attn to me. There's no rush with this.
@trancehime: defonce
is useful especially in cljs when you don’t want something to be redefined if the code is reloaded. You mentioned you were using reagent, so you might use defonce
if you were using figwheel to auto reload your code and you didn’t want your application state to be reset every time.
I am, of course, open to others' opinions on my Q too. It was just we were specifically talking about it. I'd landed on Midje as an option, but he didn't seem to think it was particularly Clojure-ish (my wording, not his), and said "just use x"
Thanks Jed.
@noonian: Yea, I know. Just thinking about atom persistence mostly
I think coming from a n00b 101 standpoint, it makes sense to get used to the baseline tools anyhow
again a beginners problem : how I can make acc work in this code : https://www.refheap.com/112203
You aren’t binding acc
to 1
there, it is just in the body of the let
expression. The syntax of the bindings for loop
is a vector of <symbol0> <val0> <symbol1> <value1>
.
Now I have to look how to make a Bigint of it. Now I see a integer overflow even on small numbers
I would probably format it like this: https://www.refheap.com/112205 I renamed number
to n
inside the loop just so its less to type and might be less confusing to not shadow the variable. I also switched the order of n
and acc
in the multiplication because I think n
will usually be smaller. I have no idea whether that matters on the jvm (probably not) but it feels nicer to me. Hehe anyway a lot of that is personal preference so feel free to develop your own personal style.
now the next challenge to think about. How to interleave two seq without using interleave
wow that video was incredibly enlightening
https://www.youtube.com/watch?v=vpJCytVQgGs <-- This video, since I was trying to understand sente
I have to re-implement interleave as exercise of 4clojure. Could this be done with a loop recur loop or am I on the wrong track
Wow. Definitely being made to feel quite thick trying to get underway with Clojure (cc @andymyers). Googling for StackOverflow answers is all right, but when I read the (accepted) answer and just go "um... OK, I know understand things less than I did before" is an interesting turn up for the books.
there are some really great books, if you’re looking @adam_cameron
@adam_cameron: who is playing dirty with StackOverflow answers.
Oh, I'm sure the answer is fine
It was this one: http://stackoverflow.com/questions/9810841/clojure-loading-dependencies-at-the-repl
like they should’ve moved the ‘use (require ‘[foo….])
in the REPL, (ns…(:require [foo….]))
in files’ part to the top
And to be fair, I'm not experimenting coherently... I'm just messing around randomly at lunchtime. Was just trying to get this example from (https://clojure.github.io/clojure/clojure.test-api.html) working (is (= 4 (+ 2 2)))
(in the REPL) and when it errored, twigged I needed to import the clojure.test lib somehow, and was getting same error as per that question.
I'm glad you think the answer had "Too Much", btw
yeah, I was kinda expecting what you suggested, and then "and if you want a more thorough explanation, it's like this, see..."
But I have finally established that - yes - 2+2 = 4. So I'm happy now
(I take my wins if and when I can get 'em)
hmm, I can take the first of each map with first but can someone give me a tip how I can make something like this of it '(1 :a ) where 1 is the first of the first list and :a the first of the second list
Any number of collections - your map function just has to accept the same number of arguments as maps
map
or reduce
will do it for you. I don’t know if there is a function in core that does that, but if there were, it would be implemented with either map
or reduce
.
maybe something like fn[list1, list2. solution ) (conj solution (first list1) (first list2)) [] ) just right out my head
but I do not like to cheat. I get the feeling that I copy past things and learn nothing
you can do it with both reduce
and map
. The reason my first choice is reduce
is because it is more “semantic” to the problem you have.
Well, map
also makes sense semantically: You are trying to map a hash to a list of keys and values.
and in the exercise there are two maps like this : (= (__ [1 2 3] [:a :b :c]) '(1 :a 2 :b 3 :c))
more specifically this https://help.github.com/articles/markdown-basics/#code-formatting
so, look into reduce
and into
. I can’t give you more hints without giving away the entire solution.
like this (reduce #(cons (first %1) (cons (first %2))) [1 2 3] [4 5 6])
but then this error message : CompilerException java.lang.IllegalArgumentException: Don't know how to create ISeq from: java.lang.Long
also (reduce #(into () (first %1) (first %2)) [1 2 3] [4 5 6])
gives the same error message
so, here is a hint. With fp languages, you can step through the evaluation of each iteration.
so, go through the first iteration, and try to figure out what the values of %1
and %2
will be
first arg is an accumulator, and the second is the item in the collection(s) for that iteration
acc is the outcome what we want and it is the current item we have to do something with
oke, now I see what is going wrong , I need two variables one for coll 1 and one for coll2
?? so do we need a variable for the acc, one for coll2 and one for coll2 or do I misunderstood you
Btw, I might be wrong about reduce. More and more it seems like map is the better option
oke, this one (map #(into %1 (first %2) (first %3)) [] [1 2 3] [4 5 6])
gives a empty set
@adam_cameron: yup learn clojure.test first then maybe look at Expectations if you want cleaner syntax
cool, will do
Also clojure.test.check (a contrib library, the next thing to "built-in") for some powerful testing constructs.
@roelof: remember that map
iterates over all it's collection arguments at once and stops when the shortest one ends (`[]` in your case).
@seancorfield: Maybe use reduce
@seancorfield: is there somewhere a good tutorial about clojure.test ?
Not that I know of (re: clojure.test).
So the current problem is interleave? You have two (or more) collections and you want to take an item from each one in sequence and then flatten the result...
So map
sounds sort of right… except you’ll want to flatten the grouped items so that sounds more like mapcat
So we’re looking at (mapcat some-function coll1 coll2)
I think...
and some-function
needs to take two arguments (in this case) and return a pair of the values
when you had (map #(into %1 (first %2) (first %3)) [] [1 2 3] [4 5 6])
you have two missteps there: you’re attempting something reduce
y assuming []
is an accumulator; and calling first
which map
does for you essentially
But reduce
only takes one collection so you can’t use it here (unless you use map
on the collections to make pairs anyway).
So back to some-function
: what’s the easiest way to take two things and produce a pair? (fn [a b] (??? a b))
— what is ???
here?
@seancorfield: moment, im just back from dinner and now looking for a function that can do that
@seancorfield: nope, set is not the right one 😞
Is [a b]
a pair of values?
Is there a function that, given a
and b
, would produce [a b]
?
it must be, im still looking and testing candidates set and conj are not the right one
Hmm, what function would create a vector from a given number of elements?
How many arguments does vec
take?
vec
turns a collection into a vector
You need something (similar) that takes a number of arguments and returns a vector of them all together
am I right that it must be in the create section of vectors on this page : https://jafingerhut.github.io/cheatsheet/grimoire/cheatsheet-tiptip-cdocs-summary.html
You can do something like (apropos #”vec.*”)
and it will return all the functions that match the regex
(mapcat (fn [a b] (vector a b)) [:a :b] [2 3 ] )
gives (:a 2 :b 3)
which looks good to me
I mean, (mapcat vector [:a :b] [2 3])
is the same as (mapcat (fn [a b] (vector a b)) [:a :b] [2 3 ])
it gives a timeout after this server http://30gigabitethernet1-3.core1.ams1.he.net
@seancorfield: I found your answer on clj-time
Your clj-time object, yes.
So if you have a Joda Time object my-date-time
(a date / time), you can require clj-time.coerce as tc and then call (tc/to-date my-date-time)
Some libraries have automatic conversions for Joda Time (java.jdbc for example, via a namespace in clj-time).
You mean a string?
Brave, learning Datomic as well as everything else
clj-time has parsing and formatting functions https://github.com/clj-time/clj-time#clj-timeformat
I haven’t even started playing with Datomic yet!
(BTW, I maintain clj-time — in case you have any questions — as well as clojure.java.jdbc and congomongo, in case you want to try a SQL DB or MongoDB)
I have played a little bit with Mongo when trying node but do not think it suitable for a accountiing/financial app
@seancorfield: that is why I play a little in Datomic
@roelof : There is a good tutorial for clojure.test on http://purelyfunctional.tv
I’m in the same time zone as you, in Switzerland near Lugano, but I run my own company ...
next problem for tomorrow : http://www.4clojure.com/problem/30
and after that this one : http://www.4clojure.com/problem/47
I’ve been hovering around the language for some time, not really clear how to get into it.
Living Clojure helped me a lot, somehow, I think simply because of the tone and pace of the book.
@nando : last question before I call it a day Which language do you then use in your own company/
CFML is the server language I know best. I do a lot of Javascript / jQuery, and I’m looking to swap that out with a React solution.
By the way, you asked about learning React earlier. Here’s a REALLY good tutorial for that.
Hi everyone… Does anyone knows if there is a Clojure function to update a map key or create the key if it doesn’t exist?
@nando thanks for the tip. I think I first learn clojure and then look into react or om
With update-in I can increment/update the value, but I get an error if the key doesn’t exist
identity
just returns itself. Instead of identity you can put a function that looks at what is coming in and returns something different.
Check out fnil
That allows you to provide a value to be used if an argument is nil/missing.
So (fnil inc 0)
is a function that increments a value but if the value is nil
it substitutes zero (and then increments that).
>looks good but do you have first learn react before you can follow this videos ? @roelof Not really, if you're just interestrd in async
anyone familiar with boot-middleman
?
I have some questions around setup… examples I’m finding aren’t running. Not sure if it’s an ENV issue