Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在F#中,当字符串具有一些轻度标记属性时,如何按块分割字符串列表?_F#_Telegram - Fatal编程技术网

在F#中,当字符串具有一些轻度标记属性时,如何按块分割字符串列表?

在F#中,当字符串具有一些轻度标记属性时,如何按块分割字符串列表?,f#,telegram,F#,Telegram,我有一个工具,它使用一个电报聊天机器人与它的用户交互 电报限制了呼叫速率,所以我使用了一个定期刷新的队列系统 因此,当前代码非常基本: // flush the message queue let flushMessageQueue() = if not messageQueue.IsEmpty then <- messageQueue is a ConcurrentQueue // get all the messages let m

我有一个工具,它使用一个电报聊天机器人与它的用户交互

电报限制了呼叫速率,所以我使用了一个定期刷新的队列系统

因此,当前代码非常基本:

// flush the message queue
let flushMessageQueue() =
    if not messageQueue.IsEmpty then       <- messageQueue is a ConcurrentQueue

        // get all the messages
        let messages =
            messageQueue
            |> Seq.unfold(fun q ->
                match q.TryDequeue () with
                | true, m -> Some (m, q)
                | _ -> None)

        // put all the messages in a single string
        let messagesString = String.Join("\n", messages)

        // send the data
        client.SendTextMessageAsync(chatId, messagesString, ParseMode.Markdown)
        |> Async.AwaitTask
        |> Async.RunSynchronously
        |> ignore
但随着信息变得更加复杂,两个问题同时出现:

部分输出是带有简单标记的格式化文本:

  • 某些行块在``段之间换行
  • 在某些行中也有一些```部分
  • 文本为UTF-8,使用了一组符号
文本的一些示例可能是:

```
this is a group of lines
with one, or many many lines
```
and sometimes there are things ```like this``` as well
而且。。。我发现电报也将消息大小限制为4kb

因此,我想到了两件事:

  • 我可以使用open/close```来维护一个状态,并从队列中拉出,根据状态将每一行用三个回标记进行包装,然后推入另一个队列,该队列将用于生成4kb块
  • 我可以继续从重新格式化的队列中获取消息并聚合它们,直到达到4kb,或者队列结束并循环
在F#中有没有一种优雅的方法可以做到这一点? 我记得看到一个片段,其中使用了一个收集函数来聚合数据,直到达到某个大小,但它看起来效率非常低,因为它正在收集line1、line1+line2、line1+line2+line3。。。然后挑一个合适尺寸的

```
this is a group of lines
with one, or many many lines
```
and sometimes there are things ```like this``` as well