Skip to content

Commit

Permalink
Rust lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
atilaneves committed Dec 30, 2018
1 parent 7e16c40 commit c029ad4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lambda.cpp
Expand Up @@ -4,7 +4,7 @@
using namespace std;

template<typename F>
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) {
Expand All @@ -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)); }
);
Expand Down
6 changes: 3 additions & 3 deletions lambda.d
Expand Up @@ -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) {
Expand All @@ -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);
}
24 changes: 24 additions & 0 deletions lambda.rs
@@ -0,0 +1,24 @@
pub fn main() {
triples(|x, y, z| {
println!("({}, {}, {})", x, y, z);
});
}


fn triples<F>(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;
}
}
}
}
}
}
7 changes: 7 additions & 0 deletions 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
Expand All @@ -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

0 comments on commit c029ad4

Please sign in to comment.