29

There is this very handy function in Clojure, which is called format. It's known for it's ability to easily template strings. The function is commonly available in most of the programming languages.

I was a bit irritated to discover that ClojureScript does not implement this function. As far as I could research it was implemented in older versions but the latest one doesn't contain the function.

Anyone knows if there is a reason for this?

Aleš Roubíček
  • 5,198
  • 27
  • 29
Anton Harald
  • 5,772
  • 4
  • 27
  • 61
  • 2
    I would have thought that `clojure.pprint/cl-format` would be available in Clojurescript. It's an alternative to Clojure's Java-based `format`. In a quick test I did, `clojure.pprint` did not seem to be available in Clojurescript, but I don't know why it wouldn't be. – Mars Jan 08 '16 at 05:52
  • 2
    If i remember correctly, cl-format is in cljs.pprint namespace in clojurescript – leetwinski Jan 08 '16 at 09:57

2 Answers2

31

format exists in ClojureScript. It comes from the Google Closure Library (GCL), which is a fundamental part of ClojureScript. Unfortunately it can be tricky to use it. The conventional way is to require both [goog.string :as gstring] and [goog.string.format], and then to use gstring.format. For example:

(ns rostering.components.services
  (:require 
    [goog.string :as gstring]
    [goog.string.format]))

(str "$" (gstring/format "%.2f" 2.5))

Pretty much the same example is at the bottom of this short page of documentation.

I can't say enough how much a part of ClojureScript is the GCL. Here is another reference. This means that format is a function that is part of ClojureScript.

Here's a quote from that reference:

The Google Closure Library is a javascript library developed by Google, based on a modular architecture and provides cross-browser functions for DOM manipulations and events, ajax and JSON, among other features.

It’s written specifically to take advantage of the Closure Compiler (that is used internally by the ClojureScript compiler).

And ClojureScript is built on Closure Compiler and Closure Library. In fact, ClojureScript namespaces are Closure modules.

Chris Murphy
  • 6,411
  • 1
  • 24
  • 42
  • 1
    Is there a reason your last line isn't just `(gstring/format "$%.2f" 2.5)`? – gfredericks Jan 08 '16 at 16:11
  • The first argument to the format function is the 'format String'. The `$` is not part of the 'format String', unlike the `%`. – Chris Murphy Jan 08 '16 at 16:43
  • 2
    why can't it be part of the format string? that just seems like a simpler way to do it. – gfredericks Jan 08 '16 at 17:06
  • 2
    Quite possibly it can be a part of the format String. However it would make me feel quite uncomfortable to use any function beyond its clearly defined limits. In fact testing any hardware or software beyond its purpose is asking for trouble, and causes maintenance problems. For instance here I could envisage someone looking up the documentation to see if `$` is in fact a formatting character, which in fact it might be for all I know! – Chris Murphy Jan 08 '16 at 18:55
  • 4
    I think the convention for all format functions in this heritage (the docs for `gstring/format` directly reference [printf](http://www.cplusplus.com/reference/cstdio/printf/)) is that all format specifiers begin with `%`, and so any other character is fine. They would be a lot less useful if you couldn't count on this. As another example the docs for [java.util.Formatter](https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html) (which underlies `clojure.core/format`) give this example with a dollar sign: `"Amount gained or lost since last statement: $ %(,.2f"`. – gfredericks Jan 08 '16 at 19:04
  • You are right of course. Unfortunately this discussion and the question itself share the same problem - no specific problem to solve - just discussing things for the sake of it. If the questioner had wanted some feature of `format` that was not implemented by `gstring/format` then discussion/answers would have been a bit more 'grounded'. – Chris Murphy Jan 15 '16 at 21:47
  • Okay; I was just trying to help make your example clearer for people who are already familiar with format functions. – gfredericks Jan 16 '16 at 14:32
20

This comment on a related jira ticket might be helpful:

Backing this one out, goog.string.format defies advanced optimization and it provides few of the capabilities of Clojure's format - which does a lot because of java.util.Formatter. Apologies for the churn, but this is a simple thing for people to stub in themselves for the little bit of functionality it actually delivers.

gfredericks
  • 1,312
  • 6
  • 11