Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Tim McNamara
notebook
Commits
acea01ae
Commit
acea01ae
authored
Feb 20, 2019
by
Tim McNamara
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add logistic-map.md
parent
e2c12632
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
0 deletions
+29
-0
rust/logistic-map.md
rust/logistic-map.md
+29
-0
No files found.
rust/logistic-map.md
0 → 100644
View file @
acea01ae
This snippet of code demonstrates how to create an infinitely long
`Iterator`
for a iterated function. A data structure stores some state, in this case
`x`
,
that is then fed into the actual function at each step.
```
rust
fn
logistic_map
(
x
:
f64
,
r
:
f64
)
->
f64
{
x
*
r
*
(
1.0
-
x
)
}
struct
LogisticMap
{
r
:
f64
,
x
:
f64
,
}
impl
Iterator
for
LogisticMap
{
type
Item
=
f64
;
fn
next
(
&
mut
self
)
->
Option
<
Self
::
Item
>
{
let
new_x
=
logistic_map
(
self
.x
,
self
.r
);
self
.x
=
new_x
;
Some
(
new_x
)
}
}
fn
main
()
{
let
res
:
Vec
<
f64
>
=
LogisticMap
{
r
:
3.995
,
x
:
0.5
}
.take
(
20
)
.collect
();
println!
(
"{:#?}"
,
res
);
}
```
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment