Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have working code where I get hash code of data structures with (hash data-structure) but it works in small amount of them. When I have few millions of data structures it will start giving me duplicate hashes (clash). How to get something more unique when structure of data structure is unknown?
You could always resort to md5 (or anything else, really).
(defn md5 [^String s]
(let [algorithm (MessageDigest/getInstance "MD5")
raw (.digest algorithm (.getBytes s))]
(format "%032x" (BigInteger. 1 raw))))
(defn md5-hash [s] (md5 (pr-str s)))
(md5-hash {:a [1 2 3 '(5 6 7)]})
=> "8f941424e629b876cdcb51509521870d"
also, you can use Java serialization instead of pr-str.
This library has existed for a while, and I've seen at least one alternative.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm just starting to learn common lisp. I do not understand well about lists' structure when I'm reading the Set and Tree chapter. Now I wonder what's exactly it like in the list. I know the cons include two values which can be any types of object. Here I found a picture by google.
But I'm very confused. It seems like general lists in data structure.
lists in lisp
lists are build using cons cells you mentioned. single cons cell with nil in second part is the simplest list:
(cons 'A nil)
=> (A)
if you want to extend list, you cons another cell to it:
(cons 'C (cons 'B (cons 'A nil)))
=> (C B A)
so list is a series of linked together cons cells.
also, to make life easier there is list function. this way you don't have to write all those cons invocations and can create list like this:
(list 'C 'B 'A)
=> (C B A)
but inside it's still series of cons cells.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Trying to convert this generator into a list of list. Got an error: TypeError: 'tuple' object is not callable. Using python 2.7
perm = itertools.permutations(range(1, 4))
If the goal is to convert to a list of lists (instead of generator of tuples), map can do this easily:
perms = map(list, itertools.permutations(range(1,4)))
If a list of tuples is all you need, it's even easier:
perms = list(itertools.permutations(range(1,4)))
Just don't do it for permutations on a larger set of inputs, or you'll exhaust memory pretty quickly.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was thinking on the meaning of side-effect in Clojure. What exactly is a side-effect in Clojure? Could any one explain this with an example?
A side effect in any programming language would be everything that is done which does not have a direct correlation between supplied arguments and the returned result.
(+ 3 4) ; ==> 7 (result is always a mapping between arguments and result. It will always be 7 no matte rhow many times you do it.
(rand-int 4) ; ==> 0,1,2, or 3. You have no idea what it will produce next.
The first expression is functional. You can make a lookup table of all the different two values with it's result and you wouldn't know the difference.
The second might give different result for the same argument. The computation must be based on something else, like internal state, and not the argument alone. It has side effects.
Typical side effects used in programs are I/O and object mutation.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Googling reveals only answers to this question from the last few years and some of the projects I've looked at in github haven't had commits for a long time. Is anyone working on anything at the moment? I'm interested in contributing. Also does anything test in parallel? I'm new to Clojure but one thing it seems to be missing is a test framework as good as ScalaTest.
I think speclj is still active.
https://github.com/zenmodeler/spexec is something relatively recent and devoted to BDD.
https://github.com/cucumber/cucumber-jvm also has clojure support and there's a runnable example in the clojure directory; an extended example of its use is here: https://github.com/gphilipp/bdd-guide-clojure
Eggplant is a exciting new BDD open source library used for Clojure. You can also read about it here.
For example:
(defspec multiplying-two-numbers
(specification
{:given "a input of :a and :b"
:when "we #* :c"
:then "we expect :result"
:data {:a 3 :b 4 :result 12}}))
(defspec change-a-string-to-uppercase
(specification
{:given "a input of :a"
:when "we #clojure.string/upper-case"
:then "we expect :result"
:data {:a "hello" :result "HELLO"}}))
(defspec finding-the-max-of-two-numbers
(specification
{:expect "the #max of :a and :b"
:where {:a 2 :b 3 :expected 3}}))
"Simplicity is key"
Five keywords:
given
when
then
expect
where
https://github.com/perkss/eggplant
Focusing on data driven development and testing it via spec by example using a human readable form which maintains specification documentation as the tests grow.
The standard clojure.test will run these so compatible with any IDE and build tool.
Developers can write a quick easy to follow BDD test in a very short time.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to implement a function which when called return a factorial of numbers in list. For example (fact '(2 4 3)) => (2 24 6) but mine is not working. I am pretty sure that the logic is correct for my implementation just I can not find my syntax error. So if you could have a look and give some feedback it would be great here is the code:
(defun fact (list)
(cond ((null list) 0)
((zerop (first list) 1))
(* first list(fact (rest list)))
))
What you appear to be trying to do is apply a factorial function to each member of a list and collect the results in a list.
For this you need a factorial function and mapcar.