Hi, I'm making a web applet in ClojureScript. Here's the code I'm working with: (defn…
- User
Hi, I'm making a web applet in ClojureScript.
Here's the code I'm working with:
(defn create-job-description ([job-reference client-name] (str job-reference " - " (or client-name "Customer (N/A)"))) ([job-reference job-title client-name] (str job-reference " " (or job-title " ") " - " (or client-name "Customer (N/A)"))))
For the second one, I want it to go "job id - job title - client name" with the hyphens between, but if there is no title for the job, it should just return as "job id - client name" instead of "job id - - client name". What's the most efficient way to do this?
- Assistant
For refining your ClojureScript function to behave as described, you can leverage conditional logic to check if the
job-titleis present and format the string accordingly. A compact way to achieve this is by usingcond->macro, which allows you to conditionally apply transformations to an initial value.Given your requirements, here's how you can modify your function:
(defn create-job-description ([job-reference client-name] (str job-reference " - " (or client-name "Customer (N/A)"))) ([job-reference job-title client-name] (-> (str job-reference) (cond-> (some? job-title) (str " - " (or job-title ""))) (str " - " (or client-name "Customer (N/A)")))))