import scala.
util.
control.
Exception.
catching
case class A1
(yoba
: String, petushok
: String
= "kukareku")
type YobaKostyl
= PartialFunction
[AnyRef, Option
[AnyRef
]]
def hasField
(name
: String
): YobaKostyl
= { catching(classOf[NoSuchFieldException])
.opt(obj.getClass.getDeclaredField(name)).map(_ ⇒ obj)
}
def hasFieldValue
[T
](name
: String, value
: T
): YobaKostyl
= { catching(classOf[NoSuchFieldException])
.
either(obj.
getClass.
getDeclaredField(name
)) match { None
field.
setAccessible(true) Some(field.get(obj)).filter(value ==).map(_ ⇒ obj)
}
}
def and
(f
: YobaKostyl
*) = f.
reduce((f1, f2
) ⇒ f1.
andThen(_.
flatMap(f2
))) def or
(obj
: AnyRef, f
: YobaKostyl
*) = f.
reduce((f1, f2
) ⇒ f1.
andThen(_.
orElse(f2
(obj
))))
val data
= Vector
(A1
("123"), A2
(321), A1
("ALLOU",
"ko-ko-ko"), A2
(900))
data.flatMap { obj ⇒
or(obj, and(hasField("yoba"), hasFieldValue("petushok", "ko-ko-ko")), hasFieldValue("peka", 900))(obj)
}.foreach(println)
}
aW1wb3J0IHNjYWxhLnV0aWwuY29udHJvbC5FeGNlcHRpb24uY2F0Y2hpbmcKCm9iamVjdCBNYWluIGV4dGVuZHMgQXBwIHsKICBjYXNlIGNsYXNzIEExKHlvYmE6IFN0cmluZywgcGV0dXNob2s6IFN0cmluZyA9ICJrdWthcmVrdSIpCiAgY2FzZSBjbGFzcyBBMihwZWthOiBJbnQpCgogIHR5cGUgWW9iYUtvc3R5bCA9IFBhcnRpYWxGdW5jdGlvbltBbnlSZWYsIE9wdGlvbltBbnlSZWZdXQoKICBkZWYgaGFzRmllbGQobmFtZTogU3RyaW5nKTogWW9iYUtvc3R5bCA9IHsKICAgIGNhc2Ugb2JqIOKHkgogICAgICBjYXRjaGluZyhjbGFzc09mW05vU3VjaEZpZWxkRXhjZXB0aW9uXSkKICAgICAgCS5vcHQob2JqLmdldENsYXNzLmdldERlY2xhcmVkRmllbGQobmFtZSkpLm1h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