Scala: General " />
logo
down
shadow

Is there a Scala compiler flag to warn when tail recursion is applied without annotation?


Is there a Scala compiler flag to warn when tail recursion is applied without annotation?

By : Adil Elsaeed
Date : November 17 2020, 11:58 AM
I wish this helpful for you I don't think the compiler has this but IntelliJ offers a "No tail recursion annotation" under Inspections > Scala: General
code :


Share : facebook icon twitter icon
Scala, tail recursion vs. non tail recursion, why is tail recursion slower?

Scala, tail recursion vs. non tail recursion, why is tail recursion slower?


By : Tamim
Date : March 29 2020, 07:55 AM
it should still fix some issue In addition to the problem shown by @monkjack (i.e multiplying small * big is faster than big * small, which does account for a greater chunk of the difference), your algorithm is different in each case so they're not really comparable.
In the tail-recursive version you're mutiplying big-to-small:
code :
n * n-1 * n-2 * ... * 2 * 1
n * (n-1 * (n-2 * (... * (2 * 1))))
def tailRecFact2(n: BigInt) = {
  def loop(x: BigInt, out: BigInt): BigInt =
    if (x > n) out else loop(x + 1, x * out)
  loop(1, 1)
}
Is there any way to turn off tail recursion optimization of Scala Compiler?

Is there any way to turn off tail recursion optimization of Scala Compiler?


By : pawan
Date : March 29 2020, 07:55 AM
hope this fix your issue For some special reasons, I want to remove the effect of all @tailrec in a big program, but don't want to do that manually, is there any optional arguments when compiling that can turn off the tail recursion optimization? I want to just leave the @tailrec in the code, but don't want to check it and do tail recursion optimization when compiling. Is that possible? , You can use the compiler's -g:notailcalls option.
From the docs:
code :
import scala.annotation.tailrec

class A {

@tailrec
final def x(i: Int): Int = if(i == 0) {i;} else {x(i-1)}

}
public final int x(int);
    descriptor: (I)I
    flags: ACC_PUBLIC, ACC_FINAL
    Code:
      stack=2, locals=2, args_size=2
         0: iload_1
         1: iconst_0
         2: if_icmpne     7
         5: iload_1
         6: ireturn
         7: iload_1
         8: iconst_1
         9: isub
        10: istore_1
        11: goto          0
public final int x(int);
    descriptor: (I)I
    flags: ACC_PUBLIC, ACC_FINAL
    Code:
      stack=3, locals=2, args_size=2
         0: iload_1
         1: iconst_0
         2: if_icmpne     9
         5: iload_1
         6: goto          16
         9: aload_0
        10: iload_1
        11: iconst_1
        12: isub
        13: invokevirtual #12                 // Method x:(I)I
        16: ireturn
Compiler Friendly Tail Recursion + Tail Recursion Checking in ATS

Compiler Friendly Tail Recursion + Tail Recursion Checking in ATS


By : Vivi
Date : March 29 2020, 07:55 AM
like below fixes the issue What is the best way to check that a function has been tail call optimized in ATS? (So far I have been running "top" to see if memory usage is constant) , There is a peculiar way to do it in ATS2.
Say you have
code :
fnx foo(...) = bar(...)
and bar(...) = ...bar...
scala tail recursion decorator not letting tail recursive function to be called

scala tail recursion decorator not letting tail recursive function to be called


By : user2741564
Date : March 29 2020, 07:55 AM
I wish this helpful for you The error is not reproducible, you may try to provide more context.
Anyways, I would also recommend you to avoid Seq for tail-recursive algorithms (you should use a List instead), and try / catch blocks (use the Try Monad instead).
code :
import scala.util.{Try, Success, Failure}

object FuncUtils {
  def tryAll[T](funcs: (() => T)*): Option[T] = {
    @annotation.tailrec
    def loop(remaining: List[() => T]): Option[T] = remaining match {
      case Nil     => None
      case x :: xs => Try(x()) match {
        case Success(t) => Some(t)
        case Failure(_) => loop(remaining = xs)
      }
    }

    loop(remaining = funcs.toList)
  }
}
object FuncUtils {
  def tryAll[T](funcs: (() => T)*): Option[T] =
    funcs.iterator.map(f => Try(f())).collectFirst { case Success(t) => t }
}
Why did the scala compiler not flag what appears to not be a tail-recursive function?

Why did the scala compiler not flag what appears to not be a tail-recursive function?


By : Jackie Ottosen
Date : March 29 2020, 07:55 AM
I hope this helps you . The spec defines the && method for Boolean.
Since Boolean is a value class, the if/else expression is inlined.
Related Posts Related Posts :
  • Assign generic method to variable in Scala
  • General Finite State Machine (Transducer) in Scala
  • What is the usage of a final var in Scala
  • Define Generic Types with String
  • Why there is a ClassCastException when isInstanceOf returns true on a scala type with wildcard parameter?
  • How to reaload scala application after code change using actors and sbt
  • akka non-blocking BoundedMailbox
  • Why are constructor parameters made into members for case classes?
  • Higher order tail recursive function needs to terminate early
  • How can I reusably filter based on type in Scala?
  • Why does Scala sbt update every time I start it from command prompt?
  • Recommended Scala io library
  • Thread-safely transforming a value in a mutable map
  • Why does chaining match expressions does not compile?
  • Ebean not persisting foreign keys
  • Difference between def m(p: T forSome {type T} and def m1(p:Any), is there any ? Explanation needed based on Scala Langu
  • macro does not find out enclosing vals
  • Spark : how to run spark file from spark shell
  • Flattening a list of lists to a set with exceptions in scala
  • flatMap implementation in Scala
  • Confused about a few lines code in a scala official document page
  • How to input parameters when running bash command with Scala
  • Location header is lost if max-redirects > 1
  • Controller Spec is using FakeApplication, but can't load test configuration
  • Scala code analyzer targets case variable names that are identical to the outer matched varables - "suspicous shado
  • Why does authorize directive execute after the code it's supposed to protect?
  • Scala. Checking if a Type is Comparable
  • Does having a private constructor on a value class negate the benefits of a value class?
  • How to transform submitted json in Play 2.0?
  • Scala warning match may not be exhaustive
  • Pure not a member of objective Promise in PlayFramework
  • How to unmarshal POST params and JSON body in a single route?
  • Spark:How to use join method?
  • is client thread-safe in Twitter Finagle
  • Why is the method accepts only one argument?
  • Scala Play 2.3 Working with gCloud Storage - any libs to go async?
  • spray.io strange get/delete/detach directives behavior
  • SBT cannot resolve class declared in src/main/scala in a src/test/scala test class
  • Scala typeclass without function argument
  • Configuring actor behavior using typesafe Config and HOCON
  • Scalatra: Migrating Jersey Filters to Scalatra
  • Compilation error when using Scaldi
  • Scalac hanging in phase typer
  • how to have different source code when cross-compiling Scala with sbt? (changes in MurmurHash)
  • How to set different scalacOptions per Scala version when cross-compiling using Build.scala?
  • Possible Bug in JDBC?
  • scala case class put methods in companion object?
  • multiproject sbt doesn't generate file structure
  • Scala "multilevel" abstract class / abstract objects replacement
  • Scala, getting the type parameters of a KList as an HList
  • Why does Play refuse form reporting "Cannot resolve method apply with such signature: Missing arguments"?
  • How to split string with trailing empty strings in result?
  • Scala group by list of list and subtracts grouped values
  • Scala - Creating a function to produce Unary string or integer values
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org