nix-playgraound/notes/033-haskell-traverse-resolution.md

70 lines
1.9 KiB
Markdown
Raw Permalink Normal View History

# Haskell Traverse for Batch Resolution
This note covers `30-haskell-traverse-resolution/`, which resolves a batch of deployment requests against a service catalog with `traverse`.
---
## 1. Why `traverse` Is the Point
Each deployment request needs an effectful resolution step:
- parse the environment,
- look up the service in the catalog,
- choose the right image tag, and
- reject environment and release-track combinations that are not allowed.
For one request, that is just `Either String ResolvedDeployment`.
For a whole batch, the example uses:
```haskell
resolveRequests :: Catalog -> [DeploymentRequest] -> Either String [ResolvedDeployment]
resolveRequests serviceCatalog = traverse (resolveRequest serviceCatalog)
```
That is the core lesson. `traverse` sequences the per-item effect and preserves the list structure.
---
## 2. What Makes the Example Non-Trivial
The resolution step is more than one lookup.
It decides:
- which image tag to use for `stable` versus `candidate`,
- whether the service even publishes a candidate image, and
- whether the service is allowed in production at all.
That makes the batch resolution behavior worth teaching on its own.
---
## 3. Why the Batch Fails as a Unit
This example uses `Either`, so the whole batch fails on the first invalid request.
That is intentional. The goal here is not accumulated validation. The goal is sequencing several effectful resolutions through one structure with
`traverse`.
`28-haskell-applicative-validation/` already covers the accumulated-error case.
---
## 4. Commands to Try
```bash
cd 30-haskell-traverse-resolution
nix develop
cabal run
cabal run -- api:production:stable worker:staging:candidate auth:production:stable
cabal test
nix build
./result/bin/mini-resolution api:production:stable worker:staging:candidate auth:production:stable
nix run . -- api:production:stable worker:staging:candidate auth:production:stable
nix flake check
```