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)))
(mapset inc [1 1 1 2] [2 2 2 3])
gives me
> ; class clojure.lang.PersistentVector cannot be cast to class java.lang.Number
(defn mapset
"map but returns set"
[func & colls]
(set (apply map func colls)))
Also (map inc [1 2] [3 4])
will fail because only a single number can be incremented.
So maybe (mapset + [1 1 1 2] [2 2 2 3])
brilliant, thank you!
super neato
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?
right, "\:" is not a valid string, the clojure reader rejects it
"C:\\Program Files"
probably
but in Java this also works on Windows I think:
C:/Program Files
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))
That’s a really good error message. S/or expects k1 p1 where the k’s are keywords.
Yeah I guess every condition has a keyword to identify it
So you would have (s/or :cond-1 #(< 0 %) :cond-2 #(> 1 %))
This is useful eg. when you call s/conform on a value, for it can let you know which condition was satisfied
Also once you understand what’s going on, you can remove the need for the s/or with (< 0 % 1)
Does s/def work on a symbol like that? Specs are usually registered as a keyword in a central registry
Oh that’s cool I forgot that’s how that works
But solve it with the or so you understand what’s going on. And then simplify
s/def can take both keywords and symbols, but they have to be fully namespace qualified. Look at res
in clojure.spec.alpha
here it is a def
so just the spec
the mouth of the crocodile has to look to the other sie @rob370
<
Calling int on the boolean seems strange
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))`
(def arousal-level (s/and pos?
int?
#(> 100 %)))
I think I got itThank you for the help everyone
There is actually a function pos-int?
in the standard library, I discovered just now.
It’s great that you solved your problem but It’s still weird that you use >
instead of <
!
They are checking for an integer less than 100, which (> 100 %)
does
function specs are registered with symbols so this is allowed (but is not what was intended)
Thanks @borkdude! Is there a function that will escape special characters for me?
specs absolutely are best thought of as predicates describing value sets