Fork me on GitHub
#beginners
<
2016-07-22
>
cezar00:07:46

oh well, for the size of my trees (less than 100 nodes) it looks like a linear search will be fast enough:

(def data
  [
   {:span [21 22] :id 13}
   {:span [10 20] :id 12}
   {:span [17 19] :id 11}
   {:span [16 17] :id 10}
   {:span [15 17] :id 9}
   {:span [13 14] :id 8}
   {:span [11 12] :id 7}
   {:span [8 11] :id 6}
   {:span [7 9] :id 5}
   {:span [6 9] :id 4}
   {:span [6 8] :id 3}
   {:span [3 5] :id 2}
   {:span [0 2] :id 1}])


(defn find-intervals [data, n]
  (filter
    (fn [{span :span}]
        (and
          (>= n (first span))
          (< n (second span))))
    data))

cezar00:07:21

(time
  (dotimes [n 20000000]
    find-intervals data (mod n 22)))
"Elapsed time: 480.723187 msecs"

roberto03:07:10

is there a merge for vectors?

akiva03:07:41

You could do something like (vec (concat [0 1 2 3] [4 5 6 7])) ;;=> [0 1 2 3 4 5 6 7]

Chris O’Donnell04:07:43

or (reduce conj [0 1 2 3] [4 5 6 7])

rauh05:07:14

@roberto: (into [0 1 2] [9 8]) is probably best, it'll use transients.

Chris O’Donnell05:07:50

into is really great. I should remember to use it more.

Alex Miller (Clojure team)05:07:46

tastes even better with transducers

Alex Miller (Clojure team)05:07:57

(into [] cat [[0 1 2 3] [4 5 6 7]]) ;;=> [0 1 2 3 4 5 6 7]

Chris O’Donnell05:07:58

And the transducer solution works with arbitrarily many vectors, though it doesn't use transients. Nice!

Chris O’Donnell05:07:15

it does? that's awesome

Alex Miller (Clojure team)05:07:31

it will collect into the vector at the front (made transient)

Alex Miller (Clojure team)05:07:58

it’s way more efficient than the vec concat version above

Chris O’Donnell05:07:12

it uses transients because into uses transients?

Alex Miller (Clojure team)05:07:25

plus you get to have your cat around

Alex Miller (Clojure team)05:07:31

so it’s internet friendly

Drew Verlee11:07:37

what does “bouncing the repl” mean?

d-side12:07:06

@drewverlee: shutting it down and starting it back up, I believe.

Drew Verlee13:07:02

@d-side: thanks that makes sense in the context i was reading.

akiva17:07:14

This is what I get for not paying attention to transducers. [looks haughty]

cezar20:07:06

can one use transients with a sorted-map? I get errors but nowhere in the documentation does it say that transients can't be used with a sorted-map

cezar20:07:35

(def tmap (transient sorted-map))
CompilerException java.lang.ClassCastException: clojure.core$sorted_map cannot be cast to clojure.lang.IEditableCollection, compiling:(form-init206135609334303987.clj:1:11) 

dg20:07:59

It looks like the core sorted maps don't implement the right protocol, but there's https://github.com/clojure/data.avl which demonstrates using them with transient.

dg20:07:29

Though in your code, note you'd need (transient (sorted-map)) even if it did work.

krchia21:07:17

what would be a good way to write this so recur is the last form?

krchia21:07:04

nvm, i just rewrote it without tail optimization

krchia21:07:11

that can wait until i’ve a working version

polymeris21:07:35

Hi. Have a cljs question regarding foreign-libs. I have this in my dev cljs build:

:foreign-libs    [{:file     "resources/browserified/foo.js"
                   :provides ["js.foo"]}
And in my cljs file I have (:require [js.foo]). This works, I get access to the stuff in foo.js from the browser console.

polymeris21:07:22

Problem is, when I try to load that same cljs file in the figwheel REPL I get java.lang.IllegalArgumentException: Namespace js.foo does not exist

polymeris21:07:32

What could I be doing wrong?

dg21:07:41

You may try asking in #C1A38UB5X, though I'm not sure how active it is

polymeris21:07:40

Seems to be the same issue

shaun-mahood22:07:51

@polymeris: Best channel to ask is #C0B22RS2Y, it's pretty active.

polymeris22:07:09

Ok. I'll try that. Thanks, @shaun-mahood & @dg