我正在浏览nubBy
Data.List中函数的源代码:
nubBy :: (a -> a -> Bool) -> [a] -> [a]
#ifdef USE_REPORT_PRELUDE
nubBy eq [] = []
nubBy eq (x:xs) = x : nubBy eq (filter (\ y -> not (eq x y)) xs)
#else
nubBy eq l = nubBy' l []
where
nubBy' [] _ = []
nubBy' (y:ys) xs
| elem_by eq y xs = nubBy' ys xs
| otherwise = y : nubBy' ys (y:xs)
Run Code Online (Sandbox Code Playgroud)
现在在我看来,上面的两个版本彼此不同.如果我拿这个USE_REPORT_PRELUDE
版本,我就知道了
nubBy (>) [1..10]
[1,2,3,4,5,6,7,8,9,10]
Run Code Online (Sandbox Code Playgroud)
而其他实施产生 …
我试图理解为什么Haskell会show
处理一个不同于整数列表的字符列表,即使没有FlexibleInstances
Pragma也是如此.
通读了文档Show
,我意识到我并不真正理解Haskell如何为类型类的实例选择方法.
请考虑以下代码:
class MyShow a where
myShow :: a -> String
myShowList :: [a] -> String
myShowTuple :: (a, b) -> String
myShowList xs = "Default List Implementation"
myShowTuple t = "Default Tuple Implementation"
instance MyShow Char where
myShow c = "One Char"
myShowList xs = "List of Chars"
myShowTuple t = "Char Tuple"
instance MyShow Int where
myShow n = "One Int"
myShowList xs = "List of Integers"
myShowTuple t = "Int …
Run Code Online (Sandbox Code Playgroud) Pyramid 中的 add_static_view(name, path) 如何工作?
从文档字符串:
“
name
参数是一个字符串,表示与应用程序相关的本地 URL 前缀。它也可以是完整的 URL。path
参数是静态文件所在磁盘上的路径。这可以是绝对路径、包相关路径或资产规范。”
不知何故,我觉得这个描述不是很准确。
如果我添加一些代码
config.add_static_view("static", "/path/to/resource/on/filesystem")
Run Code Online (Sandbox Code Playgroud)
我访问
http://localhost:PORT/static/logo.png
Run Code Online (Sandbox Code Playgroud)
我看到了 logo.png,因为它可以在
/path/to/resource/on/filesystem/
Run Code Online (Sandbox Code Playgroud)
现在,如果我有一些像下面这样的代码
config.add_static_view("http://myfilehoster.com/images", "myproject:images")
Run Code Online (Sandbox Code Playgroud)
“path
参数是静态文件所在磁盘上的路径”的描述似乎不再准确,因为实际文件驻留在 myfilehoster 的磁盘上。在我看来,我只是注册了某种标识符 (myproject:images),我可以在我的程序代码中使用它来引用“真实”位置“http://myfilehoster.com/images”。例如
request.static_url("myproject:images/logo.png")
Run Code Online (Sandbox Code Playgroud)
将被解析为“http://myfilehoster.com/images/logo.png”。
那么这里的文档不准确还是我遗漏了什么?
我正在努力做相同的事情
xdotool search "Chromium" windowactivate --sync key --clearmodifiers ctrl+r
Run Code Online (Sandbox Code Playgroud)
即我想切换到Chromium并重新加载当前页面.但是,使用XMonad,我收到以下错误:
Your windowmanager claims not to support _NET_ACTIVE_WINDOW, so the attempt to activate the window was aborted.
Run Code Online (Sandbox Code Playgroud)
在XMonad中有一种方法可以以编程方式切换到某个应用程序吗?
我正在使用Snap框架,我常常遇到这样的情况:我根据从表单字段获得的参数进行数据库查找.
考虑例如以下两个功能
getParam :: (MonadSnap m) => ByteString -> m (Maybe ByteString)
doLookup :: (MonadIO (m b v), MonadSnaplet m, MonadState s (m b b), HasAcid s UrlDB) => ByteString -> m b v (EventResult QueryByURL)
Run Code Online (Sandbox Code Playgroud)
其中UrlDB是整数和URL之间的映射.第二功能的复杂类型特征是由于使用酸状态并最终导致Maybe Integer
.
queryByURL :: Text -> Query UrlDB (Maybe Integer)
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的处理程序看起来像
indexHandler :: Handler MyApp MyApp ()
indexHandler = do
mUrl <- getParam "url"
case mUrl of
Nothing -> render "index"
Just url -> do
mId <- doLookup $ url
case mId …
Run Code Online (Sandbox Code Playgroud) 在Data.Tree.Zipper中,玫瑰树的拉链数据类型是
data TreePos t a = Loc
{ _content :: t a -- ^ The currently selected tree.
, _before :: Forest a
, _after :: Forest a
, _parents :: [(Forest a, a, Forest a)]
} deriving (Read,Show,Eq)
Run Code Online (Sandbox Code Playgroud)
现在我觉得_after和_before中的信息是多余的,因为它也应该出现在_parents字段中.(节点的兄弟姐妹是其父母的子女.)
为什么是这样?出于方便吗?