Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
QZero_0
java-design-patterns
提交
c5492184
J
java-design-patterns
项目概览
QZero_0
/
java-design-patterns
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
java-design-patterns
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
c5492184
编写于
1月 08, 2022
作者:
I
Ilkka Seppälä
提交者:
GitHub
1月 08, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
enhancement: check spelling and update topic (#1943)
Co-authored-by:
N
Subhrodip Mohanta
<
hello@subho.xyz
>
上级
4f8007d6
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
23 addition
and
24 deletion
+23
-24
trampoline/README.md
trampoline/README.md
+21
-19
trampoline/src/main/java/com/iluwatar/trampoline/Trampoline.java
...ine/src/main/java/com/iluwatar/trampoline/Trampoline.java
+0
-2
trampoline/src/main/java/com/iluwatar/trampoline/TrampolineApp.java
.../src/main/java/com/iluwatar/trampoline/TrampolineApp.java
+2
-3
未找到文件。
trampoline/README.md
浏览文件 @
c5492184
...
...
@@ -17,19 +17,19 @@ and to interleave the execution of functions without hard coding them together.
## Explanation
Recursion is a frequently adopted technique for solving algorithmic problems in a divide and conquer
style. For example
calculating fibonacci accumulating sum and factorials. In these kinds of problems
recursion is more straightforward than their loop counterpart. Furthermore recursion may need less
code and looks more concise. There is a saying that every recursion problem can be solved using
a loop with the cost of writing code that is more difficult to understand.
style. For example
, calculating Fibonacci accumulating sum and factorials. In these kinds of
problems, recursion is more straightforward than its loop counterpart. Furthermore, recursion may
need less code and looks more concise. There is a saying that every recursion problem can be solved
using
a loop with the cost of writing code that is more difficult to understand.
However
recursion type solutions have one big caveat. For each recursive call
it typically needs
However
, recursion-type solutions have one big caveat. For each recursive call,
it typically needs
an intermediate value stored and there is a limited amount of stack memory available. Running out of
stack memory creates a stack overflow error and halts the program execution.
Trampoline pattern is a trick that allows
us define
recursive algorithms in Java without blowing the
Trampoline pattern is a trick that allows
defining
recursive algorithms in Java without blowing the
stack.
Real
world example
Real
-
world example
> A recursive Fibonacci calculation without the stack overflow problem using the Trampoline pattern.
...
...
@@ -105,24 +105,26 @@ public interface Trampoline<T> {
Using the
`Trampoline`
to get Fibonacci values.
```
java
public
static
Trampoline
<
Integer
>
loop
(
int
times
,
int
prod
)
{
public
static
void
main
(
String
[]
args
)
{
LOGGER
.
info
(
"Start calculating war casualties"
);
var
result
=
loop
(
10
,
1
).
result
();
LOGGER
.
info
(
"The number of orcs perished in the war: {}"
,
result
);
}
public
static
Trampoline
<
Integer
>
loop
(
int
times
,
int
prod
)
{
if
(
times
==
0
)
{
return
Trampoline
.
done
(
prod
);
return
Trampoline
.
done
(
prod
);
}
else
{
return
Trampoline
.
more
(()
->
loop
(
times
-
1
,
prod
*
times
));
return
Trampoline
.
more
(()
->
loop
(
times
-
1
,
prod
*
times
));
}
}
log
.
info
(
"start pattern"
);
var
result
=
loop
(
10
,
1
).
result
();
log
.
info
(
"result {}"
,
result
);
}
```
Program output:
```
start pattern
result
3628800
19:22:24.462 [main] INFO com.iluwatar.trampoline.TrampolineApp - Start calculating war casualties
19:22:24.472 [main] INFO com.iluwatar.trampoline.TrampolineApp - The number of orcs perished in the war:
3628800
```
## Class diagram
...
...
@@ -133,8 +135,8 @@ result 3628800
Use the Trampoline pattern when
*
For implementing tail
recursive function
. This pattern allows to switch on a stackless operation.
*
For interleaving
the
execution of two or more functions on the same thread.
*
For implementing tail
-recursive functions
. This pattern allows to switch on a stackless operation.
*
For interleaving execution of two or more functions on the same thread.
## Known uses
...
...
trampoline/src/main/java/com/iluwatar/trampoline/Trampoline.java
浏览文件 @
c5492184
...
...
@@ -107,6 +107,4 @@ public interface Trampoline<T> {
}
};
}
}
trampoline/src/main/java/com/iluwatar/trampoline/TrampolineApp.java
浏览文件 @
c5492184
...
...
@@ -39,9 +39,9 @@ public class TrampolineApp {
* Main program for showing pattern. It does loop with factorial function.
*/
public
static
void
main
(
String
[]
args
)
{
LOGGER
.
info
(
"
start pattern
"
);
LOGGER
.
info
(
"
Start calculating war casualties
"
);
var
result
=
loop
(
10
,
1
).
result
();
LOGGER
.
info
(
"
result
{}"
,
result
);
LOGGER
.
info
(
"
The number of orcs perished in the war:
{}"
,
result
);
}
...
...
@@ -55,5 +55,4 @@ public class TrampolineApp {
return
Trampoline
.
more
(()
->
loop
(
times
-
1
,
prod
*
times
));
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录