Fork me on GitHub
Andrew Byala06:12:56

I need to create an anonymous function that returns the or of two other predicates on a single input. I've come up with two ways to represent this, and they do both work, but I suspect there's a simpler representation I can't find. Any thoughts?

#(or (pred1 %) (pred2 %))
#(some (fn [pred] (pred %)) [pred1 pred2])

Andrew Byala07:12:24

Of course it's that simple! Thanks, @U04V4KLKC!

(some-fn pred1 pred2)

Jakub Zika07:12:32

Hi, how would you call this in Clojure?

from: Thank you


Find a java example, and translate it into clojure

Jakub Zika08:12:46

This is the java example

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class GenerateV4PutObjectSignedUrl {
   * Signing a URL requires Credentials which implement ServiceAccountSigner. These can be set
   * explicitly using the Storage.SignUrlOption.signWith(ServiceAccountSigner) option. If you don't,
   * you could also pass a service account signer to StorageOptions, i.e.
   * StorageOptions().newBuilder().setCredentials(ServiceAccountSignerCredentials). In this example,
   * neither of these options are used, which means the following code only works when the
   * credentials are defined via the environment variable GOOGLE_APPLICATION_CREDENTIALS, and those
   * credentials are authorized to sign a URL. See the documentation for Storage.signUrl for more
   * details.
  public static void generateV4GPutObjectSignedUrl(
      String projectId, String bucketName, String objectName) throws StorageException {
    // String projectId = "my-project-id";
    // String bucketName = "my-bucket";
    // String objectName = "my-object";

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

    // Define Resource
    BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(bucketName, objectName)).build();

    // Generate Signed URL
    Map<String, String> extensionHeaders = new HashMap<>();
    extensionHeaders.put("Content-Type", "application/octet-stream");

    URL url =

    System.out.println("Generated PUT signed URL:");
    System.out.println("You can use this URL with any user agent, for example:");
        "curl -X PUT -H 'Content-Type: application/octet-stream' --upload-file my-file '"
            + url
            + "'");

Jakub Zika08:12:06

I am fighting with the url part You can see my last try..

(defn generate-v4-gput-object-signed-url [project-id bucket-name object-name]
  (let [storage (-> (StorageOptions/newBuilder) (.setProjectId project-id).build .getService)
        blob-info (-> (BlobInfo/newBuilder
                       (BlobId/of bucket-name object-name))
        extension-headers (-> (HashMap.)
                              (.put "Content-Type" "application/octet-stream"))
        url ((.signUrl storage blob-info 15 (TimeUnit/MINUTES)
                       ;;  doesn't work
                       (.httpMethod$SignUrlOption (HttpMethod/PUT))
                       (.withExtHeaders$SignUrlOption extension-headers)
    (println "Generated PUT signed URL:")
    (println url)
    (println "You can use this URL with any user agent, for example:")
    (println (str "curl -X PUT -H 'Content-Type: application/octet-stream' --upload-file my-file '"


That example is from the same project, so it can access package Private classes

Jakub Zika08:12:31

How should i call static function which is in class inside interface?


The sign url option class is package private, so the expectation is you don't interact with it directly, so I would find some java examples of using the library


And do whatever they do, because they won't be using package private stuff directly


Ah, interesting, the class is package private, but it has a public static method

👍 1

You are definitely not calling a static method


Maybe checkout the docs on java interop for calling a static method


But I am not entirely sure the clojure compiler will be happy about a package private class with a public static method

hiredman08:12:28 has examples of what static method calls look like

Jakub Zika08:12:51

Yeah, I already tried both (.staticfn class) and (class/staticfn)


You have the class name correct, but are trying to invoke a method named httpMethod on the Class object


You also have an extra set of parens where you at invoking the result of signUrl as a function


You'll also need to check out the section on the interop page about varargs

Jakub Zika08:12:58

Yeah, this is the result of messing with the code trying to solve it…

Jakub Zika08:12:38

My biggest problem is how to call static function on nested class…. I though that by Storage$SignUrlOption I should be able to access the inner class and then I call its static function


Possibly, but the code you shared is broken, and not calling a static method at all, and you haven't shared details of the error messages you got, so I cannot assess if you had a syntax or there was some other problem


It sounds to me like maybe you weren't calling the static method correctly (the way you are describing it, accessing the inner class then calling static methods on it, doesn't in my mind match the syntax of calling a static method)


The syntax is just (theclassname/themethodname arg1 arg2 etc) so (Storage$SignUrlOptiom/httpMethod HttpMethod/PUT)

👍 1

If you did have the syntax of the static method call correct, the error you are hitting might just be the varargs not being correct, but I dunno what error you got

Jakub Zika08:12:08

You are right, thx. I just checked the history and it was (Storage/httpMethod …)

Jakub Zika08:12:32

Now I am getting varargs error, finally 🙂 This is the first time i have needed to call a class inside interface


Clojure's java interop would be better named jvm interop, because every place java and the jvm differ, clojure exposes the jvms version

👁️ 2

varargs and inner classes are two places where java and the jvm differ

👍 1
Jakub Zika08:12:01

Would you recommend some text about details maybe? The clojure <-> java and especially clojure <-> other jvm languages interop is not my strong skill


I don't know of one

clojure-spin 1

Inner classes aren't a jvm thing, they are just normal class files, with names created by concatenating the enclosing class and the inner classes name separated by a $


Varargs don't exist on the jvm, they just methods that take a trailing array argument

Jakub Zika09:12:49

Good info.. This is the updated and working 1:1 code if anybody is interested…

(defn generate-v4-gput-object-signed-url [project-id bucket-name object-name]
  (let [storage (-> (StorageOptions/newBuilder) (.setProjectId project-id) .build .getService)
        blob-info (-> (BlobInfo/newBuilder
                       (BlobId/of bucket-name object-name))
        extension-headers (doto (HashMap.)
                            (.put "Content-Type" "application/octet-stream"))
        url (.signUrl storage blob-info 15 (TimeUnit/MINUTES)
                      (into-array [($SignUrlOption/httpMethod HttpMethod/PUT)
                                   ($SignUrlOption/withExtHeaders extension-headers)
    (println "Generated PUT signed URL:")
    (println url)
    (println "You can use this URL with any user agent, for example:")
    (println (str "curl -X PUT -H 'Content-Type: application/octet-stream' --upload-file my-file '"

👍 2

I guess making requests with http-kit for example doesn't already do retry logic e.g. when 429 or whatever it is is returned?


Http-kit does not do range-queries unfortunately


(unrelated but something else that it could use)


492... "too many requests" ... what behavior do you want? long-poll retry?

Benjamin07:12:43 I was reading this. Incremental backof I guess. Don't know about long polling

Cora (she/her)20:12:04

courier is a thing that'll handle a lot of interesting http client use cases

👀 1
Cora (she/her)20:12:23

things like retries, caching, inter-request dependencies