Fork me on GitHub
#beginners
<
2018-09-08
>
Denis G07:09:32

How can you map values of the dictionary/map

Denis G07:09:29

map key-value pairs then into {}

henrik08:09:01

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}}

Denis G08:09:17

any function in order to return first that satisfies the predicate

Denis G08:09:30

like (first pos? [-1 -2 3])

mavbozo09:09:38

@denisgrebennicov use some like this (some #(when (pos? %) %) [-1 2 -3])

deliciousowl09:09:47

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/

Timo Freiberg09:09:10

if both players kill each other at the same time, player 2 wins. is that intentional?

Timo Freiberg09:09:42

i find g easier to read than g2. i like the streaming part, but the expression that checks the winner is very complicated 😉

Timo Freiberg09:09:08

if i understand it correctly, g2 also favors player 2, so at least it's consistent 🙂

Timo Freiberg09:09:51

i think extracting the two functions that check whether the battle is over and check which player won would make g2 much more readable

deliciousowl09:09:30

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).

Timo Freiberg09:09:49

as long as the logic is only encoded in numbers, you could also just divide X by D and Y by A

schmee10:09:27

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)]))

deliciousowl10:09:45

(defn g3 [[X Y A D]]
  (let [p1 (/ X D)
        p2 (/ Y A)]
  (if (< p1 p2) [2 p1] [1 p2])))

deliciousowl10:09:30

I guess here you could replace / with a "fight" function

deliciousowl09:09:27

players attack each other until hp of either one reaches 0, then it returns the winner and the rounds it took

deliciousowl10:09:28

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?

deliciousowl13:09:26

I'm having a really weird issue

deliciousowl13:09:30

(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

henrik13:09:31

@coinedtalk (repeatedly 3 #(generate-monster 10 10)) fails?

deliciousowl13:09:36

IllegalArgumentException No implementation of method: :move of protocol: #'myapp.core/Monster

deliciousowl13:09:02

ah,, (into [] (repeatedly 3

deliciousowl13:09:10

has to be a vector(?)

henrik13:09:14

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.

henrik13:09:20

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?

deliciousowl14:09:35

OK ints, like True?

henrik14:09:51

More like, is 0 an acceptable input?

ahungry15:09:46

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

ahungry15:09:26

Rosetta code is great for language to language samples, but it's hard to beat J in most listings

ralf19:09:46

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)

ralf19:09:00

i did lein deps but my project can't find cheshire

dadair19:09:48

@gold88 usually you have to restart the REPL entirely to pick up a new dep

enforser19:09:07

could also take a look at https://github.com/pallet/alembic, which claims to load new deps without killing the REPL

dadair19:09:12

If you wanted to use emacs, https://github.com/clojure-emacs/clj-refactor.el can also hot-reload dependencies

ralf19:09:33

I did a noobie mistake and had an invalid require in another file

ralf19:09:44

that kind of led to proto-repl refusing to reload deps in my other file

ralf19:09:45

works now

ralf20:09:59

as params

ralf20:09:17

whats the difference between [method path & [opts]] and [method path & opts]

ralf20:09:39

i know that & means all following params are to be known as opts

ralf20:09:56

i think the first example destructures something?

enforser20:09:55

(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]

enforser20:09:21

The [...] decstructures the vector of additional arguments

enforser20:09:04

for example, if you have parameters such as [[a]] and pass a vector to the function, then a == the first element of the vector

ralf20:09:04

i feel stupid for not writing it out on my repl 😞 sorry @trailcapital

ralf20:09:36

so this is basically making the third parameter optional and discarding any additional

ralf20:09:53

makes sense because the third parameter is a map to configure the request

enforser20:09:33

You'll also see things like multi-arity functions to make things optional.

enforser20:09:35

(defn foo
  ([x]
   (foo x nil))
  ([x y]
   [x y]))

(foo 1) => [1 nil]
(foo 1 2) => [1 2]

ralf20:09:47

I see, so calling it with one param results in another call so I have two function calls

kenj20:09:03

are there big advantages between multi-arity functions compared to var-arg ones?

schmee20:09:14

you can have as many multi-arity ones as you like but only one varargs

kenj20:09:27

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

enforser20:09:32

It could be useful in cases where you want the optional param to be in a non-last position. Imagine you're making a function that accepts a docstring in the same way defn does...

(defn foo
  ([a c]
   (foo a "default" c))
  ([a b c]
   (foo a b c)))

enforser20:09:26

maybe you want a function that accepts only 2, 4, or 6 params - but not 1, 3, or 5

(defn foo
  ([a b]
   (foo a b nil nil nil nil))
  ([a b c d]
   (foo a b c d nil nil))
  ([a b c d e f]
   ...)))