2

I want to use a Java constructor as a first-class Clojure function. My use-case is to transform a sequence of strings into a sequence of Java objects that have a constructor with a single string:

Simple Java object:

public class Foo {
  public Foo(String aString){
    // initialize the Foo object from aString
  }
}

And in Clojure I want to do this:

(defn make-foo (memfn Foo. a-string))
(apply make-foo '("one" "two" "shoe"))

The apply should return a list of Foo objects created from Strings, but I'm getting this:

IllegalArgumentException No matching method found: org.apache.hadoop.io.Text. for class java.lang.String  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:53)

1 Answer 1

8

Don't bother. memfn is practically deprecated in favor of the anonymous function literal, with which you can also invoke constructors, e.g., #(Foo. %).

Also, your apply call is going to try to invoke make-foo once with three string args. You probably instead want:

(map #(Foo. %) ["one" "two" "three"])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.