function "attributes"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Slaughter

    function "attributes"

    is there any way to simply add an attribute like feature to a
    function/method definition?

    say I create a function like

    function Test()
    {

    //....
    }

    but I want to assign a probability to it in the definition itself... maybe
    something like

    function Test()
    {
    static $probability = 0.234321;
    //....
    }

    Now is there any way that I can get the probability from the function? I'm
    using function pointers to call the functions in a random way. Essentially
    having a list of functions that have assigned probabilities and calling them
    based on that probability(I don't want to really seperate the probability
    from the definition.


    Ultimately it would be nice to extend the syntax to handle a new keyword
    like

    function Test():[Probability=0.1 23421]
    {

    }

    or something like that but I know thats not going to happen.

    Thanks,
    Jon


  • Edward Z. Yang

    #2
    Re: function "attribute s"

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Jon Slaughter wrote:
    is there any way to simply add an attribute like feature to a
    function/method definition? [snip]
    You want the "Strategy" pattern, making the function into an object:

    class TestFunction
    {
    var $probability = 0.234321;
    function call() { /* ... */ }
    }

    $func = new TestFunction();
    $func->call();

    PHP does not treat anonymous functions as first-class objects, unlike
    JavaScript, so some acrobatics are necessary to get this working.

    - --
    Edward Z. Yang GnuPG: 0x869C48DA
    HTML Purifier <htmlpurifier.o rg Anti-XSS HTML Filter
    [[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.6 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFGZ2e0qTO +fYacSNoRAtagAJ 9IV0oTUmLhpSlWS A1LBsBY7TYjCwCf ft6H
    noiOwWISbfNGx8R Gy/+EfBM=
    =er7e
    -----END PGP SIGNATURE-----

    Comment

    • Jon Slaughter

      #3
      Re: function &quot;attribute s&quot;


      "Edward Z. Yang" <edwardzyang@th ewritingpot.com wrote in message
      news:466767B5.3 090901@thewriti ngpot.com...
      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1
      >
      Jon Slaughter wrote:
      >is there any way to simply add an attribute like feature to a
      >function/method definition? [snip]
      >
      You want the "Strategy" pattern, making the function into an object:
      >
      class TestFunction
      {
      var $probability = 0.234321;
      function call() { /* ... */ }
      }
      >
      $func = new TestFunction();
      $func->call();
      >
      PHP does not treat anonymous functions as first-class objects, unlike
      JavaScript, so some acrobatics are necessary to get this working.
      This is more work than I'm looking for unless I can someone "wrap" a
      function into a functor.


      Comment

      • Toby A Inkster

        #4
        Re: function &quot;attribute s&quot;

        Edward Z. Yang wrote:
        class TestFunction
        {
        var $probability = 0.234321;
        function call() { /* ... */ }
        }
        >
        $func = new TestFunction();
        $func->call();
        The other possibility is to use a static variable and function. The
        example below is PHP5 code -- I have no idea if it's possible to translate
        it to PHP4.

        class Test
        {
        public static $probability = 0.234321;

        public static function call()
        {
        $x = self::$probabil ity * 1000000;
        $r = rand(0, 1000000);
        return ($r<$x);
        }
        }

        // Using the function
        $truth = Test::call();

        // Retrieving the "attribute"
        $prob = Test::$probabil ity;

        --
        Toby A Inkster BSc (Hons) ARCS
        [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
        [OS: Linux 2.6.12-12mdksmp, up 103 days, 14:54.]

        URLs in demiblog

        Comment

        • Erwin Moller

          #5
          Re: function &quot;attribute s&quot;

          Jon Slaughter wrote:
          is there any way to simply add an attribute like feature to a
          function/method definition?
          >
          say I create a function like
          >
          function Test()
          {
          >
          //....
          }
          >
          but I want to assign a probability to it in the definition itself... maybe
          something like
          >
          function Test()
          {
          static $probability = 0.234321;
          //....
          }
          >
          Now is there any way that I can get the probability from the function? I'm
          using function pointers to call the functions in a random way. Essentially
          having a list of functions that have assigned probabilities and calling
          them based on that probability(I don't want to really seperate the
          probability from the definition.
          >
          >
          Ultimately it would be nice to extend the syntax to handle a new keyword
          like
          >
          function Test():[Probability=0.1 23421]
          {
          >
          }
          >
          or something like that but I know thats not going to happen.
          >
          Thanks,
          Jon
          Hi Jon,

          A simple idea but maybe it works in your situation:
          1) Make your function a class.
          2) Instantiate one for each function you need. (Function should now be named
          method in OOP)
          3) Add a simple method that stores name/value pairs you like to add to that
          instance, simply storing them in an instancevariabl e hashed array.

          class TestFunction{
          var $addedValues;
          function addValues($name ,$value) {
          $addedValues[$name]=$value;
          }

          function getValues(){
          return $addedValues;
          }
          // rest goes here
          }

          $test1 = new TestFunction();
          $test1->addValues("pro bability",0.234 321);


          Is such an approach of any use in your situation?

          Regards,
          Erwin Moller

          Comment

          • Erwin Moller

            #6
            Re: function &quot;attribute s&quot;

            Erwin Moller wrote:
            Jon Slaughter wrote:
            >
            >is there any way to simply add an attribute like feature to a
            >function/method definition?
            >>
            >say I create a function like
            >>
            >function Test()
            >{
            >>
            > //....
            >}
            >>
            >but I want to assign a probability to it in the definition itself...
            >maybe something like
            >>
            >function Test()
            >{
            > static $probability = 0.234321;
            > //....
            >}
            >>
            >Now is there any way that I can get the probability from the function?
            >I'm using function pointers to call the functions in a random way.
            >Essentially having a list of functions that have assigned probabilities
            >and calling them based on that probability(I don't want to really
            >seperate the probability from the definition.
            >>
            >>
            >Ultimately it would be nice to extend the syntax to handle a new keyword
            >like
            >>
            >function Test():[Probability=0.1 23421]
            >{
            >>
            >}
            >>
            >or something like that but I know thats not going to happen.
            >>
            >Thanks,
            >Jon
            >
            Hi Jon,
            >
            A simple idea but maybe it works in your situation:
            1) Make your function a class.
            2) Instantiate one for each function you need. (Function should now be
            named method in OOP)
            3) Add a simple method that stores name/value pairs you like to add to
            that instance, simply storing them in an instancevariabl e hashed array.
            >
            class TestFunction{
            var $addedValues;
            var $addedValues = array();

            is clearer. :-)


            function addValues($name ,$value) {
            $addedValues[$name]=$value;
            }
            >
            function getValues(){
            return $addedValues;
            }
            // rest goes here
            }
            >
            $test1 = new TestFunction();
            $test1->addValues("pro bability",0.234 321);
            >
            >
            Is such an approach of any use in your situation?
            >
            Regards,
            Erwin Moller

            Comment

            • Jon Slaughter

              #7
              Re: function &quot;attribute s&quot;


              "Erwin Moller"
              <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c omwrote in
              message news:4667c82f$0 $336$e4fe514c@n ews.xs4all.nl.. .
              Jon Slaughter wrote:
              >
              >is there any way to simply add an attribute like feature to a
              >function/method definition?
              >>
              >say I create a function like
              >>
              >function Test()
              >{
              >>
              > //....
              >}
              >>
              >but I want to assign a probability to it in the definition itself...
              >maybe
              >something like
              >>
              >function Test()
              >{
              > static $probability = 0.234321;
              > //....
              >}
              >>
              >Now is there any way that I can get the probability from the function?
              >I'm
              >using function pointers to call the functions in a random way.
              >Essentially
              >having a list of functions that have assigned probabilities and calling
              >them based on that probability(I don't want to really seperate the
              >probability from the definition.
              >>
              >>
              >Ultimately it would be nice to extend the syntax to handle a new keyword
              >like
              >>
              >function Test():[Probability=0.1 23421]
              >{
              >>
              >}
              >>
              >or something like that but I know thats not going to happen.
              >>
              >Thanks,
              >Jon
              >
              Hi Jon,
              >
              A simple idea but maybe it works in your situation:
              1) Make your function a class.
              2) Instantiate one for each function you need. (Function should now be
              named
              method in OOP)
              3) Add a simple method that stores name/value pairs you like to add to
              that
              instance, simply storing them in an instancevariabl e hashed array.
              >
              class TestFunction{
              var $addedValues;
              function addValues($name ,$value) {
              $addedValues[$name]=$value;
              }
              >
              function getValues(){
              return $addedValues;
              }
              // rest goes here
              }
              >
              $test1 = new TestFunction();
              $test1->addValues("pro bability",0.234 321);
              >
              >
              Is such an approach of any use in your situation?
              >
              >
              Its not that I can't use it but I feel that its to much "work". It defeats
              the purpose ot trying to make it easier. Essentially I would have to wrap
              all the functions into functors for just one attribute. Essentially what I
              need is a way to add meta data. I think Toby's method might work.

              Thanks,
              Jon


              Comment

              • Jon Slaughter

                #8
                Re: function &quot;attribute s&quot;


                "Toby A Inkster" <usenet200705@t obyinkster.co.u kwrote in message
                news:uunkj4-h76.ln1@ophelia .g5n.co.uk...
                Edward Z. Yang wrote:
                >
                >class TestFunction
                >{
                > var $probability = 0.234321;
                > function call() { /* ... */ }
                >}
                >>
                >$func = new TestFunction();
                >$func->call();
                >
                The other possibility is to use a static variable and function. The
                example below is PHP5 code -- I have no idea if it's possible to translate
                it to PHP4.
                >
                class Test
                {
                public static $probability = 0.234321;
                >
                public static function call()
                {
                $x = self::$probabil ity * 1000000;
                $r = rand(0, 1000000);
                return ($r<$x);
                }
                }
                >
                // Using the function
                $truth = Test::call();
                >
                // Retrieving the "attribute"
                $prob = Test::$probabil ity;
                >

                So I have to call the function first? ;/ Isn't going to work ;/ I have to
                get the probabilities from the function to determine which function to call
                ;/
                Ultimately it seems I'm going to have to use functors or just write a method
                to add the functions that takes a pointer but just seems a little overkill
                just to add one "attribute" .

                thanks,
                Jon



                Comment

                • Aerik

                  #9
                  Re: function &quot;attribute s&quot;

                  Jon,

                  What are you trying to accomplish? Do you have a list of functions,
                  and you want to call each one if the probability of it being called is
                  above a certain threshold? If so, a very simple approach might be to
                  assign the probabilities and functions to an array.

                  See if this is what you're trying to do, or perhaps clarify it to me/
                  us:

                  $funcary[] = array('probabil ity'=>.75, 'funcname'=>'fu nction1');
                  $funcary[] = array('probabil ity'=>.47, 'funcname'=>'fu nction2');
                  $funcary[] = array('probabil ity'=>.99, 'funcname'=>'fu nction3');

                  $curprob = rand(0,100)/100;

                  foreach($funcar y as $afunc=>$ary){
                  if ($afunc['probability'] < $curprob) $afunc['funcname']();
                  }

                  Where 'function1' etc. are the names of your functions. I didn't test
                  this at all, fyi, but I think it should work.

                  Aerik

                  Comment

                  • Jon Slaughter

                    #10
                    Re: function &quot;attribute s&quot;


                    "Aerik" <asylvan@gmail. comwrote in message
                    news:1181233787 .529402.108340@ i38g2000prf.goo glegroups.com.. .
                    Jon,
                    >
                    What are you trying to accomplish? Do you have a list of functions,
                    and you want to call each one if the probability of it being called is
                    above a certain threshold? If so, a very simple approach might be to
                    assign the probabilities and functions to an array.
                    >
                    See if this is what you're trying to do, or perhaps clarify it to me/
                    us:
                    >
                    $funcary[] = array('probabil ity'=>.75, 'funcname'=>'fu nction1');
                    $funcary[] = array('probabil ity'=>.47, 'funcname'=>'fu nction2');
                    $funcary[] = array('probabil ity'=>.99, 'funcname'=>'fu nction3');
                    >
                    $curprob = rand(0,100)/100;
                    >
                    foreach($funcar y as $afunc=>$ary){
                    if ($afunc['probability'] < $curprob) $afunc['funcname']();
                    }
                    >
                    Where 'function1' etc. are the names of your functions. I didn't test
                    this at all, fyi, but I think it should work.
                    >
                    Aerik
                    >
                    No, that is my application but what I'm trying to do is in some way assign
                    the probability to the function that is inline with the function definition.
                    If, say, I have to create 100 of these functions then then I want to
                    minimize the amount of excess code just to add the probability part.

                    I can automatically include the functions because they are in a class and I
                    use get_class_metho ds to get them... but this just gives me the functions
                    and in not the probabilities.

                    For your method you have to know the function name and you have to add them
                    to the array manually. With my method I do it automatically but I cannot
                    easily handle the probabilities. (right now I just use 1 as default so I can
                    get on with the other parts.

                    If there was some other function such as get_function_at tributes for
                    example, or get_function_va rs which returns the variables used in a
                    function(or static vars) then I could extract the probabilities
                    automatically. Of course there isn't anything like this.

                    I suppose I could open up the file as a text file and parse it myself but
                    this is more work that I'd rather not do unless someone as an easy way and
                    it also seems kinda inefficient.

                    Jon


                    Comment

                    • Toby A Inkster

                      #11
                      Re: function &quot;attribute s&quot;

                      Jon Slaughter wrote:
                      So I have to call the function first?
                      No. Equally, this would work:

                      if (Test::$probabi lity < 0.5)
                      {
                      $truth = Test::call();
                      }

                      --
                      Toby A Inkster BSc (Hons) ARCS
                      [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
                      [OS: Linux 2.6.12-12mdksmp, up 104 days, 1:37.]

                      URLs in demiblog

                      Comment

                      • Edward Z. Yang

                        #12
                        Re: function &quot;attribute s&quot;

                        -----BEGIN PGP SIGNED MESSAGE-----
                        Hash: SHA1

                        Jon Slaughter wrote:
                        This is more work than I'm looking for unless I can someone "wrap" a
                        function into a functor.
                        It is, however, the cleanest way of implementing it in PHP. Otherwise,
                        you'll have to use static data or globals.

                        - --
                        Edward Z. Yang GnuPG: 0x869C48DA
                        HTML Purifier <htmlpurifier.o rg Anti-XSS HTML Filter
                        [[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
                        -----BEGIN PGP SIGNATURE-----
                        Version: GnuPG v1.4.6 (MingW32)
                        Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

                        iD8DBQFGaJuGqTO +fYacSNoRAvubAJ sG9R1H20WKtxleC 7BsITCaeHU2NwCa A3DW
                        QNiJdnPN5t070dS eS0DDzvk=
                        =h6ds
                        -----END PGP SIGNATURE-----

                        Comment

                        • Norman Peelman

                          #13
                          Re: function &quot;attribute s&quot;

                          Jon Slaughter wrote:
                          is there any way to simply add an attribute like feature to a
                          function/method definition?
                          >
                          say I create a function like
                          >
                          function Test()
                          {
                          >
                          //....
                          }
                          >
                          but I want to assign a probability to it in the definition itself... maybe
                          something like
                          >
                          function Test()
                          {
                          static $probability = 0.234321;
                          //....
                          }
                          >
                          Now is there any way that I can get the probability from the function? I'm
                          using function pointers to call the functions in a random way. Essentially
                          having a list of functions that have assigned probabilities and calling them
                          based on that probability(I don't want to really seperate the probability
                          from the definition.
                          >
                          >
                          Ultimately it would be nice to extend the syntax to handle a new keyword
                          like
                          >
                          function Test():[Probability=0.1 23421]
                          {
                          >
                          }
                          >
                          or something like that but I know thats not going to happen.
                          >
                          Thanks,
                          Jon
                          >
                          >
                          Ok, this is ugly but if you name your functions by their probability:

                          function _0_234321()
                          { // test function
                          $probability = 0.234321;
                          echo "$probability<b r>";
                          ...
                          }
                          $probability = 0.234321;
                          $func_to_call = '_'.str_replace ('.','_',$proba bility);
                          //echo $func_to_call.' <br>';
                          call_user_func( $func_to_call);

                          .... or am I missing the point?

                          Norm

                          Comment

                          Working...