The code above will only ever return one CashWithdrawlEvent, and then end the sequence ... you need the loop to return multiple values. Also, have you considered using match to handle multiple cases?
(not tested as working)
let WithdrawalCash (account, amount) = seq { let bDone = ref false while not (!bDone) do match amount with | v when account.Balance.CurrentAmount - v < 0 -> yield NotEnoughMoneyForWithdrawal(account, amount) bDone := true
But even this does not look like what you would like, since it will ALWAYS output the same โamountโ every time you pull the value out of the sequence, because the account and the amount are fixed when creating the sequence. So, I would reset "seq" and make this simple function, as in:
let WithdrawalCash (account, amount) = match amount with | v when account.Balance.CurrentAmount - v < 0 -> NotEnoughMoneyForWithdrawal(account, amount) // more when clauses can go here | _ -> let newBalance = account.Balance.CurrentAmount - amount CashWithdrawnEvent(account, newBalance)
In the general case of listing a sequence and stopping when a specific condition is satisfied, consider "Seq.takeWhile", as in:
let MySeq = seq { while true do yield someValue } let code () = MySeq |> Seq.takeWhile ( fun v -> ShouldIContinueWorkingTest(v) ) |> Seq.iter ( fun v -> DoWork(v) )
source share