This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-09-08
Channels
- # 100-days-of-code (1)
- # announcements (7)
- # beginners (63)
- # cljs-dev (39)
- # clojure (78)
- # clojure-dev (40)
- # clojure-italy (4)
- # clojure-nl (22)
- # clojure-russia (5)
- # clojure-spec (5)
- # clojurescript (60)
- # cursive (8)
- # datomic (6)
- # emacs (1)
- # figwheel-main (53)
- # fulcro (19)
- # jobs-discuss (11)
- # mount (1)
- # off-topic (3)
- # om (1)
- # pedestal (9)
- # philosophy (1)
- # re-frame (19)
- # reagent (4)
- # reitit (5)
- # shadow-cljs (66)
- # tools-deps (64)
Specter can help in these cases
user> (use 'com.rpl.specter)
user> (transform [MAP-VALS MAP-VALS]
inc
{:a {:aa 1} :b {:ba -1 :bb 2}})
{:a {:aa 2}, :b {:ba 0, :bb 3}}
Hey everyone. I'm doing some coding games right now, wondering if there any other/better way of writing this? https://www.pastery.net/tcyjda/
if both players kill each other at the same time, player 2 wins. is that intentional?
ah, no
i find g easier to read than g2. i like the streaming part, but the expression that checks the winner is very complicated 😉
if i understand it correctly, g2 also favors player 2, so at least it's consistent 🙂
i think extracting the two functions that check whether the battle is over and check which player won would make g2 much more readable
definitely, although g2 is also 4x slower and it has a typo that I fixed now. I was mostly wondering if there's a third way of doing this, and/or whether the g implementation is idiomatic (I've heard both yes and no) Extracting the functions is a great idea but I'm trying to keep the character count down (this is an exercise).
as long as the logic is only encoded in numbers, you could also just divide X by D and Y by A
needs cleanup and edgecase checking but you get the idea:
(defn g [[x y a d]]
(let [p1 (quot x d)
p2 (quot y a)
winner (if (< p1 p2) 2 1)]
[winner (if (= winner 1) p2 p1)]))
(defn g3 [[X Y A D]]
(let [p1 (/ X D)
p2 (/ Y A)]
(if (< p1 p2) [2 p1] [1 p2])))
I guess here you could replace / with a "fight" function
players attack each other until hp of either one reaches 0, then it returns the winner and the rounds it took
I was recently wondering, does anyone have any cool examples of Clojure Vs. Other Language, where the clojure code is much shorter/impressive due to its paradigms?
@coinedtalk I like this one: Java: https://github.com/stuarthalloway/programming-clojure/blob/master/data/snippets/isBlank.java Clojure: https://github.com/stuarthalloway/programming-clojure/blob/192e2f28d797fd70e50778aabd031b3ff55bd2b9/src/examples/introduction.clj#L4-L5
thanks 🙂
I'm having a really weird issue
(defn generate-monster [xmax ymax]
(Zombie. (rand-int xmax) (rand-int xmax)))
(def monsters (atom [(generate-monster 10 10) (generate-monster 10 10) (generate-monster 10 10)]))
How can I replace the times that it's called with a function? Repeatedly and Repeat break it for some reason@coinedtalk (repeatedly 3 #(generate-monster 10 10))
fails?
IllegalArgumentException No implementation of method: :move of protocol: #'myapp.core/Monster
ah,, (into [] (repeatedly 3
has to be a vector(?)
That’s news to me in that case. I don’t know why wrapping it in into
should make any difference. If the inner form, (repeatedly…)
fails, it should fail before into
even gets its hands on it.
The :move
thing sounds like it has something to do with the protocol implementation, rather. Seeing as generate-monster
has randoms in it, maybe it has something to do with you randomly hitting OK ints?
OK ints, like True?
I'm going to put up a sample soon of adding persistent cookies to javafx webview in clojure vs the equivalent in java, it's about a twentieth the size of sloc
Rosetta code is great for language to language samples, but it's hard to beat J in most listings
When I add a new dependency to project.clj
what should I do for the REPL to pick it up? (proto-repl in this case)
could also take a look at https://github.com/pallet/alembic, which claims to load new deps without killing the REPL
If you wanted to use emacs, https://github.com/clojure-emacs/clj-refactor.el can also hot-reload dependencies
(def foo [method path & [opts]] opts)
(foo 1 2 3 4 5 6) => 3
(def foo [method path & opts] opts)
(foo 1 2 3 4 5 6) => [3 4 5 6]
for example, if you have parameters such as [[a]]
and pass a vector to the function, then a == the first element of the vector
i feel stupid for not writing it out on my repl 😞 sorry @trailcapital
I see, so calling it with one param results in another call so I have two function calls
does that ever come up in practice? In Python/JS, I don’t think I’ve ever come across a case where a function def needed to be way different at 2 args vs 3 vs N