learning scala Function Recursive Tail Call
可以使用scala库,可以从字面上看出是在调用 递归函数:
code
import scala.util.control.TailCalls._ val arrayDonuts: Array[String] = Array("Vanilla Donut", "Strawberry Donut", "Plain Donut", "Glazed Donut") println("\nStep : How to define a tail recursive function using scala.util.control.TailCalls._")
def tailSearch(donutName: String, donuts: Array[String], index: Int): TailRec[Option[Boolean]] = {
if(donuts.length == index) {
done(None) // NOTE: done is imported from scala.util.control.TailCalls._
} else if(donuts(index) == donutName) {
done(Some(true))
} else {
val nextIndex = index +
tailcall(tailSearch(donutName, donuts, nextIndex)) // NOTE: tailcall is imported from scala.util.control.TailCalls._
}
} println("\nStep : How to call tail recursive function using scala.util.control.TailCalls._")
val tailFound = tailcall(tailSearch("Glazed Donut", arrayDonuts, ))
println(s"Find Glazed Donut using TailCall = ${tailFound.result}") // NOTE: our returned value is wrapped so we need to get it by calling result val tailNotFound = tailcall(tailSearch("Chocolate Donut", arrayDonuts, ))
println(s"Find Chocolate Donut using TailCall = ${tailNotFound.result}")
resule:
Step : How to define a tail recursive function using scala.util.control.TailCalls._ Step : How to call tail recursive function using scala.util.control.TailCalls._
Find Glazed Donut using TailCall = Some(true)
Find Chocolate Donut using TailCall = None
learning scala Function Recursive Tail Call的更多相关文章
- learning scala Function Composition andThen
Ordering using andThen: f(x) andThen g(x) = g(f(x)) Ordering using compose: f(x) compose g(x) = f(g( ...
- [machine learning] Loss Function view
[machine learning] Loss Function view 有关Loss Function(LF),只想说,终于写了 一.Loss Function 什么是Loss Function? ...
- learning scala How To Create Variable Argument Function - varargs :_ *
Scala collection such as List or Sequence or even an Array to variable argument function using the s ...
- learning scala How To Create Implicit Function
println("Step 1: How to create a wrapper String class which will extend the String type") ...
- learning scala PartialFunction
Partial函数的定义 scala> val isVeryTasty: PartialFunction[String, String] = { case "Glazed Donut& ...
- learning scala generic classes
package com.aura.scala.day01 object genericClasses { def main(args: Array[String]): Unit = { val sta ...
- learning scala extractor object
package com.aura.scala.day01 import scala.util.Random object extractorObject { def main(args: Array[ ...
- Deep Learning: Activation Function
Sigmoid Function ReLU Function Tanh Function
- [Reinforcement Learning] Value Function Approximation
为什么需要值函数近似? 之前我们提到过各种计算值函数的方法,比如对于 MDP 已知的问题可以使用 Bellman 期望方程求得值函数:对于 MDP 未知的情况,可以通过 MC 以及 TD 方法来获得值 ...
随机推荐
- 手动编译ts的经过
动机 以前写ts或者es6,都是用在脚手架搭建的项目中,比如vue和react,当时当我识图写一个ts的demo的,我还要创建一个完整的vue或者react项目?明显不合适,那就要研究一下如何手动搭建 ...
- IDEA/WebStorm使用笔记
1.使用powershell作为默认终端 #改变powershell策略 Set-ExecutionPolicy Unrestricted -Scope CurrentUser 找到系统的powers ...
- redis集合数据类型---SET
一.概述 redis的set是string类型的无序集合 集合成员是唯一的,这就意味着集合中不能出现重复的数据. 集合中最大的成员数为2^32-1(4294967295,每个集合可存储40多亿个成员) ...
- 天梯赛 L2-023. 图着色问题
题解:用dfs遍历图的每条边就好,这里注意要求颜色的个数为k #include <cstdio> #include <iostream> #include <cstrin ...
- C#使用消息队列(MSMQ)
最近项目用到消息队列,找资料学习了下.把学习的结果 分享出来 首先说一下,消息队列 (MSMQ Microsoft Message Queuing)是MS提供的服务,也就是Windows操作系统的功能 ...
- 数据结构之链表(LinkedList)(二)
数据结构之链表(LinkedList)(一) 双链表 上一篇讲述了单链表是通过next 指向下一个节点,那么双链表就是指不止可以顺序指向下一个节点,还可以通过prior域逆序指向上一个节点 示意图: ...
- 用D3js的区域生成器实现简单波浪图
最近做控件遇到含有波浪图的图表,一开始用Echarts虽然很快完成了,但Echarts的波浪图与其他图表的响应式不同步,于是学习了D3js,D3js写起来确实复杂一些,但能够实现的效果也更丰富,做的时 ...
- weui中的picker滑动报错
html { touch-action: none; } 在页面插入上述代码即可解决
- linux 下调用wps 注意
记录笔记以防忘记 wps 是界面程序,linux 必须在界面终端中调用 wps 命令才能打开软件 xshell 连接时启动tomcat ,wps命令 会使用Xshell 的隧道转发,只有在界面下的终端 ...
- Fatal error:Call to undefined function mysqli_connect() in .php line 报错
这样的问题,多半是PHP配置问题. 修改php配置文件 1.在php(版本:php-7.2.7-Win32-VC15-x64)文件夹中一开始不会看到php.ini,而是php.ini-developm ...