У меня есть следующий код C #:
(не нужно разбираться в деталях, просто для иллюстрации вопроса)
long VolumeBeforePrice = 0;
long Volume = 0;
var ContractsCount = 0.0;
var VolumeRequested = Candle.ConvertVolumes(MinVolume);
// go through all entries
foreach (var B in Entries)
{
// can we add the whole block?
if (Volume + B.VolumeUSD <= VolumeRequested)
{
// yes, add the block and calculate the number of contracts
Volume += B.VolumeUSD;
ContractsCount += B.VolumeUSD / B.PriceUSD;
}
else
{
// no, we need to do a partial count
var Difference = VolumeRequested - Volume;
ContractsCount += Difference / B.PriceUSD;
Volume = VolumeRequested; // we reached the max
}
VolumeBeforePrice += B.VolumeUSD;
if (Volume >= VolumeRequested) break;
}
он проходит через записи торгового ордераЗапишите и рассчитайте количество контрактов, доступных для определенной суммы в долларах США.
логика довольно проста: для каждой записи есть блок контрактов по заданной цене, поэтому он либо добавляет целый блок, либодобавит частичный блок, если он не помещается в запросе.
Я пытаюсь переместить это в F #, и я столкнулся с некоторыми проблемами, так как я новичок в языке:
это частичная реализация:
let mutable volume = 0L
let mutable volumeBeforePrice = 0L
let mutable contractsCount = 0.0
entries |> List.iter (fun e ->
if volume + e.VolumeUSD <= volumeRequested then
volume <- volume + e.VolumeUSD;
contractsCount <- contractsCount + float(e.VolumeUSD) / e.PriceUSD
else
let difference = volumeToTrade - volume
contractsCount <- contractsCount + difference / B.PriceUSD
volume = volumeRequested // this is supposed to trigger an exit on the test below, in C#
)
И я остановился на этом, потому что это не выглядит как очень F # способ сделать это:)
Итак, мой вопрос: как я могуструктурировать List.iter так, чтобы:
- I can use counters from one iteration to the next? like sums and average passed to the next iteration
- I can exit the loop when I reached a specific condition and skip the last elements?