1.Maybe you can use :pre metadata:
(defn create-pin
([] (create-pin 8))
([n]
{:pre [(<= n 16)
(>= n 4)]}
(let [chars (map char (range (int \0) (inc (int \9))))]
(reduce str (repeatedly n #(rand-nth chars))))))
(create-pin 3)
AssertionError Assert failed: (>= n 4) user/create-pin (NO_SOURCE_FILE:27)
and clojure.spec is great too.
2. you can use concat
(concat seq1 seq2)
see cheat sheet for clojure: https://clojure.org/api/cheatsheet
2017-09-06 16:10 GMT+08:00 Colin Yates <[email protected]>:
> Hi Cecil - welcome back!
>
> This would be perfect to test using the new clojure.spec. I can't give
> you any advice on the specifics as I have yet to break into it myself,
> but the properties of this function are crying out for generative
> testing.
>
> Cecil Westerhof writes:
>
> > I want to start using Clojure again. I made the following simple function
> > to generate a PIN:
> > (defn create-pin
> > ([] (create-pin 8))
> > ([n]
> > (let [chars (map char (range (int \0) (inc (int \9))))]
> > (reduce str (repeatedly n #(rand-nth chars))))))
> >
> > So far so good. But I want to improve a little.
> >
> > I think n should at least be four, but not greater as 16. What is the
> > Clojure way to do this?
> >
> > The next step is that I want to use hexadecimal numbers. So I should use
> > (range (int \0) (inc (int \9))) combined with (range (int \A) (inc (int
> > \F))).
> > How would I do that?
> >
> > Is there anything I should do differently?
> >
> > Of-course I make a general function that is then called from create-pin
> and
> > create-pin-hex.
> >
> > --
> > Cecil Westerhof
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
庄晓丹
Email: [email protected]
Site: http://fnil.net
不学习,毋宁死
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.