+ - 0:00:00
Notes for current slide
Notes for next slide

Functional Programming Kata : Paths

The objective of this Kata is to be a gentle introduction to Functional Programming in 1h30 - 2h.

We'll take 15 min to introduce the context, setup the groups and prepare the IDEs

We keep 15 min a the end to share the code and retrospect.

1 / 14

Functional Programming

In FP, we split our code between pure functional code and code with side effects (IO, modifying variables ...).

This Kata can be done with pure functional style (no modification of variables => all can be declared final).

FP means that we make heavy use of functions : they can be passed as arguments to other functions. See the syntax examples for suggestions.

Note : FP also means that a function can return a function but this is not needed here.

2 / 14

TDD

3 rules

  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

Source : Uncle Bob

3 / 14

Suggestion for the process

Always start by testing the limit condition(s) : empty list, value of zero ... This allows you to focus on the usage of the method and its signature.

Then add a more relevant test to check the implementation.

4 / 14

Context : search paths produced by an API

Imagine that some Google Maps like API provides possible paths from origin to destination.

Select the best one based on distance and intermediate stops.

Expect the API to produce only valid paths.

Alt text

We consider that a path is collection of segments, rather than a collection of points

5 / 14

TDD Steps

Suggestion of implementation steps, which should match with your tests.

7 (+1) steps

6 / 14

Steps 1-3 : Find the shortest path

Find the shortest path in a list of paths :

  • Compute the distance of a segment
  • Compute the distance of a path
  • Find the shortest path in a list of paths
7 / 14

Steps 4-6 : Filter paths

Filter paths keeping only those which include a given list of stops :

  • Enumerate all stops in a path
  • Filter paths keeping only those which include a stop
  • Filter paths keeping only those which include a given list of stops
8 / 14

Step 7 : Find the best path

Find the shortest path in a list of paths, which includes a given list of stops

9 / 14

Bonus Step

Compute all possible paths between 2 points, given a list of Segments.

This step is more tricky because it involves recursion so it is usually only reachable for the fastest groups who already have experience with Functional Programming.

10 / 14

FP samples : filter

11 / 14

FP samples : map

12 / 14

FP samples : flatMap

13 / 14

Syntax examples

See Github project for quickstart projects in different languages.

Look for Demo files for syntax examples.

14 / 14

Functional Programming

In FP, we split our code between pure functional code and code with side effects (IO, modifying variables ...).

This Kata can be done with pure functional style (no modification of variables => all can be declared final).

FP means that we make heavy use of functions : they can be passed as arguments to other functions. See the syntax examples for suggestions.

Note : FP also means that a function can return a function but this is not needed here.

2 / 14
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow