CSC/ECE 517 Fall 2010/ch4 4g km: Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 3: | Line 3: | ||
---- | ---- | ||
=Introduction= | ==Introduction== | ||
In the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch4_4f previous article for which the link does not exist yet], we learn that [http://en.wikipedia.org/wiki/Command_pattern command patterns] in static and dynamic languages provide separation of objects that request actions from the objects that ultimately perform actions. This is dome by encapsulating the request for an action on a specific object. These command patterns are executed at runtime and simply hide the fact that another program is being called. Another technique of encapsulating behavior is for the program to have changes initiated at compile time. This type of design requires that the program not only can call another piece of code at runtime but it can build these calls at compile time via metaprogramming. Metaprogramming is the ability for a computer program to manipulate itself or other programs at the time of compilation as opposed to performing this manipulations at runtime. This tends to allow for greater flexibility for a program to handle new situations. In this article we take a closer look at metaprogramming in [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically typed languages]. | In the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch4_4f previous article for which the link does not exist yet], we learn that [http://en.wikipedia.org/wiki/Command_pattern command patterns] in static and dynamic languages provide separation of objects that request actions from the objects that ultimately perform actions. This is dome by encapsulating the request for an action on a specific object. These command patterns are executed at runtime and simply hide the fact that another program is being called. Another technique of encapsulating behavior is for the program to have changes initiated at compile time. This type of design requires that the program not only can call another piece of code at runtime but it can build these calls at compile time via metaprogramming. Metaprogramming is the ability for a computer program to manipulate itself or other programs at the time of compilation as opposed to performing this manipulations at runtime. This tends to allow for greater flexibility for a program to handle new situations. In this article we take a closer look at metaprogramming in [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically typed languages]. | ||
=Example of Metaprogramming= | ==Example of Metaprogramming== | ||
There is a classic example of use for metaprogramming that involves some form of writing a program that outputs some set of numbers in order but the trick is that it cannot be done in a loop. For this example we will ask for a program that outputs the numbers from 1 to 100 without using any looping. Ideally one would like to write the following: | There is a classic example of use for metaprogramming that involves some form of writing a program that outputs some set of numbers in order but the trick is that it cannot be done in a loop. For this example we will ask for a program that outputs the numbers from 1 to 100 without using any looping. Ideally one would like to write the following: | ||
1.upto 100 do |i| | 1.upto 100 do |i| | ||
Line 32: | Line 32: | ||
the elegance of this technique is that it is very easy to repeat or manipulate. | the elegance of this technique is that it is very easy to repeat or manipulate. | ||
=Dynamic Programming Languages= | ==Dynamic Programming Languages== | ||
=Conclusion= | ==Conclusion== | ||
=What’s Next?= | ==What’s Next?== | ||
In the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch4_4h next article for which the link does not exist yet], we will look at static-analysis tools for Ruby. | In the [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch4_4h next article for which the link does not exist yet], we will look at static-analysis tools for Ruby. | ||
=References= | ==References== | ||
#[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] | #[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] | ||
#[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic Programming Languages] | #[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic Programming Languages] | ||
#[http://en.wikipedia.org/wiki/Command_pattern Command Pattern] | #[http://en.wikipedia.org/wiki/Command_pattern Command Pattern] |
Revision as of 13:29, 17 October 2010
Metaprogramming in dynamically typed languages
Introduction
In the previous article for which the link does not exist yet, we learn that command patterns in static and dynamic languages provide separation of objects that request actions from the objects that ultimately perform actions. This is dome by encapsulating the request for an action on a specific object. These command patterns are executed at runtime and simply hide the fact that another program is being called. Another technique of encapsulating behavior is for the program to have changes initiated at compile time. This type of design requires that the program not only can call another piece of code at runtime but it can build these calls at compile time via metaprogramming. Metaprogramming is the ability for a computer program to manipulate itself or other programs at the time of compilation as opposed to performing this manipulations at runtime. This tends to allow for greater flexibility for a program to handle new situations. In this article we take a closer look at metaprogramming in dynamically typed languages.
Example of Metaprogramming
There is a classic example of use for metaprogramming that involves some form of writing a program that outputs some set of numbers in order but the trick is that it cannot be done in a loop. For this example we will ask for a program that outputs the numbers from 1 to 100 without using any looping. Ideally one would like to write the following:
1.upto 100 do |i| puts "#{i}" end
which outputs
1 2 ... 100
however since a loop is not allowed, you could use the loop to write code that will write the code:
1.upto 100 do |i| puts "puts #{i}" end
which outputs
puts 1 puts 2 ... puts 100
which then outputs
1 2 ... 100
the elegance of this technique is that it is very easy to repeat or manipulate.
Dynamic Programming Languages
Conclusion
What’s Next?
In the next article for which the link does not exist yet, we will look at static-analysis tools for Ruby.