CSCI 431 — Notes on Chapter 3

SQL and Group by

Iterators

Python

Start up python!

from itertools import groupby
things = [("animal", "bear"), ("animal", "duck"), ("plant", "cactus"),
          ("vehicle", "speed boat"), ("vehicle", "school bus")]
magic = groupby(things, lambda x: x[0])
for x in magic:
    print(x[0])
    for y in x[1]:
        print("  {0}".format(y[1]))
magic = groupby(things, lambda x: x[0])
for x in magic:
print(format("{0:10s}:{1:3d}".format(x[0], sum(1 for e in x[1]))))

Java

  int csciSections = (int) testList.stream()
     .filter(section -> section.getDepartment() == "CSCI")
     .count();
  System.out.printf("CSCI: %d\n", csciSections) ;
  testList.stream()
    .collect(Collectors.groupingBy(SectionInfo::getDepartment,
                                   Collectors.counting()))
    .forEach((s, c) -> System.out.println(s + ": " + c));

LISP and scheme