Functors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sushobh
    New Member
    • Sep 2006
    • 2

    Functors

    Hello All,
    Just want to put up a query with regards to functors.

    I read that Functor means a function with state. But what I am not able to understand is the following

    , What is the sate
    where is the state stored ,
    and who is responsible to maintain that state the application or the compiler inherently manages that state.

    Is the state accessible to the application , if it is maintained by the compiler.

    If possible can somebody give me an example of funtor highlighting the state thing

    Regds
    Sushobh
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    A Functor is a mathematical term. It has no meaning in computing. This doesn't mean you can't model one with a program it just means all your questions about how a compiler handles a functor are irrelevent. It doesn't because it doesn't know what they are.

    If you want to modle a functor then your application will have to handle maintaining the state.

    Comment

    • sushobh
      New Member
      • Sep 2006
      • 2

      #3
      Thanks for that,
      But what I also want to know , is can I have a example of the state of the functor, I am not very clear on that. Is it possible to provide an example. Maybe it is very trivial thing for you, but I would be grateful if I can have example , explaining that.

      Regds
      sushobh

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        simple eaxmple of a functor

        Code:
        int functor(int input)
        {
            static int functor_state = 50;
        
            if (input < 0)
            {
                functor_state = 100 - functor_state;
            }
            else
            {
                functor_state = (functor_state + input) % 101;
            }
        
            return input % functor_state;
        }
        A function with state, because I have declared functor_state static it is not stored on the stack (if present) but in program memory (a data segment) and it's value is persistent even while the flow of control of the program is outside the function.

        Because I have declared functor_state inside the function it is not accessable by any other part of the code.

        Comment

        Working...