Custom control structures in Scala?

There are several times when I ran a simple template when programming in Java or C ++, for which the user control structure could reduce the template in my code. It looks something like this:

 if( Predicate ){
     Action

     return Value
 }

that is, the "return if" statement. I tried making functions with signatures like foo[A,B]( pred:((A,A)=>Boolean), value:Option[B] ), but then I finish checking if I returned Some or None. I am disabled by the operator return.

Is there a way to inherit such control structures in functional languages ​​or, more specifically, Scala?

Edit:

I was not so clear with my description, and this confused people who are trying to help me. The main reason mine foodoesn't work is because it cannot short-circuit the evaluation of the contained function. it

def intersect( geometry:Geometry, reference:Geometry ):Geometry = {
    return_if( withinBounds( geometry, projection ), logToString( logger, "Geometry outside " + projection.toString ), EmptyGeometry() )
    return_if( topologicallyCorrect( geometry ), intersect( correct( geometry ), reference )
    //rest of the function
}

return_if.

+3
4

, , , .

, ?

, . return_if return_if, , scala, return , return_if.

, , .

+2

:

def whatevs[A, B](options : PartialFunction[(A,A), B]) : Option[B] = {
  val myInput = generateInput
  if(options.isDefined(myInput)) {
    Some(options(myInput))
  } else None
}

:

whateves {
   case (x,y) if x > y =>   "Biggerz"
   case (x,y) if y > x =>   "Tiny!"
}

, return. If-expression , . , - if, .

- , - . : x > y y > x .

whatevs , , raw pattern matching.

+7

, .

Scala ( ), . , .

, if-statements, .

Scala .

methodReturningOption.getOrElse(
  // All the code for the None case goes in here
)

, ( , ).

:

None match {  // Could use a real variable instead of None if it helped
  case _ if (Predicate1) => Action1; Value1
  case _ if (Predicate2) => Action2; Value2
  . . .
  case _ => ErrorHandlingPerhaps
}

-, . ( , .)

+2

, . , :

def intersect( geometry:Geometry, reference:Geometry ):Geometry = {

  return_if( withinBounds( geometry, projection ), 
    logToString( logger, "Geometry outside " + projection.toString ), 
    EmptyGeometry() )

  return_if( topologicallyCorrect( geometry ), 
    intersect( correct( geometry )), 
    reference )

    // rest of the function
}

"" Scala:

def intersect( geometry:Geometry, reference:Geometry ):Geometry = {

  if (withinBounds( geometry, projection )) {
    logToString( logger, "Geometry outside " + projection.toString )
    return EmptyGeometry() }

  if( topologicallyCorrect( geometry )) {
    intersect( correct( geometry ))
    return reference }

  //rest of the function
}

To me the “regular” version looks much clearer. Firstly, it shows very clearly that he is returning. This is not even more detailed. I suspect you have a more complex use case. If you show us, perhaps we can direct you to more appropriate patterns.

+1
source

Source: https://habr.com/ru/post/1761729/


All Articles