This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-10-24
Channels
- # announcements (1)
- # aws (2)
- # beginners (147)
- # boot (19)
- # cider (57)
- # clara (52)
- # cljdoc (18)
- # cljs-dev (14)
- # cljsrn (4)
- # clojure (176)
- # clojure-conj (9)
- # clojure-dev (9)
- # clojure-germany (2)
- # clojure-italy (4)
- # clojure-spec (13)
- # clojure-uk (56)
- # clojurescript (72)
- # code-reviews (11)
- # cursive (17)
- # data-science (1)
- # datomic (52)
- # duct (26)
- # emacs (6)
- # events (9)
- # figwheel (1)
- # figwheel-main (21)
- # fulcro (132)
- # funcool (1)
- # graphql (3)
- # jobs-discuss (42)
- # leiningen (3)
- # luminus (45)
- # mount (10)
- # off-topic (2)
- # re-frame (17)
- # reagent (12)
- # reitit (20)
- # ring-swagger (7)
- # rum (3)
- # shadow-cljs (256)
- # slack-help (15)
- # sql (7)
- # tools-deps (50)
- # uncomplicate (1)
- # yada (9)
Has anyone ever seen an error when using with-redefs
like:
clojure.core$constantly$fn__4614 cannot be cast to clojure.lang.IFn$OLO
?that function signature means the original function was type hinted to allow unboxed args (iirc that specific signature is "returns Object, takes Long and Object as args"), and that means that code that is compiled to use it targets that interface of IFn and not the generic interface constantly implements
you could try (fn [^Long _ _])
as a replacement for (constantly nil)
, as it should implement that same interface
I had it flipped, it's [Object, Long] args, returning Object https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/IFn.java#L109
and here's an anonymous function that has that signature and returns nil
user=> (supers (class (fn [_ ^long _])))
#{java.io.Serializable clojure.lang.IObj java.lang.Object java.util.Comparator clojure.lang.AFn clojure.lang.IFn clojure.lang.IFn$OLO clojure.lang.AFunction java.util.concurrent.Callable clojure.lang.IMeta clojure.lang.Fn java.lang.Runnable}
so use (fn [_ ^long _])
I'm getting it only when trying to redefine a particular third party function, but all my other redefs work fine. I've created a simple example to demonstrate/isolate outside the context of my actual code. This also throws the error.
(defn testing-redef
[]
(langohr.confirm/wait-for-confirms-or-die "channel" 100))
(deftest redef-test
(with-redefs [langohr.confirm/wait-for-confirms-or-die (constantly nil)]
(is (= nil (testing-redef)))))
I am doing the same thing with other functions from the same library and it works fine.
This article inspired me to try something out with tap>
(in Clojure 1.10): https://quanttype.net/posts/2018-10-18-how-i-use-tap.html -- I started a REPL and provided the JVM options to also start a Socket REPL then I connected to the REPL with my editor and I connected to the Socket REPL with nc
. In the Socket REPL, I typed (add-tap (bound-fn* clojure.pprint/pprint))
and then continued to work in my editor, adding calls to tap>
as needed (or (doto tap>)
in ->
threads), and seeing the pretty-printed values stream out of the nc
-connected terminal session without interrupting my input/output and logging etc in my editor-connected REPL. Very nice!
Is it possible to use tap>
with the emitter and receiver end in the same thread/repl in order to collect and delay the printing of debug statements ?
Yes. That's how I tried it first, when it first dropped. I like this method better tho' with multiple REPLs keeping all the interactions separate.
Hi all, I'm calling some Clojure (1.8) code in a larger Java project that I don't entirely have control over. Recently some code has been to the parent that's causing conflicts with clj-http
. Is there a way I can ignore conflicts originating outside of my Clojure code, in lein or otherwise?
if you’re requiring the Java project as a jar, you can use :exclusions
in lein to exclude deps in the same way as maven: https://github.com/technomancy/leiningen/blob/master/sample.project.clj#L55-L59
Sorry I wasn't clear, there is a parent java project - my Clojure jar and a separate Java jar are both children to this parent. The recent inclusion of the child Java jar has resulted in the dependency conflict.
hi all, is it possible to invoke passed via thread macro?
(defn hi [name] (prn :hi))
(defn hello [name] (prn :hello))
(def greets {:hi hi :hello hello})
(-> greets :hi #(% "Kirill"))
Like this?
(example of course doesnt work
right 🙂
thanks
you can use (macroexpand '(-> greets :hi #(% "Kirill")))
to see why one works and the other one doesn’t
that explaing weird message about keyword
thanks!
hi everyone. even though there’s a reagent component in this question example, it’s more of a clojure question so I think it qualifies for this channel 😄 so I had this
(defn some-component [is-active?]
[:div {:style {:opacity (if is-active? 1 0)
:width (if is-active? "100%" "20%")
:height (if is-active? "100%" "50%")
:font-size (if is-active? 14 10)}}])
and to reduce the amount of if
s, I created this
(defn some-component [is-active?]
[:div {:style (css-props is-active?
:opacity 1 0
:width "100%" "20%"
:height "100%" "50%"
:font-size 14 10)}])
and the function looks like this
(defn css-props [condition & forms]
(let [parts (partition 3 forms)
transform-fn (fn [[prop truthy falsey]]
[prop (if condition truthy falsey)])]
(->> parts (map transform-fn) (into {}))))
I was just wondering perhaps there’s a better / built-in way to do something like this?@bravilogy in this case i'd define active and inactive style maps separately and choose between those with one if.
Hi all, I'm new here. I've joined because I've gotten stuck on a problem and can't figure it out, was wondering if anyone could give pointers?
(some background):
after getting excited about Clojure, I've "deep dived" and rewrote a small web app (which is in production)
I've ported about 99% but have now hit a brick wall 😞
I understand this is probably not the best way to learn Clojure, so its likely I'm doing falling over something very basic
nice 😄
everything else has worked up to this point
but I tackled quite a big project straight away 😄 got stuck loads of times, but the Clojure community was super helpful every time
the error I get is this:
clojure.lang.LazySeq cannot be cast to clojure.lang.IFn
and all I'm doing is passing a hash map into a function 😐
you should post the code snippet that throws this error. looks like you’re trying to call a list (I think)
that's strange
one sec
this is how I construct the hash map:
that code block works perfect
the issue is when I call this in another function like so:
(do (save-email data db) ;; <----- PUT do IN HERE
(generate-string {:status "ok" :data email}))
yeah, sorry I had that inside a let, but I then moved into def when I hitting my head against a wall
and trying different things
yeah if you do ((save-email data db) (generate...))
you are calling the return value of (save-email...)
as a function
ok I'm doing to try that right now, one sec
ha it worked
another thing I’d change - instead of (header "user-agent")
, I’d put ("user-agent" header)
. because if for any reason you get anything other than map
in header
, that will throw an error as well
ahah nice
honestly I'm really happy, I spent far too long on this final bit!
yeah I had (get foo :key)
then I read somewhere its the same as (foo :key)
ah, ok yes that makes sense, because that shortcut assumes foo is a map?
if there's no legitimate usage where you'd pass a non-map for header, I'd still use it though. You can always fall back to an empty map on nil
so I'm guessing (get foo :key)
is safer?
but yeah either way. I just like code to yell at me sooner rather than later for these things
gotcha
so, this do
function does this force evaluation?
I'm just trying to understand the solution 🙂
do
is a special form. it just evaluates expressions in order and returns the value of the final one - see (doc do)
from the repl 😉
ok perfect I'll look that up
can't thank you guys enough, this blocker was driving me nuts
the next steps will be a burn out trial, with this new Clojure on a test server 🙂
if it works fine for a couple of works
I'll probably replace our prod version 😄
p.s. you could also do destructuring like this
(defn unwrap-unsub [req]
(let [{ip :remote-addr
{agent "user-agent"} :headers
{email "email"
campaign "campaign"} :form-params} req]
{:email email :ip ip :agent agent :campaign campaign}))
ah ok I see
that's even more cleaner
slight tangent question, do any of guys have Clojure in production and if so, any tips/advice?
I'm slow-rolling it (nobody else on my team knows the language.. yet and I'm paranoid about writing code that only I can maintain... generally comes back to bite me at inopportune times) so not quite yet. Soon though!
fair enough, I have interest from 2 other devs, and we almost free choice to use any language
well the JVM is another story 🙂
that's my angle on getting it at work - we're gonna have to write extensions for a java platform soon and we will turn into a java shop over my dead body
while our group has literally almost every language, the JVM hate is strong 😞
we have everything from C# to Haskell going on
and I've added Crystal in there as well 😂
JVM has its benefits, the java is the painful part IMO. But hey, there's always ClojureCLR if you do C# stuff! Not as much of a community though
I did start off on C# many moons ago
and .net core is ok kind of
I actually like the JVM, its the rest of the team
yeah, I think that's exactly the issue, as soon as the JVM is mentioned, door is closed
devs think "JVM = Java"
oh yeah, we even have production Scala
yeah, also, everyone love Metabase which just rocks
those are 2 production systems already using JVM
and also, don't hate me, but I was porting some of our legacy apps to Kotlin
personally I use different languages depending on the project
of all the languages I've used so far, Clojure has really sparked my interest (and Crystal did that for a bit too)
its ticked every feature I've wanted
the only thing, which would be super super awesome is getting GraalVM native imagine to work, which thus far I've not got to work
in fact I don't think I've every got GraalVMs native to work on any project 😐
but then again, I can put the uberjar into a docker container, so not the end of the world
yeah, but I'm just so impatient 😄
I did look into Avian, which is like 2.2mb JVM, some people where bundling their jars with it to make a single executable
but the steps looked to hairy for me
https://hackernoon.com/creating-a-self-contained-kotlin-app-using-avian-ca7f2987fdd5
😏 I was hoping there was some lein
plugin the lazy me could just do $ lein build avian
i changed some :main-opts
to a different profile and they still seem to be invoked. do i need to manually clear the .cpcache? i see there are caches of the main in there as well
Short of writing a custom macro, is there an idiom for composing transducers where some stages are optional (e.g. could be disabled)? Sort of like cond->>
. What I'm doing right now is building a list of transducers manually and then applying comp to it, but I wonder if there is another way?
you are writing a function A that takes a function B and returns a function C. C conditionally applies B to is inputs
The decision on whether a transducer is to be applied is static, e.g. I don't want to branch on every data item. And I think I'll stick with the current solution (build a vector using cond->
, then apply comp
to it)
you could also use filter
to produce the list of transducers
(defn xf [{:keys [e? o?]}]
(comp
(if e? (filter even?) identity)
(if o? (filter odd?) identity)))
I have a strange behavior with "lein ring server" for subscribe-topics-of-interest-demo. When I run this function in the repl, it runs through each entry in the map as it should. But when I run the same function via "lein ring server" then it only prints "subscribing to 5 topics of interest", and then does not execute the map. Any ideas why this happens?
the problem is map
is lazy. in the repl, lazy collections are forced automatically, but in regular code they’re not
@smeee @noisesmith Thanks! run! works perfectly.
looks like no: https://github.com/clojure/java.jdbc/blob/master/src/main/clojure/clojure/java/jdbc/spec.clj#L100-L101
there are definitely libs that use jdbc and allow providing params by key - eg. hugsql https://github.com/layerware/hugsql
hugsql turns sql files into clojure functions that you use with jdbc, and the parameters in the file become keys
oh, that's much trickier yeah
there's other dsls though
clojure.java.jdbc recommends honeysql
i don't think I can do DSLs either, unless they are really on top of advanced sql server features (probably not)
well, anything that provides the ability to do params by key would be a dsl, clojure.java.jdbc doesn't do that directly
(as far as I know at least)
strange, in .NET everyone does this (or uses something that does):
using(var conn = new SqlConnection("..."))
using (var cmd = conn.CreateCommand()) {
cmd.CommandText = "select top 10 * from foo where bar = @myParam";
cmd.Parameters.AddWithValue("myParam", 1);
var rdr = cmd.ExecuteReader();
...
}
the CommandText you see there is also what shows up in all the logs verbatim, so I don't think there is any runtime magic going on to translate it
it looks lik jdbc (as of ’10 https://stackoverflow.com/questions/2309970/named-parameters-in-jdbc) doesn’t support named args
Yeah, I've looked at supporting named parameters directly in clojure.java.jdbc
but ran into that... and I mostly point people at HoneySQL which supports named parameters as an abstraction on top of clojure.java.jdbc
.
@isak There's a #sql channel if we want to dig deep on that and avoid cluttering up this main channel. It's surprising to me how few Clojurians overall seem to use SQL DBs 🙂
HoneySQL lets you drop pieces of raw SQL into the DSL so, depending on what you need, it may get you close enough.
(! 848)-> clj -Sdeps '{:deps {honeysql {:mvn/version "RELEASE"}}}'
Clojure 1.9.0
user=> (require '[honeysql.core :as h] '[honeysql.helpers :refer :all] '[honeysql.types :refer :all])
WARNING: update already refers to: #'clojure.core/update in namespace: user, being replaced by: #'honeysql.helpers/update
nil
user=> (-> (select #sql/raw "top 10 *") (from :foo) (where [:= :bar #sql/param :my-param]) (h/format {:my-param 42}))
["SELECT top 10 * FROM foo WHERE bar = ?" 42]
user=>
like this @isakWe use HoneySQL heavily at work because we build queries on the fly and need the composability it offers.
Just checked, and the jdbc queries get translated to use SQL parameters for SQL Server. So I guess the current situation is people are writing DSLs to convert from readable forms to forms understandable by jdbc, that will then get converted back to the original form 🙂
@seancorfield cool, i'll check that out
I'm always happy to answer detailed clojure.java.jdbc
and honeysql
questions (in #sql and #honeysql respectively) as I maintain both of them...
just thought of a fairly simple solution that works around this limitation - just pass in one argument, and make it json. Can then pluck it apart as needed
Anyone(TM) know of a html-parsing lib with a deps.edn, or, if that doesn’t exist, which of hickory, clj-tagsoup, or enlive would be most likely to accept a PR for a deps.edn?
seems like a good reason to throw away several otherwise perfectly good finished projects.