Skip to content

Instantly share code, notes, and snippets.

@hohonuuli
Last active December 19, 2017 22:10
Show Gist options
  • Save hohonuuli/94b9d2451343e909663cd28e1a19af3b to your computer and use it in GitHub Desktop.
Save hohonuuli/94b9d2451343e909663cd28e1a19af3b to your computer and use it in GitHub Desktop.
Scala class for Medium article
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