Last active
December 19, 2017 22:10
-
-
Save hohonuuli/94b9d2451343e909663cd28e1a19af3b to your computer and use it in GitHub Desktop.
Scala class for Medium article
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.{Duration, Instant} | |
object BudgetApp extends App { | |
val budget = CurrentBudget | |
val endDate = Instant.now.plus(Duration.ofDays(365)) | |
def filter(s: Stream[Transaction]): Stream[Transaction] = s.takeWhile(i => i.date.isBefore(endDate)) | |
def money(s: Stream[Transaction]): Double = s.last.accumulatedValue | |
val sum = budget.events.map(_.stream) // Convert each transaction into a stream | |
.map(filter) // Include all transactions for 1 year | |
.map(money) // Convert each transaction stream to it's accumulated value | |
.sum // Sum up all the accumulated values to get a grand total | |
printf("Total: %.2f\n", sum) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment