Fork me on GitHub

I keep on forgetting -- how do we check if an object is a (float-array) ?

Alex Miller (Clojure team)01:05:51

the class name will be [F


@alexmiller: are you sugesting something like:

(let [fa (float-array 0)
      fac (class fa)]
  (defn float-array? [obj]
    (= fac (class obj))))


@qqq (Class/forName "[F")



(defn float-array? [obj]
  (= (class obj) (Class/forName "[F")))


That's how you get the class of any Java array...


[B is array of byte, [C is array of character, and the [Ljava.lang.String; for arrays of various object types.


(the odd one out is [J which is array of long)


Morning Clojurians! When checking the incoming JSON data with Spec for REST API, what would be the best way to compose a human-readable message that clearly describes a reason of failure? Do you use spec-tools or any other library, or maybe your own code? For example, if a field :foo is missing, I’d like to compose a message The field 'foo' is missing in parameters. The problem is, inside the data structure that s/explain-data returns, the :in key holds an empty vector and the :pred is a quoted form that looks smth like this: (clojure.core/fn [%] (clojure.core/contains? % :foo)). Well, it wouldn’t be a problem to parse it, but after all I found my own code a bit complex and fragile. Any thoughts?

👍 4

Right, already looking through…


How would you translate the following code into Clojure?


@U1WAUKQ3E thanks, will have a look at it.


(let [a (atom nil)]
  (locking a
    (locking a
    (println "no deadlock"))))
this does not deadlock when I try it locally; is this guaranteed to not deadlock? i.e. it's okay for a thread to acquire a lock we arleady have


yes, guaranteed by the JVM. locks are reentrant => acquired per-thread, not per-invocation


how exactly do deps.edn git dependencies work ? i'm depending upon a lein-based project that's not yet in clojars. is it required to add a deps.edn to the projects you depend upon as well ?

Alex Miller (Clojure team)11:05:38

Dependent projects are inspected to find a project manifest. It knows how to find and use deps.edn and pom.xml

Alex Miller (Clojure team)11:05:03

It does not know how to use project.clj

Alex Miller (Clojure team)12:05:09

If the project has no deps you can force it to use deps.edn with :deps/manifest :deps in your dep coord and that reader is tolerant of a missing file


What’s a nice library to make colorized output with Clojure for the console?


Since GraalVM is in town, I feel like making some cmd line fun


when concatenating a list of lists do you prefer reduce concat or mapcat identity?


or even (apply into [] xs), when you want eager evaluation

👍 4

or (apply concat xs) for laziness


i would prefer reduce concat, the intent is more clear to me than mapcat identity


Or (into [] cat xss), (reduce into [] xss) probably the latter one is fastest

👍 4

@borkdude jitpack is pretty brilliant! very simple to set up

😃 4

I’m running ‘lein test’ from a Clojure program by calling Is there a good way of printing the output from the tests while they execute? If the tests takes a long time to execute, then you miss the feedback till they have finished (and the output from sh can be printed out). I found a way of doing it in Java, but it would be nice if I could use

Alex Miller (Clojure team)12:05:46

@triss do not use reduce concat as concat builds a lazy stack that can blow your stack later when you first evaluate it.

👍 4

thanks @alexmiller! that blog looks brilliant.


Heh, I knew there was a reason we used mapcat identity, but I had forgotten what it was 😉


I have a colleague who really likes Scala’s Slick library, which lets you use scala’s map/filter/for operations on a database as if it were a native collection. Is there anything like that for Clojure?


on db side or once the collection is back in your code?


sounds a bit like linq to sql if its on the db side. but those can make some pretty bad queries.


@dpsutton Yes, it’s like linq. He’s intrigued by Clojure, but loves the ‘linq to sql’ nature of Slick. He just finished rewriting a bunch of queries to make them work efficiently, and said by simply querying with a set of id’s rather than a single id made everything batchable and flat.


So he hasn’t run into any queries that make him doubt Slick yet.


And he’s a sharp guy, so he might figure out how to make it work anyway.


ah ok. that sounds good advice in general (set versus n queries i guess which sql can handle just fine). I don't know of anything like that but i would be interested if there were


He’s challenged me to reimplement his slick/scala solution in clojure, so I’m scrambling for ideas, lol.


@jmckitrick what does his slick code look like? Or do you mean slick in general?


So you would do something like this:


val results = users.filter(_.roles.contains(Administrator.toString))


And that operates in a typesafe manner on the users collection.


val query = for {
user <- MyTables.User if user.userId inSet userIds
rolePerms <- MyTables.RolePerms if rolePerms.roleId == user.roleId
perms <- MyTables.Perm if perms.permId == rolePerms.permId
} yield {
( user,


Sorry for pasting scala here, lol.


So that for comprehension generates a query you can then run and it acts much like a native collection.


So it gives you the flexibility of SQL with the typesafe nature of native collections and their operations, without being as heavy as Hibernate, for example.


@sveri I’m trying to find an equivalent in Clojure that won’t require us to write raw SQL, but isn’t just another DSL on top of SQL.


I might check that out. It’s still very new, and it seems to require a lot of backend wiring to make the magic work. While Slick just lets you generate code from your schema, and you’re off and running.


here's the Walkale version for your Scala code above:

;; schema config, you write it once and can forget it until next time you change your schema
{:columns [:user/id :user/name :permisison/name]
 :idents {:users/all "user"}
 :joins {:user/permissions [:user/role-id :role-permission/role-id
                            :role-permission/permission-id :permission/id]}}

;; your query
;; you see, you can forget how this join is built (which tables and columns involved) and just mention its name
[{(:users/all {:filters [:in :user/id #{1 2 3}]})}
 [:user/id :user/name
  {:user/permissions [:permission/name]}]]

👍 4

@jmckitrick pigpen does this for hadoop


I’m reading now


@jmckitrick Unfortunately I am the wrong one to ask, I was just curious for an example and it reinstates my opinion of ORMs / SQL DSLs:


But in general you do this differently in Clojure. My recommendation in general though is to take a data-driven approach to this. Don't try to do any work in map or filter. Instead have them compose an AST. That's what linq does anyways. Linq IQueryables are simply ASTs composed by the execution of the program. They are then handed to the linq provider that post processes them into something the datastore can understand


In both Clojure and Linq in C# though you'll hit the same problem, arbitrary predicates will cause problems:


(filter older-than-30 some-db) doesn't work because you have no way to know what older-than-30 does. (filter #(> (:age %) 30) some-db) can work, but requires you to parse the predicate function.


In short you have a big problem here: Clojure doesn't have any sort of staged compiler facilities. Something like Scala's LMS is really hard to duplicate in Clojure without rewriting the entire runtime.


That’s not very encouraging 😞


Scala is really advanced when it comes to this sort of staged compilation. And Lisps approach metaprogramming from a completely different angle.


Without types, or compiler hooks there's little that can be done here aside from writing DSLs. Another approach is given here: But this method requires a fully polymorphic standard library, or a interpreter for the host language. Sadly we have neither in Clojure.


here's the Walkale version for your Scala code above:

;; schema config, you write it once and can forget it until next time you change your schema
{:columns [:user/id :user/name :permisison/name]
 :idents {:users/all "user"}
 :joins {:user/permissions [:user/role-id :role-permission/role-id
                            :role-permission/permission-id :permission/id]}}

;; your query
;; you see, you can forget how this join is built (which tables and columns involved) and just mention its name
[{(:users/all {:filters [:in :user/id #{1 2 3}]})}
 [:user/id :user/name
  {:user/permissions [:permission/name]}]]

👍 4

OK, so... does anybody feel like they're good at organizing things into namespaces for a project? I'm four years into Clojure, and in spite of knowing the common wisdom and pithy sayings, I'm still not liking what I produce.


I'm starting to wonder if it's maybe more like having a catalog of patterns to choose from than knowing some simple rules. Yes?


Zach Tellman's Elements Of Clojure can provide some insight on the topic

👍 4

Is it the last part, on model of abstractions or some such? I'll admit that I haven't finished that bit (love the book a lot tho).


I would say it is the "names" part, which namespaces affect


He's also got potemkin which has a macro you might like. It can import and expose functions as if they were in that namespace. This way you can separate the organization of your code as the developer from the organization exposed to the consumer


@eraserhd I had the second half of the book in mind, "indirection" and "composition" - but names come into play as well in retrospect


@dpsutton potemkin is sometimes useful but can also make code brittle (vulnerable to breaking if ns load order doesn't happen correctly is what I've seen)


I'd call it a useful patch-up to make something work if there's a lib you can't control and it can be simpler than forking, I wouldn't call it a good strategy for organizing your own library


I've used potemkin before, for hiding all sorts of algorithmic bits and breaking up a namespace. What would a better strategy be in this case?


What I do most of the times is to have packages like db, routes, service, websockets, core, common, components, views. And in the packages I have namespaces grouped by business objects like user, address, authentication, ...


you can use naming and ordering of declaration vs. definition within a namespace to clarify without having to hide something in a separate ns


@sveri One of the pithy sayings is about, "this is not OO, don't organize things into objects." I'm not sure that's good advice, though.


I wouldn't put much stock in namespaces, it is useful to slice up your code in to different parts, but beyond the sort of trivial extant, I am not sure it matters how you slice it up


I don't understand the obsession with having just a few functions per namespace

👍 16

also there's the pattern of defining a protocol or multimethod as the public interface, and then having a separate implementation namespace that is only for defining the object implementing those abstractions (and no other code that an end user would use directly from that source)


namespaces are a static phenomenon


just avoid documenting/exposing internal functions


put them in a separate namespace if you care to


@noisesmith well, I'm actually using Vim fold markers in one of my namespaces, and that makes this strategy tolerable to me (although it is editor-dependent). But Tellman does call out the burden of a large namespace.


yeah - in many many cases I've seen attempts to cosmetically groom ns contents lead to more errors and misunderstanding than a bigger namespace with good ordering / naming would have


@eraserhd For me its not about OO vs functional, but how can I find what I am looking for the fastest way and I have been sticking to this structure for many years now, which makes it convenient for me.

👆 4

obsession with namespaces also neglects the more important organization of your system at runtime


@hiredman So, if namespace organization isn't important, but runtime organization is, how do you communicate (say, to new engineers) how the system works and where to find things?


Or, also, to your future self.


@sveri good naming and code organization within the namespace should solve discoverability, having a namespace hierarchy is not the solution imo, you're just creating more bags to look into rather than making it easier to find stuff in the one bag where stuff belongs


have something that pulls together the puts the parts in to a thing and runs them, have something thing you can point to and say "this is the thing running"


Ah, so like a central dispatch or a central middleware stack, or a central bootloader, or what not?


OK, I like this.


(as awkward as middleware stacks are for data plumbing, I do like them for this reason)


@bronsa Do you have an example for this I can look at? Apart from that I would disagree, because namespace organization for me is part of code organization and good naming. I dont think you can do one thing without the other.


@sveri clojure.core? :)


consistent naming and coherent function signatures make having over 600 public functions in a single namespace not a problem


I'm not suggesting every namespace should be that big but it's a pretty good example (even tho code-org size it suffers a bit due to bootstrapping)

Alex Miller (Clojure team)18:05:40

I think clojure.core is an outlier for any such discussion

👆 4

@bronsa I'd consider clojure.core consistent with my advice 😄 I think one has to differentiate between a library and an application. Libraries usually have few entry points, filled with the accessible functionality while applications have "one" (disregarding REST, etc) entry point and most of the stuff is "internal".


I was using clojure.core as a counter to the argument that discoverability/ease of use follows from small namespaces

Alex Miller (Clojure team)18:05:38

for me, I start getting that uneasy feeling when my namespace gets bigger than 1000 LOC and a median is probably like 200 LOC.


yeah, most of my namespaces are complete after at most 1k locs


Btw. I never argued about size of namespaces, but only structure of them.


ok, I misunderstood then

Alex Miller (Clojure team)18:05:43

I prefer structure to be pretty flat

👍 8

No problem, I have the talent of disexpressing myself 🙂

Alex Miller (Clojure team)18:05:59

I’ve been trying to answer this question about structure well and even after 8 years of full-time Clojure I have hard time articulating it as I don’t usually think about it that much. I suspect that’s probably the result of spending a decade or so in big Java projects before that though


Hmm, interesting.


I think the fact that clojure disallows circular dependencies in itself is very helpful already when organizing clojure code. Because of that a path where the data flows will form almost by itself.


having no implicit forward declares is also one of my favourite things (which I love in ocaml too)


when people put their api entrypoints at the top and declare all the internal stuff, it makes me super sad


namespace structure is useful till it isn't, most of the time when I've seen people try to fix issues with code base legibility by moving namespaces around, it doesn't help, because the real issue is runtime organization, which namespaces don't deal with at all


@hiredman what exactly do you mean with runtime organization?


@alexmiller Hmm, I have a bunch of Java and Way Too Much C++, and weirdly, organizing C++ is like an artform (which makes it more like Clojure, I guess), but organizing Java feels very much "on rails" to me.


that's because java mostly has a 1:1 relation between classes and files


@sveri when your system exists and is running, are all the parts of it gathered together? do you have a clear means of starting and stopping your system? or do you just have your -main call some functions in different namespaces and stuff starts happening?


are the interfaces to different parts of your system well defined enough that you can swap them out?


I see, I think that is a different problem though. But yea, I agree, moving namespaces around usually is a sign of despair. A kind of "let's throw dirt onto the wall and hope some of it sticks" solution.


I see it more as a "if you give someone a hammer, they will try and fix everything using nails"


so namespaces are great, but keep it flat, keep it a tree, and don't sweat the names beyond some reasonable lower bound. maybe start tagging your namespaces or something


one issue with namespaces is you get a single name for things, and even though technically clojure doesn't treat them as hierarchical, people almost always treat them that way for organizing


and the issue with hierarchies like that is it is not uncommon for people to think slightly differently about things, so they have different hierarchies in mind


in contrast tagging is usually a flat thing where something can have different sets of tags and different people can tag it differently, how you would use that to make a coherent namespace system, I have no idea


ah, interesting.


Well, I do like systems which have only one global namespace, but it kind of precludes using libraries.


how is metadata carried around with transformations to persistent data structures? is appears with some testing of assoc and merge it's always the meta of the first arg that carries forward


@U09U89Y6Q I don’t know of any good and definitive sources on this topic.


I’d say that metadata preservation typically works “as you may expect” with may of the clojure.core functions. Most of the times when there are multiple objects with metadata involved and are going to be merged in some way to create a single structuer, the first from left-to-right is the metadata preserved


I would not expect any built in features to automatically try to merge or combine metadata between data structures for you though


Also, clojure.walk has had some issues in the past (and I think still a few) where metadata can be lost. There are various edge cases. Anytime you have particular worry about losing metadata, I think you should be a bit careful and test things out.


If you want some more details on various things, you may try “preserve meta” search in Clojure Jira


thanks for the links -- yeah it seems like a nice to have, but not a great way to have observability in code


Yeah, metadata can be useful


However, I think it should be something carefully considered sometimes - sometimes it makes sense for the data to be “real” data and not meta


ok how do I read a .edn file in?


As in create a PushbackReader?


I wish there was a clojure.edn/slurp


@triss (read-string (slurp "data.clj")) if its not to large.


hups, that should be data.edn.

👍 4

got it going. Thanks man!

Alex Miller (Clojure team)20:05:51

make sure you use clojure.edn/read-string

Alex Miller (Clojure team)20:05:05

slurp has the downside of slurping the whole thing into memory first. for a bit more robust impl, see something like

👍 4

thanks all… next question. Has anyone implemented anything like these ‘adverbs’ before in Clojure? I really love the, they can all be emulated easily enough but I wonder if they exist in a library somewhere… I want to be able to do this stuff with symbols/objects etc as well as numbers:


obviously .s is equivalent to map

Alex Miller (Clojure team)21:05:48

f is map-indexed, t I don’t think has a particular function but could be done with a combination, x is for

Alex Miller (Clojure team)21:05:17

t could be done with for or nested map

Alex Miller (Clojure team)22:05:52

tbh I think Clojure has a much better set of abstractions here