<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged feature</title>
<link>https://ask.clojure.org/index.php/tag/feature</link>
<description></description>
<item>
<title>bit-count in cljs but not clj</title>
<link>https://ask.clojure.org/index.php/14917/bit-count-in-cljs-but-not-clj</link>
<description>&lt;p&gt;The &lt;code&gt;bit-count&lt;/code&gt; function ships as part of CLJS but not CLJ, which means I end up using a reader conditional to use &lt;code&gt;Long/bitCount&lt;/code&gt; on the JVM side when I write cljc files. It would be nice if I could just use &lt;code&gt;bit-count&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;(I chose &quot;compiler&quot; for category because there's no option for &quot;standard library&quot;) &lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14917/bit-count-in-cljs-but-not-clj</guid>
<pubDate>Fri, 30 Jan 2026 09:21:40 +0000</pubDate>
</item>
<item>
<title>Using namespace as log originating class for JUL</title>
<link>https://ask.clojure.org/index.php/13427/using-namespace-as-log-originating-class-for-jul</link>
<description>&lt;p&gt;When using tools.logging and java.util.logging (JUL in short), by default we get logs prefixed like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Oct 23, 2023 2:14:47 PM clojure.tools.logging$eval3668$fn__3671 invoke
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice that class and method name logged are something completely unhelpful.&lt;/p&gt;
&lt;p&gt;The solution recommended by the tools.logging library is that the user should change log appender output format to not print out class and method and to print out the logger name, which is the Clojure namespace name.&lt;/p&gt;
&lt;p&gt;That is a possible solution when the authors of the app doing the logging and the author of code that configures JUL are the same person, i.e. the logging is configured and done at app level.&lt;/p&gt;
&lt;h3&gt;Use Case&lt;/h3&gt;
&lt;p&gt;I would say that the far more common use case of tools.logging is the use by a library author, who doesn't know which logging solution the end user is using. And at the same time, the end user is unaware of this logging being present in their dependency tree. In this case someone builds a Clojure app and they might have a 100 dependencies. Suddenly they start getting messages as described above in their stderr. &lt;/p&gt;
&lt;p&gt;They might want to:&lt;br&gt;
- redirect that to a file&lt;br&gt;
- suppress the messages&lt;br&gt;
- want to know which dependency is generating them&lt;/p&gt;
&lt;p&gt;All of these tasks require that the user recognizes that it's JUL logging they are looking at and then configuring the JUL to output logger name to find out the namespace which is doing the logging. &lt;strong&gt;They might also want to keep the format with the method name for any Java code they have.&lt;/strong&gt; &lt;/p&gt;
&lt;h3&gt;Approach&lt;/h3&gt;
&lt;p&gt;Use LogRecord class to force classname to Clojure namespace. Example code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(cond-&amp;gt; (doto (LogRecord. level# ~msg)
                      (.setLoggerName ~ns)
                      (.setMessage ~msg)
                      (.setParameters (object-array ~params))
                      (.setSourceClassName ~ns))
              ~e (doto (.setThrown ~e)))
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Benefits&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Replaces  &lt;code&gt;clojure.tools.logging$eval3668$fn__3671 invoke&lt;/code&gt; in output with &lt;code&gt;my.lib.namespace&lt;/code&gt;. The former string has 0 informational value, the latter enables us to know where to look for the log statement performed.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Improves performance. If &lt;code&gt;.setSourceClassName&lt;/code&gt; is called, then the logging system won't use reflection to determine caller class and method. That involves walking the stack trace and it's slow.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Even with default/no configuration of JUL it gives a hint about where to look for who is making all these messages in stderr, if user is not aware of this logging library/mechanism.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I can produce a PR to affect this change if needed.&lt;/p&gt;
</description>
<category>tools.logging</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13427/using-namespace-as-log-originating-class-for-jul</guid>
<pubDate>Sun, 05 Nov 2023 14:03:39 +0000</pubDate>
</item>
<item>
<title>alarm function</title>
<link>https://ask.clojure.org/index.php/10901/alarm-function</link>
<description>&lt;h2&gt;&lt;strong&gt;Alarm Function&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Simple description:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The alarm function is a feature, that call a function every period of time .&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;strong&gt;Use:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn function
  &quot;This function will be called every 1 minute 10 times&quot;
  []
  (println &quot;Hello, world!&quot;))

 (defn -main 
  &quot;&quot;
  [&amp;amp; args]
  (alarm 1 10 function))
&lt;/code&gt;&lt;/pre&gt;
&lt;hr&gt;
&lt;p&gt;&lt;strong&gt;Simple use case:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When a user create account and not confirmed email.  through the alarm verify if the user really not confirmed&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10901/alarm-function</guid>
<pubDate>Thu, 05 Aug 2021 02:02:35 +0000</pubDate>
</item>
<item>
<title>Conform function Specs at read time similar to macros?</title>
<link>https://ask.clojure.org/index.php/9876/conform-function-specs-at-read-time-similar-to-macros</link>
<description>&lt;p&gt;I think there would be value in having Spec conform the literal code passed to functions the same way they do for macros. Maybe &lt;code&gt;fdef&lt;/code&gt; could take a &lt;code&gt;:syntax&lt;/code&gt; spec as well which would be used for that.&lt;/p&gt;
&lt;p&gt;There are quite a few functions I think this could be useful for. A lot of functions will take options for example as keywords, those are most likely going to be provided as literals, so conforming their usage for function calls at read time would be valuable.&lt;/p&gt;
&lt;p&gt;Other examples where this could be useful are say even for simple usages like &lt;code&gt;+&lt;/code&gt;, where the args often contain literal numbers as well, where again the spec could validate that say a keyword or string literal isn't passed to it at read time as a syntax error.&lt;/p&gt;
&lt;p&gt;In effect, I find a lot of Clojure functions themselves have function specific syntax, and it would be great if Spec could be used to conform their syntax at read time the same way it lets us do so for macros.&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9876/conform-function-specs-at-read-time-similar-to-macros</guid>
<pubDate>Thu, 26 Nov 2020 21:57:39 +0000</pubDate>
</item>
<item>
<title>Add limit third argument of split to split-lines</title>
<link>https://ask.clojure.org/index.php/9325/add-limit-third-argument-of-split-to-split-lines</link>
<description>&lt;p&gt;The signature for &lt;code&gt;clojure.string&lt;/code&gt; function &lt;code&gt;split-lines&lt;/code&gt; is:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(split-lines s)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the signature for &lt;code&gt;split&lt;/code&gt; is:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(split s re)
(split s re limit)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I think it would be nice to add this overload that accepts a &lt;code&gt;limit&lt;/code&gt; to &lt;code&gt;split-lines&lt;/code&gt; as well, just as it is for &lt;code&gt;split&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(split-lines s limit)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My use case is often to split on lines but not to ignore trailing newlines. This requires passing &lt;code&gt;-1&lt;/code&gt; to limit. In those cases, I always need to switch back to using &lt;code&gt;split&lt;/code&gt; and re-implement &lt;code&gt;split-lines&lt;/code&gt;.&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9325/add-limit-third-argument-of-split-to-split-lines</guid>
<pubDate>Sun, 24 May 2020 20:09:51 +0000</pubDate>
</item>
<item>
<title>How hard is it for Clojure to have readable closure?</title>
<link>https://ask.clojure.org/index.php/9317/how-hard-is-it-for-clojure-to-have-readable-closure</link>
<description>&lt;p&gt;The language's name is obviously derived from &quot;closure&quot;, yet &lt;a rel=&quot;nofollow&quot; href=&quot;https://nullprogram.com/blog/2013/12/30/&quot;&gt;readable closures&lt;/a&gt;  seem to be Elisp unique feature.&lt;br&gt;
How hard is it for Clojure to have such ability? Of course many types can't have their readable stringified form, but what about closures containing the ones that can?&lt;/p&gt;
</description>
<category>Syntax and reader</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9317/how-hard-is-it-for-clojure-to-have-readable-closure</guid>
<pubDate>Fri, 22 May 2020 12:52:48 +0000</pubDate>
</item>
<item>
<title>Is it possible to parse positional arguments in tools.cli?</title>
<link>https://ask.clojure.org/index.php/9217/is-it-possible-to-parse-positional-arguments-in-tools-cli</link>
<description>&lt;p&gt;It's rather a rhetorical question as I've come to understand that tools.cli is not capable of doing anything with positional arguments but passing them on to the consumer of the library as strings in a vector. But I'll share my usecase.&lt;/p&gt;
&lt;p&gt;I'm writing a tool that copies files from one directory to another. It takes two positional arguments, the source and destination. Right now my code contains two passes of input verification and parsing, first one for options with tools.cli and then a second one for arguments in my own function &lt;code&gt;parse-arguments&lt;/code&gt;. It would be neat if these two things could both be done with in one step.&lt;/p&gt;
&lt;p&gt;See the below code for a usage example.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns multi-machine-rsync.cli
  (:require [clojure.tools.cli :as cli]
            [clojure.java.io   :as io]
            [clojure.string    :as string]))

(defn read-settings
  [path]
  (read-string (slurp path)))

(def cli-options
  [[&quot;-h&quot; &quot;--help&quot; &quot;Show this help text&quot;]
   [&quot;-s&quot; &quot;--settings SETTING-FILE&quot; &quot;Path to the settings file.&quot;
    :parse-fn read-settings
    :default  &quot;settings.edn&quot;]])

(defn usage
  [options-summary]
  (string/join
   \newline
   [&quot;Copies a directory from one place to another in a common&quot;
    &quot;directory structure using multiple machines.&quot;
    &quot;&quot;
    &quot;Usage: multi-machine-rsync [OPTIONS] SOURCE DESTINATION&quot;
    &quot;&quot;
    &quot;Options:&quot;
    options-summary]))

(defn parse-arguments
  &quot;Parse and validate command line arguments. Either return a map                                                                                         
  indicating the program should exit (with a error message, and                                                                                           
  optional ok status), or a map indicating the action the program                                                                                         
  should take and the options provided.&quot;
  [args]
  (let [{:keys [arguments options errors summary]} (cli/parse-opts args cli-options)
  	[source destination]                       (map io/file arguments)]
    (cond
      (:help options)              {:exit-message (usage summary) :ok? true}
      errors                       {:exit-message errors}
      (nil? source)                {:exit-message &quot;First positional argument SOURCE not given.&quot;}
      (nil? destination)           {:exit-message &quot;Second positional argument DESTINATION not given.&quot;}
      (not (.exists source))       {:exit-message &quot;Source directory does not exist.&quot;}
      (not (.exists destination))  {:exit-message &quot;Destination directory does not exist.&quot;}
      :else                        {:options      options
                                    :source       source
                                    :destination  destination})))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>tools.cli</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9217/is-it-possible-to-parse-positional-arguments-in-tools-cli</guid>
<pubDate>Tue, 07 Apr 2020 08:06:38 +0000</pubDate>
</item>
<item>
<title>Would be nice if tools.namespace.find functions return file info in ns-decl meta</title>
<link>https://ask.clojure.org/index.php/8826/would-nice-tools-namespace-find-functions-return-file-info</link>
<description>&lt;p&gt;Currently calling &lt;code&gt;tools.namespace.find/find-ns-decls&lt;/code&gt; returns a seq of ns-decl for a given dir or collection of paths but there is no way to know where did they came from (can also be coming from paths inside jar files).&lt;/p&gt;
&lt;p&gt;Looks pretty straight forward to add file info as meta of the ns-decl form. Something like &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/jpmonettas/tools.namespace/commit/1431215324ba7f76f78342742ce3716ec8c5abb8&quot;&gt;https://github.com/jpmonettas/tools.namespace/commit/1431215324ba7f76f78342742ce3716ec8c5abb8&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;What do you guys think?&lt;/p&gt;
&lt;p&gt;(Filed jira at &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.atlassian.net/browse/TNS-55&quot;&gt;https://clojure.atlassian.net/browse/TNS-55&lt;/a&gt;)&lt;/p&gt;
</description>
<category>tools.namespace</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8826/would-nice-tools-namespace-find-functions-return-file-info</guid>
<pubDate>Tue, 05 Nov 2019 13:35:01 +0000</pubDate>
</item>
<item>
<title>Add text block literal, or raw string literal, or unsescaped string literal.</title>
<link>https://ask.clojure.org/index.php/8520/text-block-literal-string-literal-unsescaped-string-literal</link>
<description>&lt;h2&gt;Problem:&lt;/h2&gt;
&lt;p&gt;Having had to include some JavaScript, XML and HTML inside of my Clojure code here and there, it can be pretty annoying and error prone to have to escape quotes. This holds true as well when scripting, and running shell command, you can get into hairy escaping scenarios.&lt;/p&gt;
&lt;h2&gt;Solution:&lt;/h2&gt;
&lt;p&gt;Add a string literal which can be adapted to contain any sort of string without the need for escaping.&lt;/p&gt;
&lt;h2&gt;Suggestions:&lt;/h2&gt;
&lt;h3&gt;Text blocks&lt;/h3&gt;
&lt;p&gt;Some other languages offer something called a text block where you can write a string using triple or more quotes, where all characters are then allowed:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(println &quot;&quot;&quot;
         This &quot; is allowed,
         and no need to escape it.
         &quot;&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Text blocks often come with additional features, such that the first and last newline isn't part of the string. And the position of the triple quote in the source code delineate the beginning of the lines in the quote. Thus the above code prints:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;This &quot; is allowed,
and no need to escape it.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and not:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;         This &quot; is allowed,
         and no need to escape it.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While text blocks are neat visually, as they have nice alignment in the source code. They are whitespace dependent, and Clojure up to now is a whitespace independent language, meaning whitespace does not matter. I think it would be best to keep it that way. Thus the next two suggestions.&lt;/p&gt;
&lt;h3&gt;Raw strings&lt;/h3&gt;
&lt;p&gt;Sometimes the text block without the &quot;block&quot; features is known as a raw string literal:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(println &quot;&quot;&quot;This &quot; is allowed,
and no need to escape it.
Also support multi-line, but
not the &quot;block&quot; style of text blocks.&quot;&quot;&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thus:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(println &quot;&quot;&quot;
         This &quot; is allowed,
         and no need to escape it.
         &quot;&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Prints:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;         This &quot; is allowed,
         and no need to escape it.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Unlike for Text Blocks.&lt;/p&gt;
&lt;p&gt;If you need a triple quote, just make the delimiter a quadruple quote:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;&quot;&quot;&quot;This &quot;&quot;&quot; is now allowed as well.&quot;&quot;&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The issue with raw string is that, if you use say double quotes as your delimiter:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;&quot;This is a raw &quot; string!&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But want your single quote to be at the beginning or the end:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;&quot;&quot;{{hello}}&quot;&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I want the string: &lt;code&gt;&quot;{{hello}}&quot;&lt;/code&gt;, not &lt;code&gt;{{hello}}&lt;/code&gt;, but the raw string can not disambiguate the two, as now it thinks this is a triple quoted delimiter.&lt;/p&gt;
&lt;p&gt;One solution is to allow an escaped quote only at the beginning or end:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;&quot;\&quot;{{hello}}\&quot;&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But not in the middle:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;&quot;\&quot;{{he\llo}}\&quot;&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is the string: &lt;code&gt;&quot;{{he\llo}}&quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;So the escape character &lt;code&gt;\&lt;/code&gt; can appear anywhere except at the beginning if followed by a quote, and at the end if followed by a quote.&lt;/p&gt;
&lt;p&gt;I still don't find this ideal. There's too many rules, and there are still cases where an escape is required.&lt;/p&gt;
&lt;h3&gt;Unescaped string (my favorite)&lt;/h3&gt;
&lt;p&gt;The idea here is to allow any string to be used as the delimiter. Thus given whatever possible string we want to nest inside our Clojure code, we can always find a string which is not contained in it to use as our delimiter.&lt;/p&gt;
&lt;p&gt;Lets say the reader macro #text is added. Which expects the following form to be a regular string which tells it the delimiter for the following form to read:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(println #text &quot;|&quot; |&quot;{{hello}}&quot;|)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Would print:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;{{hello}}&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first arg to #text tells it what the delimiter for the following raw string should be. That way, you absolutely never need an escape sequence inside the raw string. For any given string, you can find a delimiter string not contained in it to handle it properly.&lt;/p&gt;
&lt;p&gt;A crazy thought I had with this approach, just trowing it out there, is if you use a sufficiently random string as the delimiter, could be a weird way to protect against forms of injection:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(println #text &quot;xIBgdSl4TCCOIdqdMu9G&quot; xIBgdSl4TCCOIdqdMu9G
Can't nobody guess the delimiter to escape the string context :p
xIBgdSl4TCCOIdqdMu9G)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thank You&lt;/p&gt;
</description>
<category>Syntax and reader</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8520/text-block-literal-string-literal-unsescaped-string-literal</guid>
<pubDate>Tue, 03 Sep 2019 22:46:52 +0000</pubDate>
</item>
<item>
<title>Add digit separators support to number literals.</title>
<link>https://ask.clojure.org/index.php/8511/add-digit-separators-support-to-number-literals</link>
<description>&lt;p&gt;Just saw as part of Go 1.13 they added:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Digit separators: The digits of any number literal may now be separated (grouped) using underscores, such as in 1_000_000, 0b_1010_0110, or 3.1415_9265. An underscore may appear between any two digits or the literal prefix and the first digit.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And I thought that's a pretty neat feature. When writing big numbers, such as 92347683 it can be hard to read and parse mentally if this is million, billion, etc. Having a syntax for separation I think would be a nice addition to the reader. Doesn't have to be underscores, and might need some hammock time when considering all the valid Clojure number literal.&lt;/p&gt;
&lt;p&gt;Thank You&lt;/p&gt;
</description>
<category>Syntax and reader</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8511/add-digit-separators-support-to-number-literals</guid>
<pubDate>Tue, 03 Sep 2019 19:40:15 +0000</pubDate>
</item>
<item>
<title>Add ability to use ~ or reference user.home inside tools.deps local/root path</title>
<link>https://ask.clojure.org/index.php/8494/ability-reference-user-home-inside-tools-deps-local-root-path</link>
<description>&lt;p&gt;When using local/root provider in tools.deps, it would be nice if you could reference your user.home. I share a user level config between machines which have different home location, but my dependencies are all relative to it.&lt;/p&gt;
&lt;p&gt;Specifically, this is with my use of REBL, I have the Jar in my home dir, but different home dirs on different of my computers, and would like to not have to customize the deps.edn for each one.&lt;/p&gt;
&lt;p&gt;Thank You.&lt;/p&gt;
</description>
<category>tools.deps</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8494/ability-reference-user-home-inside-tools-deps-local-root-path</guid>
<pubDate>Fri, 30 Aug 2019 07:39:47 +0000</pubDate>
</item>
</channel>
</rss>