Skip to content

Commit e209589

Browse files
committed
Changed to 1.0.8 (not snapshot); Added Transformer type.
1 parent 0c929fb commit e209589

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ You can easily set up your own implicit value of _MatchLogger_ which is simply a
134134

135135
Version
136136
=======
137+
* 1.0.8 Mostly documentation but also...
138+
* Added methods: sequence, sequenceStrict, identify, invert, "~~";
139+
* introduced Transformer type;
140+
* renamed logger as matchLogger.
137141
* 1.0.7 Filter method added for MatchResult.
138142
* 1.0.6 Match sequences.
139143
* 1.0.5 Added combine and accumulate methods to MatchResult...

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ organization := "com.phasmidsoftware"
22

33
name := "Matchers"
44

5-
version := "1.0.8-SNAPSHOT"
5+
version := "1.0.8"
66

77
scalaVersion := "2.13.16"
88

src/main/scala/com/phasmidsoftware/matchers/Matchers.scala

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ trait Matchers {
1515

1616
matchers =>
1717

18+
/**
19+
* Represents a type alias `Transformer` which defines a `Matcher`
20+
* that transforms an input value of type `T` to an output value of the same type `T`.
21+
*
22+
* This type alias can be used to simplify and make the code more readable
23+
* where such a transformation operation is utilized.
24+
*
25+
* @tparam T the type of the input and output values being transformed
26+
*/
27+
type Transformer[T] = Matcher[T, T]
28+
1829
/**
1930
* Trait that defines the behavior of a `Matcher` as a function of type `T => MatchResult[R]`
2031
* together with other filter-monadic methods, etc..
@@ -1041,96 +1052,90 @@ trait Matchers {
10411052
*
10421053
* @param e a `Throwable`.
10431054
* @tparam R the result type.
1044-
* @return a `Matcher[T, R]`
1055+
* @return a `Matcher[Any, R]`
10451056
*/
10461057
def error[R](e: Throwable): Matcher[Any, R] = Matcher("error")(_ => Error(e))
10471058

10481059
/**
10491060
* Defines a `Matcher` which always succeeds and whose input type and result type are the same.
10501061
*
10511062
* @tparam R both the input type and the result type.
1052-
* @return a `Matcher[R, R]` which always succeeds.
1063+
* @return a `Transformer[R]` which always succeeds.
10531064
*/
1054-
def always[R]: Matcher[R, R] = namedLift("always")(identity)
1065+
def always[R]: Transformer[R] = namedLift("always")(identity)
10551066

10561067
/**
10571068
* Defines a `Matcher` which succeeds only if the predicate `p` evaluates to `true`.
10581069
* The result will be named "filter."
10591070
*
10601071
* @param p a predicate on type `R`.
10611072
* @tparam R both the input type and the result type.
1062-
* @return a `Matcher[R, R]` which succeeds only if `p(r)` is `true`.
1073+
* @return a `Transformer[R]` which succeeds only if `p(r)` is `true`.
10631074
*/
1064-
def filter[R](p: R => Boolean): Matcher[R, R] = Matcher("filter")(r => Match(r) filter p)
1075+
def filter[R](p: R => Boolean): Transformer[R] = Matcher("filter")(r => Match(r) filter p)
10651076

10661077
/**
10671078
* Defines a `Matcher` which succeeds only if the predicate `p` evaluates to `false`.
10681079
* The result will be named "filterNot."
10691080
*
10701081
* @param p a predicate on type `R`.
10711082
* @tparam R both the input type and the result type.
1072-
* @return a `Matcher[R, R]` which succeeds only if `p(r)` is `false`.
1083+
* @return a `Transformer[R]` which succeeds only if `p(r)` is `false`.
10731084
*/
1074-
def filterNot[R](p: R => Boolean): Matcher[R, R] = Matcher("filterNot")(r => Match(r) filterNot p)
1085+
def filterNot[R](p: R => Boolean): Transformer[R] = Matcher("filterNot")(r => Match(r) filterNot p)
10751086

10761087
/**
10771088
* Defines a `Matcher` which succeeds only if `b` is true (regardless of the input to the `Matcher`).
10781089
* The result will be named "maybe."
10791090
*
10801091
* @param b a `Boolean` value which determines whether the result succeeds.
10811092
* @tparam R both the input type and the result type.
1082-
* @return a `Matcher[R, R]` which succeeds if `b` is true.
1093+
* @return a `Transformer[R]` which succeeds if `b` is true.
10831094
*/
1084-
def maybe[R](b: Boolean): Matcher[R, R] = filter[R](_ => b).named("maybe")
1095+
def maybe[R](b: Boolean): Transformer[R] = filter[R](_ => b).named("maybe")
10851096

10861097
/**
1087-
* Defines `a` Matcher which succeeds if the input is equal to the given `t`.
1098+
* Defines a Matcher which succeeds if the input is equal to the given `r`.
10881099
* The result will be named "matches."
10891100
*
1090-
* @param t the value which must be matched.
1101+
* @param r the value which must be matched.
10911102
* @tparam R both the input type and the result type.
1092-
* @return a `Matcher[R, R]`.
1103+
* @return a `Transformer[R]`.
10931104
*/
1094-
def matches[R](t: R): Matcher[R, R] = filter[R](_ == t).named("matches")
1105+
def matches[R](r: R): Transformer[R] = filter[R](_ == r).named("matches")
10951106

10961107
/**
10971108
* The `alt` method returns a `Matcher` which tries to match the input `t` according to `m`.
10981109
* Unless there's an error, this matcher always succeeds.
10991110
* If it's a match, then the match will be returned.
11001111
* If it's a miss, then we return a match based on the original t.
1101-
* CONSIDER there are some similarities between `alt` and `~~`.
11021112
*
1103-
* @param m a `Matcher[T, T]`
1113+
* @param m a `Transformer[T]`
11041114
* @tparam T both the type of the input and the underlying type of the output.
1105-
* @return a `Matcher[T, T]`
1106-
*/
1107-
def alt[T](m: Matcher[T, T]): Matcher[T, T] = Matcher[T, T]("alt") {
1108-
t =>
1109-
m(t) match {
1110-
case z: Match[T] => z
1111-
case Miss(_, _) => Match(t)
1112-
case Error(x) => Error(x)
1113-
}
1115+
* @return a `Transformer[T]`
1116+
*/
1117+
def alt[T](m: Transformer[T]): Transformer[T] = Matcher[T, T]("alt") {
1118+
t => m(t) | success(t)
11141119
}
11151120

11161121
/**
11171122
* Creates a matcher that checks if all elements in a sequence satisfy the given matcher.
11181123
*
1119-
* @param m A matcher of type `Matcher[T,T]`.
1124+
* @param m A matcher of type `Transformer[T]`.
11201125
* @return A new matcher that validates sequences of type `Seq[T]`.
11211126
* The matcher succeeds if all elements in the sequence satisfy the given matcher `m`,
11221127
* otherwise it returns a miss with the name "matchAll" and the unmatched sequence.
11231128
*/
1124-
def matchAll[T](m: Matcher[T, T]): Matcher[Seq[T], Seq[T]] = (ts: Seq[T]) => if (ts.forall(m(_).successful)) Match(ts) else Miss("matchAll", ts)
1129+
def matchAll[T](m: Transformer[T]): Transformer[Seq[T]] = (ts: Seq[T]) => if (ts.forall(m(_).successful)) Match(ts) else Miss("matchAll", ts)
11251130

11261131
/**
11271132
* Creates a matcher that checks if any element in a sequence satisfies the given matcher.
11281133
*
1129-
* @param m A matcher of type `Matcher[T,T]`.
1130-
* @return a new matcher that takes a sequence of type `T` and returns a a sequence of type `T`.
1134+
* @param m A matcher of type `Transformer[T]`.
1135+
* @return a new matcher that takes a sequence of type `T` and returns a sequence of type `T`.
11311136
* indicating whether at least one element in the sequence satisfies the given matcher `m`.
11321137
*/
1133-
def matchAny[T](m: Matcher[T, T]): Matcher[Seq[T], Seq[T]] = (ts: Seq[T]) => if (ts.exists(m(_).successful)) Match(ts) else Miss("matchAny", ts)
1138+
def matchAny[T](m: Transformer[T]): Transformer[Seq[T]] = (ts: Seq[T]) => if (ts.exists(m(_).successful)) Match(ts) else Miss("matchAny", ts)
11341139

11351140
/**
11361141
* Matcher whose success depends on the application of a function f to the input,

0 commit comments

Comments
 (0)