-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfibonacci_recursive_in_out_demo.puml
44 lines (35 loc) · 1.19 KB
/
fibonacci_recursive_in_out_demo.puml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@startuml
!procedure $create_state($name, $instance_name, $value)
/' just to demonstrate customization based on value, nothing special about this check. '/
!if $name >= 4
!$color = "#LightSalmon"
!else
!$color = ""
!endif
state "$name" as $instance_name $color
$instance_name : $value
!endprocedure
!procedure $connect_states($instance1_name, $instance2_name)
$instance1_name -down-> $instance2_name
!endprocedure
!function $fibonacci($n, $index = 0)
!if $n == 0
!$fibonacci_n = 0
!elseif $n == 1
!$fibonacci_n = 1
!else
/' preprocessor seems unable to maintain two unnamed temporary variables,
hence create local variables to hold return values of the recursive calls. '/
!$fibonacci_n_minus_1 = $fibonacci($n - 1, $index * 2 + 1)
!$fibonacci_n_minus_2 = $fibonacci($n - 2, $index * 2 + 2)
!$fibonacci_n = $fibonacci_n_minus_1 + $fibonacci_n_minus_2
!endif
$create_state($n, $index, $fibonacci_n)
!if $n >= 2
$connect_states($index, $index * 2 + 1)
$connect_states($index, $index * 2 + 2)
!endif
!return $fibonacci_n
!endfunction
!$ignore = $fibonacci(10)
@enduml