<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged function</title>
<link>https://ask.clojure.org/index.php/tag/function</link>
<description></description>
<item>
<title>Evaluating forms using eval can create a valid undesirable recursion point</title>
<link>https://ask.clojure.org/index.php/14991/evaluating-forms-using-create-valid-undesirable-recursion</link>
<description>&lt;p&gt;When the &lt;code&gt;eval&lt;/code&gt; function in Clojure is called with an argument that is an instance of &lt;code&gt;IPersistentCollection&lt;/code&gt; which likely isn't a special def-like form (the first element isn't a symbol at all or it does not start with &lt;code&gt;&quot;def&quot;&lt;/code&gt;), the form is first wrapped in an anonymous function which then gets compiled and invoked, which indirectly evaluates the original form.&lt;/p&gt;
&lt;p&gt;There aren't any extra checks performed and it makes forms like these 2 valid, which should normally be rejected and a compiler exception should be thrown:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(recur)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(let []
  (print &quot;Hello&quot;)
  (Thread/sleep 1000)
  (recur))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where the 2nd form keeps printing &quot;Hello&quot; and loops infinitely over the outer wrapper function.&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14991/evaluating-forms-using-create-valid-undesirable-recursion</guid>
<pubDate>Mon, 16 Mar 2026 23:17:54 +0000</pubDate>
</item>
<item>
<title>Somewhat confusing sorted-set behavior</title>
<link>https://ask.clojure.org/index.php/11409/somewhat-confusing-sorted-set-behavior</link>
<description>&lt;p&gt;Just ran into some interesting behavior with &lt;code&gt;sorted-set&lt;/code&gt;. Namely, the contract to the user for a normal &lt;code&gt;set&lt;/code&gt; is &lt;code&gt;nil&lt;/code&gt; when the element doesn't exist. However, with &lt;code&gt;sorted-set&lt;/code&gt;, the call will throw if the comparator doesn't gracefully handle the type. The &lt;em&gt;reason&lt;/em&gt; makes sense (the comparator requires the ability to sanely compare elements), but the interface is somewhat confusing in the &quot;no membership&quot; case since we know that the element doesn't exist in the set if its type can't be compared by default. &lt;/p&gt;
&lt;p&gt;In other words, I guess I'm proposing that a sane default behavior for a sorted-set using the default comparator should be &lt;code&gt;nil&lt;/code&gt; rather than &lt;code&gt;throw&lt;/code&gt; when the type cannot be compared, in order to allow &lt;code&gt;nil&lt;/code&gt;-punning. &lt;/p&gt;
&lt;p&gt;As an example, this case was encountered when the form was passed to a keyword function &lt;code&gt;(:foobar form)&lt;/code&gt;, which allows nil-punning on a lot of other collections. Here's a small snippet:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;`&lt;/code&gt;&lt;br&gt;
user&amp;gt; (def normal #{&quot;0000&quot;})&lt;/p&gt;
&lt;p&gt;;; =&amp;gt; #'user/normal&lt;/p&gt;
&lt;p&gt;user&amp;gt; (def sorted-version (sorted-set &quot;0000&quot;))&lt;/p&gt;
&lt;p&gt;;; =&amp;gt; #'user/sorted-version&lt;/p&gt;
&lt;p&gt;user&amp;gt; normal&lt;/p&gt;
&lt;p&gt;;; =&amp;gt; #{&quot;0000&quot;}&lt;/p&gt;
&lt;p&gt;user&amp;gt; sorted-version&lt;/p&gt;
&lt;p&gt;;; =&amp;gt; #{&quot;0000&quot;}&lt;/p&gt;
&lt;p&gt;user&amp;gt; (:foobar normal)&lt;/p&gt;
&lt;p&gt;;; =&amp;gt; nil&lt;/p&gt;
&lt;p&gt;user&amp;gt; (:foobar sorted-version)&lt;/p&gt;
&lt;p&gt;Execution error (ClassCastException) at user/eval246355 (form-init59552761865518221.clj:8123).&lt;br&gt;
class java.lang.String cannot be cast to class clojure.lang.Keyword (java.lang.String is in module java.base of loader 'bootstrap'; clojure.lang.Keyword is in unnamed module of loader 'app')&lt;br&gt;
&lt;code&gt;`&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I think my options for handling this are either try/catch or write a custom comparator, and both seem a little wonky :) &lt;/p&gt;
&lt;p&gt;I'm probably missing something, but thought it was interesting anyway :) &lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11409/somewhat-confusing-sorted-set-behavior</guid>
<pubDate>Thu, 23 Dec 2021 12:22:35 +0000</pubDate>
</item>
<item>
<title>Does the `any?` function have the correct behavior?</title>
<link>https://ask.clojure.org/index.php/11260/does-the-any-function-have-the-correct-behavior</link>
<description>&lt;p&gt;Clojure's &lt;code&gt;any?&lt;/code&gt; function seems to just return &lt;code&gt;true&lt;/code&gt; always.&lt;br&gt;
&lt;code&gt;(defn any?
  &quot;Returns true given any argument.&quot;
  {:tag Boolean
   :added &quot;1.9&quot;}
  [x] true)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Contrast this with &lt;code&gt;not-any?&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;(def
 ^{:tag Boolean
   :doc &quot;Returns false if (pred x) is logical true for any x in coll,
  else true.&quot;
   :arglists '([pred coll])
   :added &quot;1.0&quot;}
 not-any? (comp not some))&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I believe &lt;code&gt;any?&lt;/code&gt; should take two arguments and return &lt;code&gt;true&lt;/code&gt; if any predicate is &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;(def
 ^{:tag Boolean
   :doc &quot;Returns true if (pred x) is logical true for any x in coll,
  else false.&quot;
   :arglists '([pred coll])
   :added &quot;1.9&quot;}
 any? (comp boolean some))&lt;/code&gt;&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://clojuredocs.org/clojure.core/any_q&quot;&gt;Credit to Kofrasa on ClojureDocs.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here are some tests to run:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;`&lt;/code&gt;&lt;br&gt;
(ns user&lt;br&gt;
  (:require [clojure.test :refer [are deftest]]&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;        [clojure.test.check.clojure-test :refer [defspec]]
        [clojure.test.check.generators :as gen]
        [clojure.test.check.properties :as prop])
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;  (:refer-clojure :exclude [any?]))&lt;/p&gt;
&lt;p&gt;(deftest clojure-any&lt;br&gt;
  (are [x y] (= ((comp boolean some) true? x) (clojure.core/any? y))&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[true] true
[false] false ;; this fails
[true true] true
[false true] true
[false false] false ;; so does this
))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(def&lt;br&gt;
 ^{:tag Boolean&lt;br&gt;
   :doc &quot;Returns true if (pred x) is logical true for any x in coll,&lt;br&gt;
  else false.&quot;&lt;br&gt;
   :arglists '([pred coll])&lt;br&gt;
   :added &quot;1.9&quot;}&lt;br&gt;
 any? (comp boolean some))&lt;/p&gt;
&lt;p&gt;(defspec correct-any 1000&lt;br&gt;
  (prop/for-all [v (gen/such-that not-empty (gen/vector gen/boolean))]&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;            (= ((comp boolean some) true? v)
               (any? true? v)
               (not (not-any? true? v)))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;`&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Apologies for the messed-up formatting.&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://gist.github.com/wildwestrom/9568e9c659d30a2a663a9a0a8235a6b9&quot;&gt;https://gist.github.com/wildwestrom/9568e9c659d30a2a663a9a0a8235a6b9&lt;/a&gt;&lt;/p&gt;
</description>
<category>Syntax and reader</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11260/does-the-any-function-have-the-correct-behavior</guid>
<pubDate>Tue, 09 Nov 2021 22:30:57 +0000</pubDate>
</item>
<item>
<title>function spec challenge</title>
<link>https://ask.clojure.org/index.php/9690/function-spec-challenge</link>
<description>&lt;p&gt;What is the simplest way to spec this function? Specifically, the &lt;code&gt;:fn&lt;/code&gt; portion&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def ::edges #{0.149 0.150 0.050})
(s/def ::result #{:correct :incorrect})

(s/fdef foo
        :args (s/cat :student-response ::edges :authoritative-answer ::edges)
        :ret ::result
:fn ? )
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Basically, if the &lt;code&gt;student-response&lt;/code&gt; rounded to the tenths place matches the &lt;code&gt;authoritative-answer&lt;/code&gt; rounded to the tenths place, the function returns &lt;code&gt;:correct&lt;/code&gt;, otherwise &lt;code&gt;:incorrect&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I selected edges that would hit the rounding boundaries rather than hoping the generative process for numbers would stumble on those edges.&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9690/function-spec-challenge</guid>
<pubDate>Wed, 07 Oct 2020 03:38:56 +0000</pubDate>
</item>
<item>
<title>Retrieving the domain/range of finite functions</title>
<link>https://ask.clojure.org/index.php/9251/retrieving-the-domain-range-of-finite-functions</link>
<description>&lt;p&gt;One of the great features of Clojure is that it considers collections as functions which means that it is possible to &lt;strong&gt;invoke&lt;/strong&gt; them&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;({:a 1, :b 2} :a) =&amp;gt; 1
(#{:a :b} :a) =&amp;gt; :a
([:a :b :c] 1) =&amp;gt; :b
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However it would be nice to be able to query the &lt;strong&gt;domain&lt;/strong&gt; and &lt;strong&gt;range&lt;/strong&gt; of those functions independently of their specific type.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;keys&lt;/code&gt; and &lt;code&gt;vals&lt;/code&gt; functions currently implement this but only for Maps. I think it would be a good idea to make those functions more generic by accepting sets and vectors like in the following examples:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(keys  #{:a :b}) =&amp;gt; (:a :b)
(vals #{:a :b}) =&amp;gt; (:a :b)
(keys [:a :b :c]) =&amp;gt; (0 1 2)
(vals [:a :b :c]) =&amp;gt; (:a :b :c)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The specific use case I have in mind where this feature could be useful is to define a shorter syntax for explicit foreign key mapping in a data oriented DSL for relational data modeling, where &lt;code&gt;#{:attr1 :attr2}&lt;/code&gt; could be used as a shortcut for &lt;code&gt;{:attr1 :attr1, :attr2 :attr2}&lt;/code&gt;.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9251/retrieving-the-domain-range-of-finite-functions</guid>
<pubDate>Sun, 19 Apr 2020 18:32:29 +0000</pubDate>
</item>
<item>
<title>Functions in clojure</title>
<link>https://ask.clojure.org/index.php/9112/functions-in-clojure</link>
<description>&lt;p&gt;I have a task to create a function which gives powerset of a list.&lt;/p&gt;
&lt;p&gt;Output of the list (1 2 3) should be this:&lt;/p&gt;
&lt;p&gt;1&lt;br&gt;
2&lt;br&gt;
3&lt;br&gt;
1 2&lt;br&gt;
1 3&lt;br&gt;
2 3&lt;br&gt;
1 2 3&lt;br&gt;
I tried this helper function&lt;/p&gt;
&lt;p&gt;(defn print-powerset-helper [lst] (if(not(empty? lst)) (do (println(first lst)) (print-lst (rest lst))))) &lt;br&gt;
which gives me&lt;/p&gt;
&lt;p&gt;1 &lt;br&gt;
2 &lt;br&gt;
3&lt;br&gt;
nil&lt;br&gt;
this output.&lt;/p&gt;
&lt;p&gt;I also tired this fucntion:&lt;/p&gt;
&lt;p&gt;(defn print-powerset [lst] (if (not (empty? lst)) (do (apply println lst (print-lst lst)))))&lt;br&gt;
output:&lt;/p&gt;
&lt;p&gt;1&lt;br&gt;
2&lt;br&gt;
3&lt;br&gt;
(1 2 3)&lt;br&gt;
nil&lt;br&gt;
I want to get rid of parenthesis and also don't understand how to print the middle part of the output. 1 2 1 3 2 3 , this part.&lt;/p&gt;
&lt;p&gt;Please any kind of help will be good. Thank you!&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9112/functions-in-clojure</guid>
<pubDate>Sat, 22 Feb 2020 18:30:39 +0000</pubDate>
</item>
<item>
<title>Clojure functions to implement the Map interface. Why or Why not?</title>
<link>https://ask.clojure.org/index.php/8798/clojure-functions-to-implement-the-map-interface-why-why-not</link>
<description>&lt;p&gt;From a theoretical standpoint, there really isn't a difference between a Map and a 1-arity function&lt;/p&gt;
&lt;p&gt;What do you think of having a Function implement the Map interface? Will it be useful? If this  was  thought and  you guys  decided not to go with it, why not? Would love to learn the thinking&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8798/clojure-functions-to-implement-the-map-interface-why-why-not</guid>
<pubDate>Thu, 31 Oct 2019 17:13:37 +0000</pubDate>
</item>
<item>
<title>Why does Transit not allow serialization of clojure(script) functions ? What is the inherent difficulty there ?</title>
<link>https://ask.clojure.org/index.php/8254/transit-serialization-clojure-functions-inherent-difficulty</link>
<description>&lt;p&gt;I am trying to serialize my datascript database to JSON for storage on a filesystem. &lt;br&gt;
Some of the data inside my database includes functions. For reasons that are not interesting, I need to store them in the database for now with other session info for the application. But I get an exception that transit &quot;cannot write Function&quot;&lt;/p&gt;
&lt;p&gt;I looked at the code and it simply tries to handle data structures only - maps, vectors, etc. But there is no handler for functions. So I KNOW WHY I get the error, but the gist of my question is why it was not implemented to be able to serialize functions as well ? &lt;/p&gt;
&lt;p&gt; Is it just too novel an thought ? &lt;/p&gt;
&lt;p&gt; thanks&lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8254/transit-serialization-clojure-functions-inherent-difficulty</guid>
<pubDate>Mon, 29 Jul 2019 23:10:55 +0000</pubDate>
</item>
</channel>
</rss>