break
有签名[a] -> (a -> Bool) -> ([a], [a])
,其中第一个元组等于我理解为takeWhile predicate is true
。第二个元组是负责使谓词为假加上剩余列表的项目。
> break (== ' ') "hey there bro"
("hey"," there bro")
但是,是否有一个函数会跳过负责破解的项目?
>foo? (== ' ') "hey there bro"
("hey","there bro")
答案 0 :(得分:7)
不在标准库中,但您可以使用drop 1
实例对元组的第二个元素方便地Functor
:
break (== ' ') "hey there bro"
== ("hey"," there bro")
drop 1 <$> break (== ' ') "hey there bro"
== ("hey","there bro")
<$>
是fmap
的中缀同义词。使用drop 1
代替tail
处理空后缀的情况:
drop 1 <$> break (== ' ') "hey"
== ("hey","")
tail <$> break (== ' ') "hey"
== ("hey","*** Exception: Prelude.tail: empty list
在使用元组时,我通常更喜欢使用second
Control.Arrow
而不是fmap
,因为它更好地表达了意图:
second (drop 1) $ break (== ' ') "hey there bro"
== ("hey","there bro")