繁体   English   中英

F#Seq.tryPick并将函数应用于未被挑选的成员

[英]F# Seq.tryPick and apply function to members that were not picked

这有可能吗? 我想使用tryPick,然后继续使用该集合的“休息”。

我只能想到丑陋的方法来做到这一点:

let sequence = Seq.init 5 (fun (i) -> i)
let pickedMember = 
    sequence
    |> Seq.tryPick (fun (i) ->
                        if i = 2 then
                            Some i
                        else
                            None
                    );;
let sequenceWithoutPickedMember = sequence |> Seq.filter (fun (i) -> not(i=pickedMember.Value));;

你可以同时使用fold返回一个带有单独选项的元组和其余的fold

只要孤独是None你检查你的谓词(这里与2相等)
如果它是真的(如果有的话),你将单独的一个更新为该项并从序列中跳过该项
否则你只需该项添加到生成的“休息序列”中

我使用谓词参数使其成为更通用的函数:

let firstAndRest predicate source =
  source
  |> Seq.fold (fun (lone, rest) item -> if Option.isNone lone && predicate item
                                        then Some item, rest
                                        else lone, seq { yield! rest; yield item })
     (None, Seq.empty)

// usage with example given
let sequence = Seq.init 5 id

let pickedMember, sequenceWithoutPickedMember = firstAndRest ((=) 2) sequence

// pickedMember : Some 2
// sequenceWithoutPickedMember : seq [0; 1; 3; 4]

编辑

由于嵌套序列表达式与我有关(它可以为长序列保留很多枚举器),这里是使用索引的替代方法。

想法是找到该项目的索引(如果有的话)而不是项目本身。
完成后我们得到该索引处的项目并将序列拆分为两部分。
after部分将包含找到的项目,所以我们需要丢弃它(通过跳过一个项目)
如果找不到该项目,我们只会回复原始来源。

let firstAndRest predicate source =
  let source = Seq.cache source // to prevent yielding different values for side-effect sequences

  match Seq.tryFindIndex predicate source with
    Some index -> let picked = source |> Seq.item index |> Some

                  let before, after = Seq.take index source, Seq.skip (index + 1) source
                  picked, Seq.append before after
  | None       -> None, source

根据您的评论,您说:

我想从一个集合中选择一个成员,但是然后还将一个函数应用于未被挑选的每个成员

我会通过创建一个新的函数tryPickOrApplyFunc来解决这个问题,该函数折叠集合,返回适当的值或应用提供的函数。

/// Applies the supplied choosing function to successive elements, returning the first result where the function returns `Some (x)` and applies the supplied function `f` to all other elements.
let tryPickOrApplyFunc chooser f sequ =
    let folder acc v =
        match acc, chooser v with
        |Some x, _ -> // We have already picked an element - apply the function to v and return the already picked element
            f v
            Some x
        |None, Some y -> // We haven't picked a value and the chooser function returns `Some (x)` - Pick the value 
            Some v
        |None, None -> // We haven't picked a value and the chooser function returns `None` - apply the function and return `None`
            f v
            None
    Seq.fold (folder) None sequ

示例用法 - 选择值或打印不匹配的结果:

 tryPickOrApplyFunc (fun x -> if x = 3 then Some x else None) (printfn "%d") [1; 2; 3; 4; 5; 6;];; 1 2 4 5 6 val it : int option = Some 3 

或者,如果我们超出值的范围:

 tryPickOrApplyFunc (fun x -> if x = 7 then Some x else None) (printfn "%d") [1; 2; 3; 4; 5; 6;];; 1 2 3 4 5 6 val it : int option = None 

如果您想要返回一组未挑选的值,我会将其构建为一个列表,并建议切换到foldBack以便您按原始顺序获取值:

/// Applies the supplied choosing function to successive elements, returning the first result where the function returns `Some (x)` and a sequence of all the other elements.
let tryPickWithRemaining chooser sequ =
    let folder v acc =
        match acc, chooser v with
        |(Some x, lst), _ -> // We have already picked an element - return the already picked element and append v to the list of remaining values
            Some x, v::lst
        |(None, lst), Some y -> // We haven't picked a value and the chooser function returns `Some (x)` - Pick the value and keep the list of remaining values as it is
            Some v, lst
        |(None, lst), None -> // We haven't picked a value and the chooser function returns `None` - return `None` append v to the list of remaining values
            None, v::lst
    let pick, lst = Seq.foldBack (folder) sequ (None, []) 
    pick, Seq.ofList lst

Seq.tryPick返回None时,您的示例中不清楚您想要做什么。 我假设整个表达式将返回'None'并且函数签名chooser:('a -> 'b option) -> source:seq<'a> -> ('b * 'a list) option是您的用例可以接受。

你将获得一个元组的选项,包含选中的值,其余的作为列表。 它不需要是一个序列,因为您需要急切地使用整个输入序列来返回结果。 懒惰消失了,我们也可以使用一个列表。

let tryPickWithRest chooser source =
    source
    |> Seq.fold (fun (picked, rest) x ->
        match picked, chooser x with
        | None, Some newlyPicked -> Some newlyPicked, rest
        | _ -> picked, x::rest ) (None, [])
    |> function
    | Some picked, rest -> Some(picked, List.rev rest)
    | _ -> None

[0..4]
|> tryPickWithRest (fun i -> if i = 2 then Some "found 2" else None)
// val it : (string * int list) option = Some ("found 2", [0; 1; 3; 4])

编辑

现在你已经澄清了你想要在任何情况下评估选择选项和过滤列表的元组,我们只是借用了TheInnerLight的概念并将其缩短。 当然,所以最后将是第一个。

 let tryPickWithRest' chooser source = Seq.foldBack (fun x (picked, rest) -> match picked, chooser x with | None, Some newlyPicked -> Some newlyPicked, rest | _ -> picked, x::rest ) source (None, []) [-4..0] |> tryPickWithRest' (fun i -> if i >= 2 then Some "found 2" else None) // val it : string option * int list = (null, [-4; -3; -2; -1; 0]) 

List.partitionArray.partition做你想要的吗?

> [1..5] |> List.partition ((=) 2);;
val it : int list * int list = ([2], [1; 3; 4; 5])

您可以使用Seq.toListSet.toArray将所有有限序列转换为列表或数组,然后使用适当的partition函数。 但是, 没有Seq.partition功能

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM