This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-10
Channels
- # beginners (35)
- # cider (165)
- # cljsrn (18)
- # clojars (1)
- # clojure (141)
- # clojure-greece (2)
- # clojure-italy (11)
- # clojure-nl (1)
- # clojure-spec (21)
- # clojure-uk (89)
- # clojurescript (56)
- # community-development (3)
- # cursive (3)
- # data-science (55)
- # datomic (13)
- # emacs (12)
- # fulcro (31)
- # graphql (6)
- # jobs-discuss (35)
- # lein-figwheel (10)
- # mount (2)
- # off-topic (3)
- # onyx (22)
- # parinfer (4)
- # portkey (7)
- # re-frame (29)
- # ring-swagger (4)
- # shadow-cljs (37)
- # specter (9)
- # sql (30)
- # tools-deps (15)
- # vim (2)
- # yada (17)
I'm learning the clojure.core/seque
function, and write the code like this:
(def reqs (range 10))
(defn p [s]
(println (-> (Thread/currentThread)
.getName)
s))
(defn send-reqs [reqs]
(seque (map #(do (Thread/sleep 500)
(p "+")
(str %))
reqs)))
(defn parse-resp [resp]
(doseq [r resp]
(do (Thread/sleep 500)
(p "-")
(str r "!"))))
(-> reqs
send-reqs
parse-resp) ;; <-- execute this
from the output, it's likely the parse-resp start after send-reqs finished, I suppose they will run together since in the doc
of seque
it says producer will run in background. what's going wrong with my code?I noticed that Immutant is still using HornetQ, even though development ended on it long ago and it's been turned into ActiveMQ Artemis, which is actively maintained. Are there any plans to move it over? Alternatively, is there a reason it isn't necessary?
Oh, I just found this https://github.com/projectodd/wunderboss-artemis/
@doglooksgood You're running into chunked sequences I suspect. Try it with a range bigger than 32 and you should see some interleaving.
@seancorfield yes, it's chunked. is there a simple way to dechunked. I found this:
(defn dechunk [s]
(lazy-seq
(when-let [[x] (seq s)]
(cons x (dechunk (rest s))))))
@doglooksgood Chunking doesn't normally matter at scale. If you need interleaving for small collections, you'll need to do that more explicitly.
Why does it matter?
That's what I meant.
Are you only working with small collections? Or are you planning to work with large collections too?
I want to send some request(about 100), and handle the resp one by one in another thread.
lazy-seqs and side-effects are a poor mix, if knowing precisely when and how many results are realized is important for correctness, IMHO don't use laziness
Perhaps core.async would be more suited?
I'm trying to get a regex to split a string on spaces, but include the space. I've got
(clojure.string/split "the cat sat on the (mat)" #"(?=\s)") ; => ["the" " cat" " sat" " on" " the" " (mat)"]
but really I want the space to sit at the end of each word rather than the beginning, like this:
["the " "cat " "sat " "on " "the " "(mat)"]
Is this a foolish thing to try and do with regexes?I think regex's are certainly up to the task, but maybe the function split isn't a good fit.
What would you want to happen if there were initial spaces in the string to be split? Be in an initial string that contained only spaces?
ah good question about the initial spaces
I'll trim the string before hand
just for reference this is ui code I'm splitting the text so that I can do this:
Try this: (re-seq #"\S+\s*" "the quick brown fox")
aw neat! That looks great thank you!
I didn't think of re-seq
much better 🙂
Yeah, split is best when you are trying to remove some possibly repeated occurrences of a regex match, and return what did not match. re-seq is for returning repeated matches of your regex.
I’m trying to create a little command line application. I’m using app
template and in my -main
I’m just doing (println "Hello world")
for now. whenever I create a standalone jar file using lein uberjar
and when I try to execute it after doing chmod +x app.jar
, it shows me the following error:
exec format error
I understand that this happens because the kernel doesn’t understand the language but how can I create an executable otherwise?The jar is your code, but it’s not executable without the jvm
So here you code start it with java -jar app.jar
@bravilogy This might be useful, it generates standalone apps: https://github.com/Raynes/lein-bin
now you can use Graal's native-image
to create native executables too
hi all, i was wondering why the order of function arguments is what it is in clojure core. if i was writing a new function, what should i consider when determining the argument order? i asked a stackoverflow question here: https://stackoverflow.com/questions/50275513/rules-of-thumb-for-function-arguments-ordering-in-clojure
@ben.mumford620 You mean the "sequences at the end, collections at the beginning" kind of thing?
See, for example, Rich's comments in this thread https://groups.google.com/forum/#!topic/clojure/iyyNyWs53dc (from 2008)
yeah that kind of thing
some rules of thumb would be awesome, i'll check out the link. put the answer on the SO question and i'll give you some points (yum yum!)
thanks alex
my thumb rule: i think about my options for creating partials of the function, as well as calling it in the context of a -> or ->> macro.
I have a function that checks a cache, and if nothing is in the cache, it sends a request to a remote data source
hello everyone, not sure if this is the right place to ask, but theres something in clojure similar to elixir's with
macro?
the point is that I have a mock data source I can create by giving it a map, and it will create a promise that resolves with data from that map. I want to make sure that a value from that map is never gotten
Hmmm... a custom map that performs an observable side effect when accessed? Something like sets a global that can be acted upon with an assert or similar at test time. No idea, really, never seen quite this thing before :O)
@lilactown you could probably accomplish what you want by putting something in the function that fetches from the cache to make sure that it pulled from that, and putting junk data in your map to make sure that the value returned isn't that junk value?
I don't think there's anything quite exactly like it - Clojure doesn't focus as much on pattern matching
if I'm understanding what you're asking for, anyways
@tanzoniteblack I'm kind of doing that now. it's complicated by the fact that the function doesn't return a value, but an atom
@lilactown i really like the [:ok result]
and [:error :some-id]
indiom, maybe i will write a macro myself
yeah, Erlang/Elixir have a lot of really nice pattern matching & other language features to support that. it is nice when the idiom is so supported by the language
Clojure's idioms tend more towards exceptions, so you might struggle to use the [status payload]
tuple strategy as effectively
im trying to isolate everything that throws an exception inside a try catch block and then returning the tuple, but I need a way to chain these operations (a monadic bind or something)
tricky. there are some libraries, like failjure & cats that implement more algebraic structure onto error handling & control flow in clojure
I guess what I really want is... something that I can pass to (:keyword thing)
and thing
will have some custom behavior
you can use deftype
and implement clojure.lang.ILookup
(but honestly I don’t think you should do any of this)
that might be confusing to people who are ready the code later who are expecting thing
to be a map, though
if I were reviewing this code in Clojure, I’d just tell you to use an exception
reading through the backlog again, I may be crossing the streams with @plins
you’re after “custom map that performs an observable side effect when accessed” ?
you could just pass something that is not a map and would thus blow up if you tried to use it like one
or is there some other behavior that should behave as expected
there's a bunch of async stuff going on too. also it's in clojurescript, using promises. 😅 the hole deepens
isn’t that what you expect?
I would expect when I passed in "NOTAMAP"
and it's accessed via ("NOTAMAP" :test)
at some point, that it would cause an exception
well “NOTAMAP” is definitely not a function so, I would agree. but that’s different than (:test “NOTAMAP”) which would just return nil
(perhaps unfortunately)
get
is similarly tolerant of non-associative inputs
(match (partner-db/get-partner partner-id)
[:ok partner] :do stuff
error error)
is there a better way of doing this? like go on if everything is fine, but if there is an error return it?You mean like throwing an exception?
i can't brain good today:
(let [foos '[{:type foo}
{:type bar}
[:abc [:def {:type baz}]]]]
;; desired output: (foo bar baz)
)
I want to traverse an arbitrarily nested data structure, returning only non-nil values for :type
reduce + tree-seq? or just tree-seq? what's the branching and children fn? i toyed with it and failed
I am having trouble calling the .create
method in the following code…
(ns inferno.storage
(:import
[com.google.cloud.storage BlobId]
[com.google.cloud.storage BlobInfo]
[com.google.cloud.storage StorageOptions]))
(defn storage [] (.getService (StorageOptions/getDefaultInstance)))
(def con (storage))
;; Attempting to call "create" in
(defn upload! [con bucket blob s]
(let [blobId (. BlobId (of bucket blob))
blobInfo (-> (BlobInfo/newBuilder blobId)
(.setContentType "text/plain")
(.build))
data (.getBytes s "UTF-8")
opts (into-array [])]
(.create con blobInfo data opts)))
(upload! con "" "hey.txt" "Hello World!")
;; => java.lang.IllegalArgumentException: No matching method found: create for class com.google.cloud.storage.StorageImpl
I think due to type erasure that Clojure will see getService as returning an Object
you might have to help it out with a type hint on con in the .create call
it looks like maybe that would be ^com.google.cloud.ServiceOptions
but I don’t actually see a create method on that
(->> con reflect :members (filter #(= (:name %) 'create)) pprint)
({:name create,
:return-type com.google.cloud.storage.Blob,
:declaring-class com.google.cloud.storage.StorageImpl,
:parameter-types
[com.google.cloud.storage.BlobInfo
java.io.InputStream
com.google.cloud.storage.Storage$BlobWriteOption<>],
:exception-types [],
:flags #{:varargs :public}}
{:name create,
:return-type com.google.cloud.storage.Bucket,
:declaring-class com.google.cloud.storage.StorageImpl,
:parameter-types
[com.google.cloud.storage.BucketInfo
com.google.cloud.storage.Storage$BucketTargetOption<>],
:exception-types [],
:flags #{:varargs :public}}
{:name create,
:return-type com.google.cloud.storage.Blob,
:declaring-class com.google.cloud.storage.StorageImpl,
:parameter-types
[com.google.cloud.storage.BlobInfo
byte<>
com.google.cloud.storage.Storage$BlobTargetOption<>],
:exception-types [],
:flags #{:varargs :public}}
{:name create,
:return-type com.google.cloud.storage.Blob,
:declaring-class com.google.cloud.storage.StorageImpl,
:parameter-types
[com.google.cloud.storage.BlobInfo
com.google.cloud.storage.Storage$BlobTargetOption<>],
:exception-types [],
:flags #{:varargs :public}})
Hint: type hint the first argument to upload!
as whatever the parent interface to StorageImpl is
the compiler will either figure it all out or provide you a more specific error message. (don't forget to (set! *warn-on-reflection* true)
)
Ok, I updated the sexp to look like this (defn upload! [^com.google.cloud.storage.Storage con bucket blob s] ...)
you only have to typically hint the target of a method call, and the compiler usually works it out. If there in an overload method with different types, you might have to help the compiler disambiguate the differences
I am wondering about that variadic thing at the end of this method call, which as I currently understand I all using the empty array if I am not using it.
The key was changing (into-array Object [])
=> (into-array com.google.cloud.storage.Storage$BlobTargetOption [])
I wonder if that is a general property of java variadic method dispatch, or something special that this library is doing…
Also, when calling upload!
successfully, I got the following two warnings
Reflection warning, /Users/stephen/inferno/src/inferno/storage.clj:18:14 - call to method getBytes can't be resolved (target class is unknown).
Reflection warning, /Users/stephen/inferno/src/inferno/storage.clj:20:5 - call to method create on com.google.cloud.storage.Storage can't be resolved (argument types: com.google.cloud.storage.BlobInfo, unknown, unknown).
Thanks @alexmiller & @ghadi
On a completely separate note, all of this type safety stuff seems so f&%ing worthless.
it’s all an illusion :)
the jvm doesn’t care :)