This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-12-08
Channels
- # admin-announcements (3)
- # beginners (284)
- # boot (179)
- # cider (15)
- # cljs-dev (6)
- # cljsrn (95)
- # clojure (105)
- # clojure-austria (4)
- # clojure-berlin (9)
- # clojure-germany (4)
- # clojure-japan (3)
- # clojure-russia (65)
- # clojurebridge (1)
- # clojurescript (109)
- # code-reviews (16)
- # cursive (27)
- # datavis (19)
- # datomic (68)
- # devcards (7)
- # funcool (31)
- # jobs (1)
- # ldnclj (2)
- # lein-figwheel (3)
- # leiningen (4)
- # off-topic (419)
- # om (255)
- # parinfer (39)
- # portland-or (2)
- # re-frame (28)
- # reagent (14)
- # slack-help (12)
- # spacemacs (1)
Hmm. Anyone here? I am mystified by this 4clojure problem. After several attempts I googled it (probably not best practice, I know), but I cannot understand why it works. And then I tried the code in the REPL at home, and it doesn't seem to work the same way there.
Before I googled for an answer, I assumed I needed some kind of test to see what sort of collection it was, to reverse it, and then to re-cast it as the original collection type. I was surprised that it seemed to work without doing that. But then on my REPL at home it isn't working the same way, it always gives me a list like I was expecting.
I just realized... a vector and a list with the same contents are equal.
Is that all it is?
now I think I remember that last time I asked a question on here, it was just that I was missing a parenthesis, and you answered that time too! Clearly I need to pause a bit longer before posting my questions.
That is, indeed, all it is. The rubby ducky treatment is always surprisingly effective.
*rubber
rubber ducky?
Talking out your problem to a friendly rubber ducky. They may not respond, but talking it out is often enough for you to understand your problem.
ah. yes. I definitely need to do that.
or quite literally duck typing in this case
?? I have a list (or seq?) of urls like: (["http://example.com"] ["http://gmail.com"] ["http://oajve.com"]) I would like to perform a get request on each but I don't know what to do to "get into" this data structure and perform some sort of seq operation
If you just want to get a particular element, you can use first
, second
, or nth
. But probably you want to execute some function on each element of your list, in which case you're probably looking for map
or doseq
.
You can also use get
like nth
.
hm tried map already using: (map #(client/get [%]) urls)
and (map #(client/get %) urls)
map
returns a lazy sequence, so unless you're doing something to realize those urls, nothing will happen
I believe mapv
is not lazy; you could give that a try.
The fact that the list is nested with vectors is problematic
PersistentVector cannot be cast to java.lang.string
If I gave you ["
how would you get the string out?
first?
Cool, I've been looking for an opportunity to try that. 😄
is that the best solution or can we do better?
how about flatten
? turn the list of vectors of strings into just a list of strings
That also works. I like that.
anyone a tip how to solve this one : http://www.4clojure.com/problem/166
What do people think of Intrduction to Functional Programming with Clojure from Lispcast, and there accompanying web dev course?
I'm thinking I might do it after I cover clojure for the brave and true
Ahhh crap
I've got a question
@roelof: It is simpler than it sounds. You are given the function that you need to use to return one of three keywords.
I want to use filter
to create n
separate sequences from a much bigger sequence based on n
keys, so I can compare them. How do I go about persisting the sequences returned from filter
?
For example: [{:data data :identifier 1} {:data data :identifier 2} {:data data :identifier 3}]
, I want to use filter
so I can get 3 individual sequences based on :identifier
so I can check if :data
in each of the maps are the same
My purpose is that I have a giant set of data from a database submitted by various users and I want to check if the data from the users are exactly the same
well, "giant" is an exaggeration
@cjmurphy: when I have as argument (fn [x y] (< (count x) (count y))). How can I execute only the count functions then?
Consider what you want to do to one of the hash-maps then use apply to get that function you have created to each of them.
Last two comments to @trancehime
hmm, when I do this
(defn my-eq
[operator st1 st2]
(println operator)
(my-eq (fn [x y] (< (count x) (count y))))
youre only passing 1 argument to my-eq
which is (fn [x y] ...)
i think?
But @trancehime has just told you why.
your my-eq
function is asking for operator
, st1
, st2
usually an anonymous function like what you showed in your example just now is treated as 1 argument
hmm, then I have to destruct it to find out what both counts are returning. When I do the whole function the answer would be false and I cannot say if it's then gt or eq
How do you find out if 5 and 1 are equal when you have a 'less than' function that gives the right answer?
Of course inside the function you don't know what the numbers are, you only know x and y.
I figured out group-by
has the behavior I'm looking for regarding my initial question.
Now I'm faced with a different conundrum
Though perhaps I might have to use apply
anyway at some point
No probs
I'll see if I can figure it out myself first
Just thinking out aloud mostly
I think I have to look for more "beginners" problem. The last "easy" problems are very hard for a beginner like me
hmm : this looks promosing (when (function arg1 arg2) :gt)
but if it's false how can I check for the other two
you should use cond
.
when
should only be used if when the expression returns false you don't want to do anything
@trancehime: I try to solve this challenge : http://www.4clojure.com/problem/166
@roelof: I haven't followed the whole thread but take a look at condp
as it might be useful.
@agile_geek: maybe you are right. The 5th example of this page : http://conj.io/store/v1/org.clojure/clojure/1.7.0/clj/clojure.core/condp looks almost on the challenge
condp
probably works better yeah.
the only thing I have to figure out how I can change a function from (< 5 2) to ( = 5 2) or (> 5 2)
or this one (fn [x y] (< (count x) (count y))
where I have or change < or that the function does not use it and I can compare the both counts parts
If you see the documentation on condp
you should notice that it should go through the clauses until a match is made.
So it's a matter of providing the right predicate, expression and clauses.
the function is actually just (< (number of characters in x) (number of characters in y))
but if the numbers of characters are the same in x and in y the output schould be :eq not :lt
the idea of the challenge is that you can output any of the equalities when all you have is <
(= :eq (__ (fn [x y] (< (count x) (count y))) "pear" "plum"))
the function should be such that you can output :gt
or :eq
or :lt
when your comparator is any less-than function
@rantingbob: I really like LispCast’s tutorials
Given a a content-type as image/png
and I want to get just the png
part, what's a better way than what I have now which is: (first (rest (clojure.string/split mime #"\/")))
where mime
is image/png
I'm running into java.lang.NullPointerException
problems
even though the same code does not produce the same error in other places
so (second (clojure.string/split mime #"\/"))
?
I mean I'm just really confused. I'm using this particular code in order to write save an icon's file extension to a record in a database
There's one incredibly specific case where I get a java.lang.NullPointerException
when I am uploading 3 icons at once
and it only occurs on one specific form input
The image upload part is fine, the file is saved into the filesystem no problem
or rather, files
Now, the file object when passing it to the server has a :content-type
key
the :content-type
key's value is MIME type so it has the image/png
or what have you
I'm confused
I just want to split after /
This piece of particular code works with literally every single other form input I have that behaves the same way except this one and only if I am uploading 3 icons at once even if they are all exactly the same just with different filename
The stack trace includes java.util.regex.matcher.gettextlength
which lead me to believe it has something to do with my splitting function
@cjmurphy: I have logged the object entering the server to see if anything appears weird. For cehcking, I uploaded 3 .png files. All 3 of them had :content-type
values of image/png
I believe it's from the file itself
What confuses me is if I am just uploading 1 or 2 images using this form there's no issues. But 3? No, it breaks
omg, wait
I AM SUPER DUMB
never mind, I got the issue
should be :tournamentIconLargeObj
and :tournamentIconHugeObj
pffff
What was going in was indeed null, since the form object does not have a :tournamentIconLarge
key but a :tournamentIconLargeObj
key which contains the file informations
If you could be bothered using it all the time - like Scala and Haskell people are forced to.
(the code is actually not so elegant, but I'm quite new at Clojure and they're making me do an awful lot on my own in just a month... I'll probably focus on reworking it once I'm allotted time to do so)
Nah I'm just admitting that how I'm going about the whole thing isn't exactly great xD
whats the best way to arbitrarily sort a seq of maps on a couple keywords? let's say i want all the :b
's then all the :a
's then all the :c
's. So in:
[{:k :c} {:k :a} {:k :b} {:k :a}]
I'm looking for f s.t.
(f [{:k :c} {:k :a} {:k :b} {:k :a}])
;=> [{:k :b} {:k :a} {:k :a} {:k :c}]
(let [x [{:k :c} {:k :a} {:k :b} {:k :a}]
o (->> [:b :a :c]
(map-indexed (fn [idx itm] [itm idx]))
(into {}))]
(sort (fn [{a :k} {b :k}]
(< (o a) (o b)))
x))
someone who can help me this 4clojure problem : http://www.4clojure.com/problem/166
Anyway, incase anyone's interested i have a bunch of "packages" that have "statuses" and I wanted to show them in the order I thread through to become o
.:
(defn order-packages [packages]
(let [o (->> [ "BLOCKED" "PLACED" "PROCESSING" "PROCESSED" "SENT" "PAID" "CANCELLED"]
(map-indexed (fn [idx itm] [itm idx]))
(into {}))]
(sort (fn [{a :status} {b :status}] (< (o a) (o b)))
packages)))
@escherize: sorting is surprisingly hard I found.
(defn sort-with [key-fn val-seq coll]
(let [o (->> val-seq
(map-indexed (fn [idx itm] [itm idx]))
(into {}))]
(sort (fn [a b] (< (-> a key-fn o)
(-> b key-fn o)))
coll)))
(sort-with :k [:c :b :a] [{:k :c} {:k :a} {:k :b} {:k :a}])
;=> ({:k :c} {:k :b} {:k :a} {:k :a})
The tricky bit is sorting from one column, then the next, then the next, which I think is what you are trying to do..??
Okay great. @escherize - I'll have to get back to you later on that one (and I prolly will!). An interesting kind of thing to examine here...
(defn compare-by [[k comp & more] x y]
(let [result (comp (k x) (k y))]
(if (and (zero? result) (seq more))
(recur more x y)
result)))
Another question : I do now the 3rd chapter of web development with clojure and use the librerator plugin
I actually read most of the Brave book but didn't do the exercises. A good combination of theory and exercises might be to hold off on doing the exercises on one of them.
Later on I would like to use clojure to make web apps but now that is more then i can chew now
You might want to be reading a few books then. Perhaps leave the exercises for a while, give them a rest and come back later 'refreshed' after having read a few of the books out there.
I can do that but my problem is Always if I read it , I think I understand it but then I have to do in in real
that why I choose to do a lot of exercises / challenges to get really a feeling for it
The whole 'how to do web applications' is going to be changing with Om-Next, which eschews talking through to REST services. So I would delay that part of the learning - that's certainly my strategy.
oke, I have seen a lot of Om. Looks good but I wonder if you could use it when you do not anything about react
If I have a function with an arity of 0, whats the idiomatic way to call that function 10 times and put it into an array? Right now I have
(map (fn [_] arity-zero-fn) (range 10))
(defn matching-part
[part replacement]
{:name (clojure.string/replace (:name part) #"^left-" replacement)
:size (:size part)})
(defn symmetrize-body-parts
"Expects a seq of maps that have a :name and :size"
[asym-body-parts]
(reduce (fn [final-body-parts part]
(into final-body-parts (set [part (matching-part part "right1-") (matching-part part "right2-") ])))
[]
asym-body-parts))
#error {
:cause nil
:via
[{:type clojure.lang.Compiler$CompilerException
:message java.lang.NullPointerException, compiling:(do_things/core.clj:62:1)
:at [clojure.lang.Compiler load Compiler.java 7239]}
{:type java.lang.NullPointerException
:message nil
:at [clojure.string$replace invoke string.clj 99]}]
On a random note, I totally don't get errors like that in my Clojure repl
in Cursive
are you using a beta version or has there been an update?
@rantingbob: moment i have to find out how I can check which version I use
Found it, I use 0.1.172-15 @rantingbob
I'll check what version I'm on once I get home
First priority : doing the easy challenges of 4clojure and doing all the exercises of the brave book
can someone explain what a logic list is? From http://insideclojure.org/2015/01/02/sequences/ (and probably 2 other places I have seen). What is the underlying data structure?
so it I can expect it to behave a certain way and it doesn't help me to understand how it does that? (at least at a novice level). errr, meaning at this point, when im just learning the language, its sufficient to know the behavior .
Any sequence will behave the same way for the sequence api functions. There are other functions that take seqs but do different things depending on the implementaton, like conj
. In cases where you care you will often “dump" a sequence into a concrete implementation like a vector: (into [] (map inc [1 2 3]))
and there are versions of map
and filter
that always return a vector ‘mapv’ and ‘filterv'
so in that article, first
, rest
, next
, and cons
don’t care about concrete implementation
@noonian: thanks, that helps a lot.
@drewverlee: np, glad I could help!