This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-12-13
Channels
- # bangalore-clj (5)
- # beginners (94)
- # boot (145)
- # cljs-dev (4)
- # cljsjs (79)
- # cljsrn (18)
- # clojure (343)
- # clojure-dev (21)
- # clojure-dusseldorf (6)
- # clojure-india (1)
- # clojure-italy (2)
- # clojure-nl (4)
- # clojure-russia (62)
- # clojure-sanfrancisco (1)
- # clojure-spec (109)
- # clojure-taiwan (1)
- # clojure-uk (103)
- # clojurescript (102)
- # code-reviews (30)
- # component (1)
- # cursive (39)
- # datascript (7)
- # datomic (68)
- # emacs (11)
- # figwheel (1)
- # gorilla (1)
- # hoplon (234)
- # off-topic (46)
- # om (52)
- # onyx (32)
- # planck (9)
- # proton (4)
- # protorepl (5)
- # random (1)
- # re-frame (36)
- # ring (18)
- # ring-swagger (1)
- # specter (6)
- # untangled (3)
- # vim (56)
Maybe a stupid beginners question. What is the clojure way to make a comment in a code which explains the challenges I have made. I like to store the solved 4clojure challenges in a file
(defn func-name
"This is the docstring"
[the arguments]
(the function body))
Like that?that is one of the options , @seancorfield
Since the docstring can span multiple lines you can write an essay there 🙂
There are several ways to provide actual comments in code tho'
;; this is a full line comment
(some code) ; this is an end of line comment
(comment 'and (this is any) :code you "want" as a comment)
(I think that last one will work -- I mostly use it to provide code examples in a comment so the contents are always valid Clojure code in my files)
oke, I use now this :
(defn notthing-but-the-truth
"This is a clojure form. Enter a value which will make the form evaluate to true.
Don't over think it! If you are confused, see the getting started page. Hint: true is equal to true."
(= true true)
)
You're missing the argument vector.
@seancorfield is this good clojure :
defn list-conj-1
"When operating on a list, the conj function will return a new list with one or more items \"added\" to the front."
[]
(do (= '(1 2 3 4) (conj '(2 3 4) 1))
(= '(1 2 3 4) (conj '(3 4) 2 1))
))
The issue with ☝️ is that the first check on equality of the list with the result of conj
is discarded by the second.
In other words, you don't know the result of the first test of equality and you only return the result of the last one
If you want both to be true is there a logical operator you can use that checks both things are true?
@agile_geek so I can better use and
because both have to be true
This is a sample from Clojure for the brave and true book,
(defn my-reduce
([f initial coll]
(loop [result initial remaining coll]
(if(empty? remaining)
result
(recur (f result (first remaining)) (rest remaining)))))
([f [head & tail]]
(my-reduce f head tail)))
can someone explain the behaviour of recur here please?
(recur (f result (first remaining)) (rest remaining)))))
is it calling the loop or function?
Also what is the point of using
f
here? f
is just being passed around.recur
essentially invokes the next highest target with the given arguments. In this case, it will be the loop
form, with (f result (first remaining))
in the result
binding and (rest remaining)
in the remaining
binding.
f
is called with result (first remaining)
in this evaluation
Pretty sure valid recur targets are just loop
and fn
s fwiw
e.g.
(defn myfun
[param1 {:keys [key1 key2 key3]}]
(....body 1)
[param1 {:keys [key1 key2]}]
(…body2)
(some nil? [key1 key2 key3])
To check if keys exist(defn myfun
[param1 {:keys [key1 key2 key3] :as param2}]
(if (contains? param2 :key2)
(body1)
(body2)))
@kyle_schmidt Problem with checking for nil?
is that there's a difference between:
{:a 10 :is-foo? nil}
and {:a 10}
@dominicm Is there a problem doing this:
(let [{:keys [a is-foo?]} {:a 10 :is-foo? nil}]
(some nil? [a is-foo?]))
Because this gives the same result:
(let [{:keys [a is-foo?]} {:a 10}]
(some nil? [a is-foo?]))
@kyle_schmidt Yep, sometimes the fact that those input maps are different is important.
@kyle_schmidt Also, I think you want every?
not some
there.
@dominicm do you usually check the values that you parse from a map? Is it idiomatic to do so or do you operate assuming the keys are there? Or does it depend?
@gregnwosu I probably wouldn't throw core.match at this problem. Options: 1. defmulti with (comp set keys)
as dispatcher function. 2. Destruct and use a sentinel at :or
: {:keys [a] :or {a ::sentinel}}
(fn [{:keys [a] :or {a ::sentinel}}]
(if (= a ::sentinal)
(a-was-not-provided)
(a-was-provided)))
@kyle_schmidt Most of the time, if I can possibly be given nil
, I handle it using when
for example. But mostly, it depends as you say.
@rauh I would, personally, consider contains?
more idiomatic. Why do you prefer sentinel?
Well I'd probably use some name that indicates what this options does, so kind of more self-documenting code
How can I rewrite this solution to a 4clojure challenge better :
(defn double-down
"Write a function which doubles a number"
[]
(and (= ((fn [x] ( * x 2)) 2) 4)
(= ((fn [x] ( * x 2)) 3) 6)
(= ((fn [x] ( * x 2)) 11) 22)
(= ((fn [x] ( * x 2)) 7) 14)))
@roelofw you probably want to store it some other place than 4clojure itself but if you are you logged into 4clojure you can click the solutions button and the answer you've provided is displayed as the first result
@dhruv1 are you using a particular React wrapper? (Reagent, Om, Rum, ...)
i will move onto using warppers once I understand how to get started without the wrappers
@dhruv1 well, you can use JS interop directly
i.e (js/React.createClass #js {:render (fn [] (this-as this ...))})
My advice: use interop as long as necessary to get it, then have a look at sablono: https://github.com/r0man/sablono