Fork me on GitHub
#beginners
<
2017-12-12
>
thomas.armstrong00:12:33

Fair enough. Thanks all ๐Ÿ™‚

drewverlee01:12:01

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 ๐Ÿ™‚

drewverlee01:12:34

Maybe you could do this with a watch, but the name implies thats wrong...

sova01:12:04

@drewverlee atoms have this feature, it's called...

sova01:12:33

:validator validate-fn

sova01:12:48

> If the new state is unacceptable, the validate-fn should return false or throw an exception.

sova02:12:38

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

drewverlee03:12:57

@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.

sova04:12:15

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

echristopherson04:12:47

Where can I read an approachable introduction to nREPL vs. stream REPLs? And are the latter the same as socket REPLs?

tbaldridge04:12:33

@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.

tbaldridge04:12:18

So a socket REPL is really nothing more than hooking what would normally go to stdin/stdout and piping those over a tcp socket.

echristopherson04:12:32

Doesn't nREPL go over a socket?

alexmiller05:12:36

yes. socket is really orthogonal to stream vs a framed protocol like nrepl

alexmiller05:12:00

streams are just streams of text

alexmiller05:12:49

nrepl is call and response with data structures

alexmiller05:12:07

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.

alexmiller05:12:28

You could also supply a listener that accepted the socket and talked using the nrepl protocol instead

echristopherson05:12:30

Oh, framed protocol. Gotcha.

echristopherson05:12:18

The stream way is seen as preferable now?

alexmiller14:12:26

They both have advantages

niklas.collin08:12:21

streams are more low-level, thus they need more work to integrate into but they also provide more power

niklas.collin08:12:41

you can implement nrepl on top of socket repl but not vice versa

gpapanikolaou13:12:06

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)

schmee14:12:44

@gpapanikolaou perfect use-case for Specter! https://github.com/nathanmarz/specter/

schmee14:12:57

I think (transform (srange 1 3) reverse [1 2 3 4]) will do just that, let me check

schmee14:12:26

yup ๐Ÿ™‚

gpapanikolaou14:12:10

thank you ๐Ÿ™‚

gpapanikolaou14:12:17

looks like an awesome lib

schmee14:12:41

it is extremely useful for certain problems

sabbatical201718:12:41

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?

sabbatical201718:12:55

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 ๐Ÿ˜ž

donaldball18:12:56

(defn f [coll g] (last (sort-by g coll))) is an inefficient but terse way

donaldball18:12:35

(defn f [coll g] (second (apply max (map (juxt g identity) coll)))) might work

admay18:12:53

Any reason why the first one in inefficient? @donaldball

donaldball18:12:02

You donโ€™t need to sort, you just need the max

sabbatical201718:12:22

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!

donaldball18:12:31

ha ha maybe ๐Ÿ™‚

donaldball18:12:38

Alas, it doesnโ€™t compare generally, it assumes the codomain of g is a number

sabbatical201718:12:56

@donaldball Thank you!

sabbatical201718:12:24

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.

imdaveho23:12:33

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?

admay23:12:43

Thatโ€™s how you instantiate a Java class

admay23:12:24

Check this out if you want some in depth info https://clojure.org/reference/java_interop

imdaveho23:12:28

@admay I mean, what's the difference between Throwable and Throwable.

imdaveho23:12:28

nvmd i see, the dot means any args to the right of it are passed to the contstructor

imdaveho23:12:38

whilst without the dot is just the Java class

imdaveho23:12:02

just a macro to suger up (new Class args*)

admay23:12:08

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)

admay23:12:45

With the dot, you get a java โ€˜thingโ€™. Without the dot, Clojure will look for a symbol called Exception (and in this case fail) ๐Ÿ™‚

admay23:12:27

user=> (type Exception)
java.lang.Class