Difference between revisions of "Generic function"

From CS 61A Wiki
Jump to: navigation, search
[checked revision][checked revision]
(copyedit; reword)
Line 1: Line 1:
 
{{Start-class}}
 
{{Start-class}}
A '''generic function''' is a [[function]] that can take in different [[object]] types and operate on all of them. This is especially helpful when the objects we are working with have multiple representations. A good example is [https://docs.google.com/presentation/d/1AZSqH0-tbjwJXXZcaqaFnDnfCP-g4wMOxzVPz3aPc4M/edit#slide=id.g3901816cf_022| the variety of number representations] we learned in lecture.
+
A '''generic function''' is a function that can operate on multiple types of data. This is especially useful when the objects we are working with have multiple representations. A good example is [https://docs.google.com/presentation/d/1AZSqH0-tbjwJXXZcaqaFnDnfCP-g4wMOxzVPz3aPc4M/edit#slide=id.g3901816cf_022 the variety of number representations] we learned in lecture.
  
== Creating a generic function ==
+
== Techniques ==
The following techniques are convenient ways to create a generic function:
+
The following are techniques for creating generic functions:
 +
 
 +
=== Interfaces ===
 +
An interface manipulates different data types with the same function. For example, the <code>+</code> operator works with numbers, strings, and lists:
 +
<syntaxhighlight lang="python">
 +
>>> 1 + 2
 +
3
 +
>>> 'hi ' + 'there'
 +
'hi there'
 +
>>> [1, 2] + [3, 4]
 +
[1, 2, 3, 4]
 +
</syntaxhighlight>
  
 
=== Type dispatching ===
 
=== Type dispatching ===
''Type dispatching'' does exactly as its name suggests: depending on the type of the arguments passed in, the function will proceed in different ways. This is especially convenient because it means you can create a general function that will work for a variety of different things.  
+
''Type dispatching'' does exactly as its name suggests: depending on the type of the arguments passed in, the function will proceed in different ways. We create a function for each different combination of valid input types and then choose one to use by checking the types of arguments passed in.
  
====example: a <code>drive</code> function====
+
For example, consider the following <code>drive</code> function, which takes in a variety of <code>car</code> types and returns the correct <code>drive</code> method depending on the type given:
Consider the following <code>drive</code> function, which takes in a variety of <code> car </code> types and returns the correct <code>drive</code> method depending on the type given.
+
<syntaxhighlight lang="python">
<syntaxhighlight lang="python" highlight=3>
+
 
def drive(car):
 
def drive(car):
 
     if type(car) == SportsCar:
 
     if type(car) == SportsCar:
Line 23: Line 33:
  
 
=== Coercion ===
 
=== Coercion ===
Another way of implementing generic functions is through ''coercion'', which is simply converting our original object into a new type of object so that we can use the new object's methods. This is especially helpful when the two different objects have some similar properties, and when our original object can be seen as a the new type with certain modifications.
+
''Coercion'' is converting object A into object B so that A can use B's methods. This is useful when the two different objects have some similar properties and when the original object can be seen as the new type with certain modifications.
 
+
====example: a function====
+
 
+
  
 +
Example:
  
 
== Sources ==
 
== Sources ==
[https://docs.google.com/presentation/d/1AZSqH0-tbjwJXXZcaqaFnDnfCP-g4wMOxzVPz3aPc4M/edit#slide=id.p| CS61A Su14 Lecture 17]
+
* [https://docs.google.com/presentation/d/1AZSqH0-tbjwJXXZcaqaFnDnfCP-g4wMOxzVPz3aPc4M/edit#slide=id.p CS61A Su14 Lecture 17]
 
+
* [http://inst.eecs.berkeley.edu/~cs61a/su14/discussion/discussion09/discussion09.pdf CS61A Su14 Discussion 9]
[http://inst.eecs.berkeley.edu/~cs61a/su14/discussion/discussion09/discussion09.pdf| CS61A Su14 Discussion 9]
+
* [http://wla.berkeley.edu/~cs61a/fa11/lectures/objects.html#generic-operations CS61A Fa11 2.7.3]
 
+
[http://wla.berkeley.edu/~cs61a/fa11/lectures/objects.html#generic-operations| CS61A Fa11 2.7.3]
+

Revision as of 21:11, 24 July 2014

A generic function is a function that can operate on multiple types of data. This is especially useful when the objects we are working with have multiple representations. A good example is the variety of number representations we learned in lecture.

Techniques

The following are techniques for creating generic functions:

Interfaces

An interface manipulates different data types with the same function. For example, the + operator works with numbers, strings, and lists:

>>> 1 + 2
3
>>> 'hi ' + 'there'
'hi there'
>>> [1, 2] + [3, 4]
[1, 2, 3, 4]

Type dispatching

Type dispatching does exactly as its name suggests: depending on the type of the arguments passed in, the function will proceed in different ways. We create a function for each different combination of valid input types and then choose one to use by checking the types of arguments passed in.

For example, consider the following drive function, which takes in a variety of car types and returns the correct drive method depending on the type given:

def drive(car):
    if type(car) == SportsCar:
        return car.drive_sportscar()
    elif type(car) == Van:
        return car.drive_van()
    elif type(car) == SemiTruck:
        return car.drive_semitruck()
    elif type(car) == Tractor:
        return car.drive_tractor()

Coercion

Coercion is converting object A into object B so that A can use B's methods. This is useful when the two different objects have some similar properties and when the original object can be seen as the new type with certain modifications.

Example:

Sources