Fork me on GitHub
#beginners
<
2017-06-20
>
vitruvia00:06:39

interpose puts an argument between elements in a given collection or string, but is there a way to put this argument also after the last element? For example: (some-function "a" [:x :y :z]) --> (:x "a" :y "a" :z "a")

noisesmith00:06:34

+user=> (interleave [:x :y :z] (repeat "a"))
(:x "a" :y "a" :z "a")

victora01:06:49

How can I merge two sorted-set-by so that they continue to be one? If both use the same comparator

noisesmith01:06:16

have you tried into?

victora01:06:26

No, I haven't. Let me try that

victora01:06:52

Works perfectly, thanks @noisesmith !

noisesmith01:06:54

+user=> (into (sorted-set-by #(compare (:a %) (:a %2)) {:a 0} {:a 1 :b 2}) [{:a 3} {:a -1}])
#{{:a -1} {:a 0} {:a 1, :b 2} {:a 3}}

noisesmith01:06:18

they don't need to both be the same - it will use the type of the first one

victora01:06:31

Yeah, still good though. I haven't seen that function around

noisesmith01:06:15

into is extremely powerful - it can also use transducing functions

victora01:06:27

Like, transform the element then insert?

noisesmith01:06:48

or filter it out

noisesmith01:06:54

or duplicate, or...

victora01:06:41

filter it out is exactly what i need

noisesmith01:06:55

user=> (into (sorted-set-by #(compare (:a %) (:a %2)) {:a 0} {:a 1 :b 2}) (filter (complement :b)) [{:a 3} {:a -1} {:a 2 :b 12}])
#{{:a -1} {:a 0} {:a 1, :b 2} {:a 3}}

noisesmith02:06:09

only adds items that with falsey or missing :b key

victora02:06:19

I think you forgot a (

victora02:06:34

But either way I get it

noisesmith02:06:23

that's a literal paste from my repl

victora02:06:31

is the filter inside the (sorted-set-by ... ?

noisesmith02:06:34

filter is not meant to be wrapping the vector at the end

noisesmith02:06:46

no, it is the second of three args to into

victora02:06:04

(into to fn from)

victora02:06:40

Ok, I was reading it wrong

victora02:06:49

missplacing the parenthesis in my head

victora02:06:31

Anyone uses vim to edit clojure?

dominicm08:06:50

victora: community of us in #vim-fireplace, I wrote a blog post too

dominicm08:06:35

victora: https://juxt.pro/blog/posts/vim-1.html A bit of an overview of the tools I use & know of

victora02:06:42

Is there any way I can make it indent like it was supposed to?

noisesmith02:06:28

I use vim-clojure-sexp and with the =- indents the current top level form

victora02:06:32

Like,

(filter (fn [x]
   (...
turns into
(filter (fn[x]
            (...
with my current settings

noisesmith02:06:50

that's the correct indentation

victora02:06:39

Really? That's not how it's shown in the documentation

victora02:06:25

Any plugins/stuff you recommend? I couldn't get the REPL to work inside vim. Is it worth it?

noisesmith02:06:20

fireplace is OK but I don't use it much

noisesmith02:06:58

for indenting, there's a lein plugin called cljfmt that will fix stuff most editors don't (including whitespace stuff)

victora02:06:18

Just tested cljfmt, sometimes it uses 1 space instead of 2. Strange

victora02:06:27

i'll give it a read soon(tm)

victora02:06:30

@noisesmith you told me some days ago that if I need to carry variables during a reduce then I should use a map. Is there any canonical name for this map?

noisesmith02:06:52

accumulator or acc for short

noisesmith02:06:50

or you can just destructure it inline to get the keys out of the function arg (reduce (fn [{:keys [count found blacklist]} el] ...) {:count 0 :found [] :blacklist #{}} coll)

victora02:06:53

Do I access it with count or :count inside the fn?

noisesmith02:06:12

count - bad name since that's a function in clojure.core

vitruvia02:06:32

what is the best way to take an element out of a sequence for assertion purposes. Like if I want (= (my-last [1 2 3]) 3) to return just 3 instead of (3). I'm currently just using first in front of everything but I'm not sure if that's the best way.

lilactown03:06:34

@vitruvia (last [1 2 3]) returns 3 in my repl

abarylko06:06:41

I'm trying to write a macro that generates a deftest with metadata .... but not sure how to add the metadata ... any help?

vitruvia10:06:31

@lilactown yes the original function already implements something like what I'm trying to do. But notice that I'm doing my own "last" function and would like to get a similar result

lilactown14:06:04

vitruvia: without seeing your code, I'm not sure what you might want to change

lilactown14:06:20

here's a very simple recursive implementation:

(defn my-last [[x & xs]] (if (> (count xs) 0) (my-last xs) x))

vitruvia15:06:56

(defn my-last
  [a-seq] 
  (loop [x a-seq]
    (if (empty? (rest x))
      (first x)
      (recur (rest x)))))

sb12:06:52

Hi, what do you think could I find somewhere a “html -> image rendering” library/ repository?

sb13:06:28

Still the best option the PhatomJS?

jasoons14:06:04

Hi guys, I'm brand new to cljs, any recommended tutorials? This looks good https://github.com/magomimmo/modern-cljs/, I am a pretty decent programmer with small amounts of functional programming experience (mostly Haskell though), and am looking at cljs for webdev. Any advice would be greatly appreciated 🙂 (I'm looking forward to diving in deep)

kauko18:06:43

jasoons: Luminus has good docs, wouldn't really call it a tutorial, but you might be able to follow along and pick up a thing or two

kauko18:06:26

Luminus is a template, or micro framework. I like to recommend it to beginners, even though there's quite a lot to it.. but you get started quickly with a solid base to build on 🙂

kauko18:06:06

If you're just learning Clojure, then maybe Brave New Clojure?

jasoons18:06:32

Awesome, thanks for the advice 🙂 Brave New Clojure also looks really good!

abarylko15:06:12

@mathew.vijay I’ll try that, thanks ….

jasoons15:06:04

@U0CKCLN00 Great thanks 🙂 I'll take a dig at it!

Jon15:06:24

this is how you compile ClojureScript. to try an app, you will need http://reagent-project.github.io , also #reagent

jasoons16:06:02

Awesome, thanks, I'll give that a try

okwori15:06:53

Hi guys, any really good and easy to understand material on core.async out there, I would appreciate some some links or so...

colinkahn16:06:17

I found this helpful when I was first learning about core.async: http://www.braveclojure.com/core-async/

victora18:06:45

I am having real problems getting a post body payload with compojure. Can somebody give me a quick hand?

guy18:06:58

@victora i can potentially help!

victora18:06:51

guy: How can i quickly transform a HttpInput which is a json payload to clojure's data structures?

victora18:06:27

I'm using slurp and then json/read-str but that is a huge workaround

guy18:06:49

let me check, i think compojure might do something for you already

guy19:06:32

Potentially you might want to use something like this https://github.com/ring-clojure/ring-json

guy19:06:25

here is the example site

guy19:06:35

Let me know if that helps

victora19:06:50

I've been trying to use ring-json and ultimately failing. Let me try again with this example, brb

guy06:06:25

Did it work for you in the end?

victora02:06:56

I'm using ring-json and it helps, but I couldn't make it give me the json parsed. Since this project is time-sensitive, I ended doing the workaround since it wasn't so ugly and dedicated myself to another part

patw23:06:09

Has anybody ever received this error?

No implementation of method: :kv-reduce of protocol: #'clojure.core.protocols/IKVReduce found for class: clojure.lang.ArraySeq

noisesmith23:06:51

what were you trying to do when you got it?

noisesmith23:06:09

it means that something was trying to update a hash-map and got an array

patw23:06:32

noisesmith: this is one of the function arities, it’s a bit of a mess but I’m trying to persist two arrays that need to be updated so I’m sending them in a map:

([graph] (let [distances (adjacency-matrix graph)
                 vertices (get-vertices graph)
                 parents (parent-matrix distances)]
             (reduce-kv (fn [acc intermediate-idx _]
                            (reduce-kv (fn [_ start-idx row]
                                           (reduce-kv #(floyd-warshall acc intermediate-idx start-idx %2) {} row))
                                       {} distances))
                        {:distances distances :parents parents} vertices))))

patw23:06:58

it’s getting thrown on the first call to reduce-kv

noisesmith23:06:14

right, reduce-kv needs a hash-map as its last arg

noisesmith23:06:24

and you seem to be giving it an array, that’s simply not valid

noisesmith23:06:11

why not use reduce? it looks like what you really want is reduce with two accumulators, which is easy to do with destructuring and a vector literal

patw23:06:04

yeah, good point. I’m just using the indices, so I should really just reduce over a range. Thanks !

zlrth23:06:53

is visualvm the recommended way to profile clojure code for CPU hotspots?

noisesmith23:06:39

yourkit is better, but also not free (though they have a policy of donating it to significant open source projects)

noisesmith23:06:58

and laziness makes some of the results counterintuitive (things show up under the code that realized the collection, not the code that created it - so eg. “rest” will show up as something taking a lot of time)

zlrth23:06:38

thanks. i looked at taoensso/tufte, and thought it didn't suit my needs, because it doesn't look like it can walk the tree of expressions and show hotspots. i'll try visualvm to start with, and keep in mind your tip. thanks again!

noisesmith23:06:59

last I checked there was a free preview of yourkit too

noisesmith23:06:02

@mfm also, once you have narrowed down your hotspots with your profiling tool, criterium is the best library for measuring your improvements to the hotspot function