Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
WIP: Clean-up domain.pure
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Apr 24, 2022
1 parent d784c5d commit 6c600b7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 60 deletions.
57 changes: 52 additions & 5 deletions app/src/main/java/com/ivy/wallet/domain/pure/core/IvyCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,64 @@ package com.ivy.wallet.domain.pure.core
import arrow.core.NonEmptyList
import com.ivy.fp.Pure
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.util.mapIndexedNel
import com.ivy.wallet.domain.pure.util.mapIndexedNelSuspend
import com.ivy.wallet.domain.pure.util.nonEmptyListOfZeros
import java.math.BigDecimal

@Pure
fun <Arg> calcValues(
transactions: List<Transaction>,
valueFunctions: NonEmptyList<ValueFunction<Arg>>,
arg: Arg
): NonEmptyList<BigDecimal> = calculateValueFunctionsSum(
valueFunctionArgument = arg,
transactions = transactions,
valueFunctions = valueFunctions
)


typealias ValueFunction<A> = (Transaction, A) -> BigDecimal
typealias SuspendValueFunction<A> = suspend (Transaction, A) -> BigDecimal

@Pure
internal tailrec fun <A> calculateValueFunctionsSum(
valueFunctionArgument: A,
transactions: List<Transaction>,
valueFunctions: NonEmptyList<ValueFunction<A>>,
sum: NonEmptyList<BigDecimal> = nonEmptyListOfZeros(n = valueFunctions.size)
): NonEmptyList<BigDecimal> {
return if (transactions.isEmpty())
sum
else
calculateValueFunctionsSum(
valueFunctionArgument = valueFunctionArgument,
transactions = transactions.drop(1),
valueFunctions = valueFunctions,
sum = sum.mapIndexedNel { index, sumValue ->
val valueFunction = valueFunctions[index]
sumValue + valueFunction(transactions.first(), valueFunctionArgument)
}
)
}

@Pure
internal tailrec suspend fun <A> calculateValueFunctionsSumSuspend(
valueFunctionArgument: A,
transactions: List<Transaction>,
valueFunctions: NonEmptyList<SuspendValueFunction<A>>,
sum: NonEmptyList<BigDecimal> = nonEmptyListOfZeros(n = valueFunctions.size)
): NonEmptyList<BigDecimal> {
return calculateValueFunctionsSum(
valueFunctionArgument = arg,
transactions = transactions,
valueFunctions = valueFunctions
)
return if (transactions.isEmpty())
sum
else
calculateValueFunctionsSumSuspend(
valueFunctionArgument = valueFunctionArgument,
transactions = transactions.drop(1),
valueFunctions = valueFunctions,
sum = sum.mapIndexedNelSuspend { index, sumValue ->
val valueFunction = valueFunctions[index]
sumValue + valueFunction(transactions.first(), valueFunctionArgument)
}
)
}
51 changes: 0 additions & 51 deletions app/src/main/java/com/ivy/wallet/domain/pure/core/ValueFunction.kt

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ivy.wallet.domain.pure
package com.ivy.wallet.domain.pure.util

import arrow.core.NonEmptyList
import java.math.BigDecimal
Expand Down

0 comments on commit 6c600b7

Please sign in to comment.