F# - 最新列表到连接的字符串由间隔 [英] F# - Breaking a List into Concatenated Strings by an Interval
问题描述
我的电子邮件地址列表,我需要发送电子邮件通知到每个地址。我想在同一时间做到这一点的25个地址块。是否有一个函数式语言如F#为折叠(串连)25电子邮件地址在一起......每个用分号隔开任何快捷方式。我知道有在.NET中的String.Split方法,但我需要Concat的25的时间。
I have a list of email addresses, and I need to send an email notification to each address. I'd like to do this in blocks of 25 addresses at a time. Is there any quick way in a functional language like F# to "fold" (concatenate) 25 emails addresses together... each separated by a semicolon. I know there is the String.Split method in .NET, but I need to concat 25 at a time.
什么是最优雅的方式来在F#中执行此?
What is the most elegant way to perform this in F#?
推荐答案
如果这仍然是相关的,这里是一个功能的加入使用的分隔符,但没有转换为字符串数组的序列。
If this is still relevant, here is a function to join a sequence of strings using delimiter but without conversion to array.
open System
open System.Text
/// Join a sequence of strings using a delimiter.
/// Equivalent to String.Join() but without arrays.
let join (items : seq<string>) (delim : string) =
// Collect the result in the string builder buffer
// The end-sequence will be "item1,delim,...itemN,delim"
let buff =
Seq.fold
(fun (buff :StringBuilder) (s:string) -> buff.Append(s).Append(delim))
(new StringBuilder())
items
// We don't want the last delim in the result buffer, remove
buff.Remove(buff.Length-delim.Length, delim.Length).ToString()
这篇关于F# - 最新列表到连接的字符串由间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!