This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-05-11
Channels
- # admin-announcements (10)
- # arachne (2)
- # beginners (74)
- # boot (302)
- # cider (49)
- # cljs-dev (11)
- # cljs-edn (7)
- # cljsjs (13)
- # cljsrn (1)
- # clojure (164)
- # clojure-austin (1)
- # clojure-brasil (3)
- # clojure-finland (2)
- # clojure-greece (4)
- # clojure-russia (48)
- # clojure-uk (11)
- # clojurescript (138)
- # community-development (1)
- # cursive (13)
- # datascript (1)
- # datomic (19)
- # emacs (4)
- # events (1)
- # garden (1)
- # hoplon (123)
- # jobs-discuss (9)
- # keechma (5)
- # lein-figwheel (4)
- # leiningen (2)
- # luminus (15)
- # mount (1)
- # off-topic (8)
- # om (66)
- # on (1)
- # onyx (28)
- # other-languages (2)
- # planck (1)
- # proton (5)
- # re-frame (18)
- # reagent (15)
- # untangled (15)
if i have a set of things i'm running down, are nested when-let
forms ok?
(when-let [a (something-could-be-nil)]
(when-let [b (other-could-be-nil a)]
(when-let [c (third-could-be-nil b)]
c)))
You might prefer the look of (some-> (something-could-be-nil) other-could-be-nil third-could-be-nil))
avoids the need to name the intermediate results
Hey, I am trying to define a local function in a let but issue is the function keeps running when the let is evaluated instead of when the function in the let is evaluated. What can I do about this? I just wish to bind this function and call it later in the function and have it only run when called.
@urbanslug: how does your code look?
(defn bro-life []
(let [print-msg (println “cat”)]
(println “Will run but print-msg will not")
(print-msg)))
the problem is that (println "cat")
is a function call to println
instead of being an anonymous function
the easiest is to use the shorthand syntax for anonymous functions, just put a #
in front
in your original code what happens is that (println "cat")
gets evaluated, and println
returns nil
, so now print-msg
is equal to nil
so then calling (print-msg)
is the same as calling (nil)
, which will give you a NullPointerException
thanks @donaldball !
I don’t know the exact reason, but when you give a function f
to map to a coll, refrain from using the anonymous shortcut.
So:
* use (fn [i] {:date … instead
* the get-in
s are un-necessary, you can use (:period x) instead
(defn map-invoices [invoices]
(map (fn[i] { :date (:period-end i)
:amount (str "$" (:subtotal i))
:status (if (:paid i) "Paid" "Unpaid")}) invoices))
=> xxx/map-invoices
(map-invoices [{:period-end "pe" :amount 123 :paid true}])
=> ({:date "pe", :amount "$", :status "Paid”})
as st said by using the #(…)
reader literal you putting your hashmap in the function call position b/c hashmaps can use IFn
to be used like functions
yeah when you use #(…)
and you want to return data you have to use the non reader literal so ((fn [x] [x 1 2 3]) 7) => [7 1 2 3]
becomes (#(vector % 1 2 3) 7) => [7 1 2 3]
I would have made the data munging form and then used partial to make map-invoices
like (def map-invoices (partial map transform-fn))
What you pasted in is the code that handles when the recursive calls should terminate, it feels a bit odd though. Because if you have a collection w/ a count > 1 how will you know to stop?
in general recursive solutions are “divide and conquer”. Where the solution to a problem usually follows the formula If SOME_CONDITION stop else the solution is SOMETHING and a recursive call
Each recursive call does a little bit more of the work, and when you reach the termination clause you start combining the operations on the stack until you get a value.
(defn my-add-coll [coll]
(if (empty? coll)
0
(+ (first coll) (my-add-coll (rest coll)))))
One HUGE caveat is that once you have an intuitive understanding of recursion, and how to write something recursive in clojure you should read up on loop
, recur
and reduce
those are special forms / functions.
There is a “gotcha” to recursion on the JVM when you start writing production code, but for now you are probably okay.
I tried to learn clojure and failed b/c I had no functional/CS experience. I did SICP over about 6 months and then came back to clojure.
is ^long
metadata? what is that useful for?
@jswart: ah, thank you