This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-12-23
Channels
- # admin-announcements (95)
- # beginners (16)
- # boot (50)
- # cider (15)
- # cljs-dev (47)
- # clojure (149)
- # clojure-italy (5)
- # clojure-russia (94)
- # clojurescript (163)
- # clr (1)
- # cursive (6)
- # datavis (9)
- # editors-rus (4)
- # hoplon (24)
- # jobs (9)
- # ldnclj (32)
- # lein-figwheel (4)
- # mount (5)
- # off-topic (2)
- # om (68)
- # parinfer (31)
- # proton (1)
- # reagent (32)
- # remote-jobs (1)
- # yada (5)
i regret not building this app with cljs include, but I was new to clj when i started...
I have a question for yesql
How can I write a NULL value to the database using yesql
?
@jethroksy: I have a form to submit some data which can have fields that don't always have known values
MySQL can have nullable integer values
The problem is I'm not sure how to pass a mysql "null" to the database write of yesql, which I believe uses nil
@trancehime: sorry missed this. I also use postgres, but passing nil should work the same for mysql.
The problem is I get this error: java.sql.SQLException: Incorrect integer value: '' for column 'winner_id' at row 1
@trancehime: Hmm, the error message makes it seem like empty string is being passed rather than nil
@solicode: Well, that's the crux of my problem. I have an input field in a form that might be left blank because the data that's supposed to go into that form might not necessarily be known at the time
I still would like to write the rest of the data into the database in spite of this blank form. But how do I change ''
or even detect ''
so I can handle it properly?
@trancehime: looks like you could do some pre-parsing before you do the sql query
Ahhh crap. I should do the handling in the POST route, before doing the query... of course
I am stupid
the issue here is the forms there, so you can't possibly have nil
passed in... right?
It's an [:input]
, so if it is blank, it must be an empty string
Well, anyway, even if I had changed the method of input to something like [:select]
to force a default value, I would still need to do the pre-parsing in the POST route instead of directly in the function where I run the query. welp
@spacepluk: You mean converting a Java map to a Clojure persistent map? Maybe into
is what you're looking for? Like (into {} your-java-hash-map)
.
But if you just want to call get
or whatever, you can call that on Java maps and it'll work fine (stuff like assoc
won't work on plain Java maps though).
@spacepluk: if all your values are valid read-time clojure literals, you could just pr-str
the java collection and read-string
it back
Oh no, when I see doto
I'm going to think of something entirely different from what it's supposed to be
@spacepluk: I think you want instance?
rather than isa?
Also (java->clj x {:keywordize-keys false})
should probably be (java->clj x :keywordize-keys false)
The part that iterates through the map looks strange to me. I think I would write it like this:
(into {} (for [[k v] x]
[(keyfn k) (thisfn v)]))
Hi everyone. Is there an easy way to test a library without having to incorporate it to my project dependencies. I am only aware of leiningen based projects, so, when I want to evaluate a library, most of the times I end up creating a new project and adding all the libraries I want to evaluate as dependencies. Is there a better way to do this?
@rcanepa: https://github.com/rkneufeld/lein-try is sometimes useful for this
Great!… thanks @stuartsierra !
@spacepluk: or you could use #C053K90BR build tool, which allows even to add one or more deps at the REPL: boot.user=> (set-env! :dependencies #(conj % ‘[groupid/artifactid “X.Y.Z”]))
https://github.com/zcaudate/vinyasa has it I think
If I have two database tables with some content such as ProductLines and Products (a product line may contain multiple products), how can I transform a flat representation of a join to something like a JSON nested structure?. I would like to provide an API point in which a user can ask for a specific product line (e.g.: api/v1/product-line/100) and fetch its information along with all its children.
Or there is a better way to deal with this?… in the past, I worked with web frameworks that provided that functionality under the hood, so this is new ground for me.
Also, I am using schemas to shape my data, so I know very well the final structure I want.
is there a way to attach metadata to a var which will print a warning when referenced?
@eyelidlessness: not in Clojure itself. A linting tool could maybe do it
@rcanepa: there is also https://github.com/pallet/alembic in case you want to try libs from within the running REPL (to get to similar #C053K90BR flexibility as @magomimmo mentioned)
@rcanepa: yes, use reduce
, into
etc. to transform the db rows returned into the shape you want
it's surprisingly easy once you wrap your mind around it
it's fun to work with relational databases from clojure
you may want to transform the columns to keywords for easier handling
I’ve read that clojure/lisp in general is so simple in terms of its set of native functions that you can fit such a set onto a napkin. Have you experienced that?
@pesterhazy: Cool, I will take that route then
@kopasetik: yup. It takes a while to grok it (especially when to use reduce
), but after it clicks any transformation is pretty simple
transforming sequences and maps is an area where clojure shines, especially given its arrow macros
has anyone had issues when logging with timbre across threads? I’m using the default println appender, but I’m seeing some successive logs being conflated into one line rather than two when called within the same millisecond across threads. I was under the impression println
was thread-safe?
@jarredlhumphrey: it's thread-safe in that it won't crash, but I don't see how it could guarantee that lines don't get mixed up
println
is not thread-safe
I think you'll see the same problem when using .write
directly from java (or printf
in C programs)
Basically, I want to do [1, 2, 3].map(function(element, index){ return index;}) but in Clojure
@kopasetik: try map-indexed
@stuartsierra @pesterhazy any suggestions on an appender or method thats thread-safe?
Use a real logging framework instead of println
.
@jarredlhumphrey: This might help if you want to use println: http://yellerapp.com/posts/2014-12-11-14-race-condition-in-clojure-println.html
@solicode: I tried that with a custom appender as such:
:appenders
{:standard-out
{:doc "Prints to *out*/*err*. Enabled by default."
:min-level nil
:enabled? true
:async? false
:rate-limit nil
:fn (fn [{:keys [error? output]}] ; Use any appender args
(binding [*out* (if error? *err* *out*)]
(.write *out* (str output "\n"))))}}
but still no luck.. still the same conflation
@kopasetik: (Math/pow a b)
?
@pesterhazy: Thanks
you're welcome
@kopasetik: Power function is pretty easy to write, though. For example you could do
(defn pow [x y]
(reduce * (take y (repeat x))))
OK, trying to figure out how take
works… it seems like a loop and i thought that was counter to functional programming?
@solicode: you'll eventually need to use a Java library that logs to slf4j or logback or whatever, so this might be a good time to add logback (it too can log to stdout/stderr)
@kopasetik: it's pretty easy, look
I have an atom containing a seq. Now, what I want to do is update every element in the seq inside the atom like this [1 2 3 4] -> [2 3 4 5]. So, I need a function that gives me the whole seq inside the atom, maps over it and updates the atom. All this should happen as one transaction. So first getting the vec, than updating and resetting the atom is not an option. I need swap!. But which function combines with swap gives me the whole seq and replaces it then?
@kopasetik: it doesn't return a list, exactly.
@kopasetik: for most purposes, you don't need to care if a function's result is a sequence, a list or a vector
You can think of sequences as hmm... if you're familiar with generators it could be an analogy.
And so it happens like other languages have iterators, Clojure has sequences as the ubiquitous iteration protocol.
@jarredlhumphrey: Sorry, back to your issue. I just tried this (followed the example on Timbre's project page) and it worked for me:
:appenders {:example-println-appender
{:enabled? true
:async? false
:min-level nil
:rate-limit nil
:output-fn :inherit
:fn (fn [data]
(let [{:keys [output-fn]} data
formatted-output-str (output-fn data)]
(print (str formatted-output-str "\n"))))}}
@kopasetik: So in general Clojure likes dealing with sequences - map
returns a sequence transformed by a function, reduce
reduces a sequence to one value, filter
returns a sequence only with elements that match a given predicate, take
returns n-first elements of a sequence and so on. If you decide to pick up "Programming Clojure" it's all rather very nicely introduced there.
So basically if you decide to transform something you'll get a sequence out of that, in general it won't preserve the type of sequence (as an implementation detail different types of collection have different underlying sequences, I think, but like I said - implementation detail). If you for some reason need a specific type of a collection there's into
- (into {} [[:a 1] [:b 2]])
will return {:a 1 :b 2}
and (into [] (map #(* % 2) [1 2 3]))
will give you [2 4 6]
.
I can't believe I haven't run in this doubt before, but:
is it possible to reference a key in a map at definition time?
e.g. (def x {:a 1 :b (inc (:a x))})
I mean, the above example doesn't work, but I was wondering if it would be possible to make it work without defining x first and then merging :b
or something like that
@jr: that will obviously work, thx, will use that for now, but was looking for a less verbose alternative