Fork me on GitHub
#clojure
<
2020-06-22
>
dominicm07:06:14

Is anyone doing real-time end user analytics with clojure? Eg "your shop has 100 page views" (so not business-wide batch report, but for a user). What are you using to do it? A lot of the high level java things haven't got a lot of people talking with clojure, and I'm hoping to find things like transducer support.

nick17:06:38

I'm not sure I fully understood your question but I'll try to comment on it. I guess http://metrics-clojure.readthedocs.io could be used for tracking page views(along with any other metrics for analytics). In case you're using Ring - metrics-clojure-ring.

pbaille08:06:08

Hello, I'm struggling a bit with the testing of a stateful thing As in the following exemple, it is an alternance of assertions and updates on something stateful. (for remaining as minimal as possible i've materialized my state with an atom but it is more complex in my usecase)

(def state (atom 0))

(deftest one
  (is (= @state 0)))

(swap! state inc)

(deftest two
  (is (= @state 1)))
How could I write this kind of thing with clojure test ? Thank you

Ed09:06:05

@pbaille You could always put both assertions in a single test with something like

(deftest something
  (testing "something"
    (let [state (atom 0)]
      (is (= @state 0))
      (swap! state inc)
      (is (= @state 1)))))

rickmoynihan11:06:00

@pbaille: also you could take a look at the docs on clojure.test for composing tests and test-ns-hook which would allow you to order separate deftest s — though I rarely see this form used these days.

Georgiy Grigoryan11:06:29

Is it a bad idea to have .clj and .cljc files for the same namespace? Or is this a common practice? What about .cljs and .cljc?

noisesmith14:06:10

when you use require, only the first match for an ns is used, so using cljc requires different namespaces than used by clj and cljs code

noisesmith14:06:37

one idiom I've seen is having src/clj/foo/bar.clj and src/cljs/foo/bar.cljs and then src/cljc/foo/shared/bar.cljc which both the clj and the cljs file require

Georgiy Grigoryan12:06:55

So far I don’t think it really makes sense…

wombawomba14:06:05

So I have this library that sets up a bunch of schemas used in multiple internal project. Now, I’d like to include some schemas that involve core.async objects, but I don’t want to force core.async as a dependency for all the services. Is there a good way to only define things if a given namespace/Java class exists?

wombawomba14:06:09

Oops, seems like I was just confused, and I can do this via e.g.

(when (try (import 'foo) true (catch Exception _ false))
  (def MySchema ...))

wombawomba14:06:27

Onto my next question: is doing this sort of thing a bad idea for any reason?

borkdude15:06:30

you can also use (Class/forName ..) but the other one is fine too

dominicm15:06:54

One alternative is to have a core.async namespace for core.async schemas.

wombawomba16:06:29

yeah I thought about separate namespaces, but I think there’s a risk it’ll become unwieldy

jacklombard16:06:38

I often run migrations and tasks by spawning a temporary docker container and pass it some params. But this time I want to pass it a namespace and resolve the namespace at runtime and run some functions from it. find-ns doesn’t seem to work when I pass it the name of the namespace in the command line that will be converted to a symbol in the code.

jacklombard16:06:45

Any idea how I can find-ns a namespace? Currently it returns nil

borkdude16:06:12

@frozenfire1992 find-ns only returns the ns when the namespace was already loaded:

$ clj
Clojure 1.10.1
user=> (find-ns 'clojure.string)
#object[clojure.lang.Namespace 0x35229f85 "clojure.string"]
user=> (find-ns 'clojure.set)
nil

jacklombard16:06:02

Oh damn, anyway I can achieve what I am trying to do?

borkdude16:06:08

@frozenfire1992 You can also use ((requiring-resolve 'clojure.set/union) #{1 2 3} #{3 4 5}) to force loading the namespace

borkdude16:06:23

or issue a require first

jacklombard16:06:51

will try that

jacklombard16:06:17
replied to a thread:or issue a `require` first

That worked @borkdude thanks 🙂

Empperi17:06:07

Any suggestions regarding what lens library would be the best and most reliably maintained in Clojure?

noisesmith17:06:53

use your own judgment, but IMHO lenses and monads become much less useful without static type checking - you can get pretty far with get-in / update-in

Empperi17:06:16

I have a use case where lenses would make sense

Empperi17:06:34

Normally wouldn't bother

noisesmith17:06:42

cool, I know nothing about lens implementations because of the above, best of luck :D

😛 3
phronmophobic17:06:31

I've been using specter as a lens library, https://github.com/redplanetlabs/specter

Empperi17:06:21

Hmm. Interesting idea in many ways. Been using specter myself too for several stuff

Empperi17:06:56

But specter isn't exactly a lens implementation, right?

Empperi17:06:24

But it is relatively close, true enough

noisesmith17:06:29

right, it's a dsl for data updates

phronmophobic17:06:46

i actually don't know enough theory to tell the difference. what is the difference if anyone knows?

Empperi17:06:51

I think I could do with spectre

Empperi17:06:45

Well, lenses are functions which allow get/set kind of operations on nested data structures. Basically you can do stuff like get the value, set the value or apply a transformation on that lens which will effectively update the value where the lens is pointing to

Empperi17:06:01

The thing is that lenses are functions and as such they are composable by nature

3
noisesmith17:06:05

as I understand it specter is designed in terms of queries / modifications across an input, lenses are monadic and designed to be modular and chainable

Empperi17:06:38

That's the biggest single thing that separates them from what spectre is doing

phronmophobic17:06:00

you can compose specter's queries though

phronmophobic17:06:10

is it just that it's not function composition?

Empperi17:06:21

It's not such a big deal in my use case

Empperi17:06:34

Even though it would be nice. I like when things stay as functions 🙂

Empperi17:06:54

But spectre is a well maintained and battle proven library

phronmophobic17:06:02

in some ways, I like composition via conj better

Empperi17:06:03

And would provide the basic stuff I need

noisesmith17:06:07

this smells similar to the difference between -> and the state monad

3
phronmophobic17:06:53

(def spec1 [:a])
(def spec2 [:b])
(def spec1+spec2 [spec1 spec2])

phronmophobic17:06:05

so from a theoretical perspective, for something to be a lens, it has to not just be composable, but it must allow composition via function composition?

Empperi17:06:13

Spectre is a good suggestion, I'll think a bit about my problem on top of that, thanks about that. Was thinking lenses would be a good solution but spectre should do

Empperi17:06:32

@U7RJTCH6J well, I'm not going to go too deep on the actual definition of lenses since that is a bit out of my depth but that is a very important feature of lenses in general

noisesmith17:06:38

@U7RJTCH6J right - my understanding is that lenses are explicitly functions and follow function composition rules (which is why I compared to -> - which looks like the state monad, and can compose via concatenation of forms, but isn't semantically functions at all)

👍 3
phronmophobic17:06:47

I'm curious if there's a good example of a task that's easy with a lens library that wouldn't "look similar" with specter

borkdude18:06:55

@U7MRZK43B If specter is overkill, you could also look at this one: https://github.com/vvvvalvalval/supdate

Empperi19:06:22

Yeah, found lentes already. supdate was new to me

Empperi17:06:19

Don't feel like building my own

Empperi17:06:54

Spectacles seems to be last updated year ago, Lentes has last commits 6 months ago but build has been failing for months

fabrao18:06:41

Hello all, How do I do this in clojure ?

fabrao18:06:47

System.setProperty("file.encoding","UTF-8");
Field charset = Charset.class.getDeclaredField("defaultCharset");
charset.setAccessible(true);
charset.set(null,null);

fabrao19:06:04

-> Charset.class ?

fabrao19:06:33

How do I reflect this in clojure?

noisesmith19:06:03

(System/setProperty "file.encoding" "UTF-8")
(def charset (.getDeclaredField Charset "defaultCharset"))
(.setAccessible charset true)
(.set charset nil nil)
is a first go at it

noisesmith19:06:43

the interop rules are pretty straightforward https://clojure.org/reference/java_interop

noisesmith19:06:36

for the specific question - Foo.class is a java syntax, the equivalent in clojure is Foo

fabrao19:06:28

(doto
   (.getDeclaredField Charset "defaultCharset")
    (.setAccessible true)
    (.set nil nil))

Empperi19:06:24

As a total sidenote, I do not even want to know why you would do that, hope it was an example 😄

ghadi19:06:48

it's a dubious goal