Functional Python Programming by Steven F. Lott
Author:Steven F. Lott [Steven F. Lott]
Language: eng
Format: epub
Tags: COM051210 - COMPUTERS / Programming / Object Oriented, COM051360 - COMPUTERS / Programming Languages / Python, COM051440 - COMPUTERS / Software Development and Engineering / Tools
Publisher: Packt Publishing
Published: 2018-04-12T11:54:35+00:00
Repeating a single value with repeat()
The repeat() function seems like an odd feature: it returns a single value over and over again. It can serve as an alternative for the cycle() function when a single value is needed.
The difference between selecting all of the data and selecting a subset of the data can be expressed with this. The function (x==0 for x in cycle(range(size))) emits a [True, False, False, ...] pattern, suitable for picking a subset. The function (x==0 for x in repeat(0)) emits a [True, True, True, ...] pattern, suitable for selecting all of the data.
We can think of the following kinds of commands:
all = repeat(0) subset = cycle(range(100)) choose = lambda rule: (x == 0 for x in rule)
# choose(all) or choose(subset) can be used
This allows us to make a simple parameter change, which will either pick all data or pick a subset of data. This pattern can be extended to randomize the subset chosen. The following technique adds an additional kind of choice:
def randseq(limit):
while True:
yield random.randrange(limit)
randomized = randseq(100)
The randseq() function generates a potentially infinite sequence of random numbers over a given range. This fits the pattern of cycle() and repeat().
This allows code such as the following:
[v for v, pick in zip(data, choose(all)) if pick]
[v for v, pick in zip(data, choose(subset)) if pick]
[v for v, pick in zip(data, choose(randomized)) if pick]
Using chose(all), chose(subset), or chose(randomized) is a succinct expression of which data is selected for further analysis.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12528)
Hello! Python by Anthony Briggs(9873)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9763)
The Mikado Method by Ola Ellnestam Daniel Brolund(9754)
Dependency Injection in .NET by Mark Seemann(9300)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8264)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7748)
Grails in Action by Glen Smith Peter Ledbrook(7673)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7523)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(6767)
Microservices with Go by Alexander Shuiskov(6536)
Practical Design Patterns for Java Developers by Miroslav Wengner(6428)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6406)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6385)
Angular Projects - Third Edition by Aristeidis Bampakos(5795)
The Art of Crafting User Stories by The Art of Crafting User Stories(5320)
NetSuite for Consultants - Second Edition by Peter Ries(5260)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5081)
Kotlin in Action by Dmitry Jemerov(5025)
