Fork me on GitHub
#beginners
<
2016-03-31
>
adamkowalski00:03:37

how would I get the full namespace qualified name of a function?

adamkowalski00:03:10

for clojure tools namespace they demonstrate a refresh function

(refresh :after ‘dev/start))
but they know the name of the namespace the function comes from. what if you would like to generalize it so you can use arbitrary functions and not have to worry about where it came from

jeffh-fp00:03:45

my copy of Clojure for the Brave and True arrived today bravetrue thx @nonrecursive

mkaschenko01:03:49

I'm reading Clojure for the Brave and True and I'm confused with the first 4 lines of code:

(defn recursive-printer
  ([]
     (recursive-printer 0))
  ([iteration]
     (println iteration)
     (if (> iteration 3)
       (println "Goodbye!")
       (recursive-printer (inc iteration)))))
(recursive-printer)
; => Iteration 0
; => Iteration 1
; => Iteration 2
; => Iteration 3
; => Iteration 4
; => Goodbye!
http://www.braveclojure.com/do-things/#loop Could someone explain them?

jeffh-fp01:03:00

[] says what to do when no argument is passed to the function (it calls itself, this time with zero as the argument), and [iteration] is what to do when an argument is passed. That help?

jeffh-fp01:03:27

line 4 println is a typo and should read (println (str “Iteration “ iteration))

mkaschenko01:03:15

@jeffh-fp: Oh, I see. It's destructuring here, right?

adamkowalski01:03:32

@mkaschenko: clojure allows you to have multi arity functions

mkaschenko01:03:59

My bad, thank you guys.

adamkowalski01:03:34

if you want a function that returns 0 if no arguments were passed and 5 if one argument was passed you could right this

(defn my-func
  ([] 0)
  ([anything-other-than-nothing] 5))

mkaschenko02:03:31

This is arity overloading, not destructuring.

adamkowalski02:03:54

and a function can call itself, just provide more/less arguments as shown in the example above.

adamkowalski02:03:37

this is really useful when you want to expose a nice easy to use function to others, but behind the scenes you might need an accumulator or some other thing which is irrelevant to the consumer of the function

mkaschenko02:03:17

It's clear know.