目前我有一个定义为var myArray = [(Date, Double)]()
的元组数组。
myArray
的输出可能如下所示:
[(2016-08-30 07:00:00 +0000, 1.0), (2016-09-30 07:00:00 +0000, 0.050000000000000003),
(2016-10-30 07:00:00 +0000, 20.0), (2017-06-30 07:00:00 +0000, 6.0),
(2017-07-30 07:00:00 +0000, 5.0)]
我正在尝试从元组数组中获取最大Double
值,但不知道如何去做。
我看到有一个功能:
myArray.max { (<#(Date, Double)#>, <#(Date, Double)#>) -> Bool in
}
但是,如果使用正确的功能,我还不确定如何使用它?
有人能指出我正确的方向吗?
答案 0 :(得分:2)
max
与sort
类似。您必须通过比较$0 < $1
。结果是具有最大值的(可选)元组。
if let maxItem = myArray.max(by: {$0.1 < $1.1 }) {
let maxDouble = maxItem.1
}
再一次,不鼓励您使用元组作为数据源
如果您的数据结构可能会超出临时范围,请将其建模为类或结构,而不是作为元组。
答案 1 :(得分:0)
您可以使用以下解决方案
[TestClass]
public class DiscardWhileBusyActionBlockTest
{
[TestMethod]
public void PostToConnectedBuffer_ActionNotBusy_MessageConsumed()
{
var actionPerformer = new ActionPerformer();
var block = new DiscardWhileBusyActionBlock<int>(actionPerformer.Perform);
var buffer = DiscardWhileBusyActionBlockTest.SetupBuffer(block);
buffer.Post(1);
DiscardWhileBusyActionBlockTest.WaitForCompletion(buffer, block);
var expectedMessages = new[] { 1 };
actionPerformer.LastReceivedMessage.Should().BeEquivalentTo(expectedMessages);
}
[TestMethod]
public void PostToConnectedBuffer_ActionBusy_MessagesConsumedWhenActionBecomesAvailable()
{
var actionPerformer = new ActionPerformer();
var block = new DiscardWhileBusyActionBlock<int>(actionPerformer.Perform);
var buffer = DiscardWhileBusyActionBlockTest.SetupBuffer(block);
actionPerformer.SetBusy();
// 1st message will set the actionperformer to busy, 2nd message should be sent when
// it becomes available.
buffer.Post(1);
buffer.Post(2);
actionPerformer.SetAvailable();
DiscardWhileBusyActionBlockTest.WaitForCompletion(buffer, block);
var expectedMessages = new[] { 1, 2 };
actionPerformer.LastReceivedMessage.Should().BeEquivalentTo(expectedMessages);
}
[TestMethod]
public void PostToConnectedBuffer_ActionBusy_DiscardMessagesInBetweenAndProcessOnlyLastMessage()
{
var actionPerformer = new ActionPerformer();
var block = new DiscardWhileBusyActionBlock<int>(actionPerformer.Perform);
var buffer = DiscardWhileBusyActionBlockTest.SetupBuffer(block);
actionPerformer.SetBusy();
buffer.Post(1);
buffer.Post(2);
buffer.Post(3);
buffer.Post(4);
buffer.Post(5);
actionPerformer.SetAvailable();
DiscardWhileBusyActionBlockTest.WaitForCompletion(buffer, block);
var expectedMessages = new[] { 1, 5 };
actionPerformer.LastReceivedMessage.Should().BeEquivalentTo(expectedMessages);
}
private static void WaitForCompletion(IDataflowBlock source, IDataflowBlock target)
{
source.Complete();
target.Completion.Wait(TimeSpan.FromSeconds(1));
}
private static BufferBlock<int> SetupBuffer(ITargetBlock<int> block)
{
var buffer = new BufferBlock<int>();
buffer.LinkTo(block);
buffer.Completion.ContinueWith(task => block.Complete());
return buffer;
}
private class ActionPerformer
{
private readonly ManualResetEvent resetEvent = new ManualResetEvent(true);
public List<int> LastReceivedMessage { get; } = new List<int>();
public void Perform(int message)
{
this.resetEvent.WaitOne(TimeSpan.FromSeconds(3));
this.LastReceivedMessage.Add(message);
}
public void SetBusy()
{
this.resetEvent.Reset();
}
public void SetAvailable()
{
this.resetEvent.Set();
}
}
}
或者使用$ syntax
let max = myArray.max { (left, right) -> Bool in
return left.1 < right.1
}
答案 2 :(得分:0)
试试这个:
myArray.max { (val1, val2) -> Bool in
return val1.1 < val2.1
}
答案 3 :(得分:0)
此代码给出了给定数组的最大值
var myArray = [(2016-08-30, 1.0), (2016-09-30, 0.050000000000000003),
(2016-10-30, 20.0), (2017-06-30, 6.0),
(2017-07-30, 5.0)]
// Get maximum element
var max = myArray.max {
$0.1 < $1.1
}
print(max!.1)
答案 4 :(得分:0)
为max
添加一个闭包。
let maxTuple = myArray.max{ $0.1 < $1.1 }!
let maxDouble = maxTuple.1