This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-30
Channels
- # aleph (1)
- # beginners (126)
- # cider (2)
- # clara (38)
- # cljsrn (2)
- # clojars (2)
- # clojure (49)
- # clojure-dev (31)
- # clojure-dusseldorf (1)
- # clojure-finland (1)
- # clojure-france (6)
- # clojure-italy (13)
- # clojure-nl (12)
- # clojure-russia (9)
- # clojure-sg (1)
- # clojure-spec (33)
- # clojure-uk (83)
- # clojurescript (206)
- # community-development (3)
- # core-async (40)
- # cursive (4)
- # datomic (7)
- # duct (21)
- # emacs (9)
- # fulcro (36)
- # funcool (2)
- # graphql (12)
- # instaparse (4)
- # jobs (4)
- # lumo (24)
- # mount (1)
- # nyc (4)
- # off-topic (29)
- # onyx (1)
- # pedestal (2)
- # random (4)
- # re-frame (60)
- # reagent (136)
- # remote-jobs (1)
- # schema (1)
- # shadow-cljs (20)
- # spacemacs (6)
- # specter (14)
- # tools-deps (2)
It's common to define it in a separated file called entities.clj? And that is the common way to address the handling of database records or there is some different pattern in the functional programming context?
There's no definition for a map, if you query SELECT foo, bar, qux FROM baz
you'd get a list of {:foo 1 :bar 2 :qux 3}
Actually there's no such concept as a "database record", it's just plain data and there's no DB logic coupled to it
@aj07mm If you're using org.clojure/java.jdbc
, you'd just do something like
(jdbc/query db-spec ["SELECT * FROM mytable WHERE somecol < ?" threshold])
and you'll get back a sequence of hash maps. No definitions necessary.Have a look at the community managed documentation for java.jdbc
http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html
(and if you're doing database-driven stuff and get into difficulty, pop into #sql and ask questions -- I'm guaranteed to see them there and I'm the maintainer of the Clojure Contrib wrapper for JDBC)
Welcome to the world of data @aj07mm! š
@scott.archer If you're on Java 8 or later, you should consider using Java Time, either directly or via something like clojure.java-time
.
The maintainers of Joda Time want folks to migrate away from it (to Java Time).
(which is why I added that notice to clj-time
's README recently)
sean@sean-xps:~/clojure$ clj -Sdeps '{:deps {clojure.java-time {:mvn/version "RELEASE"}}}'
Clojure 1.9.0
user=> (require '[java-time :as jt])
nil
user=> (jt/plus (jt/instant) (jt/seconds 3600))
#object[java.time.Instant 0x1c3259fa "2018-05-30T07:04:15.504Z"]
user=> (jt/java-date *1)
#inst "2018-05-30T07:04:15.504-00:00"
user=> (jt/instant)
#object[java.time.Instant 0x2954f6ab "2018-05-30T06:04:49.947Z"]
user=>
(The clojure.java-time
equivalent of what you did with clj-time
above)i went through this http://elbenshira.com/blog/understanding-transducers/
and i can do these examples using (map ... (map ... ))
and the only thing from blog (i think it was mentioned in blog)
is to not have those seq in between but if they are lazy what's the point?
what is the use case for transducers?
one point is that they can be reused in different contexts, for example for seqs and core async channels
another point is that since they donāt materialize intermediate results they allocate less which reduces garbage collection pressure
Is pomegranate supposed to work in a jar ?
It works from the repl but from jar I get Could not find a suitable classloader to modify from clojure.lang.LazySeq@385266a9
"Is pomegranate supposed to work in a jar?" Best question I've seen in ages for confusing non-programmers š
But in case you're on Java 9: https://github.com/tobias/dynapath/issues/7
Also in the Readme https://github.com/cemerick/pomegranate/#urlclassloader-modifiability > you will need to explicitly enable the java.net.URLClassLoader support in dynapath (upon which pomegranate relies for such things). but I couldn't find anything on how to enable this support.
Hello all, I have a map {:value1 1 :value2 2 :value3 3}
. Is there any easy way to convert it to array-map
?
(into (array-map) m)
@alexmiller into
wonāt work here, if |m| > PHM.threshold
so I would reccomend using a different data structure unless youāre ok with your array-map being read-only
Do you need random access into something you're explicitly ordering? Maybe just a vector of stuff is very
Iād agree with structuring your data different as the first choice. https://github.com/amalloy/ordered is pretty useful too.
Well, I don“t want to use eval to make array-map, because I“m reading it from edn file
reading from edn file just involves reading, not eval
youāre not going to get ordered map guarantees out of straight edn
the map will be read from config file and the order of map elements is important. So if it came from edn reading, it will be hash-map not array-map, so I have to change the way to write in edn file
I would recommend either using a different edn structure if you want to retain ordering
When writing a function, is it bet practice to pass in a map of data, or pass in individual pieces of data as arguments? Iām calling a REST API and there are a lot of optional fields. Is it best to pass required data in as arguments and optional as a map? (I know I can do & args) but it seems messy. If the best practice is a map, how do I best communicate the intent / structure of the map Iām expecting?
I would go with a map and use spec for declaring required/optional fields
Iām onboard with passing a map. Iām new to clojure, so spec seems a bit intimidating to me at this point. Any helpful guides on how to use spec in that type of situation?
you can:
- use spec/fdef (more advanced)
- create a helper that calls spec/valid?
and throws with the result of spec/explain
on failure, and use that in a :pre or :post clause
(also consider using expound
instead of spec/explain since it's almost unreadable)
Thanks @joelsanchez I think I see how it fits together now. :pre calls spec/valid? And enforces the spec. It also communicates your intent. I think this will be great for me to start with and learn how to add additional specs to the spec/def.
basically š good luck
New to deftype
here. Is it supported (and/or does it even make sense) to add a docstring somewhere to a deftype?
I could be wrong, but I do not think it is supported.
No way to look it up if you did. @erp12 what I'd suggest in this case is make a constructor function and put the docstring on that
Iām not sure that it could become anything useful if it did as the type turns into a class, not a var
deftype could add it to the manufactured ->T
constructor I suppose
it generates a docstring for that already
ah ok. Thanks @alexmiller and @michael.gaare. I guess I could describe the type in the docstring of the helper function(s) that create the type.
Oh ... that is what @michael.gaare said. Sorry I am still a bit confused by the use of the word "constructor" with Clojure deftypes.
->T is automatically created, and it's a clojure function that constructs an instance of T
.T doesn't exist, T. is a shortand for new T
which is interop, vm level (->T is preferred)
the deftype docstring calls ->T a constructor: āOne constructor will be defined, taking the designated fields.ā
that doc string is literally talking about a jvm level constructor though, later it describes the factory function which is different
it's a clojure function that acts like a vm level constructor, yeah
->T
is called a positional factory function
defrecord also generates a map->T
which is a map factory function
but people pretty interchangeably also call these constructor functions
What would be an idiomatic way of finding which key in a map has a specific value? Is reduce-kv
+ reduced
when found the best option?
Or would be better to āinvertā the map so keys are vals and vals are keys, and work with that?
inverting requires an invariant that all keys are distinct. also to invert it would be linear in the keys before you search for it so you may as well just search for it with reduce
I am thinking something like
(reduce-kv (fn [m k v]
(if condition-on-v
(reduced k)
m)))
why would you need reduce-kv here? I don't see what an accumulator would be for
seems like you could just use (comp first (partial keep returns-k-or-nil))
oh, that's just some
user=> (some (fn [[k v]] (when (= v 0) k)) {:a 1 :b 0 :c 2})
:b
@seancorfield i noticed that java-time does not have (interval time1 time2)
implemented. is this meant to remain in joda-time only?
Read http://blog.joda.org/2014/11/converting-from-joda-time-to-javatime.html where the architect of both Joda Time and Java Time explains the differences.
It looks like he added an Interval
type to his "ThreeTenExtra" library but there's no built-in support in Java Time. What are you using interval
for in clj-time
@sova?
I realized though that ring cookie middleware has an explicit reliance on Joda time for the cookie expiration stuff...
Yeah I talked to @weavejester about it a month ago. I'd really like to excise that dependency
I guess there is a way to do dynamic requires? As in, require this if present, if not ignore?
Hi all. What would be the best way to compile many different Clojurescript
projects? I could cd into each project and run clojure build.clj
, but then I have to start up the JVM for each project. I tried reading through the cljs.build.api
docs but didnāt see anything.
Another option is to have one deps.edn that pulls in all the sub projects which is what weāre doing now
@dehli are you wanting to generate .js files ? you can do this from project.clj
@seancorfield interval for checking overlaps & contains
@sova ya, i want to generate .js files. Currently I am doing it from project.clj but I want to build src thatās in another project
for example, I have build.clj
in my server folder and I want to it to build server/service-a/deps.edn
, server/service-b/deps.edn
seems like every time you invoke a deps.edn you're adding resolved dependencies to the classpath ... so you can probably get away with one gigantic deps.edn for all your sub projects. there may be a better way.
thanks. thatās what weāre currently doing but i was hoping there would be a nicer way
sounds like edn_smash is the way to go
but i encourage you to ask around because there may be others who have come across the same issue and solved it elegantly
i wonder if i can set the classpath manually to be what the classpath of the sub project is
Do tests get cached? My simple Luminus test seems to fail because of a cached timestamp from yesterday.
diff: - {:timestamp #inst "2018-05-30T21:38:27.158-00:00"}
+ {:timestamp #inst "2018-05-29T22:12:12.184000000-00:00"}
anything compiled can be cached, but I'd be surprised if you were compiling test namespaces. Have you restarted your repl since then?
clj or cljs?
odd, lein clean will remove stray cached data (usually), but I wonder why / where that test data would be cached
Running lein clean
doesn't seem to make a difference and I receive the same result. Now, what is strange that, although it's 1AM (which means that it's already the 31th) the test result is still expecting 30th.
Here's my test, note that the Luminus version is 2.9.9.2 as per the recommendation from the book I'm reading (Web Development with Clojure 2nd edition)
(ns guestbook.test.db.core
(:require [guestbook.db.core :as db]
[guestbook.db.migrations :as migrations]
[clojure.test :refer :all]
[clojure.java.jdbc :as jdbc]
[config.core :refer [env]]
[mount.core :as mount]))
(use-fixtures
:once
(fn [f]
(migrations/migrate ["migrate"])
(f)))
(deftest test-messages
(jdbc/with-db-transaction [t-conn db/conn]
(jdbc/db-set-rollback-only! t-conn)
(let [timestamp (java.util.Date.)]
(is (= 1 (db/create-message!
{:name "Bob"
:message "Hello world"
:timestamp timestamp})))
(is (=
{:name "Bob"
:message "Hello world"
:timestamp timestamp}
(-> (db/get-messages {} {:connection t-conn})
first
(select-keys [:name :message :timestamp])))))))
i think i've run into this before. the test profile sets up a different connection so you can test in memory maybe? and when you run it not on the test profile you just side affect the db. and then you're just running predicates on it