what's the best way to check if an item in a sequence is not nil, nor empty, as in in (map #(if not nil % nor empty % then (func %)) coll)
?
(empty? nil) ;;=> true
actually (map #(if-not (empty? %) (func %) coll) does the trick : )
(not (seq x))
?
e,g..
(defn empty-or-nil [x]
(not (seq x)))
(map empty-or-nil [[5] [] [:a :b :c] nil])
=> (false true false true)
it works, but empty?
is enough, it appears
empty?
will throw if argument is not ISeq
(empty? 42)
for example
Iโm combining sequential? with empty? to avoid unnecessary throwables
Clojure 1.10.1
user=> (empty? nil)
true
user=> (empty? false)
Execution error (IllegalArgumentException) at user/eval3 (REPL:1).
Don't know how to create ISeq from: java.lang.Boolean
well I seem to work with sequences these days, to be pragmatic. and trying to achieve polymorfism everywhere (eg for numbers) feels like premature optimisation )
This is mostly about invalidating wrong input then polymorfism but yes, if you are not expecting non-sequable arguments it will work
does anyone know a hashing function for clojure hashmaps? I'd like to obtain digests from data maps... But specifically I need {:this :cat :that :car}
to have the same digest as {:that :car :this :cat}
. Eventually I want this digest for storing it inside a file rather than storing a full map and be able to compare versions of the data from digests. Only need really will be "same digest? ok same data. Different digest? ok different data"
https://github.com/arachne-framework/valuehash this one works great
looks nice thanks, I'll give it a go!
I like (cond-> x something? f)
for these cases instead of (if something? (f x) x)
. It lets you avoid repeating the x
.
I'd write it like this:
(map #(cond-> % (seq %) func) coll)
HI. Iโd like to poll a service every 3 mins, which approach is more preferred? core.async with timeout or ?
https://github.com/jarohen/chime I used once that library for that
I had a situation where I knew that there would always be maps, and no "recursive maps". So I just sorted the maps and hashed the sequence.
(defn hash-fn [_])
(-> {:a 1 :b 2} sort pr-str hash-fn)
If I had known about valuehash, I might have used that!Are there any tools to go up and down the ladder of abstraction with Clojure functions? i.e. view concrete examples of functions all the way up to versions abstracted over all possible values of a parameter? In this example, there's an algorithm for a car trying to stay on the road by turning when it's off the road, and you can change the angle that it turns when it's off the road. One of the screenshots basically shows all of the possible values of the angle between 0 degree and 10 degree turns at once to help you figure out which is the best angle to pick. http://worrydream.com/LadderOfAbstraction/
yep I think I'm going to end up doing the same, sorting the map to edn then hashing, because I need the same hashing being done on the client side with clojurescript and valuehash doesn't seem to support cljs
the built in hash function is not order senstitive
user=> (hash {:this :cat :that :car})
534635990
user=> (hash {:that :car :this :cat})
534635990
maps are unordered
that's pretty good to know actually, thanks Alex
the order you see printed is not relevant
<https://borkdude.github.io/re-find.web/?args=0%20%5B1%202%203%5D>
can be used a bit in that manner
it's probably exactly what I need for my usecase as I only need to check equality, thanks a lot
and cljs hash should have this same property (but we don't guarantee the hashes will match between clj and cljs)
ah yeah good point
well keep in mind, two objects that are = will have the same hash, but the opposite is not true
here searching on input shows output under a number of functions. not exactly what you're asking for but the closest thing i'm aware of
it is not even gurantied to stay the same between JVM instances/runs, isnโt it?
no, or between clojure versions
but the property will remain true (= objects have the same hash)
ok I see I'll stay on sorted maps to edn
thanks for your answers
btw, looks like it should be possible to make valuehash support cljs as well
import com.twilio.jwt.accesstoken.AccessToken;
public class Main {
public static void main(String[] args) throws Exception {
AccessToken token = new AccessToken.Builder();
}
}
Hi all, any idea how I would convert the http://AccessToken.to clojure?good, I might give it a go then, could be a fun exercise for me
https://github.com/arachne-framework/valuehash/blob/master/src/valuehash/api.clj#L28-L32 this function is the only problem here.
was more concerned about the impl namespace personally to guarantee same results on the two implementations
Iโve tried
(ns dev.user
(:import (com.twilio.jwt.accesstoken AccessToken)))
(new AccessToken/Builder)
;; => Syntax error (IllegalArgumentException) compiling new
;; Unable to resolve classname: AccessToken/Builder
;;
(AccessToken/Builder.)
;; => Syntax error (NoSuchFieldException) compiling.
;; Builder.
I think inner classes are accessed with $, so maybe (new AccessToken$Builder) ?
@tero.matinlassi Thank you. Just tried it. Still getting an error: Unable to resolve classname: AccessToken$Builder
I found something though when searching inner class
Might need to import the inner class
Yeah, was thinking that too
That worked!
Thank you!
user=> (AccessToken$Builder. "foo" "bar" "baz")
#object[com.twilio.jwt.accesstoken.AccessToken$Builder
"0x1f208930"
"com.twilio.jwt.accesstoken.AccessToken$Builder@1f208930"]
@dharrigan thank you. Yes just got it working. I also needed to add the import
(:import (com.twilio.jwt.accesstoken AccessToken)
(com.twilio.jwt.accesstoken AccessToken$Builder)
(com.twilio.jwt.accesstoken VideoGrant))
@dharrigan is there a better way of doing it? Or is this the right way to import?
yes, you can put all in a package on the same line
ah nice ok
i.e., (:import [com.twilio.jwt.accesstoken AccessToken VideoGrant AccessTokenBuilder])
beautiful
thank you!
I think the style is to put each underneath, depending on your personal tastes ๐
(or pain point when it comes to line width)
Have fun! ๐
Got it, thanks again!
Hi! I'm trying to compile uberjar of a simple project and lein just freezes and I have to break it's execution after some time
Most likely you have a def
form that is performing some long-running process?
(top-level forms should not do any side effects)
The code is basically minimal example from this page https://github.com/cljfx/cljfx
called from -main
Ah, building a cljfx JAR requires some special care as I recallโฆ Ask in #cljfx
ok, thanks
Hello!
Given a map with this representation:
#:album{:name "Magical Mystery Tour", :artist #:artist{:name "The Beatles"}}
How do i convert this to the "normal" representation at the REPL ?
(set! *print-namespace-maps* false)
in your repl
If you're looking to actually unqualify the map, something like this should work
(comment
(def unqualify-keyword (comp keyword name))
(defn deep-unqualify [m]
(if (map? m)
(reduce-kv (fn [m k v]
(if (map? v)
(assoc m (unqualify-keyword k) (deep-unqualify v))
(assoc m (unqualify-keyword k) v)))
{}
m)
m))
(deep-unqualify #:album{:name "Magical Mystery Tour", :artist #:artist{:name "The Beatles"}}))
when i mean normal it's not about unqualified keywords, its:
{:album/name "Magical Mystery Tour"
:album/artist {:artist/name "The Beatles"}}
that should do it. did you try it?
user=> {:album/name "Magical Mystery Tour", :album/artist {:artist/name "The Beatles"}}
#:album{:name "Magical Mystery Tour", :artist #:artist{:name "The Beatles"}}
user=> (set! *print-namespace-maps* false)
false
user=> {:album/name "Magical Mystery Tour", :album/artist {:artist/name "The Beatles"}}
{:album/name "Magical Mystery Tour", :album/artist {:artist/name "The Beatles"}}
Iโll play with that, thanks!
You could do this with clojure.spec
https://clojure.org/guides/spec#_exercise. You would just need a way to visualize inputs and outputs
Yes it works @dpsutton thank you
ah, another unknown function, thanks )
I used this library once, very useful for testing requests, like the ab command https://github.com/brunoV/throttler