ais
ais

Reputation: 2624

What is the best way to combine local and global state

I have this global State

type GlobalState a = State Int a

but one function needs its own local state and access to the GlobalState

type LocalState a = State [String] a

But I'm not sure now to combine them.

Right now I just add local state to the global

type GlobalState a = State (Int, [String]) a

It works fine, but I don't think it's right because I only need local state in one function. Is there a better way?

Upvotes: 1

Views: 290

Answers (1)

arrowd
arrowd

Reputation: 34391

You may use a monad stack of two States:

type LocalState a = [String]
type GlobalState a = [String]
newtype MyState a = StateT GlobalState (State LocalState) a

Upvotes: 2

Related Questions