如何通过属性将 seq 附加到另一个?

发布于 2024-11-24 15:36:25 字数 534 浏览 2 评论 0原文

我在 FSharp 中有一个 seq of seq。如果谓词返回 true,我想将一个 seq 加入到前一个 seq 中。

示例:

let items = seq [seq[2;3;4];seq[1;5;6;7;1;9];seq[2;3;5;7]]

我想将一个 seq 加入到前一个 seq 中,如果 seq 以 1 开头,那么结果应该是这样的:

seq [seq[2;3;4;1;5;6;7; 1;9];seq[2;3;5;7]]

有什么好的功能方法可以做到这一点吗?

我刚刚开始将漫长的计算过程从 C# 转换为 F#,即使经过几个小时的工作以及我对 FSharp 的初级了解,我所实现的性能改进也给我留下了深刻的印象。

我从亚马逊买了一本名为“Beginning F#”的书。它真的很棒,但我现在主要应该使用序列、列表、映射、集合,并且这个主题没有像我需要的那样详细解释。有人愿意为我提供有关这些主题的好资源吗?

提前谢谢!

I have a seq of seqs in FSharp. I want to join a seq to the previous one if a predicate returns to true for it.

Sample:

let items = seq [seq[2;3;4];seq[1;5;6;7;1;9];seq[2;3;5;7]]

I want to join a seq to the previos one, if the seq starts by 1, so the result should be in this case:

seq [seq[2;3;4;1;5;6;7;1;9];seq[2;3;5;7]]

Is there any nice functional way to do it?

I am just started to translate my long computation processes from C# to F# and very impressed by the performance improvement I could achieve after even a very few hours of work and my beginner level knowledge of FSharp.

I have bought a book from Amazon entitled 'Beginning F#'. It is really great, but I mainly should work with seqs, lists, maps, collections now and this topic isn't explained as detailed as I need. Would anyone be so kind to advise me a good resource about ths topics?

Thx in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

耳根太软 2024-12-01 15:36:25
let joinBy f input =
  let i = ref 0
  input 
  |> Seq.groupBy (fun x ->
    if not (f x) then incr i
    !i)
  |> Seq.map (snd >> Seq.concat)

joinBy (Seq.head >> ((=) 1)) items
let joinBy f input =
  let i = ref 0
  input 
  |> Seq.groupBy (fun x ->
    if not (f x) then incr i
    !i)
  |> Seq.map (snd >> Seq.concat)

joinBy (Seq.head >> ((=) 1)) items
灼疼热情 2024-12-01 15:36:25

与您的最后一个问题一样,没有库函数可以完全执行此操作。最直接的解决方案是使用 IEnumerator 强制编写此代码。但是,您可以编写一个更通用的函数(然后也可以用于其他目的)。

module Seq =
  /// Iterates over elements of the input sequence and groups adjacent elements.
  /// A new group is started when the specified predicate holds about the element
  /// of the sequence (and at the beginning of the iteration).
  /// For example: 
  ///    Seq.groupWhen isOdd [3;3;2;4;1;2] = seq [[3]; [3; 2; 4]; [1; 2]]
  let groupWhen f (input:seq<_>) = seq {
    use en = input.GetEnumerator()
    let running = ref true

    // Generate a group starting with the current element. Stops generating
    // when it founds element such that 'f en.Current' is 'true'
    let rec group() = 
      [ yield en.Current
        if en.MoveNext() then
          if not (f en.Current) then yield! group() 
        else running := false ]

    if en.MoveNext() then
      // While there are still elements, start a new group
      while running.Value do
        yield group() }

为了解决原来的问题,你可以检查序列的第一个元素是否是 1 以外的数字。你将得到一个组序列,其中一个组是序列的序列 - 然后你可以连接这些组:

items 
  |> Seq.groupWhen (fun s -> Seq.head s <> 1)
  |> Seq.map Seq.concat

编辑:我还在此处发布了该函数作为代码片段(具有漂亮的 F# 格式):http://fssnip。净/6A

As with your last question, there is no library function that does exactly this. The most straightforward solution is to write this imperatively using IEnumerator. However, you can write a more generally useful function (that can then be used for other purposes too).

module Seq =
  /// Iterates over elements of the input sequence and groups adjacent elements.
  /// A new group is started when the specified predicate holds about the element
  /// of the sequence (and at the beginning of the iteration).
  /// For example: 
  ///    Seq.groupWhen isOdd [3;3;2;4;1;2] = seq [[3]; [3; 2; 4]; [1; 2]]
  let groupWhen f (input:seq<_>) = seq {
    use en = input.GetEnumerator()
    let running = ref true

    // Generate a group starting with the current element. Stops generating
    // when it founds element such that 'f en.Current' is 'true'
    let rec group() = 
      [ yield en.Current
        if en.MoveNext() then
          if not (f en.Current) then yield! group() 
        else running := false ]

    if en.MoveNext() then
      // While there are still elements, start a new group
      while running.Value do
        yield group() }

To solve the original problem, you can then check whether the first element of the sequence is a number other than 1. You'll get a sequence of groups where a group is sequence of sequences - then you can just concatenate the groups:

items 
  |> Seq.groupWhen (fun s -> Seq.head s <> 1)
  |> Seq.map Seq.concat

EDIT: I also posted the function as a snippet (with nice F# formatting) here: http://fssnip.net/6A

草莓酥 2024-12-01 15:36:25

正如其他解决方案中所见,这个问题几乎与您的最后一个问题相反。因此,为了更好地衡量,我在这里给出了我的答案的修改版本:

let concatWithPreviousWhen f s = seq {
    let buffer = ResizeArray()

    let flush() = seq { 
        if buffer.Count > 0 then 
            yield Seq.readonly (buffer.ToArray())
            buffer.Clear() }

    for subseq in s do
        if f subseq |> not then yield! flush()
        buffer.AddRange(subseq)

    yield! flush() }

你可以像这样使用它:

seq [seq[2;3;4];seq[1;5;6;7;1;9];seq[2;3;5;7]]
|> concatWithPreviousWhen (Seq.head>>(=)1)

As seen in the other solutions, this problem is almost the inverse of your last question. So for good measure, I give a modified version of my answer to that here:

let concatWithPreviousWhen f s = seq {
    let buffer = ResizeArray()

    let flush() = seq { 
        if buffer.Count > 0 then 
            yield Seq.readonly (buffer.ToArray())
            buffer.Clear() }

    for subseq in s do
        if f subseq |> not then yield! flush()
        buffer.AddRange(subseq)

    yield! flush() }

And you use it like so:

seq [seq[2;3;4];seq[1;5;6;7;1;9];seq[2;3;5;7]]
|> concatWithPreviousWhen (Seq.head>>(=)1)
醉生梦死 2024-12-01 15:36:25

对我来说看起来像折叠,如下所示。尝试在没有参考值的情况下尽可能发挥功能。

let joinBy f (s:'a seq seq) = 
    let (a:'a seq), (b:'a seq seq) = 
        s |> Seq.fold (fun (a,r) se -> 
                         if f se then (se |> Seq.append a,r) 
                         else (se, seq {yield! r; yield a} ) ) 
             (Seq.empty, Seq.empty)
    seq {yield! b; yield a} |> Seq.filter (Seq.isEmpty >> not)


seq [seq[2;3;4];seq[1;5;6;7;1;9];seq[2;3;5;7]]
|> joinBy (Seq.head >> ((=) 1))
|> printfn "%A"

Looks like a fold to me as shown below. Tried to be as functional as possible without ref values.

let joinBy f (s:'a seq seq) = 
    let (a:'a seq), (b:'a seq seq) = 
        s |> Seq.fold (fun (a,r) se -> 
                         if f se then (se |> Seq.append a,r) 
                         else (se, seq {yield! r; yield a} ) ) 
             (Seq.empty, Seq.empty)
    seq {yield! b; yield a} |> Seq.filter (Seq.isEmpty >> not)


seq [seq[2;3;4];seq[1;5;6;7;1;9];seq[2;3;5;7]]
|> joinBy (Seq.head >> ((=) 1))
|> printfn "%A"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文