File tree Expand file tree Collapse file tree 1 file changed +24
-19
lines changed
src/main/java/travelator/marketing Expand file tree Collapse file tree 1 file changed +24
-19
lines changed Original file line number Diff line number Diff line change 1
1
package travelator.marketing
2
2
3
- import com.natpryce.Failure
4
- import com.natpryce.Result
5
- import com.natpryce.Success
6
- import com.natpryce.recover
3
+ import com.natpryce.*
7
4
8
5
fun Sequence<String>.toHighValueCustomerReport (
9
6
onErrorLine : (ParseFailure ) -> Unit = {}
@@ -34,21 +31,29 @@ private fun Sequence<String>.withoutHeader() = drop(1)
34
31
35
32
internal fun String.toCustomerData (): Result <CustomerData , ParseFailure > =
36
33
split(" \t " ).let { parts ->
37
- if (parts.size < 4 )
38
- return Failure (NotEnoughFieldsFailure (this ))
39
- val score = parts[3 ].toIntOrNull() ? :
40
- return Failure (ScoreIsNotAnIntFailure (this ))
41
- val spend = if (parts.size == 4 ) 0.0 else parts[4 ].toDoubleOrNull() ? :
42
- return Failure (SpendIsNotADoubleFailure (this ))
43
- Success (
44
- CustomerData (
45
- id = parts[0 ],
46
- givenName = parts[1 ],
47
- familyName = parts[2 ],
48
- score = score,
49
- spend = spend
50
- )
51
- )
34
+ parts
35
+ .takeUnless { it.size < 4 }
36
+ .asResultOr { NotEnoughFieldsFailure (this ) }
37
+ .flatMap { parts ->
38
+ parts[3 ].toIntOrNull()
39
+ .asResultOr { ScoreIsNotAnIntFailure (this ) }
40
+ .flatMap { score: Int ->
41
+ (if (parts.size == 4 ) 0.0
42
+ else parts[4 ].toDoubleOrNull())
43
+ .asResultOr { SpendIsNotADoubleFailure (this ) }
44
+ .flatMap { spend ->
45
+ Success (
46
+ CustomerData (
47
+ id = parts[0 ],
48
+ givenName = parts[1 ],
49
+ familyName = parts[2 ],
50
+ score = score,
51
+ spend = spend
52
+ )
53
+ )
54
+ }
55
+ }
56
+ }
52
57
}
53
58
54
59
sealed class ParseFailure (open val line : String )
You can’t perform that action at this time.
0 commit comments