GitHub

Circular successor & predecessor for bounded enum types

Build and test API docs Hackage: circular-enum

import Data.Enum.Circular
data Direction = N | E | S | W deriving (Show, Eq, Enum, Bounded)
show $ take 6 (iterate csucc N)
-- "[N,E,S,W,N,E]"

csucc and cpred are compatible with succ and pred, but they behave circular on the type boundaries. Requires Eq, Enum and Bounded instances.

Note: Designed for small, finite enum types with deriving Enum. Not suitable for Int, Word, or other types with very large ranges, as the internal arithmetic overflows.

You can also use the Circular newtype:

type CDirection = Circular Direction
show $ take 6 (iterate succ (Circular N))

Contributors

Contributor Covenant 2.0

Author and License

Copyright (c) 2023-2026 Mirko Westermeier (@memowe)

Released under the MIT license. See LICENSE for details.

Read the original on github.com ↗