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)
klucar
  • 173
  • 1
  • 6

1 Answers1

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"])
Alex Taggart
  • 7,805
  • 28
  • 31