Fork me on GitHub
#beginners
<
2017-10-13
>
debamitro02:10:07

Hey beginner here, can someone point me to the simplest way to use a java library from clojure? Specifically I want to try using jsyn - I saw it being used in alda and I want to play with it a bit

noisesmith02:10:10

the easiest way is to make a leiningen project, add the library to your project.clj file, and use it via interop https://clojure.org/reference/java_interop

clarkema08:10:00

I’m trying to make a rake / mix-syle task in Lein, but am getting stuck on passing arguments in. What I want to be able to do is run ‘lein name-mapping foo.txt bar.txt’ and have that run a function for me, passing in the two filenames. Currently I have this in project.clj: :aliases {“name-mapping” [“exec” “-ep” “(use ’mapper.author-mapper) (name-mapping-main)“]})

manutter5112:10:11

Tangentially, if you’re interested in command-line clojure, you’ll want to check out planck and/or lumo (if you haven’t already). https://github.com/anmonteiro/lumo http://planck-repl.org/

clarkema08:10:28

That runs the function, but I can’t get a hold of the arguments: command-line-args is nil in the name-mapping function

clarkema08:10:46

It seems like this should be more straightforward than I’m making it — am I missing something?

clarkema08:10:13

Actually in this particular case it looks like I can alias use lein run -m instead, although I’d still be interested in knowing whether there’s an answer to the exec approach

boogie66609:10:15

hello all, does anybody know of something like "lein deps :tree" that makes a propper tree (i.e. not with indents but propper data)

madstap15:10:03

lein vizdeps makes a graphviz graph. I find it much nicer than lein deps tree for reasoning about my deps. It presumably makes some kind of tree to feed to graphviz, maybe you can check out the source code.

manas.marthi09:10:09

Hi Cheenu welcome

grierson11:10:12

When using test.check in a deftest, should I check on the :result that all tests passed. (deftest X (is (:result (tc/quick-check 100 my-prop-test)))

yogidevbear12:10:33

Hello Clojurians 👋 I'm trying to play around with the following vector of maps. I have the definition for the vector example and I'd like to use (map) to add a calculated map to each map within example (e.g. each map's :some-count value divided by the max :some-count: value from the entire vector of maps).

(def example [{:name "Foo" :some-count 18} {:name "Bar" :some-count 24}])
(def max-some-count (reduce (fn [result coll] (max result (:some-count coll))) 0 example))
(map
  (fn [max-count coll] (conj (/ (:some-count coll) max-count) coll))
  max-some-count
  example)
My example above is currently complaining about an IllegalArgumentException Don't know how to create ISeq from: java.lang.long clojure.lang.RT.seqForm (RT.java:542)

sundarj12:10:52

do you mean reduce instead of map?

yogidevbear12:10:55

I'm sure there will be a much cleaner / more idiomatic way of achieving this result, so would love seeing these to learn from my mistakes, but the current version is for specific illustration purposes (even though it is probably quite verbose)

yogidevbear12:10:19

Hmmm... Maybe this is a bad use-case for map 🤔

sundarj12:10:43

the function you pass to map only takes a single argument if you only pass it one collection

sundarj12:10:23

and you can't map over a number

yogidevbear12:10:35

Isn't it taking the max-count ( in the form of max-some-count) and the collection (`example`)?

sundarj12:10:54

no, map takes a single function, and any number of collections

yogidevbear12:10:02

Ah, I think you are right, I'm using the reduce argument definition

yogidevbear12:10:19

It's more in line with (reduce f val coll)

yogidevbear12:10:37

I was using (map f val coll), but that is invalid

sundarj12:10:56

try replacing max-some-count with (repeat max-some-count)

sundarj12:10:11

to make it a collection

yogidevbear12:10:16

I think that gets me a step further, but now I have a ClassCastException

yogidevbear12:10:38

clojure.lang.Ratio to clojure.lang.IPersistentCollection

yogidevbear12:10:05

Let me go back to the drawing board on this and think if I'm approaching this from the right point of view

sundarj13:10:00

conj takes the collection first, then the value

yogidevbear21:10:59

Hi again. Is there a way to perform inc on a range?

yogidevbear21:10:50

e.g. take the list from (range 5) and use inc to end with (1 2 3 4 5)

bwstearns21:10:52

Like, (map inc (range 5))?

bwstearns21:10:49

You could also just do (range 1 6)?

yogidevbear21:10:01

map is perfect. Thank you Brian :+1:

yogidevbear21:10:27

Irony is that I'm trying to use this within an example to better grok map

bwstearns21:10:09

lol. nice. I wasn’t sure of the context. If it helps, one of the things that made it click for me was seeing stuff like (map :fname people) where a person map might be a huge object with lots of fields. Being able to concisely slice out data is nice.

bwstearns21:10:11

or like (map #(select-keys % [:a :b]) foo)

yogidevbear21:10:11

Yes, Sundar mentioned the same method to me earlier. That makes sense. I think it's more the joining of different collections that is confusing my at the moment (i.e. (map f c1 c2)). It makes sense when I look at a pre-existing example, but when I try and apply it to something relevant to my types of data sets, I tend to hit a bit of a brick wall, if that makes sense.

yogidevbear21:10:33

Something like this, but potentially with more complex collections (map hash-map (repeat :position) (map inc (range 5)))

madstap21:10:31

(map vector coll1 coll2) can be helpful to understand what arguments the function will be called with.

bwstearns21:10:34

also zipmap might be what the map hash-map example is driving at?

yogidevbear21:10:07

I'll make a note to look further into zipmap

yogidevbear21:10:27

Might do that once I get my current example working and see how I can improve upon my result 🙂

bwstearns21:10:38

oh. let’s say you have a list of users and a list of addresses and you want to merge them into something. (map (fn [person addr] (assoc person :address addr)) people addrs)

yogidevbear21:10:13

I think that's a pretty close match for the type of thing I'm trying to get working now

yogidevbear21:10:49

Haha, this feels like a very far distance from being idiomatic: (map (fn [coll cPos] (assoc coll :position cPos)) (reverse (sort-by :follow-count awards)) (#(map inc (range (count %))) awards))

bwstearns21:10:32

lol. yeah. I’m not sure what the function being mapped over the list is doing (as far as human meaning) but maybe it could be its own function for clarity. also maybe do the counting and incrementing in the fn part since now you’re mapping over the range twice (once for each map used). Am I correct in guessing that awards is a collection of lists?

yogidevbear21:10:48

So "basically" this part (reverse (sort-by :follow-count awards)) is sorting awards (a vector of maps) by the :follow-count key value

yogidevbear21:10:29

☝️ that's the coll argument being passed into f for map

yogidevbear21:10:44

(#(map inc (range (count %))) awards) is generation a list (for cPos argument) of sequential numbers to be used to make a new key-value pair within each hash-map for an "ordering key"

yogidevbear21:10:10

It might be a little redundant (the example), but it's for illustration purposes

bwstearns21:10:11

ok cool. I think the thing that was giving me pause was that the two collections being fed into the fn were both derivative of the awards collection.

noisesmith22:10:17

@yogidevbear there’s sort-by which can take a comparison argument to decide the order of the result

noisesmith22:10:57

oh, you are already using sort-by, and both sort and sort-by take the optional comparison

noisesmith22:10:56

(ins)user=> (sort > [6 1 2 3 4])
(6 4 3 2 1)
(ins)user=> (sort-by :a > [{:a 6} {:a 1} {:a 2} {:a 3} {:a 4}])
({:a 6} {:a 4} {:a 3} {:a 2} {:a 1})

yogidevbear22:10:12

@noisesmith That's pretty neat

yogidevbear22:10:02

Is there a way to sort in different directions for juxt? e.g. (sort-by (juxt :parent-name :follow-count) awards) sorts by both :parent-name and :follow-count in ascending order

yogidevbear22:10:17

I want to sort by :parent-name asc, :follow-count desc

noisesmith22:10:33

(ins)user=> (sort-by (juxt :a :b) (complement compare) [{:a 6 :b 2} {:a 6 :b 3} {:a 1} {:a 2} {:a 3} {:a 4}])
({:a 6, :b 2} {:a 6, :b 3} {:a 1} {:a 2} {:a 3} {:a 4})

noisesmith22:10:42

oh, mixing asc and desc is trickier

yogidevbear22:10:55

Yip, that's what I'm realising 🙂

noisesmith22:10:22

instead of (complement compare) you can use a different function, as long as it returns true for the right order of args, and false for the wrong one, when given two args

noisesmith22:10:12

oh wait, complement compare was totally wrong anyway