Exemple #1
0
 def traverse(gap: Applicative[G], fa: Kind1[Option, A],\
              f: Func1[A, Kind1[G, B]]) -> Kind2[G, Option, B]:
     if dekind(fa).is_empty():
         return cast(Kind2[G, Option, B], gap.pure(Nothing()))
     else:
         ff: Func1[B, Option[B]] = Some
         return gap.map(f(dekind(fa).get), ff)
Exemple #2
0
  def reduce_left_to_option(
      cls, fa: Kind1[F, A], f: Func1[A, B], g: Func2[B, A, B]) -> "Option.Option[B]":
    from tats.data.Option import Option, Nothing, Some
    conv: Func2[
      Option[B], A,
      Option[B]] = lambda acc, el: Some(f(el)) if acc.is_empty() else Some(g(acc.get, el))

    return cls.fold_left(fa, Nothing(), conv)
Exemple #3
0
  def test_foreach(self):
    def _raise(_):
      raise Exception

    with raises(Exception):
      Some(1).foreach(_raise)

    Nothing().foreach(_raise)
Exemple #4
0
  def test_with_filter(self):
    assert Some(1).with_filter(lambda x: x == 1).map(lambda x: x * 2) == Some(2)
    assert Nothing()\
             .with_filter(lambda x: x == 1)\
             .map(lambda x: x * 2) == Nothing()
    assert Some(1)\
             .with_filter(lambda x: x == 2)\
             .map(lambda x: x * 2) == Nothing()

    assert Some(1)\
             .with_filter(lambda x: x == 1)\
             .flat_map(lambda x: Some(x * 2)) == Some(2)
    assert Nothing()\
             .with_filter(lambda x: x == 1)\
             .flat_map(lambda x: Some(x * 2)) == Nothing()
    assert Some(1) \
             .with_filter(lambda x: x == 2) \
             .flat_map(lambda x: Some(x * 2)) == Nothing()

    def _raise(_):
      raise Exception

    with raises(Exception):
      Some(1) \
               .with_filter(lambda x: x == 1) \
               .foreach(_raise)
    Nothing() \
             .with_filter(lambda x: x == 1) \
             .foreach(_raise)
    Some(1) \
             .with_filter(lambda x: x == 2) \
             .foreach(_raise)

    assert Some(1)\
             .with_filter(lambda x: x == 1)\
             .with_filter(lambda x: x % 2 == 1)\
             .map(lambda x: x * 2) == Some(2)
    assert Nothing() \
             .with_filter(lambda x: x == 1) \
             .with_filter(lambda x: x % 2 == 1) \
             .map(lambda x: x * 2) == Nothing()
    assert Some(1) \
             .with_filter(lambda x: x == 2) \
             .with_filter(lambda x: x % 2 == 1) \
             .map(lambda x: x * 2) == Nothing()
Exemple #5
0
  def test_syntax(self):
    assert TList([1, 2, 3]).eqv(TList([1, 2, 3]))
    assert TList([1, 2, 3]).neqv(TList([1, 2]))
    assert TList([1, 2]).map(lambda x: x * 2) == TList([2, 4])
    assert TList([1, 2]).product_r(TList([3, 4])) == TList([3, 4, 3, 4])
    assert TList([1, 2]).product_l(TList([3, 4])) == TList([1, 1, 2, 2])
    assert TList([1, 2]).flat_map(lambda x: TList([x, x * 2])) == TList([1, 2, 2, 4])
    assert TList([1, 2]).combine(TList([3, 4])) == TList([1, 2, 3, 4])
    assert TList([1, 2]) + TList([3, 4]) == TList([1, 2, 3, 4])
    assert TList([1, 2]) + TList([3, 4]) == TList([1, 2, 3, 4])
    assert TList([]).is_empty
    assert not TList([1, 2]).is_empty
    assert TList([1, 2, 3]).fold_left(0, lambda a, b: a + b) == 6
    assert TList([1, 2, 3]).reduce_left_to_option(str, lambda a, b: a + str(b)) == Some("123")
    assert TList([]).reduce_left_to_option(str, lambda a, b: a + str(b)) == Nothing()
    assert TList([1, 2, 3]).reduce_left_option(lambda a, b: a + b) == Some(6)
    assert TList([]).reduce_left_option(lambda a, b: a + b) == Nothing()
    assert TList([1, 2, 3]).to_tlist() == TList([1, 2, 3])
    assert TList([1, 2, 3]).map2(TList([4, 5]), lambda a, b: a * b) == TList([4, 5, 8, 10, 12, 15])
    assert TList([1, 2, 3]).product(TList([4, 5 ])) ==\
           TList([(1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)])
    assert TList([1, 2, 3]).traverse(OptionInstance(), Some) == Some(TList([1, 2, 3]))
    assert TList([1, 2, 3])\
             .traverse(OptionInstance(), lambda x: Nothing() if x > 2 else Some(x)) == Nothing()
    assert TList([Some(1), Some(2), Some(3)]).sequence(OptionInstance()) == Some(TList([1, 2, 3]))
    assert TList([Some(1), Nothing(), Some(3)]).sequence(OptionInstance()) == Nothing()
    assert TList([1, 2, 3]).flat_traverse(OptionInstance(), lambda x: Some(TList([x, x + 1]))) == \
           Some(TList([1, 2, 2, 3, 3, 4]))
    assert TList([1, 2, 3]).flat_traverse(
      OptionInstance(), lambda x: Some(TList([x, x + 1])) if x < 2 else Nothing()) == Nothing()

    assert TList.var(1, 2, 3).map_filter(lambda a: Some(a) if a < 3 else Nothing()) == \
           TList.var(1, 2)
    assert TList.var(1, 2, 3).collect(PartialFunc.cs(Case.v(1, "a"), Case.v(2, "b"))) == \
           TList.var("a", "b")
    assert TList.var(1, 2, 3).filter(lambda a: a < 3) == TList.var(1, 2)
    assert TList.var(1, 2, 3).filter_not(lambda a: a < 3) == TList.var(3)
Exemple #6
0
 def test_fold(self):
   assert Some(1).fold("a", str) == "1"
   assert Nothing().fold("a", str) == "a"
Exemple #7
0
 def test_filter_not(self):
   assert Some(1).filter_not(lambda x: x == 1) == Nothing()
   assert Some(1).filter_not(lambda x: x == 2) == Some(1)
   assert Nothing().filter_not(lambda x: x == 2) == Nothing()
Exemple #8
0
 def test_get(self):
   assert Some(1).get == 1
   with raises(ValueError):
     Nothing().get
Exemple #9
0
 def test_get_or_else(self):
   assert Some(1).get_or_else(2) == 1
   assert Nothing().get_or_else(2) == 2
Exemple #10
0
 def test_to_left(self):
   assert Some(1).to_left("a") == Left(1)
   assert Nothing().to_left("a") == Right("a")
Exemple #11
0
 def lift(self) -> "Func1[T, Option.Option[S]]":
     """lift this PartialFunc to a plain function returning an Option result"""
     from tats.data.Option import Nothing, Some
     return lambda a: Some(self.run(a)) if self.is_defined_at(
         a) else Nothing()
Exemple #12
0
 def to_option(self) -> "Option.Option[R]":
     from tats.data.Option import Nothing, Some
     return self.fold(Func1F.const(Nothing()), Some)
Exemple #13
0
 def test_from_nullable(self):
   assert Option.from_nullable(1) == Some(1)
   assert Option.from_nullable(None) == Nothing()
Exemple #14
0
 def test_when(self):
   assert Option.when(True, 1) == Some(1)
   assert Option.when(False, 1) == Nothing()
Exemple #15
0
  def test_syntax(self):
    assert Some(1).map(lambda x: x * 2) == Some(2)
    assert Nothing().map(lambda x: x * 2) == Nothing()

    assert Some(1).product_r(Some(2)) == Some(2)
    assert Nothing().product_r(Some(2)) == Nothing()
    assert Some(1).product_r(Nothing()) == Nothing()
    assert Nothing().product_r(Nothing()) == Nothing()

    assert Some(1).product_l(Some(2)) == Some(1)
    assert Nothing().product_l(Some(2)) == Nothing()
    assert Some(1).product_l(Nothing()) == Nothing()
    assert Nothing().product_l(Nothing()) == Nothing()

    assert Some(1).flat_map(lambda x: Some(x * 2)) == Some(2)
    assert Some(1).flat_map(lambda x: Nothing()) == Nothing()
    assert Nothing().flat_map(lambda x: Some(x * 2)) == Nothing()
    assert Nothing().flat_map(lambda x: Nothing()) == Nothing()

    assert Some(1).combine(IntInstance(), Some(1)) == Some(2)
    assert Nothing().combine(IntInstance(), Some(1)) == Nothing()
    assert Some(1).combine(IntInstance(), Nothing()) == Nothing()
    assert Nothing().combine(IntInstance(), Nothing()) == Nothing()

    assert Some(1).traverse(TListInstance(), lambda a: TList([a, a * 2])) == \
           TList([Some(1), Some(2)])
    assert Nothing().traverse(TListInstance(), lambda a: TList([a, a * 2])) == TList([Nothing()])
    assert Some(1).flat_traverse(TListInstance(), lambda a: TList([Some(a), Some(a * 2)])) == \
           TList([Some(1), Some(2)])
    assert Some(TList([1, 2])).sequence(TListInstance()) == TList([Some(1), Some(2)])

    assert Some(1).show() == "Some(a=1)"
    assert Nothing().show() == "Nothing()"
Exemple #16
0
 def test_apply(self):
   assert OptionInstance.ap(Some(lambda x: x * 2), Some(1)) == Some(2)
   assert OptionInstance.ap(Nothing(), Some(1)) == Nothing()
   assert OptionInstance.ap(Some(lambda x: x * 2), Nothing()) == Nothing()
   assert OptionInstance.ap(Nothing(), Nothing()) == Nothing()
Exemple #17
0
 def test_functor(self):
   OptionInstance.map(Some(1), lambda x: x * 2) == Some(2)
   OptionInstance.map(Nothing(), lambda x: x * 2) == Nothing()
Exemple #18
0
 def test_eq(self):
   assert Some(1).eqv(Some(1))
   assert not Some(1).eqv(Some(2))
   assert Some(1).neqv(Nothing())
   assert Nothing().neqv(Some(1))
   assert Nothing().eqv(Nothing())
Exemple #19
0
 def test_exists(self):
   assert Some(1).exists(lambda x: x == 1)
   assert not Some(1).exists(lambda x: x == 2)
   assert not Nothing().exists(lambda x: x == 1)
Exemple #20
0
 def flat_map(fa: Kind1["Option", A],
              f: Func1[A, Kind1["Option", B]]) -> Kind1["Option", B]:
     return Nothing() if fa.is_empty() else f(cast(Some[A], fa).a)
Exemple #21
0
 def test_contains(self):
   assert Some(1).contains(1)
   assert not Nothing().contains(1)
Exemple #22
0
 def combine(tsemi: Semigroup[A], a: "Option[A]",
             b: "Option[A]") -> "Option[A]":
     if a.non_empty() and b.non_empty():
         return Some(tsemi.combine(a.get, b.get))
     else:
         return Nothing()
Exemple #23
0
 def test_unless(self):
   assert Option.unless(True, 1) == Nothing()
   assert Option.unless(False, 1) == Some(1)
Exemple #24
0
 def test_forall(self):
   assert Some(1).forall(lambda x: x == 1)
   assert not Some(1).forall(lambda x: x == 2)
   assert Nothing().forall(lambda x: x == 1)
Exemple #25
0
 def filter_not(cls, fa: Kind1[F, A], f: Func1[A, bool]) -> Kind1[F, A]:
   from tats.data.Option import Some, Nothing
   return cls.map_filter(fa, lambda a: Nothing() if f(a) else Some(a))
Exemple #26
0
 def test_is_empty(self):
   assert not Some("").is_empty()
   assert Nothing().is_empty()
Exemple #27
0
 def test_non_empty(self):
   assert Some("").non_empty()
   assert not Nothing().non_empty()
Exemple #28
0
 def test_or_else(self):
   assert Some(1).or_else(Some(2)) == Some(1)
   assert Nothing().or_else(Some(2)) == Some(2)
   assert Some(1).or_else(Nothing()) == Some(1)
   assert Nothing().or_else(Nothing()) == Nothing()
Exemple #29
0
 def empty() -> "Option[A]":
     return Nothing()
Exemple #30
0
 def test_lift(self):
     f = PartialFunc.cs(EndoCase(str, lambda s: s + "b")).lift
     assert f("a") == Some("ab")
     assert f(0) == Nothing()