This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-11-18
Channels
- # admin-announcements (4)
- # beginners (306)
- # boot (191)
- # bristol-clojurians (4)
- # business (3)
- # cbus (4)
- # cider (6)
- # cljsrn (51)
- # clojure (147)
- # clojure-canada (1)
- # clojure-conj (6)
- # clojure-japan (2)
- # clojure-poland (8)
- # clojure-russia (57)
- # clojure-sg (1)
- # clojurecup (1)
- # clojurescript (229)
- # core-async (4)
- # cursive (47)
- # data-science (2)
- # datomic (3)
- # emacs (6)
- # events (1)
- # hoplon (16)
- # immutant (33)
- # jobs (1)
- # ldnclj (7)
- # off-topic (25)
- # om (69)
- # onyx (7)
- # re-frame (35)
- # reagent (3)
- # yada (4)
@mrg https://yobriefca.se/blog/2014/05/19/the-weird-and-wonderful-characters-of-clojure/
When used it will attempt to return the referenced var. This is useful when you want to talk about the reference/declaration instead of the value it represents.
Anyone know of a nice, clean way of handling “wrap this value in a sequence IFF it isn’t a sequence”?
Either that or a function that either concat
s or conj
s based on the input 😛
wouldn't it be (when-not (seq? v) (seq v))
@chancerussell: guessing from your two questions, I think you're probably looking for a solution to the latter (the former in support of the latter). Correct? One way to re-prhase that is that you want to choose concat or conj as the fn to apply to an existing sequential collection (not a seq) and a value, in which case ((if (sequential? v) concat conj) s v)
is a pretty clean way to go. WDYT?
Hey guys, I'm currently trying a project euler challenge in clojure and need to optimise for speed. Anyone able to help me out?
Challenge is https://www.hackerrank.com/contests/projecteuler/challenges/euler117 My code is only able to pass the first testcase and times out at the other ones. It needs to execute under 8s and either the testdata is wrong (could be! it errored with RuntimeError at my python solution) or my solution is just too slow
@dvcrn: usually the first data set is small, and then hackerrank blows the size up for the later tests
In any way, here is my code. If someone has a thought, let me know.
(defn exp [x n]
(reduce * (repeat n x)))
;; Using a vector here because of O(1) access time
(def possibilities-for-space (atom [0 1 2 4 8 15]))
(def tiles [1 2 3 4])
(def m (exp 10 18))
(defn calculate [length]
(if (<= length 5)
(@possibilities-for-space length)
(do
;; Iterate through 6 -> n
(doseq [l (range 6 (+ length 1))]
(let [possibilities (atom 0)]
;; For each length, we need to match all possible tiles
(doseq [tile tiles]
(let [r (- l tile)]
;; If we have a rest between length and tile, add with saved subresult for r
(if (>= r 0)
(swap! possibilities #(+ % (@possibilities-for-space r))))))
;; The vector is linear so we can just append.
(swap! possibilities-for-space #(conj % (mod @possibilities m)))))
;; Return last element
(@possibilities-for-space length))))
(calculate (bigint (exp 10 18)))
mmm, what about a different approach, trying to build each new length using the previous ones, something like: you know that for each 2 black that are together you can replace it for a red one, and so on, leading to just computing the replacements and multiplying by the permutations
so at the end for example with just red and black tiles for example you build the length 5 by using the different options for length 3 by adding a red one at the end, plus the options of the length 4 by adding the black one at the end
I'm doing something similar above. I calculate all the permutations for each value. Once we have that, it is solvable very easy with dynamic programming. E.g. length = 5 5 - 1 (black tile) = 4. We already know that for a space 4, we have 8 permutations 5 - 2 (red) = 3 = 4 permutations 5 -3 (green) = 2 = 2 permutations 5 - 4 (blue) = 1 = 1 permutation = 8 + 4 + 2 + 1 = 15
though vectors have O(1) access time. Besides space, I am not sure if it will impact on speed so much
not sure either, but maybe you save some stack/creation time, (I’m going to bed, will check in the morning again, good luck)
I think the solution is a math formula that we have to calculate that we can just apply to any number
atom
does CAS and since you do not use thread the compare doesn't do anything useful
let me try that. But even with this, i think the approach above is just not fast enough by itself to ever handle 10^18
doseq does chunking/(seq...), also loop prolly allows you to throw away the atom and just use a local var
well feel free to tinker around. I for my part can't optimise it more. Just by the nature on it, doing 10^18 in under 8s (minus probably 2s clojure startup time) with loops seems to be close to impossible
Hi! A question, maybe not technical, but rather about learning organization. What are the good ways to learn Clojure through participating in the projects? Of course, there is a such thing as a pull request in github. But maybe there exist richer environments around some projects, with discussions, planning and accessible for those who just start learning Clojure?
To test my library jar-copier I need the name of jars than I can expect to be present in a standard java installation, in the classpath. Does anybody know of any?
@denis: I learned Clojure here http://www.codewars.com/ and here https://tbaldridge.pivotshare.com/ after you get to a point where you feel your competent enough.. look at starting your own project. This could be a piece of software or maybe think about writing a book as a means of you learning more, whilst also teaching others.. try here: https://leanpub.com/ Its totally up to you, its just a case of doing the work.
@denik: also check hackerrank and friends. They usually allow you to implement a solution in clojure as well. I personally learned clojure by just picking up a private project I wanted to do and do it in clojure instead
Why isn’t my mocked function being called in this example:
(with-redefs-fn {#'leiningen.core.main/warn (fn [& args] (println "Mocked"))}
(leiningen.core.main/warn "foo”))
according to this page: https://clojuredocs.org/clojure.core/with-redefs-fn, I guess you missed the # sign in the second line
Ah… it gets a function, not an expression. That seems odd.
Thanks.
with-redefs
takes an expression, with-redefs-fn
can't take an expression since it's a function, so it takes a thunk
Ah, -fn is about what it is, not what it acts on. Of course, functions are no different than any other var.
correct. it is not uncommon in clojure to have both a operation
macro that takes an expression and a operation-fn
fn that takes a thunk
this is really handy for the 5% of times when you need to construct the binding map at runtime
Anyone ever encountered java.lang.RuntimeException: No such var: cljs.core.match/backtrack, compiling
when using core.match
?
hmm..then @bronsa why is there both with-redefs
and with-redefs-fn
, since it seems they have the same functionality...
so the problem / puzzle I posted above is not solvable with DP. The numbers are just too big no matter how I would optimize it
@bronsa: u r right, thanks. But a little harder to imagine some example, can you give one?
@moxaj: there is a big difference is: {} is generate hash at compiler time, (hash-map …) at run time.
@roelof take a look at http://stackoverflow.com/questions/4571042/can-someone-explain-clojures-unquote-splice-in-simple-terms
Is there a way to get CIDER to use the :repl
profile when doing cider-jack-in
? http://blog.maio.cz/2015/11/cider-slows-down-leiningen-startup-here.html seems like a super useful hack, but it also breaks CIDER...
@lvh: try this variable - https://github.com/clojure-emacs/cider/blob/master/cider.el#L92
@lvh: that should work without any modification (uhm at least it works on my machine). it just runs lein repl :headless
which should use :repl
profile.
<- emacs noob, day 3 of (spac)emacs - If I want the start (first few words) of the docstring during autocomplete or below the modeline next to the fn-signature, what is the term I should be googling for ? (autocomplete, fn-signatures and cider-doc are working)
is there a high order function in clojure that swaps the arguments so like (a-function a b)
but allow you to call it with (a-function b a)
. I thought there was but for the life of me i can’t remember the name of it
someone mentioned 'binding.pry' for Clojure recently, but I lost the link and can't find it anymore
maybe it was this one https://github.com/razum2um/clj-debugger
So here’s a really abstract, but small, clojure design question: What do you do when you are threading a value but you want to return more than just that value in a particular case? E.g. the threaded value and another piece of information? I’m not happy with any of the obvious options.
I could throw an exception. This case is kind of like a failure case, but not exactly. But I don’t want to use exceptions for flow control.
I could return [a b] where a is the threading value and b is the extra info, but this is kind of hard to work with for threading. and it makes the return value stick out like a sore thumb from the rest of the similar functions.
Break the threading flow in two at the condition point?
As I was thinking about it, I figured I wanted a monad-like thing threading (a, Option b), and bind just doesn’t execute the operation if the second in the tuple is Just _. And then I realized that I’m reimplementing exceptions.
There are a couple of implementations of those floating around.. maybe just take one and adjust as needed?
@mrg My experience has been that monads are really noisy in clojure. Like, monads the type theory is useful in building things, but monads-as-lisp-syntax is usually painful. But I think I’ve solved my problem by using exceptions.
That said, have a look at this for instance: http://adambard.com/blog/acceptable-error-handling-in-clojure/
I’d make the thing you thread a map containing the value e.g. {:a a, :b b}
instead of the value itself and can annotate it with extra stuff if you need to without altering the threading code. I’d probably call it context
.
@johanatan: Oh. I looked at that for another reason, not the syntax.