This question already has answers here:
"reduce" or "apply" using logical functions in Clojure
(2 answers)
Closed 8 years ago.
"and" seems to be a macro, so I can't use it in something like (reduce and list-of-booleans)
What should I do instead?
You can wrap it into some lambda like this.
user=> (reduce (fn [a b] (and a b)) '(true true true))
true
user=> (reduce (fn [a b] (and a b)) '(true true false))
false
Of course, you don't need reduce at all, you must use every?:
user=> (every? true? '(true false true))
false
Related
I am trying to get a list to evaluate to a true/false against a particular number. When I define the function, clojure defines it fine. But it is not doing what I want to do. Just learning clojure... so I am not very comfortable with the syntax yet... apologies if this is a noob question.
Ideally it would take in a list like (list 9 0 3) and evaluate it to (list true false true)
(defn myfunc [lst](map (> x 1) lst))
Here is the correct syntax:
(defn greater-than-one?
[x]
(< 1 x))
and then use it:
(mapv greater-than-one? (list 9 0 3)) => [true false true]
In your original format, you could also solve it like:
(defn myfunc [lst]
(map #(> % 1) lst))
and use it:
(myfunc (list 9 0 3)) => (true false true)
You may find this template project helpful in getting started. Please be sure to see the list of documentation also.
This question already has an answer here:
When should you use swap or reset
(1 answer)
Closed 2 years ago.
I want to change the atom, which only holds one boolean to true.
I came up with this workaround, is there something I'm missing?
(defn always-true [x]
"Used to change atom value"
true
)
(def x (atom false))
(swap! x always-true)
Thank you!
there's the reset! function for atoms, for this exact usecase:
(reset! x false)
This question already has answers here:
Clojure: What does [_] do in a Functions Argument List?
(3 answers)
Closed 4 years ago.
for example we have function:
(defn my-fun [param]
(let [[x _] param]
x))
Why we use "_" in this example?
(let [[x _] [1 2]] x) destructures the first element of a sequence and binds it to the name x. It also binds the second value to the name _, but by convention this name means the value can be ignored.
Note that (let [[x _] [1 2]] [x _]) would technically be valid Clojure, but the semantics of _ is that we just don't care about that value. There is no special treatment of that symbol from the perspective of the compiler, just human convention.
Strange as it may sound, I am looking for function versions of the and and or macros in Clojure.
Why? For one I am curious.
Second, I want to use or in precondition and postcondition checks. This does not work:
(defn victor
[x]
{:post (or (nil? %) (vector %))}
;; ...
)
I want the postcondition to check to see if victor returns a vector or nil, but it fails:
#<CompilerException java.lang.RuntimeException: Can't take value of a macro: #'clojure.core/or, compiling:(test/test.clj:10:1)>
I don't think bit-and and bit-or are quite what I'm looking for.
Update: This syntax works without an error:
(defn victor
[x]
{:post [(or (nil? %) (vector %))]}
;; ...
)
I'm still curious if functions exist, though.
I think the standard method is simply to wrap and and or in functions, e.g. (fn [x y] (or x y)). In some contexts, another function will work. For example, a note in the clojure docs for and suggests using (every? identity [true false]). some, not-every?, and not-any? can be used in a similar way.
In general, and and or functions would be undesirable because they cannot use short-circuiting. Consider the following code:
(and false some-expensive-fn)
(or true some-expensive-fn)
With and and or as macros the above code won't execute some-expensive-fn, because it is unnecessary to determine the overall truth value of the expression. In function expressions the arguments are evaluated before being passed to the function, but in macros they are not.
#Triangle Man is right. Short-circuiting won't work, but nevertheless you can define your own function versions:
user=> (defn && [x y] (and x y))
#'user/&&
user=> (&& true false)
false
user=> (&& true true)
true
user=> (defn || [x y] (or x y))
#'user/||
user=> (|| true false)
true
user=> (|| true true)
true
user=> (|| false false)
false
user=>
Basically...
=> (atom? 5)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: atom? in this context, compiling:(NO_SOURCE_PATH:1)
=> (atom? /a)
RuntimeException Invalid token: /a clojure.lang.Util.runtimeException (Util.java:156)
RuntimeException Unmatched delimiter: ) clojure.lang.Util.runtimeException (Util.java:156)
=> (atom? "hello world")
CompilerException java.lang.RuntimeException: Unable to resolve symbol: atom? in this context, compiling:(NO_SOURCE_PATH:1)
So does anyone know what's happening??
I am using Eclipse Juno 4.2, the CounterClockwise plugin.
What's called an atom in Clojure is something completely different than what's called an atom in other Lisps. In classic Lisp an atom is a single value, defined as being not null or not a cons cell (pair):
(define (atom? x)
(not (or (pair? x)
(null? x ))))
In Clojure an atom is a concurrency reference type. Atoms in Clojure can be either single-valued or collections/sequences, where updating (mutable state change) is guaranteed to happen atomically.
In Clojure there's far more reference types than the cons list in Lisp, and there's all the Java interop collection types to be reckoned with. That makes it hard to define a check on single-values.
If you do want to, the simplest check is to see if something can be counted. Looking at (source counted), it references clojure.lang.RT/count and countFrom. There, several classes / interfaces are specified, which I included in the following function:
=> (defn single-valued?
[x]
(not (or (nil? x)
(.. x getClass isArray)
(some #(instance? % x) [clojure.lang.Counted
clojure.lang.IPersistentCollection
java.util.Collection
java.util.Map]))))
=> (map single-valued? [1 "foo" \a 'x true not nil])
(true true true true true true false)
=> (map single-valued? ['(1 2 3 4)
[1 2 3 4]
{:a 1 :b 2}
#{1 2 3 4}
(seq [1 2 3 4])
(seq {:a 1 :b 2})
(seq "foo")
(int-array [1 2 3 4])
(seq [])])
(false false false false false false false false false)
Since (seq []) evaluates to nil it's not considered single-valued. Of course, java objects with multiple fields, as well as Clojure deftypes / defrecords will register as such, even though they're composite objects.
I suspect you are confusing a clojure atom with an atom in something like scheme.
In scheme an atom is a fundamental unit.
In clojure an atom is one of clojure's reference types (like ref and var) that can be updated atomically.
This fits nicely with clojure's concurrency model.
e.g.
user> (def a (atom '(1 2 3)]); create an atom with value (1 2 3)
user> #a ; look up (deference) the atoms value
(1 2 3)
user> (swap! a (fn [v] (map inc v))) ; add 1 to each element, v is the
; old value of the atom. other threads will
; see the three values in a change atomically
user> #a
(2 3 4)
user> (reset! a '(5 10 15))
user> #a
(5 10 15)
atom? is not a function.
You could use
(def x (atom 5))
(instance? clojure.lang.Atom x)
You can create atom? function like this:
(defn atom? [x]
(not (coll? x))
)
The complement function returns the opposite of any predicate passed to it as argument, so you can make a atom? with it:
(defn atom?
[x]
((complement coll?) x))
(atom? []) ;=> false
(atom? ()) ;=> false
(atom? {}) ;=> false
(atom? 4) ;=> true