This page is not created by, affiliated with, or supported by Slack Technologies, Inc.

## 2020-12-05

## Channels

- # adventofcode (50)
- # announcements (1)
- # asami (29)
- # babashka (56)
- # beginners (19)
- # calva (62)
- # cider (12)
- # cljs-dev (1)
- # clojure (42)
- # clojure-europe (214)
- # clojure-france (4)
- # clojure-italy (1)
- # clojurescript (58)
- # community-development (4)
- # cryogen (6)
- # cursive (7)
- # data-science (1)
- # events (3)
- # figwheel-main (1)
- # fulcro (21)
- # lambdaisland (3)
- # malli (17)
- # mid-cities-meetup (1)
- # off-topic (38)
- # pathom (3)
- # reagent (7)
- # reclojure (1)
- # reveal (15)
- # rewrite-clj (11)
- # shadow-cljs (30)
- # sql (21)
- # test-check (14)
- # tools-deps (1)
- # vim (21)
- # xtdb (5)

can bit-xor be used as logical xor?

Nope.

is there something elegant then?

@U01CLETCE1L It's easy enough to write your own:

```
user=> (defn xor [p q] (and (or p q) (not (and p q))))
#'user/xor
user=> (for [a [true false] b [true false]] (list a b (xor a b)))
((true true false) (true false true) (false true true) (false false false))
user=>
```

See https://en.wikipedia.org/wiki/Exclusive_or#Equivalences,_elimination,_and_introduction for the logic.thanks,

```
(defn xor [& args]
(odd? (count (filter true? args))))
```

That's not quite the same semantics as the version I provided. Mine works with truthy/falsey which is more like the built-in `and`

and `or`

. Yours will only work with `true`

for truthy.

If you update yours to `(filter identity args)`

it'll have truthy/falsey semantics. (mine was strictly a 2-arity version so I can see why that's not generally useful!)

oh yeah i expect only `true`

or `false`

as input

i would use `bit-xor`

as a guide to write your own multi-arity one

```
(defn bit-xor
"Bitwise exclusive or"
{:inline (nary-inline 'xor)
:inline-arities >1?
:added "1.0"}
([x y] (. clojure.lang.Numbers xor x y))
([x y & more]
(reduce1 bit-xor (bit-xor x y) more)))
```

thanks !!

Could some one tell me why the `calculate-row`

function returns `nil`

and not a number?
https://pastebin.com/TJzf7pXb (Advent of Code spoiler)

it looks like a misplaced paren. moving the `cond`

statement to the `else`

branch of the `if`

statement fixes "returning nil" (I didn't check logical correctness otherwise):

```
(defn calculate-row
[data rows]
(loop [d data
r rows]
(if (= (rest d) ())
r
(cond
(= (first d) \F) (recur (rest d) (bottom r))
(= (first d) \B) (recur (rest d) (top r))))))
```

I recommend the following idiom when looping using `loop`

(which was originally recommend by someone in this slack):
https://clojuredocs.org/clojure.core/loop#example-5a1de497e4b0a08026c48cc4

```
;; Iterating over a collection using loop
;; 1. First call (seq xs) on the given argument and then check for nil
;; 2. Then call next/first and use these.
(loop [xs (seq [1 2 3 4 5])
result []]
(if xs
(let [x (first xs)]
(recur (next xs) (conj result (* x x))))
result))
;; the same loop can be written using destructing,
;; but the compiler will generate two consecutive
;; seq calls and is slightly less efficient.
(loop [[x & r :as xs] (seq [])
result []]
(if xs
(recur r (conj result (* x x)))
result))
```

the first `if`

is ignored. it is executed but nothing uses its return value so it might as well never happen. you then have a `cond`

which will return a value if the predicate is hit, but if none of those predicates match it returns nil. also, you have a loop expression but there are no `recur`

expressions so it will never loop