CS 61A

Time: Thu 11/16/17 3 pm

Iterables & Iterators

Iterables implement the __iter__ method, and iterators (which are also iterables) additionally implement the __next__ method. In an iterator, the iteration halts at StopIterator exception.

Generators produce iterators.

SQL

Syntax

Refer to SQL As Understood By SQLite.

Demo for Recursive Selects

-- Family

create table family as
  select "Bob" as member, "Danny" as parent union
  select "Gaby", "Bob" union
  select "Lila", "Danny";

-- Discover all the ancestors for each of the family member

with ancestors(member, ancestor, distance) as (
  select member,        parent,             1                      from family union
  select family.member, ancestors.ancestor, ancestors.distance + 1 from family, ancestors
    where family.parent = ancestors.person
) select * from ancestors;

-- With a better formatting

with ancestors(member, ancestor, distance) as (
  select member,        parent,             1                      from family union
  select family.member, ancestors.ancestor, ancestors.distance + 1 from family, ancestors
    where family.parent = ancestors.person
) select person || " is of distance " || distance || " from " || ancestor from ancestors;

The basic algorithm for computing the content of the recursive table is as follows:

More details about how with clause is executed: https://sqlite.org/lang_with.html