hey all - bit of a lazyweb question really but I’m sure someone has posted about it sometime - I’m trying to build a transformation pipeline over a sequence based on a couple of query-params in an API call - there are a number of boilerplate and ‘ugly’ ways I can do it but I’m hoping there is something elegant/idiomatic that someone can point me to - thanks!
what have you tried?
or maybe, What does "boilerplate and ugly" mean to you?
Hey - I’ve put it together with a condp in the first go - works but does not feel right - was thinking about cond->> maybe next
there's nothing wrong with using condp
if that's what works for you. it's a solid function that's well understood
another idea, as i'm thinking about it, is to move both the predicate check and the change to functions so you just use ->>
and then can just list each potential change in a row, and the functions say (if (pred input) (apply-fn input) input)
. that's "cleaner" than cond->>
but has other issues (like having to repeat/keep track of the if
branches)
May be checkout as->
, it let's you set a binding name (usually $
) and thread your calls, placing the result wherever makes most sense for the next fn call in the thread
thanks all - I’ll have a play with some of this later on - good to crowdsource some methods 🙂
Hello! Can I somehow start a delayed action in current (!) thread withiout blocking current thread? This way also evaluates action in another thread:
(def d (do
(prn "initial thread" (.getId (Thread/currentThread)))
(delay (prn "delay thread" (.getId (Thread/currentThread))))))
(future (Thread/sleep 5000) @d)
@ivana Thread/sleep
if you block your thread you block your thread
if you don't want to block your thread you need to work on another thread
kinda as simple as that unless you want to make manual state machines
Thanks. It's a pity, if so
you'd need to make your own coordination. basically a loop look for tasks, which would block the thread. but this seems to be a necessity. consider if your thread went into an infinite loop calculating primes. how would this other unit of work interrupt that infinite loop and get its work done? What problem are you trying to solve though?
Actually I tried to sole a problem with starting my own custom REPL when lein starts with its profile. But I just solved it - before I put code starting my REPL in :injections
or :repl-options :init
and it crashes leins Repl-y, I tried to make a delay without blocking current thread, or starting my REPL in another thread, but I found out that _:welcome_
calls after them all and put it here seems to be working.
Looks like I found working configuration 🙂
:bb-repl {:dependencies [[bb-repl "0.0.1"]]
;; :injections [(use '[bb-repl.repl :as bb])] or keep it here instead of :welcome
:repl-options {:welcome (do
(println "Welcome to bb-repl!")
(use '[bb-repl.repl :as bb])
(bb-repl.repl/start))}}
Dockerfile:
FROM clojure:latest
RUN clojure -e "(clojure-version)"
but I get:
/bin/sh: 1: clojure: not found
Anyone run into this? Feel like I’m going mad, probably something stupid on my end. The latest tag does install Clojurewhich version of clojure CLI?
I guess maybe you can't run it to see :)
should be at least 😅
I've been having trouble all day trying to connect to a mysql host in RDS. I'm able to connect to a local Mysql on my machine, I'm able to connect via the command line on the host I'm using, but when trying to use jdbc
(let [db-host "my-rds-host"
db-port 3306
db-name "lms"]
(def mysql-db {:subprotocol "mysql"
:subname (str
"//"
db-host
":"
db-port
"/"
db-name)
:user "admin"
:password "rootroot"}))
I've tried the above, and also using a :host
parameter. Any advice on how to debug this ?I have [org.clojure/java.jdbc "0.7.12"]
in project.clj
can you see the output of running the install there?
are you trying to pass mysql-db
map to the various j/*
functions?
is that variable in scope?
what’s the error you get?
what’s the value of the (println mysql-db)
(defn get-schools [& [{:keys [limit]}]]
(j/query mysql-db
[(str "select * from school "
(when limit (str "limit " limit)))]))
yes the var is in scope
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 1 milliseconds ago. The last packet sent successfully to the server was 1 milliseconds ago.
also tried it like this
(let [db-host "rds-host"
db-port 3306
db-name "lms"]
(def mysql-db {:dbtype "mysql"
:dbname "lms"
:host db-host
:user "admin"
:password "rootroot"
:ssl true
:sslfactory "org.postgresql.ssl.NonValidatingFactory"}))
unless everything is inside that let
form top level functions wouldn’t have access to it
@jdkealy Feel free to ask in #sql — there may be folks there using RDS.
I’d be a bit surprised if PostgreSQL was on port 3306 though — that’s normally MySQL.
Given your :dbtype
say "mysql"
and your port is 3306, I’d say the :sslfactory
attribute is wrong.
right sorry i had just copy/pasted that.
i'll try to find the mysql equivilant if any
And I think for MySQL, it would be :useSSL true
(or false
), not :ssl
, but it depends on what RDS is expecting in the connection string.
that was it!
:useSSL false
Not recommended from a security p.o.v. 🙂
Hi, if I need to recurse when implementing a protocol function, is there a difference between calling recur
or the protocol function by name?
probably 1 stack frame of difference :)
Thanks @alexmiller, good to know! Was initially worried calling recur wouldn't result in a type dispatch.
it won't if I understand what you're suggesting
in the recur case, I believe the type dispatch would happen once, after that it would just be a normal loop/recur back to function target
so even if you recur'ed with a value of a different type, it's not going to "escape" the method body you're in
if you want that, you need to re-call the protocol function (and you'll incur a stack frame)
i'm just importing a legacy db into datomic 🙂
Interesting, thank you for pointing this out.
In the case where I'm worried about incurring stack frames (and I need to re-dispatch on recur), would condp instance?
be the better alternative for type dispatch?
you probably want to trampoline at that point
which is recur but for mutual recursion
yep
I'm not sure if that works through a protocol call, would have to try it tbh
I think it should based on my mental model?
I’m no sure if it’s going to be helpful for you but I was able to run an app using docker-compose like this:
version: "3.8"
services:
clojure:
image: clojure:openjdk-8-tools-deps-1.10.1.536-buster
working_dir: /app
entrypoint: ["clojure", "-R:nREPL", "-m", "hello"]
volumes:
- ./code:/app
- ~/.m2:/root/.m2
ports:
- "9500:9500"
And later connect to the repl from my host OS using VS Code (Calva)
I was using it to teach someone clojure and that person was able to replicate that in his computer.