玩箭头(->)

时间:2019-02-10 16:58:43

标签: haskell

这里是上下文:

data MonitoringCommand = ListCommandReceived
                       | ListCommandResponseReceived
                       | ListEventsGenerated
                       | ListValidationStateHistory


runMonitoringCommand :: forall stepType. MonitoringCommand ->
                                           Step stepType ->
                                           ClientSetting ->
                                           Workspace ->
                                           Byline IO (Either StepError (Step stepType))
runMonitoringCommand monitoringCommand currentStep settings Workspace {workspaceId} =
  displayCallResult currentStep (case monitoringCommand of
    ListCommandReceived -> "Commands"  (streamGsdCommandByWorkspaceId settings workspaceId)
    ListCommandResponseReceived -> "Command Responses" (streamGsdCommandResponseByWorkspaceId settings workspaceId)
    ListEventsGenerated -> "Events" (streamGsdEventByWorkspaceId settings workspaceId)
    ListValidationStateHistory -> "Validation State History" (streamGsdValidationStateByWorkspaceId settings workspaceId))


displayCallResult :: forall item stepType. Show item =>
                                             Step stepType ->
                                             String ->
                                             SerialT IO (SafeResponse (Persisted item)) ->
                                             Byline IO (Either StepError (Step stepType))
displayCallResult currentStep itemName clientCall = do
  displayBeginningOfACommand
  sayLn $ fg white <> (text . pack) itemName
  safeResponse <- liftIO $ StreamlySafe.toList $ clientCall
  nextStep <- case safeResponse of
      Left error -> return $ Left $ StepError { currentStep, errorDescription = show error }
      Right persistedItems -> persistedItems & mapM (sayLn . displayItem) >> (return $ Right currentStep)
  displayEndOfACommand
  return nextStep
  where
    displayItem :: Show item => (Persisted item) -> Stylized
    displayItem PersistedItem {offset, item} =
      fg white <> "  "<>(text.pack.show) offset <> "- " <> fg green <> (text.pack.show) item

在函数“ runMonitoringCommand”中,我想以某种方式向(displayCallResult currentStep)添加2个参数,我们可以这样做吗?

而不是像这样重复“ displayCallResult currentStep”:

runMonitoringCommand :: forall stepType. MonitoringCommand ->
                                           Step stepType ->
                                           ClientSetting ->
                                           Workspace ->
                                           Byline IO (Either StepError (Step stepType))
runMonitoringCommand monitoringCommand currentStep settings Workspace {workspaceId} =
  (case monitoringCommand of
    ListCommandReceived ->         displayCallResult currentStep  "Commands"                 (streamGsdCommandByWorkspaceId settings workspaceId)
    ListCommandResponseReceived -> displayCallResult currentStep  "Command Responses"        (streamGsdCommandResponseByWorkspaceId settings workspaceId)
    ListEventsGenerated ->         displayCallResult currentStep  "Events"                   (streamGsdEventByWorkspaceId settings workspaceId)
    ListValidationStateHistory ->  displayCallResult currentStep  "Validation State History" (streamGsdValidationStateByWorkspaceId settings workspaceId))

1 个答案:

答案 0 :(得分:1)

您可以使用uncurry

runMonitoringCommand monitoringCommand currentStep settings Workspace {workspaceId} =
  uncurry (displayCallResult currentStep) $ case monitoringCommand of
    ListCommandReceived         -> ("Commands",                 streamGsdCommandByWorkspaceId settings workspaceId)
    ListCommandResponseReceived -> ("Command Responses",        streamGsdCommandResponseByWorkspaceId settings workspaceId)
    ListEventsGenerated         -> ("Events",                   streamGsdEventByWorkspaceId settings workspaceId)
    ListValidationStateHistory  -> ("Validation State History", streamGsdValidationStateByWorkspaceId settings workspaceId)