List: Every Result of Every Branch

Each step can give several results, and the list keeps them all, in order.

List at a glance

ModelsA computation with several results, such as every shipping option or every product variant
ReplacesNothing: it is Java's own type
Effect PathNonDetPath for every combination, or ListPath, whose zipWith pairs by position
InstancesFunctor, Applicative, Selective, Monad, Alternative, MonadZero, Foldable, Traverse
Choose it whenNested loops build every combination, and you keep them all in memory; otherwise Stream, which makes each element as it is read and can be read only once, or compare every type in Choosing a Type

What You'll Learn

  • Find every way to ship an order by letting each courier branch into its services
  • Drop a branch by giving no results for it, and filter what is left
  • Choose between every combination, from NonDetPath, and pairing by position, from ListPath
  • Predict what of, map and flatMap do with null, on a Path and in generic code
  • Use List in generic code through its MonadZero instance

See Example Code

The code on this page is ListBook.java: the page includes it, so the build compiles and runs it. ListBookTest.java holds what the page says each method does.


Every way to ship an order

An order can go with either of two couriers, and each courier offers several services. To find every service within a budget, plain Java nests a loop over the services inside a loop over the couriers, and adds each match to a list. With streams it is COURIERS.stream().flatMap(courier -> servicesOf(courier).stream()), then a filter. A list used as a computation does the same: each step gives a list of results, and the next step runs once for each of them. Its Path adds a zipWith that gives every combination, and conversions to the other Paths.

Two Paths over a list

Path.list(list) gives a NonDetPath, short for non-deterministic: it keeps every possible result. Path.listPath(list) gives a ListPath. Their map and via agree. Only zipWith and zipWith3 differ: a NonDetPath gives every combination, and a ListPath pairs by position. Start with NonDetPath, which matches the List instance's ap.

The code on this page uses these imports:

import java.math.BigDecimal;
import java.util.List;
import org.higherkindedj.hkt.effect.NonDetPath;
import org.higherkindedj.hkt.effect.Path;

Each courier's services, with their prices:

  // A courier's service, and what it charges
  record Service(String courier, String name, BigDecimal price) {}

  static final List<String> COURIERS = List.of("Parcelforce", "DPD");

  // Each courier offers several services
  static List<Service> servicesOf(String courier) {
    return switch (courier) {
      case "Parcelforce" ->
          List.of(
              new Service(courier, "standard", new BigDecimal("4.99")),
              new Service(courier, "express", new BigDecimal("9.99")));
      case "DPD" ->
          List.of(
              new Service(courier, "next day", new BigDecimal("7.49")),
              new Service(courier, "timed", new BigDecimal("12.99")));
      default -> List.of();
    };
  }

Path.list(list) gives a NonDetPath. Its via runs the next step for each element and joins every result into one list, and filter keeps those that pass:

  // Every service from every courier, and only those within the budget
  static NonDetPath<Service> optionsWithin(BigDecimal budget) {
    return Path.list(COURIERS)
        .via(courier -> Path.list(servicesOf(courier)))
        .filter(service -> service.price().compareTo(budget) <= 0);
  }

    List<String> underTen =
        optionsWithin(new BigDecimal("10.00")).map(s -> s.courier() + " " + s.name()).run();
    // [Parcelforce standard, Parcelforce express, DPD next day]

Each result branches

via gives the next step each result in turn, and keeps the results in order: all of the first courier's services, then all of the second's.

flowchart TB
    accTitle: How optionsWithin branches on couriers and services
    accDescr: The couriers Parcelforce and DPD each branch into their services through via. Parcelforce gives standard at 4.99 and express at 9.99, and DPD gives next day at 7.49 and timed at 12.99. The filter drops timed, which is over 10.00, and the three services left come back in order in one list.
    couriers["COURIERS<br/>via servicesOf"]
    pf["Parcelforce"]
    dpd["DPD"]
    standard["standard<br/>4.99"]
    express["express<br/>9.99"]
    nextDay["next day<br/>7.49"]
    timed["timed<br/>12.99, dropped"]
    result["filter keeps three<br/>in one list, in order"]
    couriers --> pf
    couriers --> dpd
    pf --> standard
    pf --> express
    dpd --> nextDay
    dpd --> timed
    standard --> result
    express --> result
    nextDay --> result
    classDef domain fill:#a6d189,stroke:#40a02b,color:#232634
    classDef error fill:#e78284,stroke:#d20f39,color:#232634
    class couriers,pf,dpd,standard,express,nextDay,result domain
    class timed error

Each courier branches into its services, filter drops the one over the budget, and the rest are joined into one list, in order. A step that gives an empty list, such as Path.listEmpty(), drops its branch the same way, since it adds nothing to the result.

Every combination, or position by position

zipWith combines two lists, and the two Paths combine them differently. On a NonDetPath it pairs each element of the first list with each element of the second, so two colours and two shades give four variants:

    // Every colour with every shade
    List<String> variants =
        Path.list("black", "brass")
            .zipWith(Path.list("linen", "glass"), (colour, shade) -> colour + "/" + shade)
            .run();
    // [black/linen, black/glass, brass/linen, brass/glass]

On a ListPath, from Path.listPath(list), it pairs the elements by position, and the shorter list sets the length:

    // Each line with the day it is dispatched, position by position
    List<String> dispatched =
        Path.listPath("LAMP", "BULB", "SHADE")
            .zipWith(Path.listPath("Monday", "Tuesday"), (sku, day) -> sku + " on " + day)
            .run();
    // [LAMP on Monday, BULB on Tuesday]

SHADE, the third line, has no day, so it drops out. zipWith3 differs between the two Paths in the same way.

A list holds what a Java list holds

The Paths refuse null. Path.list and Path.listPath throw NullPointerException for a list that holds one, and so do map and via when their function returns null. The instance over Kind follows Java's List, which holds null if you put one there. Its map keeps a null that a function returns, and its of(null) gives an empty list, as Optional's and Stream's do. A flatMap function must return a list, and one that returns null throws KindUnwrapException. Where null is read or held sets out the rules for each type.

Creating instances

Path.list(list) and Path.list(a, b, c) start a NonDetPath, Path.listPure(value) one with a single element, and Path.listEmpty() an empty one. Path.listPath(...), Path.listPathPure(value), Path.listPathEmpty() and Path.listPathRange(from, to) start a ListPath. toNonDetPath() turns a ListPath into a NonDetPath, and toListPath() turns a NonDetPath back into a ListPath.

Methods

MethodWhat it does
nonDetPath.via(f), listPath.via(f)Run f for each element, and join every result into one list
map(f), filter(p)Change each element, or keep those that pass
nonDetPath.zipWith(other, f), zipWith3(b, c, f)Combine every element with every element of the others
listPath.zipWith(other, f), zipWith3(b, c, f)Combine elements by position, up to the shortest list
take(n), drop(n), takeWhile(p), dropWhile(p)Keep or skip a prefix
foldLeft(initial, f), foldMap(monoid, f)Fold the elements into one value
headOption(), lastOption(), get(index), size(), isEmpty()Read the list
run()Gives the List

Working with Kind

Generic code, written once for any container, takes List through its MonadZero instance, from Instances.monadZero(list()). Its flatMap is via, and its ap applies every function to every value, as a NonDetPath's zipWith does. zero() is the empty list, and the Alternative from Instances.alternative(list()) joins two lists with orElse. These are the extra imports:

import static org.higherkindedj.hkt.instances.Witnesses.list;
import static org.higherkindedj.hkt.list.ListKindHelper.LIST;

import java.util.function.Function;
import org.higherkindedj.hkt.Kind;
import org.higherkindedj.hkt.MonadZero;
import org.higherkindedj.hkt.instances.Instances;
import org.higherkindedj.hkt.list.ListKind;
    MonadZero<ListKind.Witness> lists = Instances.monadZero(list());

    Kind<ListKind.Witness, String> couriers = LIST.widen(COURIERS);
    Function<String, Kind<ListKind.Witness, Service>> services =
        courier -> LIST.widen(servicesOf(courier));

    int offered = LIST.narrow(lists.flatMap(services, couriers)).size();
    // 4

List is a JDK type, so widen wraps it in a small holder, and narrow gives back the same list. ListTraverse.INSTANCE traverses a list with any Applicative, as Foldable and Traverse shows.

When to use List

You needUse
Every result of several branching choicesList, or NonDetPath
Two lists paired element by elementListPath's zipWith
A long or endless sequence, read once as it is producedStream, or VStream when producing an element does I/O
A list that must hold at least one elementNonEmptyList
A loop over one list in one methodA for loop, or stream(): nothing else needs the structure

Hands-On Learning

Practise List's map in Exercise 3 of Tutorial 02: Functor Mapping (7 exercises), and its flatMap in Exercise 6 of Tutorial 04: Monad Chaining (8 exercises).


Previous: Writer Next: Stream