Loading ...
Sorry, an error occurred while loading the content.

Intro/Examples to functional objects?

Expand Messages
  • Oliver Bandel
    Hello, I sometimes heard/read about functional objects and that OCaml can handle these. But most often OO-people use OO-features and FPL-people use
    Message 1 of 7 , Jan 27, 2005
    • 0 Attachment
      Hello,

      I sometimes heard/read about functional objects and that
      OCaml can handle these.

      But most often OO-people use OO-features and FPL-people use
      FPL-features of OCaml, and so are the questions most often
      only part of these two areas.

      But how are functional objects used, and what are the
      advantages (and disadvantages) of them?

      I'm looking for some simple but impressive examples
      on functional objects.

      Someone her who can explain me this stuff?

      TIA,
      oliver
    • Stalkern 2
      ... The most impressive example of that, that I know about and use, is when you set a class value to be functional, and you feed it on-the-fly thus changing
      Message 2 of 7 , Jan 28, 2005
      • 0 Attachment
        Oliver Bandel wrote:
        > Hello,
        >
        > I sometimes heard/read about functional objects and that
        > OCaml can handle these.
        >
        > But most often OO-people use OO-features and FPL-people use
        > FPL-features of OCaml, and so are the questions most often
        > only part of these two areas.
        >
        > But how are functional objects used, and what are the
        > advantages (and disadvantages) of them?
        >
        > I'm looking for some simple but impressive examples
        > on functional objects.

        The most impressive example of that, that I know about and use, is when
        you set a class value to be functional, and you feed it on-the-fly thus
        changing the responses of your object to some messages.
        I use this when writing GUIs: this results somehow in setting methods
        on-the-fly, and I think that this is really a powerful feature. So for
        instance I can get prepared to tune a function according to the result
        obtained... isn't it a joy?

        Ernesto
      • Richard Jones
        ... Can you give a bit more detail? Code example would be nice. Rich. -- Richard Jones, CTO Merjis Ltd. Merjis - web marketing and technology -
        Message 3 of 7 , Jan 28, 2005
        • 0 Attachment
          On Fri, Jan 28, 2005 at 10:50:59AM +0100, Stalkern 2 wrote:
          >
          > Oliver Bandel wrote:
          > > Hello,
          > >
          > > I sometimes heard/read about functional objects and that
          > > OCaml can handle these.
          > >
          > > But most often OO-people use OO-features and FPL-people use
          > > FPL-features of OCaml, and so are the questions most often
          > > only part of these two areas.
          > >
          > > But how are functional objects used, and what are the
          > > advantages (and disadvantages) of them?
          > >
          > > I'm looking for some simple but impressive examples
          > > on functional objects.
          >
          > The most impressive example of that, that I know about and use, is when
          > you set a class value to be functional, and you feed it on-the-fly thus
          > changing the responses of your object to some messages.
          > I use this when writing GUIs: this results somehow in setting methods
          > on-the-fly, and I think that this is really a powerful feature. So for
          > instance I can get prepared to tune a function according to the result
          > obtained... isn't it a joy?

          Can you give a bit more detail? Code example would be nice.

          Rich.

          --
          Richard Jones, CTO Merjis Ltd.
          Merjis - web marketing and technology - http://merjis.com
          Team Notepad - intranets and extranets for business - http://team-notepad.com
        • Stalkern 2
          ... OK. A really basic example of what I meant would be: (* ---CUT-HERE -------------------------*) class integerAdaptationClass () = object (self) val mutable
          Message 4 of 7 , Jan 28, 2005
          • 0 Attachment
            Richard Jones wrote:
            > On Fri, Jan 28, 2005 at 10:50:59AM +0100, Stalkern 2 wrote:
            > >
            > > Oliver Bandel wrote:
            > > > Hello,
            > > >
            > > > I sometimes heard/read about functional objects and that
            > > > OCaml can handle these.
            > > >
            > > > But most often OO-people use OO-features and FPL-people use
            > > > FPL-features of OCaml, and so are the questions most often
            > > > only part of these two areas.
            > > >
            > > > But how are functional objects used, and what are the
            > > > advantages (and disadvantages) of them?
            > > >
            > > > I'm looking for some simple but impressive examples
            > > > on functional objects.
            > >
            > > The most impressive example of that, that I know about and use, is when
            > > you set a class value to be functional, and you feed it on-the-fly thus
            > > changing the responses of your object to some messages.
            > > I use this when writing GUIs: this results somehow in setting methods
            > > on-the-fly, and I think that this is really a powerful feature. So for
            > > instance I can get prepared to tune a function according to the result
            > > obtained... isn't it a joy?
            >
            > Can you give a bit more detail? Code example would be nice.
            >
            > Rich.

            OK. A really basic example of what I meant would be:

            (* ---CUT-HERE -------------------------*)

            class integerAdaptationClass () =

            object (self)

            val mutable funoftheday = (fun (i:int) -> ())

            method set_fun f =
            funoftheday <- f
            method get_fun =
            funoftheday
            end
            ;;

            let aBaseObj = new integerAdaptationClass ();;

            (* At the beginning, you only need to print an integer *)
            let () = aBaseObj#set_fun (fun i -> (print_endline (string_of_int i);
            flush stdout ));;

            (*and so it happens*)

            let () = (aBaseObj#get_fun) 23;;

            (*But then your birthday comes and you want some romance *)

            let birthdayFortunesKeyValList =
            [
            (0," Happy Birthday!");
            (1," o-oh I feel SOOO functional today!");
            (2," Daddy... I bring you at heart!")
            ]
            ;;

            let () =
            aBaseObj#set_fun
            (fun i ->
            (
            print_endline
            (
            (List.assoc
            (i mod (List.length birthdayFortunesKeyValList))
            birthdayFortunesKeyValList
            )^
            "("^
            (string_of_int i)^
            ")"
            );
            flush stdout
            )
            )
            ;;

            (*You don't need to keep the fortunes list any longer; you could have
            defined it just inside the function set.*)
            let birthdayFortunesKeyValList = [];;

            (*Since then, you'd be flooded with those messy integers ;-) *)

            let () = (aBaseObj#get_fun) 37;;

            let () = (aBaseObj#get_fun) 27;;

            let () = (aBaseObj#get_fun) 53;;

            (*This without need to redefine the class to make place for the fortunes.*)


            (* ---CUT-HERE -------------------------*)

            This is rather a misuse of OOP, as it just uses objects like records,
            but provides a interesting way to update the behaviour of an object with
            elements unknown at the creation time, at a very low cost even when the
            architectures get complex.

            Ernesto
          • Micha
            ... I thought a functional object is an object in which some construct like { } returns a copy of self, with maybe some changed instance variables. This
            Message 5 of 7 , Jan 28, 2005
            • 0 Attachment
              Am Freitag, 28. Januar 2005 18:51 schrieb Stalkern 2:
              > class integerAdaptationClass () =
              >
              > object (self)
              >
              > val mutable funoftheday = (fun (i:int) -> ())
              >
              > method set_fun f =
              > funoftheday <- f
              > method get_fun =
              > funoftheday
              > end
              > ;;
              >

              I thought a functional object is an object in which some construct like
              {< ... >} returns a copy of self, with maybe some changed instance variables.
              This seems to be a rather normal object with a mutable function-pointer
              inside. Maybe I've overseen something?

              Michael
            • Tony Edgin
              ... A good resource on this subject is the book A Functional Pattern System for Object-Oriented Design by Thomas Kuhne. You can download it for free from
              Message 6 of 7 , Jan 28, 2005
              • 0 Attachment
                On Fri, 28 Jan 2005 05:35, Oliver Bandel wrote:
                > Hello,
                >
                > I sometimes heard/read about functional objects and that
                > OCaml can handle these.
                >
                > But most often OO-people use OO-features and FPL-people use
                > FPL-features of OCaml, and so are the questions most often
                > only part of these two areas.
                >
                > But how are functional objects used, and what are the
                > advantages (and disadvantages) of them?
                >
                > I'm looking for some simple but impressive examples
                > on functional objects.
                >
                > Someone her who can explain me this stuff?

                A good resource on this subject is the book "A Functional Pattern System for
                Object-Oriented Design" by Thomas Kuhne. You can download it for free from
                his website
                http://www.mm.informatik.tu-darmstadt.de/~kuehne/tfps/fps-sans-escher.pdf.

                cheers.

                Tony
              • Stalkern 2
                ... Oh no, don t worry! Functional objects should be those of a class providing some methods being a partial application of the method of a superclass
                Message 7 of 7 , Jan 28, 2005
                • 0 Attachment
                  Micha wrote:
                  > Am Freitag, 28. Januar 2005 18:51 schrieb Stalkern 2:
                  > > class integerAdaptationClass () =
                  > >
                  > > object (self)
                  > >
                  > > val mutable funoftheday = (fun (i:int) -> ())
                  > >
                  > > method set_fun f =
                  > > funoftheday <- f
                  > > method get_fun =
                  > > funoftheday
                  > > end
                  > > ;;
                  > >
                  >
                  > I thought a functional object is an object in which some construct like
                  > {< ... >} returns a copy of self, with maybe some changed instance variables.
                  > This seems to be a rather normal object with a mutable function-pointer
                  > inside. Maybe I've overseen something?
                  >

                  Oh no, don't worry!
                  Functional objects should be those of a class providing some methods
                  being a partial application of the method of a superclass inherited -
                  but since I've never used them, I could not give an example.
                  So, I was rather taking the opportunity to highlight a little
                  possibility provided by functional style in objects, because it happened
                  to me to find this a very powerful issue in everyday work - e.g. when
                  you have a complex GUI or some cross dependencies make your Makefile
                  hard to set up, or the requirements for your job change suddenly and you
                  need a quick fix... things that happen when working.

                  Ernesto
                Your message has been successfully submitted and would be delivered to recipients shortly.