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 方法来获得值 ...
随机推荐
- 07 IO流(四)——文件字节流 FileInputStream/FileOutputStream与文件的拷贝
两个类的简述 专门用来对文件进行读写的类. 父类是InputStream.OutputStream 文件读入细节 FileOutputStream流的构造方法:new FileOutputStream ...
- 基于thymeleaf实现简单登录
1.引入thymeleaf.静态资源等依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...
- IDEA/WebStorm使用笔记
1.使用powershell作为默认终端 #改变powershell策略 Set-ExecutionPolicy Unrestricted -Scope CurrentUser 找到系统的powers ...
- Javascritp Array数组方法总结
合并数组 - concat() 用法一 (合并两个数组) var hege = ["Cecilie", "Lone"]; var stale = [" ...
- 怎样获取当前页面框架的数量(即iframe和frame的数量)
需要使用window.length, 或者window.frames.length; 如果页面中不包含frame和iframe元素, 则返回0; window.length === window.fr ...
- Linux mount/unmount 挂载和卸载指令
对于Linux用户来讲,不论有几个分区,分别分给哪一个目录使用,它总归就是一个根目录.一个独立且唯一的文件结构 Linux中每个分区都是用来组成整个文件系统的一部分,她在用一种叫做“挂载”的处理方法, ...
- Go part 7 反射,反射类型对象,反射值对象
反射 反射是指在程序运行期间对程序本身进行访问和修改的能力,(程序在编译时,变量被转换为内存地址,变量名不会被编译器写入到可执行部分,在运行程序时,程序无法获取自身的信息) 支持反射的语言可以在程序编 ...
- vue导出Excel文件
1.需要安装file-saver和script-loader.xlsx npm install file-saver / yarn add file-saver npm install script- ...
- 【已解决】极速迅雷win10闪退解决方案
[已解决]极速迅雷win10闪退解决方案 本文作者:天析 作者邮箱:2200475850@qq.com 发布时间: Wed, 17 Jul 2019 18:01:00 +0800 在吾爱下载了个极速迅 ...
- win10 系统解决mysql中文乱码问题
问题: 向mysql 数据库插入数据是,出现中文乱码(中文均显示为‘??’) 原因: mysql 默认的字符集是latin1,所以我么需要改为ut8编码才可以 解决: 1.以管理员权限运行cmd窗口 ...