-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
85 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
// This file was automatically generated from working-with-typed-errors.md by Knit tool. Do not edit. | ||
package arrow.website.examples.exampleTypedErrors17 | ||
|
||
data class User(val name: String, val age: Int) | ||
import arrow.core.raise.either | ||
import arrow.core.raise.ensure | ||
import arrow.core.raise.forEachAccumulating | ||
|
||
fun example() = either { | ||
forEachAccumulating(1 .. 10) { i -> | ||
ensure(i % 2 == 0) { "$i is not even" } | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,4 @@ | ||
// This file was automatically generated from working-with-typed-errors.md by Knit tool. Do not edit. | ||
package arrow.website.examples.exampleTypedErrors18 | ||
|
||
import arrow.core.Either | ||
import arrow.core.Either.Left | ||
import arrow.core.raise.either | ||
import arrow.core.raise.ensure | ||
import io.kotest.matchers.shouldBe | ||
|
||
sealed interface UserProblem { | ||
object EmptyName: UserProblem | ||
data class NegativeAge(val age: Int): UserProblem | ||
} | ||
|
||
data class User private constructor(val name: String, val age: Int) { | ||
companion object { | ||
operator fun invoke(name: String, age: Int): Either<UserProblem, User> = either { | ||
ensure(name.isNotEmpty()) { UserProblem.EmptyName } | ||
ensure(age >= 0) { UserProblem.NegativeAge(age) } | ||
User(name, age) | ||
} | ||
} | ||
} | ||
|
||
fun example() { | ||
User("", -1) shouldBe Left(UserProblem.EmptyName) | ||
} | ||
data class User(val name: String, val age: Int) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,31 @@ | ||
// This file was automatically generated from working-with-typed-errors.md by Knit tool. Do not edit. | ||
package arrow.website.examples.exampleTypedErrors20 | ||
|
||
import arrow.core.* | ||
import arrow.core.raise.* | ||
import arrow.core.Either | ||
import arrow.core.Either.Left | ||
import arrow.core.NonEmptyList | ||
import arrow.core.nonEmptyListOf | ||
import arrow.core.raise.either | ||
import arrow.core.raise.ensure | ||
import arrow.core.raise.zipOrAccumulate | ||
import io.kotest.matchers.shouldBe | ||
|
||
val stringError: Either<String, Boolean> = "problem".left() | ||
sealed interface UserProblem { | ||
object EmptyName: UserProblem | ||
data class NegativeAge(val age: Int): UserProblem | ||
} | ||
|
||
val intError: Either<Int, Boolean> = either { | ||
// transform error String -> Int | ||
withError({ it.length }) { | ||
stringError.bind() | ||
data class User private constructor(val name: String, val age: Int) { | ||
companion object { | ||
operator fun invoke(name: String, age: Int): Either<NonEmptyList<UserProblem>, User> = either { | ||
zipOrAccumulate( | ||
{ ensure(name.isNotEmpty()) { UserProblem.EmptyName } }, | ||
{ ensure(age >= 0) { UserProblem.NegativeAge(age) } } | ||
) { _, _ -> User(name, age) } | ||
} | ||
} | ||
} | ||
|
||
fun example() { | ||
intError shouldBe Either.Left("problem".length) | ||
User("", -1) shouldBe Left(nonEmptyListOf(UserProblem.EmptyName, UserProblem.NegativeAge(-1))) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// This file was automatically generated from working-with-typed-errors.md by Knit tool. Do not edit. | ||
package arrow.website.examples.exampleTypedErrors21 | ||
|
||
import arrow.core.* | ||
import arrow.core.raise.* | ||
import io.kotest.matchers.shouldBe | ||
|
||
val stringError: Either<String, Boolean> = "problem".left() | ||
|
||
val intError: Either<Int, Boolean> = either { | ||
// transform error String -> Int | ||
withError({ it.length }) { | ||
stringError.bind() | ||
} | ||
} | ||
fun example() { | ||
intError shouldBe Either.Left("problem".length) | ||
} |
This file contains 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