Skip to content

Instantly share code, notes, and snippets.

@danidiaz
Created February 7, 2014 21:04
Show Gist options
  • Select an option

  • Save danidiaz/8871821 to your computer and use it in GitHub Desktop.

Select an option

Save danidiaz/8871821 to your computer and use it in GitHub Desktop.
Defining lenses and prisms without depending on lens.
streams :: forall f. Functor f => ((StdStream,StdStream,StdStream) -> f (StdStream,StdStream,StdStream)) -> CreateProcess -> f CreateProcess
streams f c = setStreams c <$> f (getStreams c)
where
getStreams c = (std_in c,std_out c, std_err c)
setStreams c (s1,s2,s3) = c { std_in = s1
, std_out = s2
, std_err = s3
}
pipe3 :: (StdStream,StdStream,StdStream)
pipe3 = (CreatePipe,CreatePipe,CreatePipe)
handle3 :: forall m. Applicative m => ((Handle, Handle, Handle, ProcessHandle) -> m (Handle, Handle, Handle, ProcessHandle)) -> (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> m (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
handle3 f quad = case impure quad of
Left l -> pure l
Right r -> fmap justify (f r)
where
impure (Just h1, Just h2, Just h3, phandle) = Right (h1, h2, h3, phandle)
impure x = Left x
justify (h1, h2, h3, phandle) = (Just h1, Just h2, Just h3, phandle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment