Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Aggregate pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Computer programming == In ''[[Design Patterns]]'', an aggregate is not a [[Software design pattern|design pattern]] but rather refers to an object such as a list, vector, or generator which provides an interface for creating [[iterator]]s. The following example code is in [[Python (programming language)|Python]]. <syntaxhighlight lang="python"> def fibonacci(n: int): a, b = 0, 1 count = 0 while count < n: count += 1 a, b = b, a + b yield a for x in fibonacci(10): print(x) def fibsum(n: int) -> int: total = 0 for x in fibonacci(n): total += x return total def fibsum_alt(n: int) -> int: """ Alternate implementation. demonstration that Python's built-in function sum() works with arbitrary iterators. """ return sum(fibonacci(n)) myNumbers = [1, 7, 4, 3, 22] def average(g) -> float: return float(sum(g)) / len(g) # In Python 3 the cast to float is no longer be necessary </syntaxhighlight> Python hides essentially all of the details using the [https://docs.python.org/3/library/stdtypes.html#iterator-types iterator protocol]. Confusingly, ''[[Design Patterns]]'' uses "aggregate" to refer to the blank in the code <code>for x in ___:</code> which is unrelated to the term "aggregation".<ref>[[Design Patterns]], p. 22: "Aggregation implies that one object owns or is responsible for another object. ... Aggregation implies that an aggregate object and its owner have identical lifetimes."</ref> Neither of these terms refer to the statistical aggregation of data such as the act of adding up the Fibonacci sequence or taking the average of a list of numbers.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)