This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-12-12
Channels
- # adventofcode (112)
- # architecture (1)
- # beginners (55)
- # boot (26)
- # cider (19)
- # cljs-dev (19)
- # cljsjs (1)
- # cljsrn (7)
- # clojure (140)
- # clojure-android (3)
- # clojure-austin (3)
- # clojure-china (3)
- # clojure-gamedev (1)
- # clojure-greece (43)
- # clojure-spec (75)
- # clojure-sweden (5)
- # clojure-uk (21)
- # clojurescript (66)
- # core-async (2)
- # core-logic (1)
- # cursive (63)
- # datascript (5)
- # datomic (4)
- # devcards (2)
- # duct (13)
- # editors (5)
- # emacs (9)
- # figwheel (4)
- # fulcro (42)
- # graphql (25)
- # immutant (7)
- # jobs (29)
- # leiningen (13)
- # lumo (7)
- # numerical-computing (3)
- # off-topic (22)
- # om (2)
- # onyx (25)
- # pedestal (3)
- # re-frame (14)
- # reagent (20)
- # remote-jobs (1)
- # ring-swagger (3)
- # rum (12)
- # shadow-cljs (9)
- # uncomplicate (1)
- # unrepl (6)
Is there a way to set a validation on data going into an atom? I dont see to see docs for that, but it seems like an obvious thing to have. If not, i'm guessing there is an interesting reason why 🙂
Maybe you could do this with a watch, but the name implies thats wrong...
@drewverlee atoms have this feature, it's called...
:validator validate-fn
> If the new state is unacceptable, the validate-fn should return false or throw an exception.
in fact I think you could write a validate function to let only prime numbers into an atom and just iterate over all the integers. not the most efficient way to get a list of primes, but maybe that demonstrates how it works
@sova Your right. Afterwards i realized my question assumed a lot of things about the atom. Namely that it was holding a collection, which your not limited to. In my case, i could build an interface to my atom and restrict the arguments passed to it.
Yeah, the validate-fn will prevent things from getting placed into the atom, but you probably want something that will inform you why it didn't get into the atom in the first place.. depends on what you're using it for
Where can I read an approachable introduction to nREPL vs. stream REPLs? And are the latter the same as socket REPLs?
@echristopherson REPLs are really just a stream of characters in and out. What is known as nREPL is really a RPC protocol over common Clojure interactions.
So a socket REPL is really nothing more than hooking what would normally go to stdin/stdout and piping those over a tcp socket.
Doesn't nREPL go over a socket?
yes. socket is really orthogonal to stream vs a framed protocol like nrepl
streams are just streams of text
nrepl is call and response with data structures
Clojure includes a socket server on which you can supply a listener function. one provided and common function to use is a stream repl listener.
You could also supply a listener that accepted the socket and talked using the nrepl protocol instead
Oh, framed protocol. Gotcha.
The stream way is seen as preferable now?
They both have advantages
streams are more low-level, thus they need more work to integrate into but they also provide more power
hello, is there an easy way to replace a sublist of a list with its reverse? for example in (1 2 3 4)
replace the (2 3)
part with its reverse, and get: (1 3 2 4)
@gpapanikolaou perfect use-case for Specter! https://github.com/nathanmarz/specter/
What's the easiest way to create a function f
with the property that (f coll g)
is the element x
of the collection coll
for which (g x)
is maximal?
I thought that maybe I could map g
over coll
and then zip the results with coll
go get a map, then call max-key
, finally look it up. But that seems way too complicated for such an apparently simple operation 😞
(defn f [coll g] (last (sort-by g coll)))
is an inefficient but terse way
(defn f [coll g] (second (apply max (map (juxt g identity) coll))))
might work
Any reason why the first one in inefficient? @donaldball
You don’t need to sort, you just need the max
I think I just realized in the documentation of max-key
, the name k
stands for a collection, not a map. Thus perhaps max-key
is the function I am looking for!
ha ha maybe 🙂
Alas, it doesn’t compare generally, it assumes the codomain of g
is a number
@donaldball Thank you!
For the record, max-key
is not quite it. For example, (max-key count [[1 2] [3 2 4]])
does not work. You need to do (apply max-key count [[1 2] [3 2 4]])
to obtain [3 2 4]
which is what I originally wanted.
quick question y'all, when throwing a custom exception eg with (throw (Throwable ...
why is Throwable.
or Exception.
with a period? What difference is that without a period?
@imdaveho as per the Clojure Docs? https://clojuredocs.org/clojure.core/throw
Check this out if you want some in depth info https://clojure.org/reference/java_interop
nvmd i see, the dot means any args to the right of it are passed to the contstructor
user=> (throw (Exception. "Foo"))
java.lang.Exception: Foo
user=> (throw (Exception "Foo"))
java.lang.RuntimeException: Expecting var, but Exception is mapped to class java.lang.Exception
clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Expecting var, but Exception is mapped to class java.lang.Exception, compiling:(/private/var/folders/95/bq20sfr15kbbnw_z3j8njxw54nfw83/T/form-init5049763821089224505.clj:1:8)