This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-05-17
Channels
- # aws (3)
- # beginners (104)
- # boot (11)
- # calva (3)
- # clj-kondo (1)
- # cljdoc (6)
- # cljs-dev (23)
- # cljsrn (1)
- # clojure (144)
- # clojure-dev (54)
- # clojure-europe (6)
- # clojure-italy (2)
- # clojure-nl (26)
- # clojure-spec (6)
- # clojure-sweden (1)
- # clojure-uk (13)
- # clojurescript (38)
- # core-async (9)
- # cursive (14)
- # data-science (3)
- # datascript (22)
- # datomic (17)
- # figwheel (1)
- # fulcro (4)
- # graphql (6)
- # hoplon (59)
- # jackdaw (2)
- # jobs (6)
- # jobs-discuss (44)
- # juxt (14)
- # leiningen (1)
- # luminus (3)
- # nrepl (3)
- # off-topic (12)
- # re-frame (24)
- # reagent (7)
- # reitit (7)
- # rewrite-clj (1)
- # schema (1)
- # shadow-cljs (37)
- # spacemacs (4)
- # sql (25)
- # testing (12)
- # tools-deps (11)
- # utah-clojurians (1)
I'm storing a vector in memory, to kind of simulate a database:
(def ^:private state (atom []))
(defn store
[simulation]
(swap! state (fn [_] simulation)))
(defn retrieve [] @state)
It has a function to store and retrieve. The state is an atom.
This is my application code:
(defn place-dinosaur [coords]
(domain/serialize-simulation
(store
(domain/place-dinosaur (retrieve) coords))))
I'm feeling that might be some inconsistencies between retrieve and store. I'm I right? I mean, one deref is inside retrieve
, and there's a swap also inside store
, meaning that it's just a datastructure between these calls, that does not careabout other threads. Is there a mechanism to orchestrate that?@marcocontatopro You could use (reset! state simulation)
inside store
instead of that swap!
call.
I already asked this earlier yesterday and @alexmiller and @seancorfield kindly told me to use test-vars
when calling deftest in repl.
I have deftest get-product-groups-test
in namespace simpleserver.domain.domain-test
.
If I call it in repl like:
(clojure.test/test-vars [#'simpleserver.domain.domain-test/get-product-groups-test])
I can see in log that all test fixtures and the actual deftest is being called, but it returns nil and there is no difference if I make a change in a deftest to make if fail (or the test succeeds).
Should I do something else to see whether the test fails or succeeds when I call it like that in repl?
I quickly tested it and seems to output FAIL to stdout from REPL. Have you re-compiled your test? Example from my REPL:
Loading test/clj/app/query/query_test.clj... done
(clojure.test/test-var #'app.query.query-test/failing-test)
=> nil
Loading test/clj/app/query/query_test.clj... done
(clojure.test/test-var #'app.query.query-test/failing-test)
FAIL in (failing-test) (query_test.clj:22)
1 +1=2
expected: (= (+ 1 1) 1)
actual: (not (= 2 1))
=> nil
So the first call to test-var
is successful test, I only get nil. Then I change the code to fail, reloaded the file an run test again -> FAIL output in stdout, return value still nil
.Hello, I'm learning clojure and I'm trying to use a google java library to the translator API (https://cloud.google.com/translate/docs/quickstart-client-libraries#client-libraries-usage-java) In the API docs, the library is imported with:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-translate</artifactId>
<version>1.49.0</version>
</dependency>
I imported it as: [com.google.cloud/google-cloud-translate "1.49.0"]
and used lein deps
to download it.
My core.clj
is as follows:
(:import [com.google.cloud.translate.Translate])
(:import [com.google.cloud.translate.Translate.TranslateOption])
(:import [com.google.cloud.translate.TranslateOptions])
(:import [com.google.cloud.translate.Translation])
(def translate ((.getDefaultInstance (.getService))) TranslateOptions.)
(def translate (->> (.getService)
(.getDefaultInstance)
(TranslateOptions.)))
But this doesn't work. It says that there is no class Translate. How can I make this work?Try
(ns my-namespace
(:import [com.google.cloud.translate Translate TranslateOptions Translation]
[com.google.cloud.translate.Translate TranslateOption]))
@vasco_mmf The (:import ...)
form is only valid inside the (ns ...)
macro.
What @alex.joseph.whitt said.
Thank you @alex.joseph.whitt, it worked for the first, but the second is giving an error java.lang.ClassNotFoundException: com.google.cloud.translate.Translate.TranslateOption
This is my whole code:
(ns google-translation-java-interop.core
(:gen-class)
(:import [com.google.cloud.translate Translate TranslateOptions Translation]
[com.google.cloud.translate.Translate TranslateOption]))
(def translate (. (. TranslateOptions getDefaultInstance) getService))
(def text-to-translate "Hello, World!")
(def translation
(. translate translate
text-to-translate
(. sourceLanguage TranslateOption "en")
(. targetLanguage TranslateOption "ru")))
(println (. translation getTranslatedText))
@vasco_mmf (:import com.google.cloud.translate.Translate)
then call Translate$TranslateOptions
Oh if it's a nested class then I think you'd want this instead:
(ns google-translation-java-interop.core
(:gen-class)
(:import [com.google.cloud.translate Translate Translate$TranslateOption TranslateOptions Translation]]))
The api link is: https://cloud.google.com/translate/docs/quickstart-client-libraries#client-libraries-install-java, it's the only one it is not working
By the way, has someone already done what you're working on? https://github.com/kolov/glingo
I'm trying to do it by myself 😄
Well practice is practice! (oh there seem to be several other libraries as well)
In case you are struggling with learning Clojure, you can always get re-energised by remembering that “Java is confusing while Clojure is simple” 😎. Of course, as Rich Hickey taught us “Simple is not always easy”:thinking_face: https://blog.klipse.tech/clojure/2019/05/10/java-is-confusing-clojure-is-simple.html
I still have some trouble when thinking in a functional way. I did a simple quicksort algorithm in python and tried to do it in Clojure, it was mind opening. Python:
def quicksort(array):
"""Sort an array using the quick sort algorithm.
Takes an array as argument.
This implementation is recursive."""
if len(array) <= 1:
return array
# Get the element in the center of the array
middle_point = int(len(array) / 2)
# Calculate the median of 3 values, the first,
# the middle and the last elements of the array
pivot = np.median([array[0], array[middle_point], array[len(array)-1]])
# Filter array values by its relation with the pivot
smaller = [x for x in array if x < pivot]
equal = [x for x in array if x== pivot]
larger = [x for x in array if x > pivot]
return quicksort(smaller) + equal + quicksort(larger)
Clojure:
(defn quick-sort [[pivot & coll]]
(when pivot
(concat (quick-sort (filter #(< % pivot) coll))
[pivot]
(quick-sort (filter #(>= % pivot) coll)))))
What am I doing wrong with this part? This is the error I'm getting: No matching method found: translate for class com.google.cloud.translate.TranslateImpl
(def translate (.. TranslateOptions (getDefaultInstance) getService))
(defn translate! [txt src-lang target-lang]
(. translate translate
text-to-translate
(. Translate$TranslateOption sourceLanguage "en")
(. Translate$TranslateOption targetLanguage "ru")))
This is the java code:
Translation translation =
translate.translate(
text,
TranslateOption.sourceLanguage("en"),
TranslateOption.targetLanguage("ru"));
Hey, I want to use this snapshot testing lib: https://github.com/juxt/snap#usage
It says I should install with {:deps {juxt/snap {:git/url "
but that's not the lein :dependencies keys. Do you know what kind of sintax is this?
@marcocontatopro that is deps.edn syntax
Yes i didnt found the package in the clojars website...
Makes sense
With javascript's NPM i'm able to download a dependency from githab, is there a way to do that with lein?
Ok, then. I think i will download the source code for now. Thank you so much guys. You have been awesome on helping the beginners 🙂
there are a couple plugins you could try: https://github.com/reifyhealth/lein-git-down
but yeah, simply downloading the source code is probably the easiest + simplest way
@vasco_mmf I'm not sure, but I'll bet that the translate method uses varargs in Java. Varargs aren't supported directly in Clojure so you have to explicitly wrap those parameters in an array.
I have bought practically all mainstream Clojure books and also some less known Clojure books. But there are two Clojure books I'm still looking for: 1. Effective Clojure Development - 100 Clojure Coding and REPL Tricks You Must Know. 2. Idiomatic Clojure - Learn to Use the Full Power of the Clojure Standard Library.
... the reason being: 1. I'm still learning (bit by bit, from various sources) how to use e.g. repl efficiently (e.g. "Do not write expressions in the repl editor - write expressions in a file and send the expressions for evaluation to the repl using a hotkey") 2. To know that you can do e.g. stuff like:
(->> items
(map (juxt (comp :S :pgid) (comp :S :pgname)))
(into {}))
instead of:
(reduce
(fn
[mymap item]
(assoc mymap
(-> item :pgid :S) (-> item :pgname :S)))
{}
items)
(there is nothing wrong with reduce, recur etc. but I'd like to learn other ways as well)Where can I find these two books?
I've never heard of them, so I'll be interested in the answer to that...
@kari.marttila where did you hear about those books?
Sorry @seancorfield, just my Finnish black humor - meaning that I hope some Clojure guru would write those books for us mortal ones. 🙂
I would immediately buy a copy. 🙂
:rolling_on_the_floor_laughing: Ah yes, I would like to read those too!
I'd say the closest today is Clojure Cookbook for #1 and Clojure Applied for #2?
the Clojure Cookbook was supposed to be kind of like 1
there's the upcoming book from Renzo that might also be a candidate for 2
I love Clojure books, and love to read about how experts believe Clojure should be written. A few that I've found are Joy of Clojure, Elements of Clojure, and Clojure Applied
@kari.marttila I'd highly recommend Eric Normand's REPL-Driven Development course for #1 as well.
Ok. Good to know. I make a order request to our corporation's horrific purchasing system immediately. And you can bet that I don't touch that system without a good reason.
(I'd highly recommend this for everyone by the way!)
And I just noticed Eric has added four more lessons that I haven't watched yet 👀 The guy is a machine!
Oh, god. It's a commercial course. And not a book you can order. I can already see the email discussion with our purchasing department that they tell me that they cannot order that Video course for reasons X, Y and Z. 🙂
Is it so good a course that I'm going to skip our purchasing department and buy the course with my personal money? 🙂 (Which I am very reluctant to use as all my three daughters know very well). 🙂
Yeah, I think it's good enough to pay personal money for. But that's mostly because I'm a big, big advocate of RDD and think everyone should be exposed to a productive workflow based on it.
Thanks. 🙂
I believe he has some free/sample courses you could look at to help you decide.
Yihaa! That book is in the only online book store that they can order books without a major hassle: https://www.adlibris.com/fi/kirja/clojure-the-essential-reference-9781617293580 🙂
What a lucky day! 🙂
he could also probably craft something to satisfy your purchasing department. he's a nice and reasonable fella
Yeah, I think it's good enough to pay personal money for. But that's mostly because I'm a big, big advocate of RDD and think everyone should be exposed to a productive workflow based on it.
This is what I'm going to do next (though I already hate the idea). I'm going to start VPN session. I go to that horrific purchasing system. I fill all those weird fields, click next button add the urls as attachment, click next again, read the error message that I forgot to fill in some field and start the process again. Maybe in the 10th round I have made the order for that book and the functional tv yearly membership. Then I'm ready to have a lengthy email conversation with our purchase department why they should order the video course even though it is not in their list of preferred training providers . And all this because of my love to Clojure. 🙂
Good for you! I mostly buy books etc directly and sometimes I expense them (in theory, we can always expense training purchases at work easily, but I usually just forget).
I also want the book forever and I'd feel guilty about work buying me books that I took away with me if I ever left 🙂
Kari, do it now!
Do it!
All right. I'm in the system and this time I didn't have to change my password and try 20 different passwords before it accepted a new password. The first obstacle is over.
I guess the purchasing system is deliberately so horrific so that employees would not spend money for anything. 🙂
Non-catalog request. Oh, god here we go.
Note to approver: "An essential Clojure book to teach me idiomatic ways to use Clojure." 🙂
I knew this. They have changed the project numbers - again, and the system won't accept the previous project number for the purchase. I'm going to make seppuku now.
The easier one, the book is ordered. Let's see what happens this time. I had to put a note there: "NOTE: The purchasing project number X was not found in the system so I used number Y . If this is an issue I hope that you can move this order to the right project number. " I hate bureaucracy over anything.
Yearly subscription for http://functional.tv ordered. Let's see what happens.
BTW. I was working earlier in a new "start-up" department in our corporation. I actually could just send an email to our department secretary that I want books X, Y and Z and she ordered them from Amazon, can you believe that! Anyway. One day the secretary brought me a new book delivery. Some younger "front-end" developer was laughing beside me with his friends: "Is someone really reading books nowadays - everything is in the net!"
Yeah, right. I didn't bother to teach these guys some cognitive psychology. If someone has spent a year figuring out what is the best structure and format to deliver his/her ideas of some complex topic so that a reader can grasp those ideas reading a book of some 300 pages - I would say that it is worth to read the book.
I don't think you'll be disappointed - I'd forgotten about Eric's courses (wasn't coding clojure for a while) , and I just signed back up. His stuff is great , very high quality.
Ok. Thanks for the recommendation. If the purchasing department approves the order (and they are able to make the order) I'm good. Otherwise I guess I have to buy the monthly subscription with my personal money and try to watch the most important stuff in a month.
And back to business. How do I learn to "see" what's the current status of my repl (I mean what namespaces have been loaded etc.). Since I remember that someone said earlier that he just keeps the repl running for days without the need of restarting it. I experience practically every day some weird things in my repl (namespace not found...) and I have to restart repl, refresh namespaces, restart Mount states etc. and magically everything works again.
hm, I never had such a need so cannot help. I dont know your workflow but I use Cursive (I expect most tooling to be similar), when working with a namespace I load-file it , which also (re)loads all namespaces needed. Hopefully Eric's REPL course will help you.
Ok. Hopefully I get to purchase the course. I also use Cursive and nowadays I write my experimental expressions in a scratch file (without a namespace and send the expressions from there for evaluation to repl).
this might be insightful https://clojureverse.org/t/what-are-the-alternatives-to-using-remove-ns-to-clean-up-definitions/4121/19
regarsing some workflob best practices
Thanks. I need to read that ClojureVerse discussion carefully.
(loaded-libs)
Any high level resource for setting up ring-swagger?
(loaded-libs)
user=> (count (loaded-libs))
405
user=>
That REPL has been running for four days.(I can't remember why I restarted it late on Monday night... probably because I rebooted my Mac for a security update?)
(all-ns)
all defined namespaces - in my clj
repl that's a superset of loaded-libs
the data for the first one is probably readily available. last eval’d form is probably all in nrepl
Yeah, (all-ns)
will include namespaces created on the fly? The difference for me is
#{"flatland.ordered.map" "ws.domain.member.update" "flatland.ordered.set" "ws.domain.member.validation"}
yeah, those flatland namespaces are in my difference too
I wonder if deps.edn is using flatland.ordered to map to keep those dependency hash-maps straight
maybe there's something about how those deps load then...
The codes all on the add-lib branch if you want to look at it
For me, flatland is coming in via lacinia @noisesmith
And I'm going to guess that flatland.ordered
creates the .map
and .set
namespaces without actually requiring them from disk?
that must be it
FWIW, in our code, ws.domain.member
specifically calls create-ns
to provide aliases to specs and keywords that act "as-if" they came from the .update
and .validation
namespaces.
Ah, ws.profile.field
does that
(alias 'mu (create-ns 'ws.domain.member.update))
(alias 'mv (create-ns 'ws.domain.member.validation))