<//>在Haskell中是什么意思?

时间:2019-08-25 13:53:00

标签: haskell

从使用Spock的简单服务器中的以下代码中:

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Web.Spock
import Web.Spock.Config

import Data.Time.Clock
import Control.Concurrent
import Network.HTTP.Types.Status
import Network.HTTP.Types.URI
import Control.Monad.Trans
import Control.Concurrent.STM 
import qualified Data.Text as T   

app :: MyApp ()
    app =
        do get root $ redirect' "https://google.no"
           -- Store params
           get ("oauth2" <//> var) $ \path' ->
             do ... 

大多数进口与该问题无关。

1 个答案:

答案 0 :(得分:2)

这是(<//>) :: Path as Open -> Path bs ps -> Path (Append as bs) ps function。如文档所述:

  

组合两个路径分量

source code [GitHub]中,我们看到它的实现方式为:

(<//>) :: Path as 'Open -> Path bs ps -> Path (Append as bs) ps
(<//>) = (</>)

(</>)函数源自(</>) :: Path as Open -> Path bs ps -> Path (Append as bs) ps包中的reroute。是implemented as [GitHub]

(</>) :: Path as 'Open -> Path bs ps2 -> Path (Append as bs) ps2
(</>) Empty xs = xs
(</>) (StaticCons pathPiece xs) ys = StaticCons pathPiece (xs </> ys)
(</>) (VarCons xs) ys = VarCons (xs </> ys)

因此,它基本上将一些路径片段附加在一起。您可以在此处将其视为某种形式的链接列表。字符串文字(如"oauth2")可以与OverloadedStrings一起转换为Path,因为它是instance of the IsString class [GitHub]

instance (a ~ '[], pathState ~ 'Open) => IsString (Path a pathState) where
    fromString = static

每次路径的一部分都会生成一个StaticCons(由于"oauth2"不包含任何斜杠,所以只有一个块):

static :: String -> Path '[] 'Open
static s =
  let pieces = filter (not . T.null) $ T.splitOn "/" $ T.pack s
  in foldr StaticCons Empty pieces