Elm 将列表分为两部分以上

Elm 将列表分为两部分以上,elm,Elm,因此,我想将Elm中的列表项模型划分为列表(列表项模型)列表。分区仅将列表分成两个列表 我写了一些代码,使列表成为我想要的部分(下面的代码) 但这并不像我所希望的那样是一个好的解决方案,而且因为这似乎是许多人都会遇到的问题,我想知道有没有更好的例子可以做到这一点 partition : List (ItemModel -> Bool) -> List ItemModel -> List (List ItemModel) partition filters models =

因此,我想将Elm中的
列表项模型
划分为
列表(列表项模型)
<代码>列表。分区仅将列表分成两个列表

我写了一些代码,使列表成为我想要的部分(下面的代码)

但这并不像我所希望的那样是一个好的解决方案,而且因为这似乎是许多人都会遇到的问题,我想知道有没有更好的例子可以做到这一点

partition : List (ItemModel -> Bool) -> List ItemModel -> List (List ItemModel)
partition filters models =
    let
        filterMaybe =
            List.head filters
    in
        case filterMaybe of
            Just filter ->
                let
                    part =
                        Tuple.first (List.partition filter models)
                in
                    part :: (partition (List.drop 1 filters) models)

            Nothing ->
                []

返回的列表直接从
filters
参数映射而来,因此使用
list.map
list.filter
(这就是您真正要做的,因为您正在丢弃从
list.partition
返回的剩余列表):

multifilter:List(a->Bool)->List a->List(a)
多重滤波器值=
过滤器|>List.map(\filter->List.filter过滤器值)

重复分区需要使用每个步骤的剩余部分作为下一步的输入。这不同于由多个滤波器对同一序列进行简单的重复滤波

在Haskell(这个问题最初也被标记为Haskell)中

分区::[a->Bool]->[a]->[a]]
分区preds xs=go preds xs
哪里
go[]xs=[]
go(p:ps)xs=let{(a,b)=分区pxs}in(a:go-psb)
也就是说,

partitions preds xs=foldr g(const[])preds xs
哪里
gprxs=let{(a,b)=分区pxs}in(a:rb)

--mapAccumL::(acc->x->(acc,y))->acc->[x]->(acc,[y])
partitions preds xs=snd$mapacuml(\xs p->partition(not.p)xs)xs preds
测试:

>分区[(