Fork me on GitHub
#clojure
<
2019-04-21
>
Ivan Koz11:04:10

@gseyffert see, transient wants implementation of IEditableCollection interface as its input, only few collections implement that interface https://gyazo.com/23105ead02ad141f14e8243c3e17ea17.png

Ivan Koz11:04:03

anyway i found the answer in Emerick's book

Graham Seyffert11:04:55

Yeah, you’re right. I’ve gotten in the bad habit of just referring to anything I can call seq on as a seq, which is incorrect

Graham Seyffert11:04:13

But my original answer is correct I believe

Ivan Koz11:04:39

answer to my question was, 'yes'

Graham Seyffert11:04:45

If you pass a collection to transient, you can still safely use the original reference

Graham Seyffert11:04:33

That would be terrible if you couldn’t!

Ivan Koz11:04:49

yeah, transients is a bit tricky to understand at first

Ivan Koz11:04:30

but thanks to awesome clojure design it's all uniform https://gyazo.com/8c6e5901537e02c95073ef290956eb64.png

usametov13:04:26

People of Clojure, I have a question. I read somewhere that it's possible to connect Clojure REPL to java app in debug mode. Did Anybody try that? Could you please share your experience/links here ? I saw one repo in GitHub, but it's more than 5 years old. It would be nice to have a modern way of doing that.

sobel13:04:23

Is there a way to force use-context-classloader false for an Uberjar?

sobel13:04:38

axiom-dom.jar won't load because it expects to classload /META-INFO/axiom.xml

roti14:04:15

is there a way to get the "next" item of an item in a sorted map?

Ivan Koz14:04:48

@roti what kind of items do you store in the sorted map?

roti14:04:27

a record type

roti14:04:50

but I have found another way to implement what I want

roti14:04:21

essentially I need to keep an index which will allow traversing the collection one at a time

roti14:04:59

and I just found out .indexOf is implemented for vectors, therefore I can use a vector instead of a sorted map and just play with the index

Ivan Koz14:04:03

@roti drop over seq of map doesn't work for you?

roti14:04:42

the use case is following: the keys are strings (the filename), the values are record types, and I need to iterate over the sortedmap (either by keeping the index, but this does not exist in a map, or by keeping the key)

Ivan Koz14:04:31

i'm not an expert, i think we can split sequence by take-while \\ drop-while so effectively next item will be the first element of drop-while sequence

yuhan14:04:28

I wonder if it's possible to implement the functions "loeb" and "moeb" in Clojure? https://github.com/quchen/articles/blob/master/loeb-moeb.md

yuhan15:04:01

just out of curiosity, I tried playing around with lazy-seq and memoization but couldn't get it to behave lazy enough to actually evaluate

yuhan15:04:23

here's a (non-working) translation of the Haskell code

(defn loeb [fs]
  (lazy-seq
    (map (fn [f]
           (f (loeb fs)))
      fs)))

(loeb
  [(constantly 1)
   #(inc (second %))
   #(inc (first %))])
;; should=> (1 3 2)

Ivan Koz15:04:13

@roti

(def my-map (sorted-map "e" 5 "d" 4 "c" 3 "b" 2 "a" 1))

(defn create-iterator
  [coll]
  (let [state (atom (seq coll))
        update-state (fn [state] (swap! state rest))]
    (fn []
      (let [item (first @state)]
        (update-state state)
        item))))


(def my-iterator (create-iterator my-map))

;;=> #'junk.core/my-map
;;=> #'junk.core/create-iterator
;;=> #'junk.core/my-iterator
;;(my-iterator)
;;=> ["a" 1]
;;(my-iterator)
;;=> ["b" 2]
;;(my-iterator)
;;=> ["c" 3]
;;(my-iterator)
;;=> ["d" 4]
;;(my-iterator)
;;=> ["e" 5]
;;(my-iterator)
;;=> nil

Ivan Koz15:04:44

must be thread safe

hlolli15:04:33

Hi, does anyone have good solution to a fast reset-able timeout. I'm already using core.async, but I'm thinking about a classical problem of making a action foo depending on the last time action bar was called, but action bar could be called dozen or hundred times a second and so performance matters a bit.

lilactown15:04:37

are you saying you only want the reaction to be run once every x interval, even if the trigger is fired n number of times within the interval?

hlolli15:04:47

@lilactown think of it a bit like idle counter, last time there was an interaction then fire the action once (screen saver for example)

hlolli15:04:32

I'm thinking now of timeout in an atom and close! it every time there's an action (and replace it with a new timeout)

lilactown15:04:34

I can't tell if you're saying no, my interpretation is wrong, or yes, my interpretation is right

lilactown15:04:06

I think you're talking about debouncing

hlolli15:04:27

🙂 that could be

lilactown15:04:18

in which case there's this simple CLJS example: https://gist.github.com/swannodette/5888989

🎉 4
lilactown15:04:47

there's probably a simpler way, without core.async

hlolli15:04:42

thanks, exactly what I need!

yuhan16:04:07

How can I "subclass" a lazy sequence to extend a custom protocol?

yuhan16:04:32

similar to defrecord for maps

yuhan16:04:43

ie. I don't want all clojure.lang.Sequential or such to support the protocol, but only a custom type which should still remain compatible with all library functions

lilactown20:04:57

you could perhaps extend a particular sequence to your custom protocols via metadata

yuhan20:04:47

Oh! I didn't know you could extend protocols using metadata, looks like a new thing in 1.10

roti19:04:05

@nxtk thanks, but I need to go both ways, forward and backward (it's essentially scrolling though a file list). it needs to be fast. i started with a sorted map, because I wanted to also have fast access by filename. i am now using a vector instead, which works, but does not have fast access by filename

jumpnbrownweasel20:04:40

There is https://github.com/frankiesardo/linked although I haven't tried it. I would probably maintain both a vector and a hash map, if both types of access must be extremely fast and the list is largish. Another approach is to just use a vector in name order, and do a binary search within the vector when doing a name lookup.

Ivan Koz20:04:28

@UBRMX7MT7 just checked it, doesnt allow for previous\next node navigation

jumpnbrownweasel00:04:56

A vector seems like the best choice, if binary search is fast enough for you.

jumpnbrownweasel00:04:06

If you're using Clojure on the JVM you can use Java's binary search:

(java.util.Collections/binarySearch ["a" "b" "c"] "b")
=> 1

roti19:04:10

a combination of sorted-map with vector would probably work, I was just trying to use only sorted-map, but I guess I need something like LinkedHashMap

theeternalpulse22:04:01

i'm trying to calculate a per-frame movement factor for a cursor i'm drawing For example I have

(defn next-pointer
  "Moves the pointer, taking to account speed"
  [{:keys [x y direction speed active] :as pointer} frame fps]
  (let [[dx dy] direction
        norm-time (mod frame fps)
        d-pixels (if (zero? norm-time)
                   speed
                   0)]
    (assoc pointer
           :x (+ x (* dx d-pixels))
           :y (+ y (* dy d-pixels)))))
In this example, there's a speed of the format pixels/second. I want to determine at each frame by what factor I should increase x. My first approach was to determine the position of x at an origin, but due to adding to the x, it became more of an acceleration. In this case, I kind of lazily just get the next speed only when I'm at a "second" which is hardly smooth. The missing piece is the other part of that if. I've tried making norm-time (/ (rem frame fps) fps) to see if that would help, but it gives me kind of a stepping movement. I'm always bad at factoring time in my drawing apps, is there a missing link I'm not seeing

theeternalpulse22:04:54

direction is simply a unit vector so it's either 0 1 or -1 for any element in the vector

theeternalpulse23:04:56

I guess I need to keep track of dt

theeternalpulse00:04:35

that seemd to be it, I just had to pass in dt, and keep it updated each frame

(defn next-pointer
  "Moves the pointer, taking to account speed"
  [{:keys [x y direction speed active] :as pointer} dt]
  (let [[dx dy] direction
        d-pixels (* speed dt)
        next-x (+ x (* dx d-pixels))
        next-y (+ y (* dy d-pixels))]
    (assoc pointer
           :x next-x
           :y next-y)))

Eric Ervin03:04:59

Thanks got posting this. I was dozing off in front of an Elixir tutorial when I saw this and it woke me up. Downloaded your experiments and went through them, getting acquainted with clojure2d. I'll certainly have to show 07_dedication.clj to a cat (actual feline) I know. It seems like some complexity with this speed thing can be removed. In my doodles, I typically express the speed in the number of pixels x/y it will change each frame. Then the distance it will travel each second is dependent on the frame rate, which is actually an effect I'd want as if I was changing the speed of a projector.

Eric Ervin03:04:55

Ya. I have speed and direction combined.

theeternalpulse05:04:10

Haha, dedication is actually something I found in a tweet for pico8, I wanted to disect it and replicate it in clojure2d, to little success. Need to find the tweet to show it.

theeternalpulse05:04:05

I try to translate as much to something "relatable" so I don't have to do guesswork or adding magic numbers to get movevement down. But most of it I'm trying to work my way into understanding without looking at formal graphics programming info.

Eric Ervin18:04:29

Sounds good. I might translate some of your experiments into Quil

theeternalpulse03:04:02

feel free, many of them are unfinished, or simply ideas that I took to a point that I was satisfied with, but may not make much sense now. It's my sketchpad really.