This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-02-15
Channels
- # announcements (2)
- # aws (3)
- # babashka (2)
- # beginners (100)
- # calva (3)
- # chlorine-clover (22)
- # cider (7)
- # clj-kondo (1)
- # clojure (240)
- # clojure-france (6)
- # clojure-hungary (2)
- # clojure-spec (7)
- # clojure-sweden (3)
- # clojure-uk (19)
- # clojurescript (21)
- # core-typed (8)
- # cryogen (2)
- # cursive (8)
- # data-science (7)
- # datomic (7)
- # duct (7)
- # fulcro (13)
- # keechma (1)
- # luminus (3)
- # malli (3)
- # off-topic (258)
- # pathom (5)
- # reagent (4)
- # shadow-cljs (111)
- # sql (4)
- # tools-deps (12)
- # vim (1)
- # vrac (1)
- # vscode (35)
i don't know much quoting and splicing. how can i make the following return unqualified symbols?
(let [v 99]
`(?entity :item/stock ~v))
=> (myproject.core/?entity :item/stock 99)
but instead return
(?entity :item/stock 99)
take a look at backtick https://github.com/brandonbloom/backtick#usage
Hey friends, if namespace A requires namspace B, how do I get access to namespace A's symbols in namespace B without a cyclical dependency error? I'm using namespaces to separate concerns in my project, so namespace B isn't some sort of stand alone library. Thanks!
You have to avoid cyclical dependencies. Try refactoring your code, perhaps by making a 3rd shared namespace.
So there are actually 4 namespaces, and the purpose of namespace A, core, is supposed to be to hold the symbols that are needed in every namespace, and then to require all of the "specific" namespaces. How would I restructure that so all I have access to certain symbols in every namespace?
It was supposed to be the thing you're suggesting but I clearly didn't quite understand π
You just can't have "certain symbols in every namespace" unless the namespace with those symbols doesn't depend on anything. Try to keep your dependencies minimal and explicit.
without seeing your code it is hard to say, but usually in a situation like yours I have ended up breaking out the common/shared/whatever functions into a separate namespace, say common
or utils
or tools
or something which des not depend on anything. Then you can use common from all your other namespaces and still tie everything together in say core
but like @UCPGSBNQ4 said above, you need to think through be dependencies between namespaces
it sounds like the two things that core does, (define common behavior and load all of the files) are actually seperate tasks that should have separate namespaces. Thanks, I'll make "core" just be a bunch of requires and nothing else and move all it's code into "common"!
That worked!! Thanks a bunch My "core" namespace is literally 8 lines of code now which bothers me a bit but I can get over it π
I tend to see core as the place to stick my "central logic", at least for smaller projects.
I have a question, I find myself writing first
a lot in front of reduce
calls where I keep some multi-value-state. I.e.:
(first
(reduce
(fn [[a i] x] [...])
[[] state]
coll))
I thought I saw a pattern once which made this cleaner than first
, but can for the life of me not remember or find it. One way I guess is (let [[result _] (reduce β¦)])
, but I thought I saw something better. Might seem nit-picky, but this shape has been bothering me for a while so figured I would askseems fine to me, but another option might be to write it as a loop/recur and just return the value you need when you get to the termination condition - that lets you separate out the collection and the state more cleanly.
(loop [a nil, i nil, state state, [x & xs] coll] (if x (...(recur a i state xs)) [a i]))
Thank you @alexmiller - I think I have harboured some subconscious resistance to the loop construct, but now that I think about it I can't come up with any actual reasoning behind it...,perhaps time to let it go. Thanks for the pointer, on the spot as usual.
To avoid an explicit loop (which is often recommended when practical) another way is to keep using reduce
but use reduced
to short circuit and return the first value when it is found.
Here's an example.
(defn limited-sum [limit]
(reduce
(fn [acc n]
(let [acc' (+ n acc)]
(if (>= acc' limit)
(reduced acc)
acc')))
(range)))
(limited-sum 1000)
=> 990
can somebody explain what's up with that error, i'm running cursive and shadow-cljs nrepl, calling hello-world constructor generated by rum which should return ReactElement instance https://i.gyazo.com/53285d3705fc2decb3fae02baf387499.png
in java every object has a toString method, in js every object is a dictionary as far as i know
how to convert byte var to char
how to conbine two bytes into int? I am getting 8 bytes from
(let [in (DataInputStream. (BufferedInputStream. (FileInputStream. "/dev/input/js0")))
data (make-array Byte/TYPE 8)]
(while true
(let [result (.read in data)
data that is there documented in here https://www.kernel.org/doc/Documentation/input/joystick-api.txt
So how to combine (nth data 4) (nth data 5)
into an signed integer?I am using spacemacs with CIDER and clj.
I pull the okhttp mockwebserver dependency and import it in a test file.
Running the test from clj
works fine, but when I eval ns
from spacemacs it fails with a ClassNotFoundException
.
Any idea what I am doing wrong?
{:paths ["src"]
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
amazonica {:mvn/version "0.3.152"}}
:aliases
{:test
{:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "0.10.0"}
com.cognitect/test-runner {:git/url ""
:sha "f7ef16dc3b8332b0d77bc0274578ad5270fbfedd"}
com.squareup.okhttp3/mockwebserver {:mvn/version "4.3.1"}}}
:all
{:main-opts ["-m" "cognitect.test-runner"
"-d" "test"]}}}
test-file
(ns myproj.e2etests.rss-server-driver
(:import [okhttp3.mockwebserver MockWebServer]))
Any help is appreciated :)Hi guys, quick question. Do you think it's mandatory to have a good Java experience before trying to find a career in Clojure? In fact, most of the "Clojure" jobs' description ask for a good understanding of Java and the JVM. I guess there is very few 100% Clojure jobs out there and the programmer will probably have to work with other technology based on the JVM.
No. You can be good at Clojure and not know Java. There will be a point where you will want to lvl up and when this happens knowledge of Java will be an asset, but you can build to this knowledge.
Iβd say that at least a shallow understanding is quite useful but you can get very far without deep knowledge. It also depends a lot on the context (company, team, project)
I've found that there are certain areas where you'll need to be able to understand the Java l;anguage's docs. Specifically in my case, working with File IO for more than what's available in
I don't know Java at all. Sometimes there's something weird I can't immediately figure out but Java has so much info out there on Stack Exchange that it's hard to go too far wrong.
I thought that's because it's so hard to find Clojure devs that companies are willing to hire Java devs and then train them.
hey team i have a quick question regarding the Component lib. I was able to get a web server up and running and i am now trying to hook up a database. I have the following DB component file
(def pg-db
"Connect map we will use to connect to PostgreSQL"
{:dbtype "postgresql"
:dbname "patentfitness"
:host (System/getenv "DATABASE_HOST")
:user (System/getenv "DATABASE_USER")
:password (System/getenv "DATABASE_PASSWORD")})
(defn connect-to-database [config]
(jdbc/get-datasource config))
(defrecord Database [config connection]
component/Lifecycle
(start [this]
(println "Starting database...")
(if conn
this
(let [conn (connect-to-database config)]
(assoc this :connection conn))))
(stop [this]
(println "Stoping database...")
(dissoc this :connection)))
(defn setup-database []
(map->Database pg-db))
my question is how would i pass the DB as a constructor? assume i have something like this
(defn my-query [db]
;; do work)
when i call my-query
what should i be passing into the db
param?some people would pass the Database
component itself, from which you can access to the connection via destructuring
well an instance of that defrecord, which is typically part of the system and the component that is trying to reach the database got via its dependencies
hmmm sorry i am not following. im pretty new to clojure π if i access the defrecord
it should just return the instantiated component, right?
Oh no donβt apologize! Let me take one step back and explain more thoroughly!
In the component libraries, you have components and systems. You defined your database component, and a function to create that component. You did not yet specify a system
This system defines the dependencies between components
Lets say we have a system of a database connection and a webserver, then we could say that we have a system of two components: database
and webserver
, on which the webserver
component depends on this database
ah ok! yes this
(defn new-system
"Set up our system map"
([port]
(component/system-map :database (database/setup-database)
:web-server (server/web-server port))))
yes! thats your system definition, but you currently donβt have any dependencies between your components
you can specify them using component/using
(defn web-server
"Map web server to component"
([port]
(component/using (map->WebServer {:port port}) [])))
well I can imagine that as part of handling a web request, you may want to access the database
Iβd say yes
(defn web-server
"Map web server to component"
([port]
(component/using (map->WebServer {:port port})
[:database])))
I would specify that in my system map tho, to maintain overview of which components depend on what
(defn new-system
"Set up our system map"
([port]
(component/system-map
:database (database/setup-database)
:web-server (component/using (server/web-server port)
[:database]))))
(formatting went weird on slack)
now, the component library promises you that the database component runs its start function before the web-server component, and assocs the database component into the web component
the components are started in dependency order
ok this is perfect thank you so much for explaining things to me @UDF11HLKC!this all makes sense now
Donβt be afraid to ask for advice ^^! People hanging out in #beginners are probably here because they like helping
another probably dumb question π cross post from #sql https://clojurians.slack.com/archives/C1Q164V29/p1581800277208200
sounds like you are passing in a nil
that should be something else
which function are you calling, the error doesnt say
(defrecord Database [config connection]
component/Lifecycle
(start [this]
(println "Starting database...")
(if connection
this
(let [conn (jdbc/get-datasource config)]
(assoc this :connection conn))))
(stop [this]
(println "Stoping database...")
(dissoc this :connection)))
defrecords have two constructors
map->Database
which takes a map and gives you a record instance, and ->Database
which takes a positional list of arguments
it is cool if i do
(defn setup-database []
(map->Database {:config pg-db}))
clojure is smart enough to set config
to pg-db
and leave connection nil(actually there is a third, but you can forget about that for now)
no worries!
I'm writing an http server that uses the clj-http
library. I'd like to pass in
the post
and get
functions as parameters explicitly so I can pass in mocks
when testing. I have two questions - 1) does that seem like a good design? 2) If
so, is the most idiomatic way to accomplish that to wrap up
clj-http.client/get
and clj-http.client/post
into a map and pass it around,
like this?
(def my-http-client-wrapper {:get clj-http.client/get :post clj-http.client/post})
Check out with-redefs https://clojuredocs.org/clojure.core/with-redefs
oh no kidding! thanks @USGKE8RS7
@kamuela Not quite sure what you're asking -- can you share code that relates to the question?