<html><head></head><body><div class="ydp4b524e96yahoo-style-wrap" style="font-family: Helvetica Neue, Helvetica, Arial, sans-serif; font-size: 13px;"><div></div>
<div dir="ltr" data-setdir="false">I see. So when you mention proxies, are you referring to wrapping java:jnew-runtime-class with some macros? Something like this:</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><div>(java:jnew-runtime-class<br> "get-length"<br> :interfaces (list "org.apache.spark.api.java.function")<br> :methods `(("call" ,"java.lang.Integer" (,"java.lang.String")<br> (lambda (s)<br> (length s))<br> :modifiers (:public)))<br> :access-flags '(:public :static :final))</div><div><br></div><div>?</div><div><br></div><div dir="ltr" data-setdir="false">I'll give that a try today and see how it goes, but I notice that I've got to specify the RETURN-TYPE, where the JDK8 lambda's do not.</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">Looking at how the well existing lambda works, barring returning java.util.function, I can't help but wonder if a variant of the ABCL lambda that returns an implementation of <span>org.apache.spark.api.java.function and the 'call' method might not be the most elegant route, the one with the most natural syntax.</span><br></div></div><div><br></div>
</div><div id="yahoo_quoted_6880165045" class="yahoo_quoted">
<div style="font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;color:#26282a;">
<div>
On Wednesday, July 29, 2020, 4:11:40 PM GMT+8, Alessio Stalla <alessiostalla@gmail.com> wrote:
</div>
<div><br></div>
<div><br></div>
<div><div id="yiv1258928861"><div><div dir="ltr"><div>Java lambdas are syntax sugar to implement interfaces with a single non-default method. ABCL doesn't translate them automatically and doing so dynamically would be slow (creating a proxy each time is costly) without some advanced optimizations, but it could be done.</div><div>You could use some functions and macros to hide the complexity of creating the proxy.<br clear="none"></div></div><br clear="none"><div class="yiv1258928861yqt3430123520" id="yiv1258928861yqt94356"><div class="yiv1258928861gmail_quote"><div class="yiv1258928861gmail_attr" dir="ltr">On Wed, 29 Jul 2020 at 09:30, Steven Nunez <<a rel="nofollow" shape="rect" ymailto="mailto:steve_nunez@yahoo.com" target="_blank" href="mailto:steve_nunez@yahoo.com">steve_nunez@yahoo.com</a>> wrote:<br clear="none"></div><blockquote class="yiv1258928861gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex;"><div><div dir="ltr" style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;font-size:13px;"><div>Greetings all,</div><br clear="none">I'm trying to convert the following Java code in the "Basics" section of the <a rel="nofollow" shape="rect" target="_blank" href="http://spark.apache.org/docs/latest/rdd-programming-guide.html">Spark Programming Guide</a> to ABCL:<br clear="none"><div><br clear="none"></div><div><font face="lucida console, sans-serif">JavaRDD<Integer> lineLengths = lines.map(s -> s.length());</font></div><div><br clear="none"></div><div>I know that "s -> s.length" is a JDK8 style lambda function with one parameter, returning the result of calling length() on 's'. What I'd like to be able to do is write:</div><br clear="none"><font face="lucida console, sans-serif">(let ((line-lengths (#"map" *lines* (lambda (s) (#"length" s)))))</font><br clear="none"><br clear="none"><div dir="ltr">but this isn't getting me anywhere, with Java saying there is no applicable method '<a rel="nofollow" shape="rect" target="_blank" href="https://spark.apache.org/docs/latest/api/java/org/apache/spark/api/java/JavaRDDLike.html#map-org.apache.spark.api.java.function.Function-">map</a>' on *lines* (an instance of <a rel="nofollow" shape="rect" target="_blank" href="https://spark.apache.org/docs/latest/api/java/org/apache/spark/api/java/JavaRDD.html">JavaRDD</a>). There is such a method (if it matters, it is inherited by JavaRDD from interface <a rel="nofollow" shape="rect" target="_blank" href="https://spark.apache.org/docs/latest/api/java/org/apache/spark/api/java/JavaRDDLike.html">JavaRDDLike</a>). Investigating that map method a bit further, it seems to want an <a rel="nofollow" shape="rect" target="_blank" href="https://spark.apache.org/docs/latest/api/java/org/apache/spark/api/java/function/Function.html">org.apache.spark.api.java.function</a>. Here's a clip from the Spark description:</div><blockquote><div><i>Spark’s API relies heavily on passing functions in the driver program to run on the cluster. In Java, functions are represented by classes implementing the interfaces in the org.apache.spark.api.java.function package. There are two ways to create such functions:</i></div><ul><li><i>Implement the Function interfaces in your own class, either as an anonymous inner class or a named one, and pass an instance of it to Spark.</i></li><li><i>Use <a rel="nofollow" shape="rect" target="_blank" href="http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html">lambda expressions</a> to concisely define an implementation.</i></li></ul></blockquote><div>I think what I'm getting from the ABCL lambda expression is a java.util.function:</div><div><br clear="none"></div><div><font face="lucida console, sans-serif">SPARK> (describe (lambda (s) #"length" s))</font></div><div><font face="lucida console, sans-serif">#<FUNCTION #<FUNCTION (LAMBDA (S)) {70B82AD0}> {70B82AD0}> is an object of type FUNCTION.</font></div><div><font face="lucida console, sans-serif">The function's lambda list is:</font></div><div><font face="lucida console, sans-serif"> (S)</font></div><div><br clear="none"></div><div dir="ltr">I do wonder though how the Java lambda "s -> s.length()" manages to produce the correct result, so this theory may not be correct.</div><div dir="ltr"><br clear="none"></div><div dir="ltr">The Spark guide goes on to say:<br clear="none"></div><div><br clear="none"></div><blockquote><div><i>While much of this guide uses lambda syntax for conciseness, it is easy to use all the same APIs in long-form. For example, we could have written our code above as follows:</i></div><i> </i><br clear="none"><i>JavaRDD<String> lines = sc.textFile("data.txt");</i><br clear="none"><i>JavaRDD<Integer> lineLengths = lines.map(new Function<String, Integer>() {</i><br clear="none"><i> public Integer call(String s) { return s.length(); }</i><br clear="none"><i>});</i><br clear="none"><i>int totalLength = lineLengths.reduce(new Function2<Integer, Integer, Integer>() {</i><br clear="none"><i> public Integer call(Integer a, Integer b) { return a + b; }</i><br clear="none"><i>});</i><br clear="none"><div><i><br clear="none"></i></div><div><i>Or, if writing the functions inline is unwieldy:</i></div><div><i><br clear="none"></i></div><div><i>class GetLength implements Function<String, Integer> {</i></div><i> public Integer call(String s) { return s.length(); }</i><br clear="none"><i>}</i><br clear="none"><i>class Sum implements Function2<Integer, Integer, Integer> {</i><br clear="none"><i> public Integer call(Integer a, Integer b) { return a + b; }</i><br clear="none"><i>}</i><br clear="none"><i> </i><br clear="none"><i>JavaRDD<String> lines = sc.textFile("data.txt");</i><br clear="none"><i>JavaRDD<Integer> lineLengths = lines.map(new GetLength());</i><br clear="none"><i>int totalLength = lineLengths.reduce(new Sum());</i><br clear="none"></blockquote><br clear="none">To me, both of those examples look unwieldy. There was a stack overflow discussion on <a rel="nofollow" shape="rect" target="_blank" href="https://stackoverflow.com/questions/4785969/can-you-write-a-java-class-with-abcl">creating Java classes with ABCL</a> from 9 years ago, and I've read the <a rel="nofollow" shape="rect" target="_blank" href="https://abcl.org/trac/browser/trunk/abcl/examples/java-interface">java interface examples</a> on <a rel="nofollow" shape="rect" target="_blank" href="http://abcl.org">abcl.org</a>, but both of those techniques look like they will produce code just as unwieldy as the Java syntax above.<br clear="none"><br clear="none"><div dir="ltr">Is there any way to use a lisp-style lambda syntax to produce a function that will satisfy Spark's requirement to implement the org.apache.spark.api.java.function interface? If not, the obvious route would be some macrology to create a defsparkfun, defsparkfun2, etc. that wrap the ABCL function I want to use with something that implements 'call' in the <span>org.apache.spark.api.java.function interface. I'm hoping there's a better way.</span><br clear="none"></div><br clear="none"><br clear="none"></div></div></blockquote></div></div></div></div></div>
</div>
</div></body></html>