Schould I make my own c-in method here


In the brave book there is this example

(def character
  {:name "Smooches McCutes"
   :attributes {:intelligence 10
                :strength 4
                :dexterity 5}})
(def c-int (comp :intelligence :attributes))
(def c-str (comp :strength :attributes))
(def c-dex (comp :dexterity :attributes))

(c-int character)
; => 10

(c-str character)
; => 4

(c-dex character)
; => 5

now I have this challenge

You used (comp :intelligence :attributes) to create a function that returns a character’s intelligence. Create a new function, attr, that you can call like (attr :intelligence) and that does the same thing.

is it now the purpose to make the c-* methods but then without using comp ?

To be honest, this is a pretty poor example. Typically, if you want to access nested maps you would use get-in (and assoc-in and update-in if you need to modify). Using comp here is not idiomatic.

I guess the point of the exercise would be for you to define your own comp or partial (did you see that in previous chapters?) based solution, which is trivial.

The Brave Clojure book is a bit old, and some “best practices” have evolved. In general, you’d try to avoid nesting too deep (this case is ok, but in general, if you have deep nesting, it ends up being painful), and maybe you’d use namespaced keys instead.

oke, the next exercises are to implement conj and one of the last one is to use assoc-in or update-in

yes, I have seen partial in a previous chapter

I was thinking of using get for this one.

so something like (get intelligence(get attributes character) ?

You would use keys instead of symbols there, and you got the arguments in the wrong order, but otherwise that’d work. It’s a bit clumsy, though.

In practice you could just use (get-in character [:attributes :intelligence]), so you could do

(defn c-intelligence [character]
  (get-in character [:attributes :intelligence]))
1 Like


Now time for the next challenges I think

Implement the comp function.
Implement the assoc-in function. Hint: use the assoc function and define its parameters as [m [k & ks] v].
Look up and use the update-in function.
Implement update-in.

all very vague. apperently I have to take out the tutorial what all those functions exactly do

Remember to check for docs on those functions, and examples of how they work. They are all fairly simple to implement (in a naïve way, at least)…

This is a good start :

; Implement the comp function.

(defn my-comp
  ([] identity)
  ([f] f)
  ([f g]
     ([] (f (g)))
     ([x] (f (g x))))))

That’s getting close… :slight_smile:
How would you generalize it to an arbitrary number of functions (and arguments)?

you mean something like [x & args] in the fn
I think I would use apply here

so the right answer is : [x & args] f (apply g x args)

Maybe im better in clojure then I thought

and I must say you the only one who helped me to get better in clojure

and I must say you the only one who helped me to get better in clojure

Nah, that’s all you, for keeping at it. :slight_smile:

?? what do you mean with that is all you.
I have also asked some questions in the clojure slack but there not so many help.

You’re the one putting in the effort, dude, I am just happy to steer you a bit in the right direction.

yep, do I have the code right for a number of arguments ?

You’re missing some parens around the f (apply g...). And the case of using comp over multiple functions (you’ll probably want to use reduce for that

hmm, something like this (reduce comp (f g args)) ?

Well, it’d be better to avoid explicit recursion (you’re defining your own comp, so using the built-in one sounds like cheating :slight_smile: )
Another thing to consider is the arguments you take. comp itself takes just functions, but the function you return should accept any number of arguments, which should be taken by the first function in your composition chain.

If you do get stuck, you can peek at how clojure.core/comp is implemented :slight_smile:

Looked at it and also that one uses recursion

 (reduce1 comp (list* f g fs)))

I only have to look what reduce1 and list* do


again stuck how I have to deal with this :

; Implement update-in.

(def my-update-in [m k f]

I think at some way I have to find the part with the key and then apply the function at the value and put the whole m again together.

Do I thinkin the right way ?