Skip to Main Content
Exercises in Programming Style
book

Exercises in Programming Style

by Cristina Videira Lopes
November 2015
Intermediate to advanced content levelIntermediate to advanced
304 pages
5h 23m
English
Chapman and Hall/CRC
Content preview from Exercises in Programming Style

Chapter 27

Lazy Rivers

image

27.1 Constraints

  • Data is available in streams, rather than as a complete whole.
  • Functions are filters/transformers from one kind of data stream to another.
  • Data is processed from upstream on a need basis from downstream.

27.2 A Program in this Style

  1 #!/usr/bin/env python
  2 import sys, operator, string
  3
  4 def characters(filename):
  5 for line in open(filename):
  6  for c in line:
  7    yield c
  8
  9 def all_words(filename):
 10 start_char = True
 11 for c in characters(filename):
 12  if start_char == True:
 13    word = ""
 14    if c.isalnum():
 15    # We found the start of a word
 16    word = c.lower()
 17    start_char = False
 18    else: pass ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Exercises for Programmers

Exercises for Programmers

Brian P. Hogan
Street Coder

Street Coder

Sedat Kapanoglu
Handbook of Constraint Programming

Handbook of Constraint Programming

Francesca Rossi, Peter van Beek, Toby Walsh

Publisher Resources

ISBN: 9781482227376