knrafto / language-bash

Parse and pretty-print Bash shell scripts
BSD 3-Clause "New" or "Revised" License
35 stars 9 forks source link

Provide optics? #38

Open hasufell opened 4 years ago

hasufell commented 4 years ago

I tried to get all top-level assignments and ended up with:

extractAssignments :: List -> [Assign]
extractAssignments (List stms) = join $ fmap getAssign $ getCommands stms
 where
  getCommands :: [Statement] -> [Command]
  getCommands = join . fmap commands . catMaybes . fmap findPipes
   where
    findPipes (Statement (Last p@(Pipeline{})) Sequential) = Just p
    findPipes _ = Nothing

  getAssign :: Command -> [Assign]
  getAssign (Command (SimpleCommand ass _) _) = ass
  getAssign _ = []

I believe this would be easier with optics. I would propose the optics package, because it has better documentation, an opaque interface with useful type errors and safer behavior in some corner cases (e.g. less implicit monoidal behavior) than lens.