Can I write this function without doing the action on a different line?

1. packageBase <- do 2. curDir <- getCurrentDirectory 3. return . last . splitDirectories . normalise . joinPath $ curDir:maybeToList package

On Sat, Apr 5, 2014, at 11:31 PM, Matt Joiner wrote: 1. packageBase <- do 2. curDir <- getCurrentDirectory 3. return . last . splitDirectories . normalise . joinPath $ curDir:ma ybeToList package Sure. The most obvious transformation to me would have these three steps: 1. The right argument of (:) isn't coming from a monadic action, so you can extract it as a section and move it to your big long chain of compositions: return . last . splitDirectories . normalise . joinPath . (: maybeToList package) $ curDir 2. Get rid of the binding of curDir: getCurrentDirectory >>= return . last . splitDirectories . normalise . joinPath . (: maybeToList package) 3. Notice that the right argument of (>>=) really isn't using your monad, so you could use fmap or (<$>) (assuming this is a well-behaved monad which also has a Functor instance): last . splitDirectories . normalise . joinPath . (: maybeToList package) <$> getCurrentDirectory Fair warning: I'm tired and not type-checking this. But it looks right to me. -Karl

On Sun, Apr 6, 2014 at 2:15 PM, Karl Voelker
last . splitDirectories . normalise . joinPath . (: maybeToList package) <$> getCurrentDirectory
The thing to notice is that we really want to write getCurrentDirectory : maybeToList package but the effect-typing won't let us. This kludge: let x = flip (<$>) in getCurrentDirectory `x` (: maybeToList package) restores the smile in our haskell, where 'x' can be as imperceptibly unicoded as desired. -- Kim-Ee
participants (3)
-
Karl Voelker
-
Kim-Ee Yeoh
-
Matt Joiner