SlideShare a Scribd company logo
declarative thinking,
declarative practice
@KevlinHenney
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
Computer Science in the 1960s to 80s
spent a lot of effort making languages
which were as powerful as possible.
Nowadays we have to appreciate the
reasons for picking not the most
powerful solution but the least powerful.
Tim Berners-Lee
https://www.w3.org/DesignIssues/Principles.html
The reason for this is that the less
powerful the language, the more you can
do with the data stored in that language.
If you write it in a simple declarative
form, anyone can write a program to
analyze it in many ways.
Tim Berners-Lee
https://www.w3.org/DesignIssues/Principles.html
Declarative Thinking, Declarative Practice
The makefile language is similar
to declarative programming.
This class of language, in which
necessary end conditions are
described but the order in
which actions are to be taken is
not important, is sometimes
confusing to programmers used
to imperative programming.
http://en.wikipedia.org/wiki/Make_(software)
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
Representation
Is the Essence of
Programming
Show me your flowcharts and
conceal your tables, and I
shall continue to be mystified.
Show me your tables, and I
won’t usually need your
flowcharts; they’ll be obvious.
Excel is the world's
most popular
functional language.
Simon Peyton-Jones
try {
Integer.parseInt(time.substring(0, 2));
}
catch (Exception x) {
return false;
}
if (Integer.parseInt(time.substring(0, 2)) > 12) {
return false;
}
...
if (!time.substring(9, 11).equals("AM") &
!time.substring(9, 11).equals("PM")) {
return false;
}
Burk Hufnagel
"Put the Mouse Down and Step Away from the Keyboard"
Burk Hufnagel
"Put the Mouse Down and Step Away from the Keyboard"
public static boolean validateTime(String time) {
return time.matches("(0[1-9]|1[0-2]):[0-5][0-9]:[0-5][0-9] ([AP]M)");
}
Many programming languages support
programming in both functional and
imperative style but the syntax and
facilities of a language are typically
optimised for only one of these styles,
and social factors like coding conventions
and libraries often force the programmer
towards one of the styles.
https://wiki.haskell.org/Functional_programming
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
intension, n. (Logic)
 the set of characteristics or properties by which
the referent or referents of a given expression is
determined; the sense of an expression that
determines its reference in every possible world,
as opposed to its actual reference. For example,
the intension of prime number may be having
non-trivial integral factors, whereas its extension
would be the set {2, 3, 5, 7, ...}.
E J Borowski and J M Borwein
Dictionary of Mathematics
{ x2 | x  , x ≥ 1  x ≤ 100 }
select from where
A list comprehension is a syntactic
construct available in some programming
languages for creating a list based on
existing lists. It follows the form of the
mathematical set-builder notation (set
comprehension) as distinct from the use
of map and filter functions.
http://en.wikipedia.org/wiki/List_comprehension
Declarative Thinking, Declarative Practice
{ x2 | x  , x ≥ 1 }
Declarative Thinking, Declarative Practice
Lazy
evaluation
Declarative Thinking, Declarative Practice
https://twitter.com/richardadalton/status/591534529086693376
def fizzbuzz(n):
result = ''
if n % 3 == 0:
result += 'Fizz'
if n % 5 == 0:
result += 'Buzz'
if not result:
result = str(n)
return result
def fizzbuzz(n):
if n % 15 == 0:
return 'FizzBuzz'
elif n % 3 == 0:
return 'Fizz'
elif n % 5 == 0:
return 'Buzz'
else:
return str(n)
def fizzbuzz(n):
return (
'FizzBuzz' if n % 15 == 0 else
'Fizz' if n % 3 == 0 else
'Buzz' if n % 5 == 0 else
str(n))
def fizzbuzz(n):
return (
'FizzBuzz' if n in range(0, 101, 15) else
'Fizz' if n in range(0, 101, 3) else
'Buzz' if n in range(0, 101, 5) else
str(n))
fizzes = [''] + ([''] * 2 + ['Fizz']) * 33 + ['']
buzzes = [''] + ([''] * 4 + ['Buzz']) * 20
numbers = list(map(str, range(0, 101)))
def fizzbuzz(n):
return fizzes[n] + buzzes[n] or numbers[n]
actual = [fizzbuzz(n) for n in range(1, 101)]
truths = [
every result is 'Fizz', 'Buzz', 'FizzBuzz' or a decimal string,
every decimal result corresponds to its ordinal position,
every third result contains 'Fizz',
every fifth result contains 'Buzz',
every fifteenth result is 'FizzBuzz',
the ordinal position of every 'Fizz' result is divisible by 3,
the ordinal position of every 'Buzz' result is divisible by 5,
the ordinal position of every 'FizzBuzz' result is divisible by 15
]
all(truths)
actual = [fizzbuzz(n) for n in range(1, 101)]
truths = [
all(a in {'Fizz', 'Buzz', 'FizzBuzz'} or a.isdecimal() for a in actual),
all(int(a) == n for n, a in enumerate(actual, 1) if a.isdecimal()),
all('Fizz' in a for a in actual[2::3]),
all('Buzz' in a for a in actual[4::5]),
all(a == 'FizzBuzz' for a in actual[14::15]),
all(n % 3 == 0 for n, a in enumerate(actual, 1) if a == 'Fizz'),
all(n % 5 == 0 for n, a in enumerate(actual, 1) if a == 'Buzz'),
all(n % 15 == 0 for n, a in enumerate(actual, 1) if a == 'FizzBuzz')
]
all(truths)
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
https://twitter.com/wm/status/7206700352
/ WordFriday
bi-quinary coded decimal, noun
 A system of representing numbers based on
counting in fives, with an additional indicator to
show whether the count is in the first or second half
of the decimal range, i.e., whether the number
represented is in the range 0–4 or 5–9.
 This system is found in many abacus systems, with
paired columns of counters (normally aligned)
representing each bi-quinary range.
 The Roman numeral system is also a form of bi-
quinary coded decimal.
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
// Get the unique surnames in uppercase of the
// first 15 book authors that are 50 years old
// or older?
library.stream()
.map(book -> book.getAuthor())
.filter(author -> author.getAge() >= 50)
.limit(15)
.map(Author::getSurname)
.map(String::toUpperCase)
.distinct()
.collect(toList()))
// Get the first 15 unique surnames in
// uppercase of the book authors that are 50
// years old or older.
library.stream()
.map(book -> book.getAuthor())
.filter(author -> author.getAge() >= 50)
.map(Author::getSurname)
.map(String::toUpperCase)
.distinct()
.limit(15)
.collect(toList()))
// Get the unique surnames in uppercase of the
// first 15 book authors that are 50 years old
// or older.
library.stream()
.map(book -> book.getAuthor())
.filter(author -> author.getAge() >= 50)
.distinct()
.limit(15)
.map(Author::getSurname)
.map(String::toUpperCase)
.distinct()
.collect(toList()))
Simple filters that can be arbitrarily
chained are more easily re-used,
and more robust, than almost any
other kind of code.
Brandon Rhodes
http://rhodesmill.org/brandon/slides/2012-11-pyconca/
/ WordFriday
paraskevidekatriaphobia, noun
 The superstitious fear of Friday 13th.
 Contrary to popular myth, this superstition is
relatively recent (19th century) and did not originate
during or before the medieval times.
 Paraskevidekatriaphobia also reflects a particularly
egocentric attributional bias: the universe is
prepared to rearrange causality and probability
around the believer based on an arbitrary and
changeable calendar system, in a way that is
sensitive to geography, culture and time zone.
Declarative Thinking, Declarative Practice
function NextFriday13thAfter($from) {
(1..500) |
%{ $from.AddDays($_) } |
?{ $_.Day -eq 13} |
?{ $_.DayOfWeek -eq [DayOfWeek]::Friday } |
select –first 1
}
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
Stack
Stack
{push, pop, depth, top}
Stack[T]
{
push(T),
pop(),
depth() : Integer,
top() : T
}
An interface is a contract to deliver
a certain amount of service.
Clients of the interface depend on
the contract, which is usually
documented in the interface
specification.
Butler W Lampson
"Hints for Computer System Design"
Declarative Thinking, Declarative Practice
Stack[T]
{
push(T item),
pop(),
depth() : Integer,
top() : T
}
given:
before = depth()
postcondition:
depth() = before + 1 ∧ top() = item
precondition:
depth() > 0
given:
before = depth()
precondition:
before > 0
postcondition:
depth() = before – 1
given:
result = depth()
postcondition:
result ≥ 0
Declarative Thinking, Declarative Practice
alphabet(Stack) =
{push, pop, depth, top}
trace(Stack) =
{⟨ ⟩,
⟨push⟩, ⟨depth⟩,
⟨push, pop⟩, ⟨push, top⟩,
⟨push, depth⟩, ⟨push, push⟩,
⟨depth, push⟩, ⟨depth, depth⟩,
⟨push, push, pop⟩,
...}
Non-EmptyEmpty
depth depth
top
push
pop [depth > 1]
push
pop [depth = 1]
public class Stack_spec
{
public static class A_new_stack
{
@Test
public void has_no_depth() 
@Test()
public void has_no_top() 
}
public static class An_empty_stack
{
@Test()
public void throws_when_popped() 
@Test
public void acquires_depth_by_retaining_a_pushed_item_as_its_top() 
}
public static class A_non_empty_stack
{
@Test
public void becomes_deeper_by_retaining_a_pushed_item_as_its_top() 
@Test
public void on_popping_reveals_tops_in_reverse_order_of_pushing() 
}
}
public class
Stack_spec
{
public static class
A_new_stack
{
@Test
public void has_no_depth() 
@Test()
public void has_no_top() 
}
public static class
An_empty_stack
{
@Test()
public void throws_when_popped() 
@Test
public void acquires_depth_by_retaining_a_pushed_item_as_its_top() 
}
public static class
A_non_empty_stack
{
@Test
public void becomes_deeper_by_retaining_a_pushed_item_as_its_top() 
@Test
public void on_popping_reveals_tops_in_reverse_order_of_pushing() 
}
}
public class
Stack_spec
{
public static class
A_new_stack
{
@Test
public void has_no_depth() 
@Test()
public void has_no_top() 
}
public static class
An_empty_stack
{
@Test()
public void throws_when_popped() 
@Test
public void acquires_depth_by_retaining_a_pushed_item_as_its_top() 
}
public static class
A_non_empty_stack
{
@Test
public void becomes_deeper_by_retaining_a_pushed_item_as_its_top() 
@Test
public void on_popping_reveals_tops_in_reverse_order_of_pushing() 
}
}
Declarative Thinking, Declarative Practice
Propositions
are vehicles
for stating
how things are
or might be.
Thus only indicative
sentences which it
makes sense to think
of as being true or as
being false are
capable of expressing
propositions.
Declarative Thinking, Declarative Practice
































Declarative Thinking, Declarative Practice
{P} S {Q}
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
Are human beings
"noble in reason" and
"infinite in faculty" as
William Shakespeare
famously wrote?
Perfect, "in God's
image," as some
biblical scholars have
asserted?
Hardly.
def is_leap_year(year):
...
# What is the postcondition?
def is_leap_year(year):
...
# Given
# result = is_leap_year(year)
# Then
# result == (
# year % 4 == 0 and
# year % 100 != 0 or
# year % 400 == 0)
def is_leap_year(year):
return (
year % 4 == 0 and
year % 100 != 0 or
year % 400 == 0)
def test_is_leap_year():
...
def test_is_leap_year_works():
...
Express intention
of code usage
with respect to data
class LeapYearSpec:
@test
def years_not_divisible_by_4_are_not_leap_years(self):
assert not is_leap_year(2015)
@test
def years_divisible_by_4_but_not_by_100_are_leap_years(self):
assert is_leap_year(2016)
@test
def years_divisible_by_100_but_not_by_400_are_not_leap_years(self):
assert not is_leap_year(1900)
@test
def years_divisible_by_400_are_leap_years(self):
assert is_leap_year(2000)
class LeapYearSpec:
@test
def years_not_divisible_by_4_are_not_leap_years(self):
assert not is_leap_year(2015)
@test
def years_divisible_by_4_but_not_by_100_are_leap_years(self):
assert is_leap_year(2016)
@test
def years_divisible_by_100_but_not_by_400_are_not_leap_years(self):
assert not is_leap_year(1900)
@test
def years_divisible_by_400_are_leap_years(self):
assert is_leap_year(2000)
def test(function):
function.is_test = True
return function
def check(suite):
tests = [
attr
for attr in (getattr(suite, name) for name in dir(suite))
if callable(attr) and hasattr(attr, 'is_test')]
for to_test in tests:
try:
to_test(suite())
except:
print('Failed: ' + to_test.__name__ + '()')
Express intention
of code usage
with respect to data
Express intention
Naming
Grouping and nesting
of code usage
Realisation of intent
with respect to data
One exemplar or many
Explicit or generated
class LeapYearSpec:
@test
def years_not_divisible_by_4_are_not_leap_years(self):
assert not is_leap_year(2015)
assert not is_leap_year(1999)
assert not is_leap_year(1)
@test
def years_divisible_by_4_but_not_by_100_are_leap_years(self):
assert is_leap_year(2016)
@test
def years_divisible_by_100_but_not_by_400_are_not_leap_years(self):
assert not is_leap_year(1900)
@test
def years_divisible_by_400_are_leap_years(self):
assert is_leap_year(2000)
class LeapYearSpec:
@test
def years_not_divisible_by_4_are_not_leap_years(self):
assert not is_leap_year(2015)
assert not is_leap_year(1999)
assert not is_leap_year(1)
@test
def years_divisible_by_4_but_not_by_100_are_leap_years(self):
assert is_leap_year(2016)
assert is_leap_year(1984)
assert is_leap_year(4)
@test
def years_divisible_by_100_but_not_by_400_are_not_leap_years(self):
assert not is_leap_year(1900)
@test
def years_divisible_by_400_are_leap_years(self):
assert is_leap_year(2000)
class LeapYearSpec:
@test
def years_not_divisible_by_4_are_not_leap_years(self):
assert not is_leap_year(2015)
assert not is_leap_year(1999)
assert not is_leap_year(1)
@test
def years_divisible_by_4_but_not_by_100_are_leap_years(self):
assert is_leap_year(2016)
assert is_leap_year(1984)
assert is_leap_year(4)
@test
def years_divisible_by_100_but_not_by_400_are_not_leap_years(self):
assert not is_leap_year(1900)
@test
def years_divisible_by_400_are_leap_years(self):
for year in range(400, 2401, 400):
assert is_leap_year(year)
class LeapYearSpec:
@test
def years_not_divisible_by_4_are_not_leap_years(self):
assert not is_leap_year(2015)
assert not is_leap_year(1999)
assert not is_leap_year(1)
@test
def years_divisible_by_4_but_not_by_100_are_leap_years(self):
assert is_leap_year(2016)
assert is_leap_year(1984)
assert is_leap_year(4)
@test
def years_divisible_by_100_but_not_by_400_are_not_leap_years(self):
for year in range(100, 2101, 100):
if year % 400 != 0:
assert not is_leap_year(year)
@test
def years_divisible_by_400_are_leap_years(self):
for year in range(400, 2401, 400):
assert is_leap_year(year)
class LeapYearSpec:
@test
def years_not_divisible_by_4_are_not_leap_years(self):
assert not is_leap_year(2015)
assert not is_leap_year(1999)
assert not is_leap_year(1)
@test
def years_divisible_by_4_but_not_by_100_are_leap_years(self):
assert is_leap_year(2016)
assert is_leap_year(1984)
assert is_leap_year(4)
@test
def years_divisible_by_100_but_not_by_400_are_not_leap_years(self):
for year in range(100, 2101, 100):
if year % 400 != 0:
assert not is_leap_year(year)
@test
def years_divisible_by_400_are_leap_years(self):
for year in range(400, 2401, 400):
assert is_leap_year(year)
def test(function):
function.is_test = True
return function
def data(*values):
def prepender(function):
function.data = values + getattr(function, 'data', ())
return function
return prepender
def check(suite):
tests = [
attr
for attr in (getattr(suite, name) for name in dir(suite))
if callable(attr) and hasattr(attr, 'is_test')]
for to_test in tests:
try:
if hasattr(to_test, 'data'):
for value in to_test.data:
call = '(' + str(value) + ')'
to_test(suite(), value)
else:
call = '()'
to_test(suite())
except:
print('Failed: ' + to_test.__name__ + call)
class LeapYearSpec:
@test
@data(2015)
@data(1999)
@data(1)
def years_not_divisible_by_4_are_not_leap_years(self, year):
assert not is_leap_year(year)
@test
def years_divisible_by_4_but_not_by_100_are_leap_years(self):
assert is_leap_year(2016)
@test
def years_divisible_by_100_but_not_by_400_are_not_leap_years(self):
assert not is_leap_year(1900)
@test
def years_divisible_by_400_are_leap_years(self):
assert is_leap_year(2000)
class LeapYearSpec:
@test
@data(2015)
@data(1999)
@data(1)
def years_not_divisible_by_4_are_not_leap_years(self, year):
assert not is_leap_year(year)
@test
@data(2016, 1984, 4)
def years_divisible_by_4_but_not_by_100_are_leap_years(self, year):
assert is_leap_year(year)
@test
def years_divisible_by_100_but_not_by_400_are_not_leap_years(self):
assert not is_leap_year(1900)
@test
def years_divisible_by_400_are_leap_years(self):
assert is_leap_year(2000)
class LeapYearSpec:
@test
@data(2015)
@data(1999)
@data(1)
def years_not_divisible_by_4_are_not_leap_years(self, year):
assert not is_leap_year(year)
@test
@data(2016, 1984, 4)
def years_divisible_by_4_but_not_by_100_are_leap_years(self, year):
assert is_leap_year(year)
@test
def years_divisible_by_100_but_not_by_400_are_not_leap_years(self):
assert not is_leap_year(1900)
@test
@data(*range(400, 2401, 400))
def years_divisible_by_400_are_leap_years(self, year):
assert is_leap_year(year)
class LeapYearSpec:
@test
@data(2015)
@data(1999)
@data(1)
def years_not_divisible_by_4_are_not_leap_years(self, year):
assert not is_leap_year(year)
@test
@data(2016, 1984, 4)
def years_divisible_by_4_but_not_by_100_are_leap_years(self, year):
assert is_leap_year(year)
@test
@data(*(year for year in range(100, 2101, 100) if year % 400 != 0))
def years_divisible_by_100_but_not_by_400_are_not_leap_years(self, year):
assert not is_leap_year(year)
@test
@data(*range(400, 2401, 400))
def years_divisible_by_400_are_leap_years(self, year):
assert is_leap_year(year)
class LeapYearSpec:
@test
@data(2015)
@data(1999)
@data(1)
def years_not_divisible_by_4_are_not_leap_years(self, year):
assert not is_leap_year(year)
@test
@data(2016, 1984, 4)
def years_divisible_by_4_but_not_by_100_are_leap_years(self, year):
assert is_leap_year(year)
@test
@data(*range(100, 2101, 400))
@data(*range(200, 2101, 400))
@data(*range(300, 2101, 400))
def years_divisible_by_100_but_not_by_400_are_not_leap_years(self, year):
assert not is_leap_year(year)
@test
@data(*range(400, 2401, 400))
def years_divisible_by_400_are_leap_years(self, year):
assert is_leap_year(year)
class LeapYearSpec:
@test
@data(2015)
@data(1999)
@data(1)
def years_not_divisible_by_4_are_not_leap_years(self, year):
assert not is_leap_year(year)
@test
@data(2016, 1984, 4)
def years_divisible_by_4_but_not_by_100_are_leap_years(self, year):
assert is_leap_year(year)
@test
@data(*range(100, 2101, 400))
@data(*range(200, 2101, 400))
@data(*range(300, 2101, 400))
def years_divisible_by_100_but_not_by_400_are_not_leap_years(self, year):
assert not is_leap_year(year)
@test
@data(*range(400, 2401, 400))
def years_divisible_by_400_are_leap_years(self, year):
assert is_leap_year(year)
class LeapYearSpec:
@test
@data(2015)
@data(1999)
@data(1)
def years_not_divisible_by_4_are_not_leap_years(self, year):
assert not is_leap_year(year)
@test
@data(2016, 1984, 4)
def years_divisible_by_4_but_not_by_100_are_leap_years(self, year):
assert is_leap_year(year)
@test
@data(*range(100, 2101, 400))
@data(*range(200, 2101, 400))
@data(*range(300, 2101, 400))
def years_divisible_by_100_but_not_by_400_are_not_leap_years(self, year):
assert not is_leap_year(year)
@test
@data(*range(400, 2401, 400))
def years_divisible_by_400_are_leap_years(self, year):
assert is_leap_year(year)
Express intention
Naming
Grouping and nesting
of code usage
Realisation of intent
with respect to data
One exemplar or many
Explicit or generated
intention
declaration
of intent
Our task is not to find the
maximum amount of
content in a work of art.
Our task is to cut back
content so that we can see
the thing at all.
Susan Sontag

More Related Content

What's hot (20)

PDF
Python Cheat Sheet
Muthu Vinayagam
 
PPTX
Python 101++: Let's Get Down to Business!
Paige Bailey
 
PPTX
Basics of Python programming (part 2)
Pedro Rodrigues
 
PDF
Functions in python
Ilian Iliev
 
PPTX
Java 7, 8 & 9 - Moving the language forward
Mario Fusco
 
PDF
Python Puzzlers - 2016 Edition
Nandan Sawant
 
PPTX
Learn python - for beginners - part-2
RajKumar Rampelli
 
PDF
Java Class Design
Ganesh Samarthyam
 
PPTX
Python
Sameeksha Verma
 
PDF
The Functional Programming Triad of Map, Filter and Fold
Philip Schwarz
 
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Philip Schwarz
 
PDF
Real World Haskell: Lecture 1
Bryan O'Sullivan
 
PDF
Introduction to python
Marian Marinov
 
PDF
Real World Haskell: Lecture 6
Bryan O'Sullivan
 
PDF
Python Functions (PyAtl Beginners Night)
Rick Copeland
 
PDF
Logic programming a ruby perspective
Norman Richards
 
PDF
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
PDF
Declare Your Language: Transformation by Strategic Term Rewriting
Eelco Visser
 
PDF
Declarative Semantics Definition - Term Rewriting
Guido Wachsmuth
 
PPTX
07. Java Array, Set and Maps
Intro C# Book
 
Python Cheat Sheet
Muthu Vinayagam
 
Python 101++: Let's Get Down to Business!
Paige Bailey
 
Basics of Python programming (part 2)
Pedro Rodrigues
 
Functions in python
Ilian Iliev
 
Java 7, 8 & 9 - Moving the language forward
Mario Fusco
 
Python Puzzlers - 2016 Edition
Nandan Sawant
 
Learn python - for beginners - part-2
RajKumar Rampelli
 
Java Class Design
Ganesh Samarthyam
 
The Functional Programming Triad of Map, Filter and Fold
Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Philip Schwarz
 
Real World Haskell: Lecture 1
Bryan O'Sullivan
 
Introduction to python
Marian Marinov
 
Real World Haskell: Lecture 6
Bryan O'Sullivan
 
Python Functions (PyAtl Beginners Night)
Rick Copeland
 
Logic programming a ruby perspective
Norman Richards
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
Declare Your Language: Transformation by Strategic Term Rewriting
Eelco Visser
 
Declarative Semantics Definition - Term Rewriting
Guido Wachsmuth
 
07. Java Array, Set and Maps
Intro C# Book
 

Similar to Declarative Thinking, Declarative Practice (20)

PDF
Functional Programming You Already Know
Kevlin Henney
 
PPTX
Good functional programming is good programming
kenbot
 
PDF
Cooking Software101
Pere Urbón-Bayes
 
PPT
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
PPT
Functional Programming Past Present Future
IndicThreads
 
PDF
Functional Programming with Groovy
Arturo Herrero
 
KEY
Five Languages in a Moment
Sergio Gil
 
PDF
02. haskell motivation
Sebastian Rettig
 
PDF
Ti1220 Lecture 7: Polymorphism
Eelco Visser
 
PDF
A Few of My Favorite (Python) Things
Michael Pirnat
 
PDF
Frege - consequently functional programming for the JVM
Dierk König
 
PDF
Fun never stops. introduction to haskell programming language
Pawel Szulc
 
PPTX
1. Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
KEY
Pontificating quantification
Aaron Bedra
 
PDF
The Next Great Functional Programming Language
John De Goes
 
PDF
01. haskell introduction
Sebastian Rettig
 
PDF
Stanford splash spring 2016 basic programming
Yu-Sheng (Yosen) Chen
 
PDF
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
PDF
Scala Functional Patterns
league
 
Functional Programming You Already Know
Kevlin Henney
 
Good functional programming is good programming
kenbot
 
Cooking Software101
Pere Urbón-Bayes
 
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
Functional Programming Past Present Future
IndicThreads
 
Functional Programming with Groovy
Arturo Herrero
 
Five Languages in a Moment
Sergio Gil
 
02. haskell motivation
Sebastian Rettig
 
Ti1220 Lecture 7: Polymorphism
Eelco Visser
 
A Few of My Favorite (Python) Things
Michael Pirnat
 
Frege - consequently functional programming for the JVM
Dierk König
 
Fun never stops. introduction to haskell programming language
Pawel Szulc
 
1. Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
Pontificating quantification
Aaron Bedra
 
The Next Great Functional Programming Language
John De Goes
 
01. haskell introduction
Sebastian Rettig
 
Stanford splash spring 2016 basic programming
Yu-Sheng (Yosen) Chen
 
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
Scala Functional Patterns
league
 
Ad

More from Kevlin Henney (20)

PDF
Program with GUTs
Kevlin Henney
 
PDF
The Case for Technical Excellence
Kevlin Henney
 
PDF
Empirical Development
Kevlin Henney
 
PDF
Lambda? You Keep Using that Letter
Kevlin Henney
 
PDF
Solid Deconstruction
Kevlin Henney
 
PDF
Get Kata
Kevlin Henney
 
PDF
Procedural Programming: It’s Back? It Never Went Away
Kevlin Henney
 
PDF
Structure and Interpretation of Test Cases
Kevlin Henney
 
PDF
Agility ≠ Speed
Kevlin Henney
 
PDF
Refactoring to Immutability
Kevlin Henney
 
PDF
Old Is the New New
Kevlin Henney
 
PDF
Turning Development Outside-In
Kevlin Henney
 
PDF
Giving Code a Good Name
Kevlin Henney
 
PDF
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Kevlin Henney
 
PDF
Thinking Outside the Synchronisation Quadrant
Kevlin Henney
 
PDF
Code as Risk
Kevlin Henney
 
PDF
Software Is Details
Kevlin Henney
 
PDF
Game of Sprints
Kevlin Henney
 
PDF
Good Code
Kevlin Henney
 
PDF
Seven Ineffective Coding Habits of Many Programmers
Kevlin Henney
 
Program with GUTs
Kevlin Henney
 
The Case for Technical Excellence
Kevlin Henney
 
Empirical Development
Kevlin Henney
 
Lambda? You Keep Using that Letter
Kevlin Henney
 
Solid Deconstruction
Kevlin Henney
 
Get Kata
Kevlin Henney
 
Procedural Programming: It’s Back? It Never Went Away
Kevlin Henney
 
Structure and Interpretation of Test Cases
Kevlin Henney
 
Agility ≠ Speed
Kevlin Henney
 
Refactoring to Immutability
Kevlin Henney
 
Old Is the New New
Kevlin Henney
 
Turning Development Outside-In
Kevlin Henney
 
Giving Code a Good Name
Kevlin Henney
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Kevlin Henney
 
Thinking Outside the Synchronisation Quadrant
Kevlin Henney
 
Code as Risk
Kevlin Henney
 
Software Is Details
Kevlin Henney
 
Game of Sprints
Kevlin Henney
 
Good Code
Kevlin Henney
 
Seven Ineffective Coding Habits of Many Programmers
Kevlin Henney
 
Ad

Recently uploaded (20)

PDF
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PPTX
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
 
PPTX
CONCEPT OF PROGRAMMING in language .pptx
tamim41
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PDF
Transform Retail with Smart Technology: Power Your Growth with Ginesys
Ginesys
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
PPTX
WYSIWYG Web Builder Crack 2025 – Free Download Full Version with License Key
HyperPc soft
 
PDF
LPS25 - Operationalizing MLOps in GEP - Terradue.pdf
terradue
 
PDF
2025年 Linux 核心專題: 探討 sched_ext 及機器學習.pdf
Eric Chou
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PDF
interacting-with-ai-2023---module-2---session-3---handout.pdf
cniclsh1
 
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
 
CONCEPT OF PROGRAMMING in language .pptx
tamim41
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Transform Retail with Smart Technology: Power Your Growth with Ginesys
Ginesys
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
WYSIWYG Web Builder Crack 2025 – Free Download Full Version with License Key
HyperPc soft
 
LPS25 - Operationalizing MLOps in GEP - Terradue.pdf
terradue
 
2025年 Linux 核心專題: 探討 sched_ext 及機器學習.pdf
Eric Chou
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
interacting-with-ai-2023---module-2---session-3---handout.pdf
cniclsh1
 

Declarative Thinking, Declarative Practice

  • 4. Computer Science in the 1960s to 80s spent a lot of effort making languages which were as powerful as possible. Nowadays we have to appreciate the reasons for picking not the most powerful solution but the least powerful. Tim Berners-Lee https://www.w3.org/DesignIssues/Principles.html
  • 5. The reason for this is that the less powerful the language, the more you can do with the data stored in that language. If you write it in a simple declarative form, anyone can write a program to analyze it in many ways. Tim Berners-Lee https://www.w3.org/DesignIssues/Principles.html
  • 7. The makefile language is similar to declarative programming. This class of language, in which necessary end conditions are described but the order in which actions are to be taken is not important, is sometimes confusing to programmers used to imperative programming. http://en.wikipedia.org/wiki/Make_(software)
  • 12. Show me your flowcharts and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won’t usually need your flowcharts; they’ll be obvious.
  • 13. Excel is the world's most popular functional language. Simon Peyton-Jones
  • 14. try { Integer.parseInt(time.substring(0, 2)); } catch (Exception x) { return false; } if (Integer.parseInt(time.substring(0, 2)) > 12) { return false; } ... if (!time.substring(9, 11).equals("AM") & !time.substring(9, 11).equals("PM")) { return false; } Burk Hufnagel "Put the Mouse Down and Step Away from the Keyboard"
  • 15. Burk Hufnagel "Put the Mouse Down and Step Away from the Keyboard" public static boolean validateTime(String time) { return time.matches("(0[1-9]|1[0-2]):[0-5][0-9]:[0-5][0-9] ([AP]M)"); }
  • 16. Many programming languages support programming in both functional and imperative style but the syntax and facilities of a language are typically optimised for only one of these styles, and social factors like coding conventions and libraries often force the programmer towards one of the styles. https://wiki.haskell.org/Functional_programming
  • 20. intension, n. (Logic)  the set of characteristics or properties by which the referent or referents of a given expression is determined; the sense of an expression that determines its reference in every possible world, as opposed to its actual reference. For example, the intension of prime number may be having non-trivial integral factors, whereas its extension would be the set {2, 3, 5, 7, ...}. E J Borowski and J M Borwein Dictionary of Mathematics
  • 21. { x2 | x  , x ≥ 1  x ≤ 100 } select from where
  • 22. A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It follows the form of the mathematical set-builder notation (set comprehension) as distinct from the use of map and filter functions. http://en.wikipedia.org/wiki/List_comprehension
  • 24. { x2 | x  , x ≥ 1 }
  • 29. def fizzbuzz(n): result = '' if n % 3 == 0: result += 'Fizz' if n % 5 == 0: result += 'Buzz' if not result: result = str(n) return result
  • 30. def fizzbuzz(n): if n % 15 == 0: return 'FizzBuzz' elif n % 3 == 0: return 'Fizz' elif n % 5 == 0: return 'Buzz' else: return str(n)
  • 31. def fizzbuzz(n): return ( 'FizzBuzz' if n % 15 == 0 else 'Fizz' if n % 3 == 0 else 'Buzz' if n % 5 == 0 else str(n))
  • 32. def fizzbuzz(n): return ( 'FizzBuzz' if n in range(0, 101, 15) else 'Fizz' if n in range(0, 101, 3) else 'Buzz' if n in range(0, 101, 5) else str(n))
  • 33. fizzes = [''] + ([''] * 2 + ['Fizz']) * 33 + [''] buzzes = [''] + ([''] * 4 + ['Buzz']) * 20 numbers = list(map(str, range(0, 101))) def fizzbuzz(n): return fizzes[n] + buzzes[n] or numbers[n]
  • 34. actual = [fizzbuzz(n) for n in range(1, 101)] truths = [ every result is 'Fizz', 'Buzz', 'FizzBuzz' or a decimal string, every decimal result corresponds to its ordinal position, every third result contains 'Fizz', every fifth result contains 'Buzz', every fifteenth result is 'FizzBuzz', the ordinal position of every 'Fizz' result is divisible by 3, the ordinal position of every 'Buzz' result is divisible by 5, the ordinal position of every 'FizzBuzz' result is divisible by 15 ] all(truths)
  • 35. actual = [fizzbuzz(n) for n in range(1, 101)] truths = [ all(a in {'Fizz', 'Buzz', 'FizzBuzz'} or a.isdecimal() for a in actual), all(int(a) == n for n, a in enumerate(actual, 1) if a.isdecimal()), all('Fizz' in a for a in actual[2::3]), all('Buzz' in a for a in actual[4::5]), all(a == 'FizzBuzz' for a in actual[14::15]), all(n % 3 == 0 for n, a in enumerate(actual, 1) if a == 'Fizz'), all(n % 5 == 0 for n, a in enumerate(actual, 1) if a == 'Buzz'), all(n % 15 == 0 for n, a in enumerate(actual, 1) if a == 'FizzBuzz') ] all(truths)
  • 41. bi-quinary coded decimal, noun  A system of representing numbers based on counting in fives, with an additional indicator to show whether the count is in the first or second half of the decimal range, i.e., whether the number represented is in the range 0–4 or 5–9.  This system is found in many abacus systems, with paired columns of counters (normally aligned) representing each bi-quinary range.  The Roman numeral system is also a form of bi- quinary coded decimal.
  • 48. // Get the unique surnames in uppercase of the // first 15 book authors that are 50 years old // or older? library.stream() .map(book -> book.getAuthor()) .filter(author -> author.getAge() >= 50) .limit(15) .map(Author::getSurname) .map(String::toUpperCase) .distinct() .collect(toList()))
  • 49. // Get the first 15 unique surnames in // uppercase of the book authors that are 50 // years old or older. library.stream() .map(book -> book.getAuthor()) .filter(author -> author.getAge() >= 50) .map(Author::getSurname) .map(String::toUpperCase) .distinct() .limit(15) .collect(toList()))
  • 50. // Get the unique surnames in uppercase of the // first 15 book authors that are 50 years old // or older. library.stream() .map(book -> book.getAuthor()) .filter(author -> author.getAge() >= 50) .distinct() .limit(15) .map(Author::getSurname) .map(String::toUpperCase) .distinct() .collect(toList()))
  • 51. Simple filters that can be arbitrarily chained are more easily re-used, and more robust, than almost any other kind of code. Brandon Rhodes http://rhodesmill.org/brandon/slides/2012-11-pyconca/
  • 53. paraskevidekatriaphobia, noun  The superstitious fear of Friday 13th.  Contrary to popular myth, this superstition is relatively recent (19th century) and did not originate during or before the medieval times.  Paraskevidekatriaphobia also reflects a particularly egocentric attributional bias: the universe is prepared to rearrange causality and probability around the believer based on an arbitrary and changeable calendar system, in a way that is sensitive to geography, culture and time zone.
  • 55. function NextFriday13thAfter($from) { (1..500) | %{ $from.AddDays($_) } | ?{ $_.Day -eq 13} | ?{ $_.DayOfWeek -eq [DayOfWeek]::Friday } | select –first 1 }
  • 59. Stack
  • 62. An interface is a contract to deliver a certain amount of service. Clients of the interface depend on the contract, which is usually documented in the interface specification. Butler W Lampson "Hints for Computer System Design"
  • 64. Stack[T] { push(T item), pop(), depth() : Integer, top() : T } given: before = depth() postcondition: depth() = before + 1 ∧ top() = item precondition: depth() > 0 given: before = depth() precondition: before > 0 postcondition: depth() = before – 1 given: result = depth() postcondition: result ≥ 0
  • 67. trace(Stack) = {⟨ ⟩, ⟨push⟩, ⟨depth⟩, ⟨push, pop⟩, ⟨push, top⟩, ⟨push, depth⟩, ⟨push, push⟩, ⟨depth, push⟩, ⟨depth, depth⟩, ⟨push, push, pop⟩, ...}
  • 69. public class Stack_spec { public static class A_new_stack { @Test public void has_no_depth()  @Test() public void has_no_top()  } public static class An_empty_stack { @Test() public void throws_when_popped()  @Test public void acquires_depth_by_retaining_a_pushed_item_as_its_top()  } public static class A_non_empty_stack { @Test public void becomes_deeper_by_retaining_a_pushed_item_as_its_top()  @Test public void on_popping_reveals_tops_in_reverse_order_of_pushing()  } }
  • 70. public class Stack_spec { public static class A_new_stack { @Test public void has_no_depth()  @Test() public void has_no_top()  } public static class An_empty_stack { @Test() public void throws_when_popped()  @Test public void acquires_depth_by_retaining_a_pushed_item_as_its_top()  } public static class A_non_empty_stack { @Test public void becomes_deeper_by_retaining_a_pushed_item_as_its_top()  @Test public void on_popping_reveals_tops_in_reverse_order_of_pushing()  } }
  • 71. public class Stack_spec { public static class A_new_stack { @Test public void has_no_depth()  @Test() public void has_no_top()  } public static class An_empty_stack { @Test() public void throws_when_popped()  @Test public void acquires_depth_by_retaining_a_pushed_item_as_its_top()  } public static class A_non_empty_stack { @Test public void becomes_deeper_by_retaining_a_pushed_item_as_its_top()  @Test public void on_popping_reveals_tops_in_reverse_order_of_pushing()  } }
  • 73. Propositions are vehicles for stating how things are or might be.
  • 74. Thus only indicative sentences which it makes sense to think of as being true or as being false are capable of expressing propositions.
  • 86. Are human beings "noble in reason" and "infinite in faculty" as William Shakespeare famously wrote? Perfect, "in God's image," as some biblical scholars have asserted?
  • 88. def is_leap_year(year): ... # What is the postcondition?
  • 89. def is_leap_year(year): ... # Given # result = is_leap_year(year) # Then # result == ( # year % 4 == 0 and # year % 100 != 0 or # year % 400 == 0)
  • 90. def is_leap_year(year): return ( year % 4 == 0 and year % 100 != 0 or year % 400 == 0)
  • 93. Express intention of code usage with respect to data
  • 94. class LeapYearSpec: @test def years_not_divisible_by_4_are_not_leap_years(self): assert not is_leap_year(2015) @test def years_divisible_by_4_but_not_by_100_are_leap_years(self): assert is_leap_year(2016) @test def years_divisible_by_100_but_not_by_400_are_not_leap_years(self): assert not is_leap_year(1900) @test def years_divisible_by_400_are_leap_years(self): assert is_leap_year(2000)
  • 95. class LeapYearSpec: @test def years_not_divisible_by_4_are_not_leap_years(self): assert not is_leap_year(2015) @test def years_divisible_by_4_but_not_by_100_are_leap_years(self): assert is_leap_year(2016) @test def years_divisible_by_100_but_not_by_400_are_not_leap_years(self): assert not is_leap_year(1900) @test def years_divisible_by_400_are_leap_years(self): assert is_leap_year(2000)
  • 96. def test(function): function.is_test = True return function def check(suite): tests = [ attr for attr in (getattr(suite, name) for name in dir(suite)) if callable(attr) and hasattr(attr, 'is_test')] for to_test in tests: try: to_test(suite()) except: print('Failed: ' + to_test.__name__ + '()')
  • 97. Express intention of code usage with respect to data
  • 98. Express intention Naming Grouping and nesting of code usage Realisation of intent with respect to data One exemplar or many Explicit or generated
  • 99. class LeapYearSpec: @test def years_not_divisible_by_4_are_not_leap_years(self): assert not is_leap_year(2015) assert not is_leap_year(1999) assert not is_leap_year(1) @test def years_divisible_by_4_but_not_by_100_are_leap_years(self): assert is_leap_year(2016) @test def years_divisible_by_100_but_not_by_400_are_not_leap_years(self): assert not is_leap_year(1900) @test def years_divisible_by_400_are_leap_years(self): assert is_leap_year(2000)
  • 100. class LeapYearSpec: @test def years_not_divisible_by_4_are_not_leap_years(self): assert not is_leap_year(2015) assert not is_leap_year(1999) assert not is_leap_year(1) @test def years_divisible_by_4_but_not_by_100_are_leap_years(self): assert is_leap_year(2016) assert is_leap_year(1984) assert is_leap_year(4) @test def years_divisible_by_100_but_not_by_400_are_not_leap_years(self): assert not is_leap_year(1900) @test def years_divisible_by_400_are_leap_years(self): assert is_leap_year(2000)
  • 101. class LeapYearSpec: @test def years_not_divisible_by_4_are_not_leap_years(self): assert not is_leap_year(2015) assert not is_leap_year(1999) assert not is_leap_year(1) @test def years_divisible_by_4_but_not_by_100_are_leap_years(self): assert is_leap_year(2016) assert is_leap_year(1984) assert is_leap_year(4) @test def years_divisible_by_100_but_not_by_400_are_not_leap_years(self): assert not is_leap_year(1900) @test def years_divisible_by_400_are_leap_years(self): for year in range(400, 2401, 400): assert is_leap_year(year)
  • 102. class LeapYearSpec: @test def years_not_divisible_by_4_are_not_leap_years(self): assert not is_leap_year(2015) assert not is_leap_year(1999) assert not is_leap_year(1) @test def years_divisible_by_4_but_not_by_100_are_leap_years(self): assert is_leap_year(2016) assert is_leap_year(1984) assert is_leap_year(4) @test def years_divisible_by_100_but_not_by_400_are_not_leap_years(self): for year in range(100, 2101, 100): if year % 400 != 0: assert not is_leap_year(year) @test def years_divisible_by_400_are_leap_years(self): for year in range(400, 2401, 400): assert is_leap_year(year)
  • 103. class LeapYearSpec: @test def years_not_divisible_by_4_are_not_leap_years(self): assert not is_leap_year(2015) assert not is_leap_year(1999) assert not is_leap_year(1) @test def years_divisible_by_4_but_not_by_100_are_leap_years(self): assert is_leap_year(2016) assert is_leap_year(1984) assert is_leap_year(4) @test def years_divisible_by_100_but_not_by_400_are_not_leap_years(self): for year in range(100, 2101, 100): if year % 400 != 0: assert not is_leap_year(year) @test def years_divisible_by_400_are_leap_years(self): for year in range(400, 2401, 400): assert is_leap_year(year)
  • 104. def test(function): function.is_test = True return function def data(*values): def prepender(function): function.data = values + getattr(function, 'data', ()) return function return prepender def check(suite): tests = [ attr for attr in (getattr(suite, name) for name in dir(suite)) if callable(attr) and hasattr(attr, 'is_test')] for to_test in tests: try: if hasattr(to_test, 'data'): for value in to_test.data: call = '(' + str(value) + ')' to_test(suite(), value) else: call = '()' to_test(suite()) except: print('Failed: ' + to_test.__name__ + call)
  • 105. class LeapYearSpec: @test @data(2015) @data(1999) @data(1) def years_not_divisible_by_4_are_not_leap_years(self, year): assert not is_leap_year(year) @test def years_divisible_by_4_but_not_by_100_are_leap_years(self): assert is_leap_year(2016) @test def years_divisible_by_100_but_not_by_400_are_not_leap_years(self): assert not is_leap_year(1900) @test def years_divisible_by_400_are_leap_years(self): assert is_leap_year(2000)
  • 106. class LeapYearSpec: @test @data(2015) @data(1999) @data(1) def years_not_divisible_by_4_are_not_leap_years(self, year): assert not is_leap_year(year) @test @data(2016, 1984, 4) def years_divisible_by_4_but_not_by_100_are_leap_years(self, year): assert is_leap_year(year) @test def years_divisible_by_100_but_not_by_400_are_not_leap_years(self): assert not is_leap_year(1900) @test def years_divisible_by_400_are_leap_years(self): assert is_leap_year(2000)
  • 107. class LeapYearSpec: @test @data(2015) @data(1999) @data(1) def years_not_divisible_by_4_are_not_leap_years(self, year): assert not is_leap_year(year) @test @data(2016, 1984, 4) def years_divisible_by_4_but_not_by_100_are_leap_years(self, year): assert is_leap_year(year) @test def years_divisible_by_100_but_not_by_400_are_not_leap_years(self): assert not is_leap_year(1900) @test @data(*range(400, 2401, 400)) def years_divisible_by_400_are_leap_years(self, year): assert is_leap_year(year)
  • 108. class LeapYearSpec: @test @data(2015) @data(1999) @data(1) def years_not_divisible_by_4_are_not_leap_years(self, year): assert not is_leap_year(year) @test @data(2016, 1984, 4) def years_divisible_by_4_but_not_by_100_are_leap_years(self, year): assert is_leap_year(year) @test @data(*(year for year in range(100, 2101, 100) if year % 400 != 0)) def years_divisible_by_100_but_not_by_400_are_not_leap_years(self, year): assert not is_leap_year(year) @test @data(*range(400, 2401, 400)) def years_divisible_by_400_are_leap_years(self, year): assert is_leap_year(year)
  • 109. class LeapYearSpec: @test @data(2015) @data(1999) @data(1) def years_not_divisible_by_4_are_not_leap_years(self, year): assert not is_leap_year(year) @test @data(2016, 1984, 4) def years_divisible_by_4_but_not_by_100_are_leap_years(self, year): assert is_leap_year(year) @test @data(*range(100, 2101, 400)) @data(*range(200, 2101, 400)) @data(*range(300, 2101, 400)) def years_divisible_by_100_but_not_by_400_are_not_leap_years(self, year): assert not is_leap_year(year) @test @data(*range(400, 2401, 400)) def years_divisible_by_400_are_leap_years(self, year): assert is_leap_year(year)
  • 110. class LeapYearSpec: @test @data(2015) @data(1999) @data(1) def years_not_divisible_by_4_are_not_leap_years(self, year): assert not is_leap_year(year) @test @data(2016, 1984, 4) def years_divisible_by_4_but_not_by_100_are_leap_years(self, year): assert is_leap_year(year) @test @data(*range(100, 2101, 400)) @data(*range(200, 2101, 400)) @data(*range(300, 2101, 400)) def years_divisible_by_100_but_not_by_400_are_not_leap_years(self, year): assert not is_leap_year(year) @test @data(*range(400, 2401, 400)) def years_divisible_by_400_are_leap_years(self, year): assert is_leap_year(year)
  • 111. class LeapYearSpec: @test @data(2015) @data(1999) @data(1) def years_not_divisible_by_4_are_not_leap_years(self, year): assert not is_leap_year(year) @test @data(2016, 1984, 4) def years_divisible_by_4_but_not_by_100_are_leap_years(self, year): assert is_leap_year(year) @test @data(*range(100, 2101, 400)) @data(*range(200, 2101, 400)) @data(*range(300, 2101, 400)) def years_divisible_by_100_but_not_by_400_are_not_leap_years(self, year): assert not is_leap_year(year) @test @data(*range(400, 2401, 400)) def years_divisible_by_400_are_leap_years(self, year): assert is_leap_year(year)
  • 112. Express intention Naming Grouping and nesting of code usage Realisation of intent with respect to data One exemplar or many Explicit or generated
  • 115. Our task is not to find the maximum amount of content in a work of art. Our task is to cut back content so that we can see the thing at all. Susan Sontag