beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
Jeffery Swensen 2021-06-26T01:48:04.034200Z

hey all. from http://braveclojure.com exercises, I'm trying to write mapset which works like map but returns a set. I'm struggling with getting it to work for more than one collection argument

(defn mapset
  "map but returns set"
  [func & colls]
  (set (map func colls)))

Jeffery Swensen 2021-06-26T01:48:57.034300Z

(mapset inc [1 1 1 2] [2 2 2 3]) gives me > ; class clojure.lang.PersistentVector cannot be cast to class java.lang.Number

indy 2021-06-26T01:54:17.034500Z

(defn mapset
   "map but returns set"
   [func & colls]
   (set (apply map func colls)))

indy 2021-06-26T01:55:30.034700Z

Also (map inc [1 2] [3 4]) will fail because only a single number can be incremented.

indy 2021-06-26T01:55:48.034900Z

So maybe (mapset + [1 1 1 2] [2 2 2 3])

Jeffery Swensen 2021-06-26T01:57:00.035200Z

brilliant, thank you!

sova-soars-the-sora 2021-06-26T03:09:13.035500Z

super neato

j 2021-06-26T20:34:05.037900Z

I'm taking a windows file path as an argument (i.e. "C:\Program Files") and feeding it to http://java.io/file. I get an error because of special characters in the string. What's the idiomatic way to escape those chars?

2021-06-30T16:49:45.282700Z

right, "\:" is not a valid string, the clojure reader rejects it

borkdude 2021-06-26T21:02:04.038200Z

"C:\\Program Files" probably

borkdude 2021-06-26T21:02:38.038500Z

but in Java this also works on Windows I think:

C:/Program Files

2021-06-26T21:52:49.039500Z

Can someone help me fix this Spec please? I’m trying to check if something is a valid arousal level, which must be between 0 and 1 (s/def arousal-level? (s/or #(< 0 %) #(> 1 %))) Unexpected error (AssertionError) macroexpanding s/or at (src/anxiety_loop/core.clj:1:166). Assert failed: spec/or expects k1 p1 k2 p2..., where ks are keywords (c/and (even? (count key-pred-forms)) (every? keyword? keys))

dpsutton 2021-06-26T21:57:29.040600Z

That’s a really good error message. S/or expects k1 p1 where the k’s are keywords.

vncz 2021-06-26T21:57:54.041Z

Yeah I guess every condition has a keyword to identify it

vncz 2021-06-26T21:58:27.042100Z

So you would have (s/or :cond-1 #(&lt; 0 %) :cond-2 #(&gt; 1 %))

Fredrik 2021-06-26T21:59:38.043700Z

This is useful eg. when you call s/conform on a value, for it can let you know which condition was satisfied

dpsutton 2021-06-26T22:00:03.044400Z

Also once you understand what’s going on, you can remove the need for the s/or with (< 0 % 1)

🙌 2
dpsutton 2021-06-26T22:05:44.047100Z

Does s/def work on a symbol like that? Specs are usually registered as a keyword in a central registry

2021-06-26T22:06:38.047600Z

Oh that’s cool I forgot that’s how that works

dpsutton 2021-06-26T22:07:50.049Z

But solve it with the or so you understand what’s going on. And then simplify

Fredrik 2021-06-26T22:08:48.050Z

s/def can take both keywords and symbols, but they have to be fully namespace qualified. Look at res in clojure.spec.alpha

dgb23 2021-06-26T22:09:34.051Z

here it is a def so just the spec

dgb23 2021-06-26T22:10:30.052200Z

the mouth of the crocodile has to look to the other sie @rob370

dgb23 2021-06-26T22:10:42.052400Z

&lt;

dpsutton 2021-06-26T22:11:57.053500Z

Calling int on the boolean seems strange

Fredrik 2021-06-26T22:12:50.054600Z

The value 101 satisfies pos? , so it 'passes' the spec (specs are not best thought of as predicates, though, rather as descriptions of the shape of data). What you want is to use s/and instead of s/or when your value has to satisfies all conditions, ie. both be positive and less than 100. In this case you can get away with using a single function:`(s/def ::arousal-level #(< 0 % 100))`

2021-06-26T22:17:10.055200Z

(def arousal-level (s/and pos?
                          int?
                          #(&gt; 100 %)))
I think I got it

2021-06-26T22:17:27.055400Z

Thank you for the help everyone

🎉 3
Fredrik 2021-06-26T22:18:37.056400Z

There is actually a function pos-int? in the standard library, I discovered just now.

dgb23 2021-06-26T22:18:39.056600Z

It’s great that you solved your problem but It’s still weird that you use &gt; instead of &lt; !

Fredrik 2021-06-26T22:19:41.056700Z

They are checking for an integer less than 100, which (&gt; 100 %) does

👍 2
alexmiller 2021-06-26T22:43:14.057100Z

function specs are registered with symbols so this is allowed (but is not what was intended)

j 2021-06-26T22:44:02.057300Z

Thanks @borkdude! Is there a function that will escape special characters for me?

alexmiller 2021-06-26T22:44:07.057500Z

specs absolutely are best thought of as predicates describing value sets