In that direction, you will probably face the situation when a function only handles the empty use case and delegates the others, and you don't want to transform items into a Collection or a Sequence. Performance shall be handle as a daily task, not as a firemen task force. Then it's the caller responsibility to transform data when necessary, such as: my_func_3(tuple(x**2 for x in range(100)))Īctually, all this is really about performance when scaling the length of items.Īlways prefer Iterator when possible. using a set when a Sequence is required). Don't forget that all this is only about typing, to help a static type checker to report incorrect calls (e.g. Hence use a Sequence: from typing import Sequenceĭef my_func_3(items: Sequence) -> None:Ĭonclusion: The strategy is: "use the most generic type that the function can handle". If the function algorithm ( my_func_3) has to access by index to specific items, then both Iterable and Collection will fail if the caller provides a set, a Mapping or a 'strict' Iterator. Hence use a Collection: from typing import Collectionĭef my_func_2(items: Collection) -> None: In general, the terms of an arithmetic sequence with the first term a0 and common difference d, have the form an dn+a0 (n0,1,2.). However if the function algorithm (say my_func_2) requires more than one iteration, then Iterable will fail if the caller provides a 'strict' Iterator because the first iteration exhausts it. set), and because the iteration breaks before StopIteration, it is also more performing if he provides an 'strict' Iterator. tuple, list) or as a non- Sequence Collection (e.g. In case the caller doesn't have data as a Sequence (e.g. The implicit message to a function caller is: transfer data "as-is", just don't transform it. My_func_1(x**2 for x in range(100)) # "strict' Iterator, i.e. My_func_1(range(10)) # range is Sequence, Collection, Iterator My_func_1(my_dict.keys()) # dict.keys() is MappingKeys, Set, Collection, Iterator My_func_1(my_dict) # dict is Mapping, Collection, Iterator My_func_1() # set is Collection, Iterator My_func_1() # list is MutableSequence, Sequence, Collection, Iterator Correct calls include: my_func_1((1, 2, 3)) # tuple is Sequence, Collection, Iterator Iterable offers the maximum possibilities to the caller. Say my_func_1 is: from typing import Iterableĭef my_func_1(items: Iterable) -> None: Hereafter is why and I hope it will help understanding the difference. The resource at the bottom is a formula chart for geometric and arithmetic sequences and series.When writing a function/method with an items argument, I often prefer Iterable to Sequence. The third resource is an arithmetic and geometric sequence and series game. The second resource would be a great follow up after teaching arithmetic sequences. I’m working on the geometric sequence activity now and hope to finish in a week or so. I’ve attached a couple more of my resources. The pattern is continued by adding 3 to the last number each time, like this: Example: 3, 8, 13, 18, 23, 28, 33, 38. This sequence has a difference of 3 between each number. I wanted to create something that students could learn from and see how these patterns are involved in real-life situations. Arithmetic Sequences An Arithmetic Sequence is made by adding the same value each time. In the previous example the common ratio was 3: We can start with any number: Example: Common Ratio of 3, But Starting at 2. The pattern is continued by adding 3 to the last number each time, like this: Example: 3, 8, 13, 18, 23, 28, 33. When I was creating this resource, it really stretched my thinking. This sequence has a difference of 3 between each number. Some of the examples I used above are in my Arithmetic Sequence Activity seen below. Students need to know that their math is real and useful! I hope this encourages you to use some of these examples or make up some of your own. It’s really fun to create these problems. I hope I’ve given you plenty to think about.
0 Comments
Leave a Reply. |