Fork me on GitHub
#clojure
<
2017-06-12
>
j-po01:06:03

I've been reading about stateful transducers and I'm having trouble understanding a thing: Why should the state be visible to other threads (as volatiles are) at all?

phill02:06:32

@j-po, If you hang a transducer on an async channel, the transducer is not pinned to any particular thread, although access should be coordinated by the async system. Right?

j-po02:06:29

Aha! Fair enough. Thanks!

qqq03:06:53

(fn [] ... how can I pass this function itself as an argument?)

noisesmith03:06:04

give it a name

madstap03:06:13

@qqq You can give names to anon fns (fn foo [] ,,,)

noisesmith03:06:14

(fn foo [] ... (... foo ...))

danielgrosse09:06:00

Somybody tried https://github.com/sveri/closp with 3.0.0? Can't run lein migrate as it doesn't exist.

sveri09:06:49

@danielgrosse I removed the migrate dependency and did the migration by hand

sveri09:06:21

None of the existing migration libs fitted my needs and in a production environment I dont want to have it automatized anyway

sveri09:06:30

But, you could add one yourself if you want to

sveri09:06:09

the docs are not rewritten yet, btw. as I am currently working on something different which I will show today at our meetup 😉

danielgrosse09:06:44

Okay. Additionally I get an error, that the user ns you'll define in {:repl-option {:init-ns {closp.user}} isn't available

kevinludwig13:06:48

4clojure question, #143. I came up with the following solution which fails the first unit test on the site but passes locally for me. Any ideas on what Im doing wrong?

kevinludwig13:06:14

also notably annoying that the question doesn't explicitly say I should produce a lazy sequence; I had a reduce based solution that just fails on the last test...

ghadi14:06:10

what is the it asking you to do @kevinludwig ?

kevinludwig14:06:42

it says "You failed the unit tests"

kevinludwig14:06:56

which typically means the output didn't match what it expected

kevinludwig14:06:09

but I verified in the REPL that the output is identical

ghadi14:06:14

I mean what is the problem statement?

kevinludwig14:06:20

"Insert between two items"

kevinludwig14:06:30

"Write a function that takes a two-argument predicate, a value, and a collection; and returns a new collection where the value is inserted between every two items that satisfy the predicate."

kevinludwig14:06:52

Unit test they give is (= '(1 :less 6 :less 7 4 3) (__ < :less [1 6 7 4 3]))

michaelblume17:06:23

I’m having some trouble figuring out how to configure eastwood to ignore an isolated warning

michaelblume17:06:59

the macroexpansion of compojure-api’s POST produces a (merge map) expression, and eastwood gets upset because it’s just one map

michaelblume17:06:14

(compojure-api has a change to fix this in master, but it’s not in release yet)

michaelblume17:06:45

I have an eastwood-config.clj file that looks like

michaelblume17:06:47

(disable-warning
 {:linter :suspicious-expression
  :within-depth nil
  :if-inside-macroexpansion-of '#{compojure.api.core/POST
                                  compojure.api.core/GET
                                  compojure.api.sweet/POST
                                  compojure.api.sweet/GET}})

michaelblume17:06:14

and in my project.clj I have :eastwood {:config-files ["eastwood-config.clj"]}

michaelblume17:06:20

but the warnings still show up

waffletower19:06:03

I want to do something like below to destructure arrays in a let context. Is there something better than this in clojure.core already that I overlooked?

(defn intersplit [coll]
  (when (coll? (first coll))
    (partition (count coll) (apply interleave coll))))
user> (intersplit [[1 2 3] [4 5 6] [7 8 9]])
((1 4 7) (2 5 8) (3 6 9))

noisesmith19:06:55

@waffletower (apply map list coll)

waffletower19:06:17

nice par! thanks!

waffletower19:06:59

much more general approach

noisesmith19:06:13

bonus, you can define it as (partial apply map list) and people will squint at it and have no idea what it’s for

noisesmith19:06:31

(not actually a bonus)

waffletower19:06:17

I already added a doc string 🙂

waffletower19:06:37

but your reminder that I can use map as an argument to apply feels like I held an epic spell staff for a moment 🤓

ikitommi19:06:47

@michaelblume unrelated to how to configure eastwood, but the compojure-api 2.0.0-alpha2 should be out in a day or two (with initial spec coercion, finally!)

sebastianrkg20:06:28

is there any convenient function for validating that data is of the form of a specific record?

sebastianrkg20:06:42

as in

(defrecord Circle [radius])
(def circle (->Circle {:radius 1}))
(is-circle circle) ; => true

sebastianrkg20:06:54

is there any function like is-circle?

sebastianrkg20:06:04

or maybe even an is-record-type Circle

sebastianrkg20:06:42

ah, excellent. Thanks!

sebastianrkg20:06:34

Now will that apply if I have a map that is isomorphic to the record? If not, can I convert that map to an instance of the record?

Alex Miller (Clojure team)20:06:10

(Circle->map {:radius 1})

Alex Miller (Clojure team)20:06:27

which is actually what you should be using in your code example above

Alex Miller (Clojure team)20:06:02

(def circle (->Circle 1)) is what you would use

Alex Miller (Clojure team)20:06:09

those two are equivalent here

sebastianrkg20:06:15

ah, I see. Thanks! That should be map->Circle, right? I couldn't find Circle->map

sebastianrkg20:06:27

darn, it seems that map->Circle happily returns true in this situation (instance? Circle (map->Circle {:l 2}))

sebastianrkg20:06:44

so this is a valid Circle #user.Circle{:radius nil, :l 2}

sebastianrkg20:06:02

which means that I can't use this for validation of request JSON from Compojure

Alex Miller (Clojure team)20:06:45

that is a valid Circle - records are open by design

Alex Miller (Clojure team)20:06:04

it’s not really public API, but you can also utilize Circle/getBasis

Alex Miller (Clojure team)20:06:32

user=> (Circle/getBasis)
[radius]

Alex Miller (Clojure team)20:06:38

to tell you the expected fields

sebastianrkg20:06:01

that's cool, I'll keep that in mind. There's no way to detect that a map contains all the default fields -- non-`nil` -- of a record, without making a custom function, I guess?

Alex Miller (Clojure team)20:06:12

no, but getBasis is enough to build that

sebastianrkg20:06:44

yeah, I can see that mapping that over an instance would be enough. I'll get on it 🙂

noisesmith20:06:28

considering you mention it’s not considered part of the official api, is Circle/getBasis less hacky than (keys (map->Circle {})) ?

Alex Miller (Clojure team)20:06:43

either seems fine (although I’d defonce the latter)

madstap20:06:45

Wouldn't the latter return the keys in an arbitrary order, though?

madstap21:06:57

I suppose that doesn't really matter as records are unordered anyways....

Alex Miller (Clojure team)21:06:58

I think getBasis always returns them in the definition order (or at least that would be my expectation)

Alex Miller (Clojure team)21:06:23

oh sorry, read that as former

Alex Miller (Clojure team)21:06:41

agreed - the latter would not be good for determining field order for positional constructor

bfabry21:06:46

^ but then if you have the list of field names you don't need the positional constructor 🙂

stuartrexking21:06:47

Is it possible to ^

stuartrexking21:06:12

Or do I have to use (compile …)

hiredman22:06:03

you shouldn't be doing of that though

hiredman22:06:11

regardless of if it works

hiredman22:06:41

if you are aot compiling 1. don't use a single segment namespace 2. use a build tool

hiredman22:06:13

but you most likely don't need to aot compile

hiredman22:06:59

and are just making your life more painful now, by trying to figure this out, and someone else's life more painful in the future when your code base hits the edge cases around aot compilation

spieden22:06:26

has anyone seen a good approach for having pluggable logging in your library?

spieden23:06:36

tempted to stick an atom wrapping a function in a namespace and calling it good

bfabry23:06:37

isn't the java logging ecosystem already pluggable?

bfabry23:06:57

my assumption is you can use clojure.tools.logging and call it g

spieden23:06:03

kinda, you can pick one and there’s usually some adapter classes to bridge everything into it

bfabry23:06:19

"Logging macros which delegate to a specific logging implementation. At runtime a specific implementation is selected from, in order, slf4j, Apache commons-logging, log4j2, log4j, and finally java.util.logging."

spieden23:06:20

hmm yeah, was considering that too

bfabry23:06:23

sounds pretty pluggable

spieden23:06:44

ah yeah, i’d forgotten it works that way

bfabry23:06:05

no worries

stuartrexking23:06:21

@hiredman thanks, I understand this is a completely contrived example. I want to demonstrate compiling a clj to a class and running it from the command line without using lein or repl or any other means.

stuartrexking23:06:35

But thanks for the response!

hiredman23:06:01

yeah, that is not the way to demonstrate it

hiredman23:06:04

you shouldn't demonstrate how to shoot your foot off with a gun to a 3 year old

stuartrexking23:06:42

Not sure I follow your analogy.

hiredman23:06:42

I don't know to what audience you are using that demo, but my guess would be clojure novices

hiredman23:06:02

you don't want them thinking that is an appropriate way to do that

hiredman23:06:13

and you don't want them thinking they always need to aot compile

stuartrexking23:06:28

Sure, which is why there are other examples further in the demonstration.

spieden23:06:42

@stuartrexking in my experience AOT compilation is only useful for a few java interop edge cases these days

spieden23:06:01

@stuartrexking might be more interesting to show an example of loading your clojure main from the Java CLI ala

java -cp ... clojure.main -m my.clojure.namespace

stuartrexking23:06:21

Yeah, I have that one.

hiredman23:06:24

I would never ever tell a novice aot exists, if you tell them it exists they think they need to use it

spieden23:06:04

yeah like showing people bytecode while teaching them java =)

spieden23:06:27

or maybe JNI is a more appropriate example

ravster23:06:13

I just discovered pre- and post-conditions. While it seems to be working in the REPL, it doesn't seem to be working in the tests. Are pre-conditions disabled during testing?

ravster23:06:34

I'm trying to write an unhappy test about what happens if bad-input-data is given

hiredman23:06:59

what are you doing that makes in you think it works at the repl and not in tests?

ravster23:06:46

@hiredman In the repl I'll get a AssertionError Assert failed:, but the tests say :fail 1 :errors 0

hiredman23:06:10

what are you doing in the repl and what are you doing in the test?

hiredman23:06:36

and how are you running the tests?

ravster23:06:34

I'm running the test by calling a public function, but in the repl I'm calling the private function with the pre-condition directly