Movatterモバイル変換


[0]ホーム

URL:


Scala 3
3.7.4
LearnInstallPlaygroundFind A LibraryCommunityBlog
Scala 3
LearnInstallPlaygroundFind A LibraryCommunityBlog
DocsAPI
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL
Scala 3/scala/scala.util/scala.util.control/TailCalls

TailCalls

scala.util.control.TailCalls
objectTailCalls

Methods exported by this object implement tail calls via trampolining.

Tail calling methods must either return their result usingdone or call the next method usingtailcall. Both return an instance ofTailRec. The result of evaluating a tailcalling function can be retrieved from aTailRec value using methodresult.

Implemented as described in "Stackless Scala with Free Monads"https://blog.higher-order.com/assets/trampolines.pdf

Here's a usage example:

import scala.util.control.TailCalls._def isEven(xs: List[Int]): TailRec[Boolean] =  if (xs.isEmpty) done(true) else tailcall(isOdd(xs.tail))def isOdd(xs: List[Int]): TailRec[Boolean] = if (xs.isEmpty) done(false) else tailcall(isEven(xs.tail))isEven((1 to 100000).toList).resultdef fib(n: Int): TailRec[Int] =  if (n < 2) done(n) else for {    x <- tailcall(fib(n - 1))    y <- tailcall(fib(n - 2))  } yield x + yfib(40).result

Attributes

Source
TailCalls.scala
Graph
Supertypes
classObject
traitMatchable
classAny
Self type

Members list

Type members

Classlikes

sealed abstractclassTailRec[+A]

This class represents a tailcalling computation.

This class represents a tailcalling computation.

Attributes

Source
TailCalls.scala
Supertypes
classObject
traitMatchable
classAny

Value members

Concrete methods

defdone[A](result:A):TailRec[A]

Return the final result from a tailcalling computation.

Return the final result from a tailcalling computation.

Value parameters

`result`

the result value

Attributes

Returns

aTailRec object representing a computation which immediately returnsresult

Source
TailCalls.scala
deftailcall[A](rest:=>TailRec[A]):TailRec[A]

Perform a tailcall.

Perform a tailcall.

Value parameters

rest

the expression to be evaluated in the tailcall

Attributes

Returns

aTailRec object representing the expressionrest

Source
TailCalls.scala
In this article
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL

[8]ページ先頭

©2009-2025 Movatter.jp