0

I am newbie in Clojure. I have following Expressions:

(= (__ "Dave") "Hello, Dave!")

(= (__ "Jenn") "Hello, Jenn!")

(= (__ "Rhea") "Hello, Rhea!")

In place of __, in all 3 places must be inserted same expression so that the equality check in all 3 cases are true. At this point i have come up with str "Hello, ". As i understand this should produce "Hello, Dave" "Hello, Jenn" "Hello, Rhea" How do i put "!" mark at the and of each string? (I can only "write" expression in place of __)

Thank you

5
  • 2
    Not so interesting answer is (= (format "Hello, %s!" "Dave") "Hello, Dave!"). but I don't know this is expected one. Commented Sep 13, 2019 at 6:44
  • @ymonad Thank you for the reply. I will have look at function format. But it certainly returns expected values Commented Sep 13, 2019 at 6:48
  • (= (str "Hello, " "Dave" "!") "Hello, Dave!") will work just fine as well :) Commented Sep 13, 2019 at 7:27
  • 1
    @JonasJohansson It certainly would work but only for the first problem (Dave) for others (Jenn, Rhea) it would not work. The idea is that it should work for all three Commented Sep 13, 2019 at 7:35
  • 1
    I see. Yeah, then make it a funcion like (fn [name] (str "Hello, " name "!")) or similar ;) But I see that there is a perfectly good answer below now. Happy Clojuring! Commented Sep 13, 2019 at 7:37

1 Answer 1

5

You want to drop a function into the place of __.

This function shall take a string s and shall return a string that is based on s so as to satisfy the three test cases.

A possible function is

(fn [s] (str "Hello, " s "!"))

which can written using Clojure syntactic sugar

#(str "Hello, " % "!"))

Thus

(= (#(str "Hello, " % "!") "Dave") "Hello, Dave!")

Bonus: Using testing framework

Clojure comes with a nice testing library, clojure.test (I don't know why it is called an API, which would mean there is a component on the other side of the callable functions; it's just a library)

We can use the testing library for good effect:

(require '[clojure.test :as t]) ; make library visible

(def myfun (fn [s] (str "Hello, " s "!"))) ; our function as symbol myfun

(t/deftest test-stringmanip
   (t/is (= (myfun "Dave") "Hello, Dave!"))
   (t/is (= (myfun "Jenn") "Hello, Jenn!"))
   (t/is (= (myfun "Rhea") "Hello, Rhea!")))

(t/run-tests) ; do it!
Sign up to request clarification or add additional context in comments.

1 Comment

API stands for Application Programming Interface. It is the interface you are meant to program against when using a component, such as a testing library. There's no implication that it's a client/server protocol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.