From c029ad4311c81b6f6f3d56afb53326f9396c18cf Mon Sep 17 00:00:00 2001 From: Atila Neves Date: Sun, 30 Dec 2018 13:40:56 +0000 Subject: [PATCH] Rust lambda --- lambda.cpp | 4 ++-- lambda.d | 6 +++--- lambda.rs | 24 ++++++++++++++++++++++++ timings.txt | 7 +++++++ 4 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 lambda.rs diff --git a/lambda.cpp b/lambda.cpp index b9ec4d8..76dd6fc 100644 --- a/lambda.cpp +++ b/lambda.cpp @@ -4,7 +4,7 @@ using namespace std; template -void printTriples(int N, F&& func) { +void triples(int N, F&& func) { int i = 0; for (int z = 1; ; ++z) { for (int x = 1; x <= z; ++x) { @@ -21,7 +21,7 @@ void printTriples(int N, F&& func) { int main() { - printTriples( + triples( 1000, [](auto t) { printf("(%i, %i, %i)\n", get<0>(t), get<1>(t), get<2>(t)); } ); diff --git a/lambda.d b/lambda.d index 0a352c7..bf98424 100644 --- a/lambda.d +++ b/lambda.d @@ -2,7 +2,7 @@ import core.stdc.stdio: printf; import std.typecons: tuple; -void printTriples(alias func)(int N) { +void triples(alias func)(int N) { int i = 0; for (int z = 1; ; ++z) { for (int x = 1; x <= z; ++x) { @@ -19,6 +19,6 @@ void printTriples(alias func)(int N) { void main() { - printTriples!((t) { printf("(%i, %i, %i)\n", t[0], t[1], t[2]); }) - (1000); + triples!((t) { printf("(%i, %i, %i)\n", t[0], t[1], t[2]); }) + (1000); } diff --git a/lambda.rs b/lambda.rs new file mode 100644 index 0000000..6b1c95f --- /dev/null +++ b/lambda.rs @@ -0,0 +1,24 @@ +pub fn main() { + triples(|x, y, z| { + println!("({}, {}, {})", x, y, z); + }); +} + + +fn triples(func: F) where F: Fn(i32, i32, i32) { + let mut i = 0 as i32; + for z in 1.. { + for x in 1..=z { + for y in x..=z { + if x*x + y*y == z*z { + func(x, y, z); + println!("({}, {}, {})", x, y, z); + i = i + 1; + if i == 1000 { + return; + } + } + } + } + } +} diff --git a/timings.txt b/timings.txt index aa1d44a..36d8e8c 100644 --- a/timings.txt +++ b/timings.txt @@ -1,3 +1,8 @@ +dmd version: 2.083.1 +clang version: 7.0.1 +rustc version: 1.31.1 + + Simple CT (ms) RT (ms) clang debug 59 599 @@ -18,3 +23,5 @@ dmd debug 33 368 dmd release 37 154 ldc debug 59 580 ldc release 79 154 +rustc debug 111 9252 +rustc release 134 352