CSC/ECE 517 Fall 2007/wiki1 3 b6: Difference between revisions
No edit summary |
No edit summary |
||
(16 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Example use of Currying in Ruby by Ben Parees | == Example use of Currying in Ruby by Ben Parees == | ||
This is an original work, the sample code provided was created entirely by the author of this page. References are provided to offer additional background on the concept of currying. | |||
The purpose of this class is to allow programmers to easily reconfigure their program to provide varying degrees of debugging output without changing code. They can simply change the log level that is configured, and automatically filter out messages which are below that log level. In addition, messages are automatically tagged with a string indicating their severity, which makes analyzing logs much easier. | |||
== Definition of Function Currying == | |||
Currying refers to a programming technique in which the number of parameters to a function are reduced by creating a new function based on the original, but fixing some of the original arguments. That is, if we have a function ''f(x,y,z)'', we can define a new function ''g(x)'' which is defined as ''f(x,1,2)''. | |||
Now ''f(x,1,2)==g(x)'', allowing us to invoke the simpler g(x) rather than ''f(x,1,2)''. To curry a function means to create the simplified function with fixed arguments, from a function with more arguments. | |||
== Benefits of Performing Function Currying == | |||
Currying has several benefits. | |||
<ol> | |||
<li>It simplifies writing code by reducing the number of arguments that must be typed each time</li> | |||
<li>It reduces the risk of error because there are fewer arguments for which values could be incorrect</li> | |||
<li>It can enhance code readability by defining multiple functions with descriptive names rather than a single function with obscure arguments</li> | |||
</ol> | |||
== Currying in Ruby == | |||
Ruby provides for function currying using the lambda operator. The lambda operator allows the programmer to define a new function. To curry'' g(x)'' from ''f(x,1,2)'' in Ruby would be written as: ''g=lambda {|x| f(x,1,2)}'' | |||
This would assign variable g to be a new function which invokes f() with the final two arguments fixed as 1 and 2 respectively. | |||
=== Example Program in Ruby === | |||
The following pair of programs demonstrates the value of currying functions in Ruby. The program itself implements a simple account balance management tool which allows for withdrawals and deposits as specified by the user. | |||
In the first program, a Logger class is defined with a single function, "logMessage" which takes an integer log level parameter, and the message to be logged. Depending on the log level specified, and the overall logging level set in the Logger class, the message may or may not be displayed to the user. If the message is to be displayed, it will be prefixed with a string indicating the message's relative importance. | |||
For example, logMessage(0,"starting withdrawal") will print a debugging message as: | |||
<pre>DEBUG: starting withdrawal</pre> | |||
In addition, this message will only be displayed if the logLevel is set to 0 or less. The more important the message, the higher the log level that should be used, to ensure it is displayed to the user. Since debug messages are often unimportant, setting the logLevel to 1 would prevent all debug messages from being shown to the user while still showing the user normal output and warning/error messages. When debugging the program, the programmer can set the logLevel to 0 to enable the output of these debug messages. | |||
The purpose of this Logger class is to allow programmers to easily reconfigure their program to provide varying degrees of debugging output without changing code. They can simply change the log level that is configured, and automatically filter out messages which are below that log level. In addition, messages are automatically tagged with a string indicating their severity, which makes analyzing logs much easier. Unfortunately, the logMessage method is fairly cumbersome due to the requirement that the programmer specify the correct log level at each invocation. | |||
==== Sample Program without Currying ==== | |||
Here is the initial version of the program: | Here is the initial version of the program: | ||
<pre> | |||
class Logger | class Logger | ||
@logLevel | @logLevel | ||
Line 13: | Line 52: | ||
@logLevel=n | @logLevel=n | ||
end | end | ||
def logMessage(n,msg) | def logMessage(n,msg) | ||
if(n>=@logLevel) | if(n>=@logLevel) | ||
Line 20: | Line 58: | ||
when 1 then '' | when 1 then '' | ||
when 2 then 'WARNING: ' | when 2 then 'WARNING: ' | ||
when 3 then 'ERROR: ' | when 3 then 'ERROR: ' | ||
end | end | ||
puts prepend+msg | puts prepend+msg | ||
Line 37: | Line 74: | ||
logger.logMessage(0,"initialized account with balance of #{n}"); | logger.logMessage(0,"initialized account with balance of #{n}"); | ||
end | end | ||
def getAmount | def getAmount | ||
@logger.logMessage(0,"enter getAmount method") | @logger.logMessage(0,"enter getAmount method") | ||
Line 69: | Line 104: | ||
@logger.logMessage(0,"new balance is #{balance}") | @logger.logMessage(0,"new balance is #{balance}") | ||
end | end | ||
end | end | ||
action="d" | action="d" | ||
logger=Logger.new(0) | logger=Logger.new(0) | ||
account=Account.new(100,logger) | account=Account.new(100,logger) | ||
while action!="q" | while action!="q" | ||
Line 90: | Line 122: | ||
end | end | ||
end | end | ||
</pre> | |||
==== Limitations of the Initial Program ==== | |||
In the initial form of the program, the programmer must specify the log level parameter for each trace statement they wish to create. This approach has two drawbacks: | In the initial form of the program, the programmer must specify the log level parameter for each trace statement they wish to create. This approach has two drawbacks: | ||
<br> | |||
<ol> | |||
<li>The programmer must know what log level corresponds to what level of trace (0=debug, 1=info, 2=warning, 3=error, etc). If they make a mistake in their numbering, the trace will not be correct.</li> | |||
<li>The code is hard to read since it is not obvious just by looking at a piece of code, whether a message is going to be printed as a debug statement, informational statement, warning, etc.</li> | |||
</ol> | |||
==== Enhancing the program via Currying ==== | |||
Now the same program, but with 4 new methods defined by currying the original logMessage method. The new methods are: | Now the same program, but with 4 new methods defined by currying the original logMessage method. The new methods are: | ||
<ol> | |||
<li> debug - print a debug level message by currying with logMessage(0,msg) </li> | |||
<li> print - print an informational message by currying with logMessage(1,msg) </li> | |||
<li> warn - print a warning level message by currying with logMessage(2,msg)</li> | |||
<li> error - print an error level message by currying with logMessage(3,msg)</li> | |||
</ol> | |||
The actual currying is done in the Logger class, and the invocation of these methods can be seen in the body of the main program and the Account class. | |||
Here is the section of code which performs the currying: | |||
<pre> | |||
def initialize(n) | |||
@logLevel=n | |||
@debug=lambda{|msg| logMessage(0,msg)} | |||
@print=lambda{|msg| logMessage(1,msg)} | |||
@warn= lambda{|msg| logMessage(2,msg)} | |||
@error=lambda{|msg| logMessage(3,msg)} | |||
end | |||
</pre> | |||
==== Improved Version of the Program ==== | |||
And here is the entire program rewritten to utilize the new methods. | |||
<pre> | |||
class Logger | class Logger | ||
@logLevel | @logLevel | ||
Line 128: | Line 186: | ||
when 2 then 'WARNING: ' | when 2 then 'WARNING: ' | ||
when 3 then 'ERROR: ' | when 3 then 'ERROR: ' | ||
end | end | ||
puts prepend+msg | puts prepend+msg | ||
Line 194: | Line 251: | ||
end | end | ||
action="d" | action="d" | ||
logger=Logger.new(0) | logger=Logger.new(0) | ||
account=Account.new(100,logger) | account=Account.new(100,logger) | ||
while action!="q" | while action!="q" | ||
Line 215: | Line 270: | ||
end | end | ||
end | end | ||
</pre> | |||
== Conclusion == | |||
Notice how much easier it is to follow the flow of the sample program now through the clear labeling of debug/print/warning/error statements. It is very clear which output will be shown to the user and which is only provided for debugging purposes. | |||
In addition, observe how simple it would be to add a new trace statement at any log level simply by invoking the desired method rather than having to look up the appropriate log level value. | |||
In general, currying allows programmers to write stricter code which is less likely to contain misspecified arguments, and be easier to read by virtue of not requiring the reader to know what each argument value means. It is a very valuable technique for enhancing code reliability and readability. | |||
== References == | |||
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide] | |||
#[http://www.cs.nott.ac.uk/~gmh/faq.html#currying Discussion of the origins and definition of currying] | |||
#[http://moonbase.rydia.net/mental/blog/programming/currying-in-ruby A blog entry about the concept of currying] | |||
#[http://en.wikipedia.org/wiki/Currying Wikipedia entry on currying] | |||
#Lecture notes provided by Dr. Gehringer about Ruby and Currying | |||
== See Also == | |||
#[http://rightfootin.blogspot.com/2006/09/spicey-curry-in-your-ruby.html Code to make currying easier in Ruby] | |||
--[[User:Bmparees|Bmparees]] 00:48, 12 September 2007 (EDT) |
Latest revision as of 04:16, 16 September 2007
Example use of Currying in Ruby by Ben Parees
This is an original work, the sample code provided was created entirely by the author of this page. References are provided to offer additional background on the concept of currying.
Definition of Function Currying
Currying refers to a programming technique in which the number of parameters to a function are reduced by creating a new function based on the original, but fixing some of the original arguments. That is, if we have a function f(x,y,z), we can define a new function g(x) which is defined as f(x,1,2).
Now f(x,1,2)==g(x), allowing us to invoke the simpler g(x) rather than f(x,1,2). To curry a function means to create the simplified function with fixed arguments, from a function with more arguments.
Benefits of Performing Function Currying
Currying has several benefits.
- It simplifies writing code by reducing the number of arguments that must be typed each time
- It reduces the risk of error because there are fewer arguments for which values could be incorrect
- It can enhance code readability by defining multiple functions with descriptive names rather than a single function with obscure arguments
Currying in Ruby
Ruby provides for function currying using the lambda operator. The lambda operator allows the programmer to define a new function. To curry g(x) from f(x,1,2) in Ruby would be written as: g=lambda {|x| f(x,1,2)} This would assign variable g to be a new function which invokes f() with the final two arguments fixed as 1 and 2 respectively.
Example Program in Ruby
The following pair of programs demonstrates the value of currying functions in Ruby. The program itself implements a simple account balance management tool which allows for withdrawals and deposits as specified by the user.
In the first program, a Logger class is defined with a single function, "logMessage" which takes an integer log level parameter, and the message to be logged. Depending on the log level specified, and the overall logging level set in the Logger class, the message may or may not be displayed to the user. If the message is to be displayed, it will be prefixed with a string indicating the message's relative importance.
For example, logMessage(0,"starting withdrawal") will print a debugging message as:
DEBUG: starting withdrawal
In addition, this message will only be displayed if the logLevel is set to 0 or less. The more important the message, the higher the log level that should be used, to ensure it is displayed to the user. Since debug messages are often unimportant, setting the logLevel to 1 would prevent all debug messages from being shown to the user while still showing the user normal output and warning/error messages. When debugging the program, the programmer can set the logLevel to 0 to enable the output of these debug messages.
The purpose of this Logger class is to allow programmers to easily reconfigure their program to provide varying degrees of debugging output without changing code. They can simply change the log level that is configured, and automatically filter out messages which are below that log level. In addition, messages are automatically tagged with a string indicating their severity, which makes analyzing logs much easier. Unfortunately, the logMessage method is fairly cumbersome due to the requirement that the programmer specify the correct log level at each invocation.
Sample Program without Currying
Here is the initial version of the program:
class Logger @logLevel attr_accessor :logLevel def initialize(n) @logLevel=n end def logMessage(n,msg) if(n>=@logLevel) prepend=case n when 0 then 'DEBUG: ' when 1 then '' when 2 then 'WARNING: ' when 3 then 'ERROR: ' end puts prepend+msg end end end class Account @balance @logger attr_reader :balance def initialize(n,logger) @balance=n @logger=logger logger.logMessage(0,"initialized account with balance of #{n}"); end def getAmount @logger.logMessage(0,"enter getAmount method") amount=gets.chomp!.to_f @logger.logMessage(0,"Got amount #{amount}") if(amount<0) @logger.logMessage(3,"Amount is less than zero, amount will be set to zero") amount=0 end return amount end def withdraw @logger.logMessage(0,"enter withdraw method") @logger.logMessage(1,"How much would you like to withdraw?") amount=getAmount @logger.logMessage(0,"withdrawing amount of #{amount}") @balance=@balance-amount if(@balance<0) @logger.logMessage(2,"Account is overdrawn") end @logger.logMessage(0,"new balance is #{@balance}") end def deposit @logger.logMessage(0,"enter depost method") amount=getAmount @logger.logMessage(0,"depositing amount of #{amount}") @balance=@balance+amount @logger.logMessage(0,"new balance is #{balance}") end end action="d" logger=Logger.new(0) account=Account.new(100,logger) while action!="q" logger.logMessage(1,"Balance=#{account.balance}, would you like to withdraw, deposit, or quit [w/d/q]?") action=gets.chomp! logger.logMessage(0,"user chose action #{action}") if action=="w" account.withdraw elsif action=="d" account.deposit elsif action=="q" logger.logMessage(1,"goodbye") end end
Limitations of the Initial Program
In the initial form of the program, the programmer must specify the log level parameter for each trace statement they wish to create. This approach has two drawbacks:
- The programmer must know what log level corresponds to what level of trace (0=debug, 1=info, 2=warning, 3=error, etc). If they make a mistake in their numbering, the trace will not be correct.
- The code is hard to read since it is not obvious just by looking at a piece of code, whether a message is going to be printed as a debug statement, informational statement, warning, etc.
Enhancing the program via Currying
Now the same program, but with 4 new methods defined by currying the original logMessage method. The new methods are:
- debug - print a debug level message by currying with logMessage(0,msg)
- print - print an informational message by currying with logMessage(1,msg)
- warn - print a warning level message by currying with logMessage(2,msg)
- error - print an error level message by currying with logMessage(3,msg)
The actual currying is done in the Logger class, and the invocation of these methods can be seen in the body of the main program and the Account class.
Here is the section of code which performs the currying:
def initialize(n) @logLevel=n @debug=lambda{|msg| logMessage(0,msg)} @print=lambda{|msg| logMessage(1,msg)} @warn= lambda{|msg| logMessage(2,msg)} @error=lambda{|msg| logMessage(3,msg)} end
Improved Version of the Program
And here is the entire program rewritten to utilize the new methods.
class Logger @logLevel @debug @print @warn @error attr_accessor :logLevel def initialize(n) @logLevel=n @debug=lambda{|msg| logMessage(0,msg)} @print=lambda{|msg| logMessage(1,msg)} @warn= lambda{|msg| logMessage(2,msg)} @error=lambda{|msg| logMessage(3,msg)} end def logMessage(n,msg) if(n>=@logLevel) prepend=case n when 0 then 'DEBUG: ' when 1 then '' when 2 then 'WARNING: ' when 3 then 'ERROR: ' end puts prepend+msg end end def debug(msg) @debug.call(msg) end def print(msg) @print.call(msg) end def warn(msg) @warn.call(msg) end def error(msg) @error.call(msg) end end class Account @balance @logger attr_reader :balance def initialize(n,logger) @balance=n @logger=logger logger.debug("initialized account with balance of #{n}"); end def getAmount @logger.debug("enter getAmount method") amount=gets.chomp!.to_f @logger.debug("Got amount #{amount}") if(amount<0) @logger.error("Amount is less than zero, amount will be set to zero") amount=0 end return amount end def withdraw @logger.debug("enter withdraw method") @logger.print("How much would you like to withdraw?") amount=getAmount @logger.debug("withdrawing amount of #{amount}") @balance=@balance-amount if(@balance<0) @logger.warn("Account is overdrawn") end @logger.debug("new balance is #{@balance}") end def deposit @logger.debug("enter depost method") amount=getAmount @logger.debug("depositing amount of #{amount}") @balance=@balance+amount @logger.debug("new balance is #{balance}") end end action="d" logger=Logger.new(0) account=Account.new(100,logger) while action!="q" logger.print("Balance=#{account.balance}, would you like to withdraw, deposit, or quit [w/d/q]?") action=gets.chomp! logger.debug("user chose action #{action}") if action=="w" account.withdraw elsif action=="d" account.deposit elsif action=="q" logger.print("goodbye") end end
Conclusion
Notice how much easier it is to follow the flow of the sample program now through the clear labeling of debug/print/warning/error statements. It is very clear which output will be shown to the user and which is only provided for debugging purposes.
In addition, observe how simple it would be to add a new trace statement at any log level simply by invoking the desired method rather than having to look up the appropriate log level value.
In general, currying allows programmers to write stricter code which is less likely to contain misspecified arguments, and be easier to read by virtue of not requiring the reader to know what each argument value means. It is a very valuable technique for enhancing code reliability and readability.
References
- Programming Ruby: The programmatic programmer’s guide
- Discussion of the origins and definition of currying
- A blog entry about the concept of currying
- Wikipedia entry on currying
- Lecture notes provided by Dr. Gehringer about Ruby and Currying
See Also
--Bmparees 00:48, 12 September 2007 (EDT)