我尝试了以下代码来在F#中实现do。
let listObjects bucketName = asyncSeq {
use client = new AmazonS3Client(RegionEndpoint.USEast2)
let request = new ListObjectsV2Request(BucketName = bucketName, MaxKeys = 10)
while (
let! response = client.ListObjectsV2Async(request) |> Async.AwaitTask
for entry in response.S3Objects do
yield entry.Key
response.IsTruncated) do ()
但是,它不能编译?以下代码是唯一的选择吗?
let listObjects bucketName = asyncSeq {
use client = new AmazonS3Client(RegionEndpoint.USEast2)
let request = new ListObjectsV2Request(BucketName = bucketName, MaxKeys = 10)
let! response = client.ListObjectsV2Async(request) |> Async.AwaitTask
for entry in response.S3Objects do // do while
yield entry.Key
while response.IsTruncated do
let! response = client.ListObjectsV2Async(request) |> Async.AwaitTask
for entry in response.S3Objects do
yield entry.Key
答案 0 :(得分:4)
通常,您可以在F#中使用尾递归来解决与C#中的循环所要解决的同类问题。对于您打算的while循环行为,我还不太清楚,但是这是一个尾递归产生S3Objects
直到response.IsTruncated
为假的例子:
let listObjects bucketName =
asyncSeq {
use client = new AmazonS3Client(RegionEndpoint.USEast2)
let request = new ListObjectsV2Request(BucketName = bucketName, MaxKeys = 10)
let! response = client.ListObjectsV2Async(request) |> Async.AwaitTask
let rec getKeys () =
asyncSeq {
for entry in response.S3Objects do
yield entry.Key
if response.IsTruncated
then yield! getKeys ()
}
yield! getKeys()
}