Aggregate pattern: Difference between revisions

Content deleted Content added
m rm {{split}} tag - not discussed and not needed.
simplify
 
(16 intermediate revisions by 13 users not shown)
Line 1:
{{Short description|Concepts in statistics and computer science}}
An '''Aggregate pattern''' can refer to concepts in either statistics or computer programming. Both uses deal with considering a large case as composed of smaller, simpler, pieces.
{{one source |date=March 2024}}
An '''Aggregate pattern''' can refer to concepts in either statistics or computer programming. Both uses dealsimplify withcomplexity considering a large case as composed ofinto smaller, simpler, piecesparts.
 
== Statistics ==
 
An aggregate pattern is an important statistical concept in many fields that rely on [[statistics]] to predict the behavior of large groups, based on the tendencies of subgroups to consistently behave in a certain way. It is particularly useful in [[sociology]], [[economics]], [[psychology]], and [[criminology]].
 
== 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">
<pre>
def fibonacci(n: int):
mylist = ['apples','oranges','milk','eggs','flour']
a, b = b0, a+b1
 
count += 10
for item in mylist:
while count < n:
print "Mom, we're out of " + item + "!"
count += 01
 
a, b = b, a + b
def fibonacci(n):
a,b = 0,1 yield a
count = 0
while count < n:
count += 1
a,b = b, a+b
yield a
 
for x in fibonacci(10):
print print(x)
 
def fibsum(n):
total = 0
for x in fibonacci(n):
total += x
return total
 
def fibsum_alt(n):
"Alternate implementation. demonstration that Python's built-in function sum() works with arbitrary iterators"
return sum(fibonacci(n))
 
def fibsum(n: int) -> int:
myNumbers = [1,7,4,3,22]
total = 0
for x in fibonacci(n):
total += x
return total
 
def averagefibsum_alt(gn: int) -> int:
"""
return float(sum(g))/len(g) #in Python 3.0 the cast to float will no longer be necessary
" Alternate implementation. demonstration that Python's built-in function sum() works with arbitrary iterators"
works with arbitrary iterators.
"""
return sum(fibonacci(n))
 
myNumbers = [1, 7, 4, 3, 22]
</pre>
 
def average(g) -> float:
Python hides essentially all of the details using the [http://www.python.org/doc/lib/typeiter.html 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.
return float(sum(g)) / len(g) #in In Python 3.0 the cast to float willis no longer be necessary
</syntaxhighlight>
Python hides essentially all of the details using the [httphttps://wwwdocs.python.org/doc3/liblibrary/typeiterstdtypes.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.
 
== See also ==
Line 55 ⟶ 52:
 
[[Category:Software design patterns]]
[[Category:Articles with example PerlPython (programming language) code]]