Fork me on GitHub
#beginners
<
2022-12-31
>
zach02:12:00

Is it possible to define a spec of a sequence of values where some values are required, some are optional, and the order doesn’t matter? For example, a sequence that requires 1 string, 1 boolean, 1 number, and an optional set of two-digit numbers?

(require clojure.spec.alpha :as s)

(s/valid? ::toy-ex ["hi" 5 true]) ;=> true
(s/valid? ::toy-ex ["hi" 5 true 25]) ;=> true
(s/valid? ::toy-ex ["hi" 25]) ;=> false

(s/valid? ::toy-ex [true "hi" 5]) ;=> true
(s/valid? ::toy-ex [25 5 true "hi"]) ;=> true
(s/valid? ::toy-ex [25 5 true "hi" false]) ;=> false

zach02:12:25

real-world example is I am taking in ical entries split on the line. So they are a sequence of strings each matching a regex, and some are required and some are optional, but there is no guaranteed or required order to the lines. The closest I have is:

(require clojure.spec.alpha :as s)
;...defining all the various regexes i used below...

(s/def :vevent/vevent-coll
  (s/cat
   ::required
   (s/* (s/alt ::summary :vevent/summary
               ::dtstart :vevent/dtstart
               ::dtend :vevent/dtend
               ::uid :vevent/uid))
   ::optional
   (s/* (s/alt ::location :vevent/location
               ::description :vevent/description
               ::description-text :vevent/description-text
               :created :vevent/created
               ::ignorable string?))))
This works well enough when conforming a valid ical sequence, but it also allows invalid ones too easily.

hiredman03:12:40

Spec provides a super set of the regex operators, so if conceptually it could be matched by a regex, then spec can match it

hiredman03:12:07

In a regex, the operator you are looking for is '?' so just look at the docs for the spec namespace and find the spec combinator that does that

zach04:12:49

thank you!

Michał Frątczak10:12:44

Hey all. Is it worth to try clojure on windows? And if yes, is this link best choice to start and up-to-date ? https://github.com/clojure/tools.deps.alpha/wiki/clj-on-Windows

Michał Frątczak11:12:19

Actually I am looking for a best frictionless setup to try clojure on my windows laptop (with VS code optionally)

pavlosmelissinos11:12:13

I think WSL + https://clojure.org/guides/install_clojure#_linux_instructions is still your best option on Windows

❤️ 2
lread14:12:58

There is also #CFN4QDHPS if you get stuck.

practicalli-johnny15:12:31

If someone is adept at Windows and Powershell, then installing Clojure should be okay, but probably not as smooth as Unix (Linux/Mac). Using clojure on Windows should work okay, although its the little things like escaping command line arguments (e.g. for Clojure CLI clojure.exec method) that can be a little fiddly. Consensus seems to be that WSL is the smoothest approach for windows, especially if its a recent version of Windows. There seems to also be advantage if using VS Code as the editor of choice (it can tap into WSL a little more smoothly) On older versions of Windows or if WSL is not available, then there is https://github.com/littleli/scoop-clojure/wiki/Getting-started

Michał Frątczak15:12:49

I will try wsl way (win10)

dumrat16:12:44

Recommend WSL also

Bob B18:12:06

while it's "non-standard", I also find deps.clj (<https://github.com/borkdude/deps.clj>) quite nice for windows - if you're doing most things in VSC, then Calva will handle a lot of the things fairly independently of the OS (invoking java directly)

Michał Frątczak18:12:00

I now have vscode/wsl/calva setup running and I go through calva's "getting started REPL". So far so good !

येसुदीप मंगलापिल्ली Yesudeep Mangalapilly06:01:53

If I get my clojure(script) rules working with bazel, all you'll need is https://bazel.build and the following irrespective of your operating system or shell. You won't need any installer scripts either. Just install bazel and add definitions (resembling your module dependency graph) like the following in your build file (bazel fetches clojure(script) and your dependencies automagically, and runs your code):

# MODULE.bazel

bazel_dep(name="rules_clojure", version="...")

# ... some more boilerplate ...
# BUILD.bazel

# ...

clojure_library(
    name = "core_clojure_lib",
    srcs = [
        "core.clj",
    ],
    deps = [
        ":greet_java_lib",
        ":math_clojure_lib",
    ],
)

clojure_test(
    name = "core_clojure_test",
    srcs = [
        "core_test.clj",
    ],
    deps = [
        ":core_clojure_lib",
    ],
)

clojure_binary(
    name = "hello_world_clojure",
    ns = "tests.hello-world.core",
    deps = [
        ":core_clojure_lib",
    ],
)

clojure_repl(
    name = "hello_world_clojure_repl",
    ns = "tests.hello-world.core",
    deps = [
        ":core_clojure_lib",
    ],
)
Hopefully, I can get this thing and https://github.com/bazelbuild/bazel-gazelle (build file generator) integration working so you don't even have to write the build files by hand. fingers crossed https://www.youtube.com/shorts/b7rKAfPuNFw

येसुदीप मंगलापिल्ली Yesudeep Mangalapilly06:01:43

And running tests looks like this https://www.youtube.com/watch?v=eqrw3X3ddPY To answer your question "Is it worth to try clojure on windows?" I'd say certainly. As you can see I've almost gotten it to work without too many hiccups using both wsl and powershell. (Msys2 works too btw)