How to implement a Function1 in Java (its compose and andThen methods)?

2,821 views
Skip to first unread message

Emre Sevinc

unread,
Feb 2, 2015, 9:22:57 AM2/2/15
to scala-user

Hello,

Maybe the Scala experts here can help me. I'm trying to implement a Function1 in Java, such as:

Function1<Path, Object> fileFilter = new Function1<Path, Object>() {
    @Override
    public Object apply(Path v1) {
      return true;
    }

    @Override
    public <A> Function1<A, Object> compose(Function1<A, Path> g) {
      return Function1$class.compose(this, g);
    }

    @Override
    public <A> Function1<Path, A> andThen(Function1<Object, A> g) {
      return Function1$class.andThen(this, g);
    }
  };

But apparently my implementation of compose and andThen are wrong, and I couldn't understand how I should implement it. It complains that the anonymous function:

is not abstract and does not override abstract method <A>andThen$mcVJ$sp(scala.Function1<scala.runtime.BoxedUnit,A>) in scala.Function1


I've also tried to implement AbstractFunction1 so that I don't have to deal with compose and andThen but then I bumped into some issues about serialization, therefore I'm trying to do it via Function1)

Any ideas about how I can implement the compose and andThen methods in Java?

--
Emre

Sonnenschein

unread,
Feb 2, 2015, 10:21:33 AM2/2/15
to scala...@googlegroups.com
Did you try to extend scala.AbstractFunction1?

Sonnenschein

unread,
Feb 2, 2015, 10:25:07 AM2/2/15
to scala...@googlegroups.com
Resp. what is wrong with serialization? Are you sure you need it at all?

Emre Sevinc

unread,
Feb 2, 2015, 10:48:08 AM2/2/15
to Sonnenschein, scala-user
Hello,

Thanks for the answer. I managed to get it working by moving my implementation of scala.AbstractFunction1 to another .java file, making it implement Serializable and then calling it. (I think it was related to some mechanisms of Apache Spark). In other words, problem solved.

--
Emre


On Mon, Feb 2, 2015 at 4:25 PM, Sonnenschein <peter...@arcor.de> wrote:
Resp. what is wrong with serialization? Are you sure you need it at all?

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Emre Sevinc

François Valadier

unread,
Jun 18, 2015, 12:20:15 PM6/18/15
to scala...@googlegroups.com
Emre Sevinc <emre.sevinc@...> writes:

>
>
>
>
> Hello,
> Thanks for the answer. I managed to get it working by moving my
implementation of scala.AbstractFunction1 to another .java file, making it
implement Serializable and then calling it. (I think it was related to some
mechanisms of Apache Spark). In other words, problem solved.--
> Emre
>

Hello Emre,

Could you explain how you managed to do this because I'm also trying to
implement a serializable version of a scala.Function1 in Java in order to
distribute my Spark computation. I tried to pu the abstractFunction1
implementation in another java file which i made serializable but no change
in the results...
Thanks a lot !

François


Patanachai Tangchaisin

unread,
Jun 26, 2015, 8:54:49 AM6/26/15
to scala...@googlegroups.com, francois...@openvalue.fr
Hello,

Just come across this. I believe this is the same solution.

public abstract class SerializableFunction1<T1,R> extends AbstractFunction1<T1, R> implements Serializable {
}

Then use it. For example

new SerializableFunction1<Integer, Integer>(){
 
public Integer apply(Integer in){
   
return in + 1;
 
}
}

----
Patanachai
Reply all
Reply to author
Forward
0 new messages