Circular successor & predecessor for bounded enum types
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 forInt,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
Author and License
Copyright (c) 2023-2026 Mirko Westermeier (@memowe)
Released under the MIT license. See LICENSE for details.