This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-24
Channels
- # adventofcode (11)
- # architecture (12)
- # beginners (132)
- # boot (19)
- # cider (26)
- # clojure (69)
- # clojure-dusseldorf (4)
- # clojure-gamedev (1)
- # clojure-italy (46)
- # clojure-nl (4)
- # clojure-serbia (1)
- # clojure-switzerland (2)
- # clojure-uk (91)
- # clojurescript (79)
- # css (4)
- # cursive (2)
- # datomic (16)
- # docs (9)
- # duct (20)
- # editors (94)
- # fulcro (15)
- # graphql (2)
- # hoplon (1)
- # instaparse (7)
- # jobs (3)
- # lein-figwheel (3)
- # leiningen (2)
- # lumo (40)
- # mount (35)
- # off-topic (19)
- # reagent (18)
- # reitit (1)
- # shadow-cljs (123)
- # specter (7)
- # sql (5)
- # test-check (4)
- # tools-deps (38)
- # vim (20)
- # yada (9)
A question about data-structures: In a view I'm storing a bunch of panels in my app state (Reagent-style). Each panel has an uuid. I want to be able to add and delete panels. But I also want to be able to reach into the panel and access them by ID.
On the face of it, I have two options. I could store the panels in a vector, which means that access by ID O(n).
Or I could use a hash-map as the primary data structure. In that case, access by ID is fast, but I need to keep track of order in some other way, like another vector or using a "position" attribute.
Neither seems like a great option. Is there an approach I'm missing? What data structure is the most appropriate?
Since it's UI n will be in [0..100] at most so you shouldn't be thinking about algorithmic complexity. In practice this will look like everything happens in constant time, no ? But to answer your question you can use a sorted-map (you must provide an ordering function) or an ordered-map (keeps track of the insertion order). The former is in clojure.core while the latter is at https://github.com/amalloy/ordered
Not sure it support clojurescript though
@tristefigure interesting point about algorithmic complexity... Certainly a cljs.core/PersistentArrayMap is used for up to 32 elements anyway.
Was this irony ? I'm definitively interested in having a conversation about when to apply algorithmic complexity. I don't know the specificities of the context you're working on but to me it doesn't look like one should care about time complexity when it comes to traditionnal ui elements since to be meaningful to the user there must be few elements. The only time where I really have to take care of algorithmic complexity is when I write optimization algorithms which has yet to happen in my professional life. And when I'm optimizing stuff at work it's never pure algorithmic complexity or at least it's masked under higher level concerns, like doing batch updates to a DB. It's never has pure as in the textbooks. The kind of complexity I always have on my mind is what I call code complexity, i.e. the complexity of the algorithm that is writing the code, ie. my tools my abstraction and I. Just yesterday, out of curiosity I rewrote almost 2000 lines of ruby in 150 lines of Clojure. As a result I could perform changes that whould have taken me 1 day to unroll in 1 minute.
Which I guess scans rather than relying on hashes
I don't think cljs.core/sorted-map
supports supplying your own sort function, so the map would be ordered the UUIDs no?
@pesterhazy typically a separate entry works best {:order [5 1 2] :entries {5 {...} 1 {...} 2 {...}}
To complement your quest of datastructures there is also https://github.com/clojure/data.priority-map >A priority map is very similar to a sorted map, but whereas a sorted map produces a sequence of the entries sorted by key, a priority map produces the entries sorted by value.
position attr works as well but depending on how often the list is updated might incur extra overhead for the sort
@tristefigure that also doesn't help for the use case I'm afraid
@thheller that's what we were leaning towards as well
I guess you could abstract this away to a certain extent
There could be versions of conj
and dissoc
that work on a {:order [] :entries {}}
map (or defrecord)
A positional-map 🙂
I would recommend sticking to standard stuff and don't get too fancy. just calling (-> db (assoc-in [:entries 1] {:id 1 ...}) (update :order conj 1))
is easy enough
if you start writing custom deftypes/defrecord you have to start worrying about how they serialize
agreed
the pos-conj
and pos-dissoc
functions could still work on regular maps
I noticed whenever node_modules
appear in directory I have error during start figwheel:
internal/modules/cjs/loader.js:573
throw err;
^
Error: Cannot find module '@cljs-oss/module-deps'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:571:15)
at Function.Module._load (internal/modules/cjs/loader.js:497:25)
at Module.require (internal/modules/cjs/loader.js:626:17)
at require (internal/modules/cjs/helpers.js:20:18)
at [eval]:3:13
at Script.runInThisContext (vm.js:91:20)
at Object.runInThisContext (vm.js:298:38)
at Object.<anonymous> ([eval]-wrapper:6:22)
at Module._compile (internal/modules/cjs/loader.js:678:30)
at evalScript (internal/bootstrap/node.js:542:27)
According to https://clojurescript.org/guides/javascript-modules#node-modules I added:
npm i -D module-deps resolve browser-resolve
But error didn’t disappear. What I miss?I am trying to do http://blob.tomerweller.com/reagent-import-react-components-from-npm
@pesterhazy would https://clojuredocs.org/clojure.core/sorted-map-by work for your use case?
@curlyfry I didn't know about that one! But it also keeps the map sorted by the key, which would be the UUID. AIUI a custom comparator wouldn't help when all you know is the item's key
I'm trying to get cljs.main working on windows, following the instructions in the quick start (https://clojurescript.org/guides/quick-start). It works but when I add a dependency to the deps file
{:deps {org.clojure/clojurescript {:mvn/version "1.10.238"}
com.taoensso/timbre {:mvn/version "4.10.0"}}}
and then (:require [taoensso.timbre :as log])
in the clojure namespace, and run the cljs.main command again I get clojure.lang.ExceptionInfo: No such namespace: taoensso.timbre, could not locate taoensso/timbre.cljs, taoensso/timbre.cljc, or JavaScript source providing "taoensso.timbre"
am I missing something here?
Edit: even following the last section of the Quick Start guide and adding react results in the same error. It seems like the deps aren't actually being fetched/added to the classpath?gotcha, thanks, wasn't sure if that was supposed to be automatic, (i kinda assumed the cljs.jar had the same features as cljs.main)
@pesterhazy I personally group the position/rank into a larger structure because once you have a position you most certainly will have other metadata that will follow
@bhauman interesting, what other kind of metadata are you thinking of?
right
one thing I'm worried about is inserting an element between two existing elements
with a rank attribute, you need to using floats to squeeze in between [1 2], [1 1.5] etc., with the obvious problem that you may run out of floating point precision
the alternative would be to re-number the elements, which touches all elements
right, that may always be fast enough
"selected" may be another piece of meta-data to track in the wrapper map
Anyone had this problem before, or any ideas?
user> (require '[figwheel-sidecar.repl-api :as ra])
CompilerException java.lang.Exception: namespace 'cljs.closure' not found, compiling:(cljs/repl.cljc:9:1)
@michael216 lein deps :tree
@souenzzo A different googling strategy got me to a useful answer - old guava was coming in from another dep. Fixed now. Thanks
anyone know how the :open-file-command
key in figwheel works? I'd prefer to just have it use a file from my project rather than something in my home directory. Everything tells me to put the file in my ~/bin
directory
@idiomancy :open-file-command just needs a path, I'm not sure off the top of my head if that path can be relative to the project directory
thanks, yeah I just was having trouble finding the source, so I didn't know what the path was relative to
i am porting clojure code to cljs and have heard that recently the macro referral has been simplified
@whilo https://clojurescript.org/guides/ns-forms#_implicit_sugar. basically just a cljs ns that has a require-macros for itself.
hmm I guess we need to advertise this better. it has actually been around for about 3 years now. surprised it is not common knowledge by now. https://dev.clojure.org/jira/browse/CLJS-948