Skip to content

Commit

Permalink
Adds bitap to Result
Browse files Browse the repository at this point in the history
Signed-off-by: Albert Pastrana <albert.pastrana@gmail.com>
  • Loading branch information
albertpastrana committed Apr 13, 2018
1 parent c826a13 commit dfcc10a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions result/src/main/scala/uscala/result/Result.scala
Expand Up @@ -52,6 +52,8 @@ sealed abstract class Result[+A, +B] extends Product with Serializable {
x
}

def bitap[U](sideEffect: => U): Result[A, B] = this.bimap(a => { sideEffect; a }, b => { sideEffect; b })

def swap: Result[B, A] = fold(Ok(_), Fail(_))

def merge[AA >: A](implicit ev: B <:< AA): AA = fold(identity, ev.apply)
Expand Down
20 changes: 17 additions & 3 deletions result/src/test/scala/uscala/result/ResultSpec.scala
Expand Up @@ -106,19 +106,33 @@ class ResultSpec extends Specification with ScalaCheck {
"tap" >> {
"should not execute the given f if Fail" >> prop { n: Int =>
var executed = false
Fail(n).tap { _ => executed = true }
Fail(n).tap(_ => executed = true)
executed must beFalse
}

"should execute the given f if Ok, passing the Ok value" >> prop { n: Int =>
var received: Option[Int] = None
Ok(n).tap { x => received = Some(x) }
Ok(n).tap(x => received = Some(x))
received must beSome(n)
}

"should execute the given f if Ok, leaving the result untouched" >> prop { n: Int =>
var executed = false
Ok(n).tap { _ => executed = true } must_=== Ok(n)
Ok(n).tap(_ => executed = true) must_=== Ok(n)
executed must beTrue
}
}

"bitap" >> {
"should execute the given effect if Fail, leaving the result untouched" >> prop { n: Int =>
var executed = false
Fail(n).bitap { executed = true } must_=== Fail(n)
executed must beTrue
}

"should execute the given effect if Ok, leaving the result untouched" >> prop { n: Int =>
var executed = false
Ok(n).bitap { executed = true } must_=== Ok(n)
executed must beTrue
}
}
Expand Down

0 comments on commit dfcc10a

Please sign in to comment.