This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-06-26
Channels
- # announcements (6)
- # aws (5)
- # beginners (42)
- # cider (24)
- # clojure (81)
- # clojure-canada (1)
- # clojure-europe (122)
- # clojured (5)
- # cursive (6)
- # data-science (5)
- # datahike (9)
- # emacs (6)
- # events (2)
- # fulcro (12)
- # helix (4)
- # honeysql (1)
- # jobs-discuss (1)
- # lsp (28)
- # mid-cities-meetup (1)
- # off-topic (31)
- # polylith (13)
- # shadow-cljs (22)
- # spacemacs (8)
- # tools-deps (33)
- # vim (10)
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
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?
Thanks @U04V15CAJ! Is there a function that will escape special characters for me?
If you read a string like that from a file or user input, there is no need to escape them.
If you have a string typed into your Clojure source code, then it is a lot like a Java or C string, where there are special characters like \n for a newline, so a backslash itself must also be escaped if you want a backslash in the resulting string.
I've never heard of anyone writing a bit of code that takes such strings you want to put in your source code, and escape them for you, but it wouldn't be difficult to write one if you really want it.
Most such strings in source code are relatively infrequent, and I believe developers typically escape them by hand-editing their programs.
right, "\:" is not a valid string, the clojure reader rejects it
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))
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)
Oh that’s cool I forgot that’s how that works
Good advice. So for some reason this still isn’t working like I expected… I decided to change it to an integer between 0 and 100.
Does s/def work on a symbol like that? Specs are usually registered as a keyword in a central registry
function specs are registered with symbols so this is allowed (but is not what was intended)
Good advice. So for some reason this still isn’t working like I expected… I decided to change it to an integer between 0 and 100.
s/def can take both keywords and symbols, but they have to be fully namespace qualified. Look at res
in clojure.spec.alpha
You’re right. I think what I’m actually trying to do is make sure the argument is an int
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))`
specs absolutely are best thought of as predicates describing value sets
You're right, I was being crude. I should have said: Having defined a spec, you also get more than something just capable of producing boolean-valued expressions, like ways to generate data of wanted shape and s/conform to "pick apart" and say in what way a value satisfies a spec.
:thumbsup:
You’re right. I think what I’m actually trying to do is make sure the argument is an int
(def arousal-level (s/and pos?
int?
#(> 100 %)))
I think I got itThere is actually a function pos-int?
in the standard library, I discovered just now.