What is the idiomatic way of scala for this? I have a list, and I want to return "Y" if I find something that meets certain conditions, otherwise "N". I have a solution that works, but I don’t like it ...
def someMethod( someList: List[Something]) : String = { someList.foreach( a => if (a.blah.equals("W") || a.bar.equals("Y") ) { return "Y" } ) "N" }
Simples:
if (someList.exists{ a=> a.blah == "W" || a.bar == "Y"}) "Y" else "N"
def condition(i: Something) = i.blah.equals("W") || i.bar.equals("Y") somelist.find(condition(_)).map(ignore => "Y").getOrElse("N")
or simply
if (somelist exists condition) "Y" else "N"
, - .
def someMethod(someList: List[Something]): String = if (someList forall { a => a.blah == "W" || a.blah == "Y" }) "Y" else "N"
val l = List(1,2,3,4) (l.find(x => x == 0 || x == 1)) match { case Some(x) => "Y"; case _ => "N"} res: java.lang.String = Y
, Something , . case, ...
Something
case class Something(blah:String, bar:String)
... ...
def someMethod(someList: List[Something]) = someList.collect{ case Something("W",_) | Something(_,"Y") => "Y"}.headOption.getOrElse("N")
Source: https://habr.com/ru/post/1790014/More articles:ColdFusion security by checking ARGUMENTS.TargetPage in Application.onRequestStart? - securityCan I name different classes (with different methods and types) that perform similar functions? - c #Print a new line in a Google application - pythonSet selected value for dropdown using javascript - javascriptHow to deal with incompatible XPath expression expression in PHP - phpAnother UISwitch user question is objective-cThe NetworkStream.DataAvailable property does not return the correct result when using SslStream - c #Ajax request is not asynchronous - ajaxИсключить столбец сортировки в JTable - javaList of anonymous type and dynamics ... Confused - dynamicAll Articles