This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-08-13
Channels
- # beginners (28)
- # boot (3)
- # clara (9)
- # cljs-dev (127)
- # clojure (82)
- # clojure-belgium (3)
- # clojure-italy (7)
- # clojure-russia (2)
- # clojure-spec (4)
- # clojure-uk (2)
- # clojurescript (351)
- # data-science (3)
- # datascript (1)
- # datomic (24)
- # fulcro (15)
- # jobs (3)
- # juxt (16)
- # off-topic (2)
- # onyx (18)
- # parinfer (1)
- # pedestal (3)
- # perun (6)
- # re-frame (14)
- # spacemacs (20)
@juanjo.lenero What, specifically, are you trying to achieve? There's probably an easier, more functional way to do it (without a macro).
I’m going through the clojure for the brave and true book and there is an example with a vector of maps that contain body parts.
(def asym-hobbit-body-parts [{:name "head" :size 3}
{:name "left-eye" :size 1}
{:name "left-ear" :size 1}
{:name "mouth" :size 1}
You have to then define a function that constructs a symmetric hobby.
So I wanted to do something like:
(reduce (fn [full-hobbit part] (into full-hobbit [part (only-if (left-part? part) (right-part part)])) [] half-hobbit)
But I extracted that whole [part (only-if …] expr into a function that returns either 1 or two parts depending on if the part has a counterpart.
So I guess you were right, I didn’t need it.
Macros are very rarely needed. Always try to write a function first. Sometimes you need a macro, but it's not often -- and they don't compose like functions. Sometimes a macro can provide useful syntactic sugar -- but those are usually just wrappers around functions that do the real work @juanjo.lenero
BTW @juanjo.lenero You might want to look at cond->
: (cond-> [part] (left-part? part) (conj (right-part part)))
@seancorfield Thank you, I did run into it, I’ll keep it in the back of my mind, but I’ll avoid it in the meantime.
I am referring to an example from http-kit. I am new to clojure
(defn -main [& args]
;; The #' is useful when you want to hot-reload code
;; You may want to take a look:
;; and
(reset! server (run-server #'app {:port 8080})))
From the above example what is this construct called #'app
?@gdrte What is your question?
(and, btw, if you're new to Clojure, #beginners might be a useful channel for you)
#'app
is a Var reference -- so it provides a level of indirection over just app
-- does that answer your question?
I'm a bit confused about how to exactly set up honeysql and postgres depedencies in a project
I added at the moment
[org.clojure/java.jdbc "0.7.0"]
[org.postgresql/postgresql "9.4-1201-jdbc41"]
[honeysql "0.9.0"]
[migratus "0.9.8"]
[nilenso/honeysql-postgres "0.2.3"]
[clj-postgresql "0.7.0"]
which I think it's correctbut following the docs from honeysql and this code
(def db
{:connection-uri (get env :database-url LOCAL-DB-CONN)})
(def all-people
{:select [:first-name] :from [:people]})
(j/execute! LOCAL-DB-CONN (honey/format all-people))
I get
PSQLException A result was returned when none was expected. org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate (AbstractJdbc2Statement.java:371)
If I instead do (j/execute! db (honey/format all-people))
I get No suitable driver found for postgresql://....
and the last thing I don't get is that looking up org.postgresql
in Clojars returns nothing, and what actually instead comes back is
which I'm not sure if it's the same thing or not, but surely it's a different version
@andrea.crotti have you tried using query
instead of execute!
?
@schmee query seems to work, and I understand the error
but that's in the honeysql docs so I thought it was correct
hmm, according to jdbc docs execute!
“…performs a general (non-select) SQL operation”, so I guess the example is just wrong
and I don't get the driver error though
ah ok I can open an issue about that then
the driver error though I think it's just misleading then
these are the dependencies I have:
[org.clojure/java.jdbc "0.6.2-alpha2"]
[org.postgresql/postgresql "9.4.1212.jre6"]
uhm jdbc there is 0.7.0 stable, and you have "jre6" instead of "jdbc41"
anyway if with query it's fine then I don't really mind, I thought it was related with my dependencies
it also looks like I never really need to use {:connection-uri (get env :database-url LOCAL-DB-CONN)}
but I'm passing LOCAL-DB-CONN directly
which is a DATABASE_URL, and also seems to differ from what the docs say
but well as long as it works
thanks
hello all. I'm new to clojurians and wondering, do I need to pay money to see the full slack history or is that not even an option?
@gcast have you looked at https://clojurians-log.clojureverse.org/clojure/index.html ? I don’t know if it’s complete though
Is there an official way of testing expression equality in clojure? https://stackoverflow.com/questions/45663629/testing-equality-of-clojure-expressions
@beoliver no, there's nothing that does this in clojure itself, and I'm not aware of libraries that have the feature either.
@noisesmith ok cool... Im writing a function to do it now. Just wanted to check that I wan't reinventing the wheel 🙂
interesting - I guess that would involve a deterministic renaming of symbols for all locals?
so that (let [a 0] a) and (let [b 0] b) would be equal
Yeah, just implemented it. Seems to work... It also means that (-> foo :blah)
= (:blah foo)
@noisesmith if you are interested a minimal "working" example is
(defn normalize [expr]
(let [hygenic-form (-> expr jvm/analyze emit-form/emit-hygienic-form)]
hygenic-form))
(defn unify
"throws an assetion error if impossible to unify"
[mappings expr1 expr2]
(if-let [bound (get @mappings expr1)]
(assert (= bound expr2))
(swap! mappings assoc expr1 expr2)))
(defn zipCompare [mappings expr1 expr2]
(assert (= (type expr1) (type expr2)))
(cond
(symbol? expr1) (unify mappings expr1 expr2)
(coll? expr1)
(do (assert (= (count expr1) (count expr2)))
(map (partial zipCompare mappings) expr1 expr2))
:else (assert (= expr1 expr2))))
(defn unifiable
[expr1 expr2]
(let [mappings (atom {})]
(if (zipCompare mappings (normalize expr1) (normalize expr2))
true)))
and
(:require [clojure.tools.analyzer.passes.jvm.emit-form :as emit-form]
[clojure.tools.analyzer.jvm :as jvm]))
not sure that would work, tools.analyzer uses the base name to generate hygienic let symbols
although something is going wrong... as the following should not be equal
(unifiable '(let [a 20] (-> {:a a :b 20} :a)) '(let [b 10] (:a {:a b :b 20})))
true
but when I step debug after instrumenting zipCompare
, it throws the correct assertion error on the branch :else (assert (= 20 10))
Hi! Quick question: what would be the idiomatic way in Clojure to pipe data through a number of function, each of which may or may not be applied based on a condition (the condition does not depend on the data). Something like this:
(-> data
(#(if condition1 (f %) %))
(#(if condition2 (g %) %))
...)
juxt
might be what you’re looking for. It takes a number of functions and returns a new function that applies the arguments to all, returning a vector of the results.
@chrisjd thanks but not quite! I want the data to be piped through: the result of applying the first function (if the condition passes) should be used when applying the second function, etc
Hi guys. I have faced such problem. I write my personal ai assistant(chatbot), using http://api.ai, clojure as backend and deploy it to aws lambda. My problem is, that on cold requests, it takes up to 5 seconds to respond. I understand that it happens because of 2 reasons. First is aws internal container/server start up time. And second, jvm start up time. So i want to optimize my side - jvm. I don’t want to write backend in ClojureScript and then use Node.js as host platform. Do someone know, some methods of optimisation of jvm start up time. GC params or something. I appreciate any answers.
Not quite the answer you are looking for, but if I was you I would try to keep the AWS function warm
@hmaurer yeah, i was thinking about that. That will work, but if i found a way to increase for 1-2 seconds start up time, that will be definitely better. Now i play with memory settings. Oracle says that big heap sizes lead to big startup time. So i am identifying the minimum at the moment. Will see if it help… Thank you for reply.
Oh also. It is impossible to pass jvm param in aws lambda. You use it as is.
@U4N27TADS You might try these options to see if they help startup time -client -XX:+TieredCompilation -XX:TieredStopAtLevel=1
(I think I got those from the Leiningen site?)
Another question: is there an “if-let” construct in Clojure that short-circuits? if-let only allows one binding. I would like a version which allows multiple bindings but short-circuits if any of those bindings are nil
do i need to manage serialization/deserialization of requests on server/client manually? (from transit/json to clj/cljs data structures) i am using luminus template for web apps meaning do i use libs like transit.clj/cljs, chashire.. it's not magically done for me via some built in thing?