This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-11-21
Channels
- # beginners (165)
- # boot (9)
- # cider (9)
- # cljs-dev (5)
- # cljsjs (5)
- # clojars (4)
- # clojure (207)
- # clojure-brasil (1)
- # clojure-greece (3)
- # clojure-poland (2)
- # clojure-russia (6)
- # clojure-spec (85)
- # clojure-taiwan (1)
- # clojure-uk (53)
- # clojurescript (96)
- # community-development (2)
- # cursive (4)
- # datomic (14)
- # emacs (41)
- # events (2)
- # hoplon (184)
- # leiningen (1)
- # nginx (1)
- # off-topic (16)
- # om (7)
- # onyx (63)
- # pedestal (27)
- # planck (17)
- # protorepl (3)
- # rdf (9)
- # re-frame (62)
- # reagent (7)
- # ring-swagger (5)
- # schema (2)
- # spacemacs (5)
- # test-check (25)
- # untangled (93)
- # yada (3)
whats the best way to walk a hashmap given a vector of keys?
{:a {:b 1}}
[:a :b]
=> 1
Is that what you mean, @drewverlee ?
man i must have been googling the wrong set of words
@jswart I use your code this way :
(map (fn [input]
(let [reverser
(if (string? input)
(comp (partial apply str) reverse)
reverse)]
(= input (reverser input))))
but then the first test fails here : http://www.4clojure.com/problem/27
@roelofw Are you using an editor + REPL or are you typing the solutions directly into 4clojure? Working in the REPL makes it a lot easier to see what your functions do
For example, what do you get when you call your function with '(1 2 3 4 5) as input?
@roelofw I am guessing that's a potential soln to an anagram detector? In the snippet you've shared you don't seem to have passed a collection to map
to map over?
Actually returns a transducer (not sure ClojureBot can handle that!
Nope must be unbalanced parens
Even if you fix that - I'm not sure what problem you are try to solve but that is expecting a sequence of either sequences or strings?
@agile_geek how do you talk to the clojurebot ?
Start a line with /clj
followed by your Clojure code
thanks, this solution is too difficult for me. I think im going for : ' (fn [x] (string? x (= x (str(reverse x)) (reverse x))))
Hmm, think about how many args string?
takes
Also what it returns
I would try out some of these in a REPL - I think 4Clojure has a link to tryclj repl?
How many args are you passing string?
in that code you copied above?
(fn [x] (string? x (= x (str(reverse x)) (reverse x))))
I changed the code to :
(fn [x](string? x)
(= x (str(reverse x)))
(= x reverse x) "racecar" )
Again- think what string?
does
correct but you want to control the flow of expressions
at the moment you're executing each of these in sequence but none of them are doing anything with the result of the string?
predicate...
the answer is easier than I'm making it
you're missing a control flow statement...
the simplest one there is
oke, what I want is if x is a string then this part must be evaluted (= x (str(reverse x)))
else the other part
yep... I think the most important part of your statement above is if
and I'm not sure your parens are balanced
I have now this :
(fn [x](if (string? x)
(= x (str(reverse x)))
(= x reverse x)) "racecar" )
oke, this works :
(defn roelof [x](if (string? x)
(= x (str(reverse x)))
(= x reverse x)))
You are missing parens around second reverse
I don't think you need to make a sequence a string as all you want as a result is true or false
if x
is a sequence and (reverse x)
is a reversed sequence then you can just compare them
oke, this one works for the first two :
(fn [x](if (string? x)
(= x (clojure.string/join(reverse x)))
(= x reverse x)))
yep or you could use (apply str (reverse x))
see ☝️ about missing parens
look at (= x reverse x)
is x equal to the fn reverse and to x again?
i.e. =
is taking 3 arguments, two values (of x
) and the unevaluated function reverse
@agile_geek thanks, solved another challenge
@roelofw I highly recommend following some people to see how they solve the challenges! I follow some of the top users: maximental, hypirion, jafingerhut, chouser, _caterpillar
@roelofw the trick to the palindrome stuff was that reverse converts it's input to a sequential, so you should do the same.
@roelofw a popular Fibonacci sequence solution uses recursion. It's worth trying to use recursion to solve it as it's educational
someone who can help me why I see this error message : ' ArityException Wrong number of args (1) passed to: runner/fibtill/fn--1885 clojure.lang.AFn.throwArity (AFn.java:429) on this code :
`` (defn fibtill [n]
(take n (map first (iterate ( fn[ a b ] ( + b (+ a b))) [0 1])))) `
Hello all, is there any way to use something like this? (re-seq #(str "some " thing) "some thing")
a function already takes a vector as its argument declaration, if you want to destructure in there you need to nest another vector
oke, I did .
(defn fibtill [n]
(take n (map first (iterate ( fn[ [a b] ] ( + b (+ a b))) [0 1]))))
but now this error message : IllegalArgumentException Don't know how to create ISeq from: java.lang.Long clojure.lang.RT.seqFrom (RT.java:542)
Hey, what am I missing, as can I do (require …)
in user
ns in repl, but I get Unable to resolve symbol require …
if I switch to namespace using (in-ns ‘my-example.core)
?
When you run (in-ns ‘foo)
the repl creates a new namespace for you. This namespace does not have clojure.core
in it. To see what you have you can type (clojure.core/ns-map clojure.core/*ns*)
this will show you a mapping of symbols to their values. You can type the symbol to run it in the repl.
@roelofw important part of clojure is learning to read these error messages. You should watch the Stuart Halloway talk on Debugging with the Scientific Method (or something similar to this in name)
@jswart wow, thanks! I didn’t know it doesn’t include clojure.core
. Do you know reasoning behind that? Why wouldn’t I want clojure.core
to be loaded each namespace?
Yes, this assumption from the "source code” led me to this issue. But I still wonder why it is like that… reading in-ns
documentation again...
There it its (in the example), I missed that...
;; The "in-ns" function works almost the same as "ns", but does not load
;; clojure.core
So I guess the real culprit is that I should’ve used ns
instead of in-ns
.
Thanks @jswart!
Depends on what you are doing, if you are trying to test out some code then I usually put it into a file and run (load-file “thefile.clj”)
this is assuming I’m not just running a REPL with a little bit of code for fleshing out some idea
in ~5 years i don’t think I’ve ever run (ns…)
at the REPL, but maybe I’m doing it wrong.
to switch to a namespace that exists, I often (require foo)
then (in-ns ‘foo)
to switch to a namespace that does not exist, I would (ns foo)
I usually whole load buffer in cider, but sometimes I just want to run just a function without writing it in a file, now 90% of those get saved in a test anyways...
if you accidentally in-ns
to a new namespace and don’t have core, the easiest way to correct is to (clojure.core/refer-clojure)
(which is all ns
does)
Those are good, I’ve honestly never thought to (ns '…) at the REPL. I usually play in the REPL until the idea is done, grab the .nrepl-history with VIM and make a file.
@alexmiller perfect!
@jswart I learned a lot as well, thanks!
bear in mind that (ns)
does not take symbol unlike (in-ns)
technically both take symbols, but ns
is a macro so the symbol will not be evaluated and thus doesn’t need to be quoted
@roelofw I finished with Clojure-Nginx server setup here is the final ssh code https://github.com/damesek/clojure-nginx-setup-ssh
How do I use clojurebot?
@clojurebot help
@echristopherson /clj <command>
@roelofw Thanks. Where is that documented?
@dpsutton thanks , now I have to find out how to use it on this part :
(re-seq #"[A_Z]" s)
but the code ' (re-seq #"[A_Z]" s) ` needs to be evaluated. The outcome needs to be quoted I think
The code I work on is this one :
(defn filter-capitals
[s]
(re-seq #"[A_Z]" s)
)
(filter-capitals "Roelof Wobben")
why does (first {:a 1 :b 2})
always return [:a 1]
i would expect it to be random as there is no first in hash-maps as i understand?
> Returns the first item in the collection. Calls seq on its > argument. If coll is nil, returns nil.
gotcha. I have this database that the clj-http’s :as :json
functionality interoperates as a hashmap {:1 0.0 :2 10 :3 20}
. I think the database wants to serve me a sorted collection of [timestamp/long value] sorted by timestamp, but clj-http doesn't know its a sorted-map
I’ll probably have to tell it that otherwise ill run into trouble.