Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6546709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:44:28+00:00 2026-05-25T11:44:28+00:00

Can someone explain what a functor is and provide a simple example?

  • 0

Can someone explain what a functor is and provide a simple example?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:44:29+00:00Added an answer on May 25, 2026 at 11:44 am

    A function object is just that. Something which is both an object and a function.

    Aside: calling a function object a “functor” is a serious abuse of the term: a different kind of “functors” are a central concept in mathematics, and one that has a direct role in computer science (see “Haskell Functors”). The term is also used in a slightly different way in ML, so unless you are implementing one of these concepts in Java (which you can!) please stop using this terminology. It makes simple things complicated.

    Back to the answer:
    Java does not have “first class functions” that is to say, you can not pass a function as an argument to a function. This true at multiple levels, syntactically, in the byte code representation, and in that the type system lacks the “function constructor”

    In other words, you can’t write something like this:

     public static void tentimes(Function f){
        for(int i = 0; i < 10; i++)
           f();
     }
     ...
     public static void main{
        ...
        tentimes(System.out.println("hello"));
        ...
     }
    

    This is really annoying, since we want to be able to do things like have Graphical User Interface libraries where you can associate a “callback” function with clicking on a button.

    So what do we do?

    Well, the general solution (discussed by the other posters) is to define an interface with a single method that we can call. For example, Java uses an interface called Runnable for these kinds of things all the time, it looks like:

    public interface Runnable{
        public void run();
    }
    

    now, we can rewrite my example from above:

    public static void tentimes(Runnable r){
        for(int i = 0; i < 10; i++)
           r.run();
    }
    ...
    public class PrintHello implements Runnable{
        public void run{
           System.out.println("hello")
        }
    }
    ---
    public static void main{
        ...
        tentimes(new PrintHello());
        ...
     }
    

    Obviously, this example is contrived. We could make this code a little bit nicer using anonymous inner classes, but this gets the general idea.

    Here is where this breaks down: Runnable is only usable for functions that don’t take any arguments, and don’t return anything useful, so you end up defining a new interface for each job. For example, the interface Comparator in Mohammad Faisal’s answer. Providing a more general approach, and one that takes syntax, is a major goal for Java 8 (The next version of Java), and was heavily pushed to be included in Java 7. This is called a “lambda” after the function abstraction mechanism in the Lambda Calculus. Lambda Calculus is both (perhaps) the oldest programming language, and the theoretical basis of much of Computer Science. When Alonzo Church (one of the main founders of computer science) invented it, he used the Greek letter lambda for functions, hence the name.

    Other languages, including the functional language (Lisp, ML, Haskell, Erlang, etc), most of the major dynamic languages (Python, Ruby, JavaScript, etc) and the other application languages (C#, Scala, Go, D, etc) support some form of “Lambda Literal.” Even C++ has them now (since C++11), although in that case they are somewhat more complicated because C++ lacks automatic memory management, and won’t save your stack frame for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain why the HashMap acts like it does in this example: Simple
Can someone explain ProxyFactoryBean in simple terms? I see this being quoted lot of
Can someone explain to me something about related fields. For example - How it
Can someone explain what is happening in the example below: glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glRotatef(45,0,0,1);
Can please someone explain the differences between Functor and Monad in the Scala context?
Can someone explain the steps of the VF2 algorithm for graph isomorphism in simple
Can someone explain the mechanics of a jump table and why is would be
Can someone explain to me the advantages of using an IOC container over simply
Can someone explain this result to me. The first test succeeds but the second
Can someone explain what exactly the string 0 but true means in Perl? As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.