Add basic closure example

This commit is contained in:
Timothy Warren 2019-02-01 14:50:18 -05:00
parent a05a4eb790
commit 4015bdac47
5 changed files with 116 additions and 51 deletions

View File

@ -21,6 +21,7 @@
<cargoProject FILE="$PROJECT_DIR$/lifetimes/Cargo.toml" /> <cargoProject FILE="$PROJECT_DIR$/lifetimes/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/adder/Cargo.toml" /> <cargoProject FILE="$PROJECT_DIR$/adder/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/minigrep/Cargo.toml" /> <cargoProject FILE="$PROJECT_DIR$/minigrep/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/closures/Cargo.toml" />
</component> </component>
<component name="ComposerJsonPluginSettings"> <component name="ComposerJsonPluginSettings">
<unboundedVersionInspectionSettings> <unboundedVersionInspectionSettings>

View File

@ -82,8 +82,13 @@
<sourceFolder url="file://$MODULE_DIR$/minigrep/examples" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/minigrep/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/minigrep/tests" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/minigrep/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/minigrep/benches" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/minigrep/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/closures/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/closures/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/closures/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/closures/benches" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/adder/target" /> <excludeFolder url="file://$MODULE_DIR$/adder/target" />
<excludeFolder url="file://$MODULE_DIR$/branches/target" /> <excludeFolder url="file://$MODULE_DIR$/branches/target" />
<excludeFolder url="file://$MODULE_DIR$/closures/target" />
<excludeFolder url="file://$MODULE_DIR$/ctof/target" /> <excludeFolder url="file://$MODULE_DIR$/ctof/target" />
<excludeFolder url="file://$MODULE_DIR$/enums/target" /> <excludeFolder url="file://$MODULE_DIR$/enums/target" />
<excludeFolder url="file://$MODULE_DIR$/errors/target" /> <excludeFolder url="file://$MODULE_DIR$/errors/target" />

View File

@ -13,30 +13,12 @@
<option name="LAST_RESOLUTION" value="IGNORE" /> <option name="LAST_RESOLUTION" value="IGNORE" />
</component> </component>
<component name="FileEditorManager"> <component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300"> <leaf>
<file pinned="false" current-in-tab="true"> <file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/minigrep/src/main.rs"> <entry file="file://$PROJECT_DIR$/closures/src/main.rs">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="320"> <state relative-caret-position="1136">
<caret line="20" lean-forward="true" selection-start-line="20" selection-end-line="20" /> <caret line="71" lean-forward="true" selection-start-line="71" selection-end-line="71" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/minigrep/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="517">
<caret line="33" column="6" selection-start-line="33" selection-start-column="6" selection-end-line="33" selection-end-column="6" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/minigrep/poem.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="128">
<caret line="8" column="19" selection-start-line="8" selection-start-column="19" selection-end-line="8" selection-end-column="19" />
</state> </state>
</provider> </provider>
</entry> </entry>
@ -53,9 +35,11 @@
<component name="FindInProjectRecents"> <component name="FindInProjectRecents">
<findStrings> <findStrings>
<find>ftoc</find> <find>ftoc</find>
<find>expensive_closure(intensity)</find>
</findStrings> </findStrings>
<replaceStrings> <replaceStrings>
<replace>f_to_c</replace> <replace>f_to_c</replace>
<replace>expensive_result.value(intensity)</replace>
</replaceStrings> </replaceStrings>
</component> </component>
<component name="Git.Settings"> <component name="Git.Settings">
@ -97,6 +81,7 @@
<option value="$PROJECT_DIR$/minigrep/poem.txt" /> <option value="$PROJECT_DIR$/minigrep/poem.txt" />
<option value="$PROJECT_DIR$/minigrep/src/lib.rs" /> <option value="$PROJECT_DIR$/minigrep/src/lib.rs" />
<option value="$PROJECT_DIR$/minigrep/src/main.rs" /> <option value="$PROJECT_DIR$/minigrep/src/main.rs" />
<option value="$PROJECT_DIR$/closures/src/main.rs" />
</list> </list>
</option> </option>
</component> </component>
@ -106,11 +91,11 @@
<annotation-enable>false</annotation-enable> <annotation-enable>false</annotation-enable>
<other-services-enabled>false</other-services-enabled> <other-services-enabled>false</other-services-enabled>
</component> </component>
<component name="ProjectFrameBounds" extendedState="6"> <component name="ProjectFrameBounds">
<option name="x" value="1680" /> <option name="x" value="1920" />
<option name="y" value="371" /> <option name="y" value="-438" />
<option name="width" value="1050" /> <option name="width" value="1080" />
<option name="height" value="876" /> <option name="height" value="1897" />
</component> </component>
<component name="ProjectLevelVcsManager" settingsEditedManually="true"> <component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="1" id="Add" /> <ConfirmationsSetting value="1" id="Add" />
@ -127,22 +112,10 @@
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" /> <item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" /> <item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
</path> </path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="minigrep" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="minigrep" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
</expand> </expand>
<select /> <select />
</subPane> </subPane>
</pane> </pane>
<pane id="PackagesPane" />
<pane id="ProjectPane"> <pane id="ProjectPane">
<subPane> <subPane>
<expand> <expand>
@ -165,11 +138,12 @@
<select /> <select />
</subPane> </subPane>
</pane> </pane>
<pane id="PackagesPane" />
</panes> </panes>
</component> </component>
<component name="PropertiesComponent"> <component name="PropertiesComponent">
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" /> <property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1548969593161" /> <property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549047706729" />
<property name="javascript.nodejs.core.library.configured.version" value="7.1.0" /> <property name="javascript.nodejs.core.library.configured.version" value="7.1.0" />
<property name="js.eslint.eslintPackage" value="$USER_HOME$/.yarn-config/global/node_modules/.bin/eslint" /> <property name="js.eslint.eslintPackage" value="$USER_HOME$/.yarn-config/global/node_modules/.bin/eslint" />
<property name="js.eslint.nodeInterpreter" value="project" /> <property name="js.eslint.nodeInterpreter" value="project" />
@ -183,7 +157,7 @@
<property name="nodejs_npm_path_reset_for_default_project" value="true" /> <property name="nodejs_npm_path_reset_for_default_project" value="true" />
<property name="org.rust.cargo.project.model.PROJECT_DISCOVERY" value="true" /> <property name="org.rust.cargo.project.model.PROJECT_DISCOVERY" value="true" />
<property name="run.code.analysis.last.selected.profile" value="pProject Default" /> <property name="run.code.analysis.last.selected.profile" value="pProject Default" />
<property name="settings.editor.selected.configurable" value="reference.settingsdialog.IDE.editor.colors.Language Defaults" /> <property name="settings.editor.selected.configurable" value="reference.settingsdialog.IDE.editor.colors.General" />
</component> </component>
<component name="RunDashboard"> <component name="RunDashboard">
<option name="ruleStates"> <option name="ruleStates">
@ -197,7 +171,7 @@
</list> </list>
</option> </option>
</component> </component>
<component name="RunManager" selected="Cargo Command.Test lib::tests"> <component name="RunManager" selected="Cargo Command.Run closures">
<configuration name="&lt;template&gt;" type="TestNG" default="true" selected="false"> <configuration name="&lt;template&gt;" type="TestNG" default="true" selected="false">
<option name="MAIN_CLASS_NAME" /> <option name="MAIN_CLASS_NAME" />
<option name="VM_PARAMETERS" value="-ea" /> <option name="VM_PARAMETERS" value="-ea" />
@ -216,13 +190,13 @@
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="Run lifetimes" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true"> <configuration name="Run closures" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" /> <option name="channel" value="DEFAULT" />
<option name="command" value="run --package lifetimes --bin lifetimes" /> <option name="command" value="run --package closures --bin closures" />
<option name="allFeatures" value="false" /> <option name="allFeatures" value="false" />
<option name="nocapture" value="false" /> <option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" /> <option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/lifetimes" /> <option name="workingDirectory" value="file://$PROJECT_DIR$/closures" />
<envs /> <envs />
<method v="2" /> <method v="2" />
</configuration> </configuration>
@ -275,11 +249,11 @@
</configuration> </configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Cargo Command.Run closures" />
<item itemvalue="Cargo Command.Test lib::tests" /> <item itemvalue="Cargo Command.Test lib::tests" />
<item itemvalue="Cargo Command.Test tests::greater_than_100" /> <item itemvalue="Cargo Command.Test tests::greater_than_100" />
<item itemvalue="Cargo Command.Test tests::greeting_contains_name" /> <item itemvalue="Cargo Command.Test tests::greeting_contains_name" />
<item itemvalue="Cargo Command.Test tests::it_works" /> <item itemvalue="Cargo Command.Test tests::it_works" />
<item itemvalue="Cargo Command.Run lifetimes" />
</list> </list>
</recent_temporary> </recent_temporary>
</component> </component>
@ -304,11 +278,11 @@
</history-entry> </history-entry>
</component> </component>
<component name="ToolWindowManager"> <component name="ToolWindowManager">
<frame x="1680" y="-410" width="1050" height="1657" extended-state="6" /> <frame x="1920" y="-438" width="1080" height="1897" extended-state="6" />
<editor active="true" /> <editor active="true" />
<layout> <layout>
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.49710983" visible="true" weight="0.26141885" /> <window_info active="true" content_ui="combo" id="Project" order="0" sideWeight="0.49692565" visible="true" weight="0.2540132" />
<window_info id="Structure" order="1" sideWeight="0.50289017" side_tool="true" visible="true" weight="0.26141885" /> <window_info id="Structure" order="1" sideWeight="0.50307435" side_tool="true" visible="true" weight="0.2540132" />
<window_info id="Image Layers" order="2" /> <window_info id="Image Layers" order="2" />
<window_info id="Designer" order="3" /> <window_info id="Designer" order="3" />
<window_info id="Capture Tool" order="4" /> <window_info id="Capture Tool" order="4" />
@ -316,7 +290,7 @@
<window_info id="Cargo" order="6" sideWeight="0.49791494" weight="0.3286119" /> <window_info id="Cargo" order="6" sideWeight="0.49791494" weight="0.3286119" />
<window_info anchor="bottom" id="Message" order="0" /> <window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32979318" /> <window_info anchor="bottom" id="Find" order="1" weight="0.32979318" />
<window_info anchor="bottom" id="Run" order="2" weight="0.29761136" /> <window_info anchor="bottom" id="Run" order="2" weight="0.29737285" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" /> <window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" /> <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" /> <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
@ -324,7 +298,7 @@
<window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" /> <window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" />
<window_info anchor="bottom" id="Version Control" order="8" /> <window_info anchor="bottom" id="Version Control" order="8" />
<window_info anchor="bottom" id="Inspection Results" order="9" weight="0.32979318" /> <window_info anchor="bottom" id="Inspection Results" order="9" weight="0.32979318" />
<window_info active="true" anchor="bottom" id="Terminal" order="10" visible="true" weight="0.32989025" /> <window_info anchor="bottom" id="Terminal" order="10" weight="0.28563443" />
<window_info anchor="bottom" id="Event Log" order="11" side_tool="true" /> <window_info anchor="bottom" id="Event Log" order="11" side_tool="true" />
<window_info anchor="right" id="Commander" order="0" weight="0.4" /> <window_info anchor="right" id="Commander" order="0" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" /> <window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
@ -627,5 +601,12 @@
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/closures/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1136">
<caret line="71" lean-forward="true" selection-start-line="71" selection-end-line="71" />
</state>
</provider>
</entry>
</component> </component>
</project> </project>

7
closures/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "closures"
version = "0.1.0"
authors = ["Timothy Warren <twarren@nabancard.com>"]
edition = "2018"
[dependencies]

71
closures/src/main.rs Normal file
View File

@ -0,0 +1,71 @@
use std::thread;
use std::time::Duration;
// @TODO: use a hashmap to memoize based on teh argument passed
// @TODO: make more generic, to accept different types of arguments
struct Cacher<T>
where T: Fn(u32) -> u32
{
calculation: T,
value: Option<u32>,
}
impl<T> Cacher<T>
where T: Fn(u32) -> u32
{
fn new(calculation: T) -> Cacher<T> {
Cacher {
calculation,
value: None,
}
}
fn value(&mut self, arg: u32) -> u32 {
match self.value {
Some(v) => v,
None => {
let v = (self.calculation)(arg);
self.value = Some(v);
v
},
}
}
}
fn generate_workout(intensity: u32, random_number: u32) {
let mut expensive_result = Cacher::new(|num| {
println!("calculating slowly...");
thread::sleep(Duration::from_secs(2));
num
});
if intensity < 25 {
println!(
"Today do {} pushups!",
expensive_result.value(intensity)
);
println!(
"Next, do {} situps!",
expensive_result.value(intensity)
);
} else {
if random_number == 3 {
println!("Take a break today! Remember to stay hydrated!");
} else {
println!(
"Today, run for {} minutes!",
expensive_result.value(intensity)
);
}
}
}
fn main() {
let simulated_user_specified_value = 10;
let simulated_random_number = 7;
generate_workout(
simulated_user_specified_value,
simulated_random_number
);
}