Home > Community > Forums > Custom IC SKILL > Is there a way i can cretea a function on the fly?

Email

* Required Fields

Recipients email * (separate multiple addresses with commas)

Your name *

Your email *

Message *

Contact Us

* Required Fields
First Name *

Last Name *

Email *

Company / Institution *

Comments: *

 Is there a way i can cretea a function on the fly? 

Last post Thu, Dec 20 2012 6:39 PM by kb how. 2 replies.
Started by kb how 19 Dec 2012 06:37 PM. Topic has 2 replies and 566 views
Page 1 of 1 (3 items)
Sort Posts:
  • Wed, Dec 19 2012 6:37 PM

    • kb how
    • Top 100 Contributor
    • Joined on Thu, May 5 2011
    • Melaka, Melaka
    • Posts 79
    • Points 1,075
    Is there a way i can cretea a function on the fly? Reply

    Hi,

    May i know is there are way to create function at the fly. Meanings that create a control loop flow script to create unique function name.

    Example as below (But this doesn't works)

    foreach( ele list( list("funcA" funcB") list("funC" "funcD"))

        newFunctionName = stringToSymbol(strcat( car(ele) cadr(ele) ))

        (defun newFunctionName args

              (printf "Hello World - %L\n" args))

    )

     

    My intention is to create a function at the fly.Any idea? Is this possible? 

     

     

    -How-
    • Post Points: 20
  • Thu, Dec 20 2012 1:51 AM

    Re: Is there a way i can create a function on the fly? Reply

    foreach( ele list( list("funcA" "funcB") list("funC" "funcD"))
        newFunctionName = stringToSymbol(strcat( car(ele) cadr(ele) ))
        putd(newFunctionName lambda((args)
              printf("Hello World - %L\n" args)
              )
        )
    )

    funcAfuncB("test")

    Hello World - "test"

    However, I would say that creating functions on the fly is something that is even more powerful in SKILL++ mode, because that supports the concept of "closures". For example (either type toplevel('ils) and then type this in the CIW, or put in a file with a ".ils" suffix):

    defun(CCFmakeCounter (@key (init 0) (step 1))
      lambda(() init=init+step)
    )

    c1=CCFmakeCounter()
    c2=CCFmakeCounter(?init 4 ?step 3)

    c1() => 1
    c1() => 2
    c2() => 7
    c2() => 10
    c1() => 3

    This concept is covered in more detail in the SKILL documentation. The idea is that CCFmakeCounter() is returning a function object with the specific environment containing the arguments that were passed in (it's the same function object, but the environment changes in essence). In SKILL++ mode functions are variables, so you can much more easily pass around function objects without needing to use funcall or apply to call them.

    Regards,

    Andrew.

     

     

    • Post Points: 20
  • Thu, Dec 20 2012 6:39 PM

    • kb how
    • Top 100 Contributor
    • Joined on Thu, May 5 2011
    • Melaka, Melaka
    • Posts 79
    • Points 1,075
    Re: Is there a way i can create a function on the fly? Reply

    Thanks Adrew for the solution and great sharing. I should start learning SKILL++ now.

    -How-
    • Post Points: 5
Page 1 of 1 (3 items)
Sort Posts:
Started by kb how at 19 Dec 2012 06:37 PM. Topic has 2 replies.