import scala.
util.
control.
Exception.
catching
case class A1
(yoba
: String, petushok
: String
= "kukareku") case class A2
(peka
: Int, list
: Seq
[String
] = Nil
)
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 hasListValue
[T
](name
: String, value
: T
): YobaKostyl
= { catching(classOf[NoSuchFieldException])
.
either(obj.
getClass.
getDeclaredField(name
)) match { None
field.
setAccessible(true) Some(field.get(obj)).collect {
case list
: Iterable
[_] ⇒ list
}.filter(_.exists(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, Vector
("PODI",
"PODMOJSA",
"MANYA")), A1
("ALLOU",
"ko-ko-ko"), A2
(900))
Iterator.
continually(readLine
()).
takeWhile(null ne
).
foreach(f ⇒
{ println(data.flatMap(yoba ⇒ hasListValue("list", f)(yoba)).mkString(","))
})
}
aW1wb3J0IHNjYWxhLnV0aWwuY29udHJvbC5FeGNlcHRpb24uY2F0Y2hpbmcKCm9iamVjdCBNYWluIGV4dGVuZHMgQXBwIHsKICBjYXNlIGNsYXNzIEExKHlvYmE6IFN0cmluZywgcGV0dXNob2s6IFN0cmluZyA9ICJrdWthcmVrdSIpCiAgY2FzZSBjbGFzcyBBMihwZWthOiBJbnQsIGxpc3Q6IFNlcVtTdHJpbmddID0gTmlsKQoKICB0eXBlIFlvYmFLb3N0eWwgP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