|
51 | 51 | }, |
52 | 52 | { |
53 | 53 | "id": 8, |
54 | | - "length": 37, |
| 54 | + "length": 36, |
55 | 55 | "source": "Iterate over list values - programming-idioms.org", |
56 | | - "text": "for x in items {\n\t\tdo_something(x);\n}" |
| 56 | + "text": "for x in items {\n\tdo_something(x);\n}" |
57 | 57 | }, |
58 | 58 | { |
59 | 59 | "id": 9, |
|
255 | 255 | }, |
256 | 256 | { |
257 | 257 | "id": 43, |
258 | | - "length": 145, |
| 258 | + "length": 143, |
259 | 259 | "source": "Parallelize execution of 1000 independent tasks - programming-idioms.org", |
260 | | - "text": "use std::thread;\nlet threads: Vec<_> = (0..1000).map(|i| {\n\t\tthread::spawn(move || f(i))\n}).collect();\nfor thread in threads {\n\t\tthread.join();\n}" |
| 260 | + "text": "use std::thread;\nlet threads: Vec<_> = (0..1000).map(|i| {\n\tthread::spawn(move || f(i))\n}).collect();\nfor thread in threads {\n\tthread.join();\n}" |
261 | 261 | }, |
262 | 262 | { |
263 | 263 | "id": 44, |
|
297 | 297 | }, |
298 | 298 | { |
299 | 299 | "id": 50, |
300 | | - "length": 149, |
| 300 | + "length": 147, |
301 | 301 | "source": "First-class function : compose - programming-idioms.org", |
302 | | - "text": "fn compose<'a, A, B, C, G, F>(f: F, g: G) -> Box<dyn Fn(A) -> C + 'a>\n\t\twhere F: 'a + Fn(A) -> B, G: 'a + Fn(B) -> C\n{\n\t\tBox::new(move |x| g(f(x)))\n}" |
| 302 | + "text": "fn compose<'a, A, B, C, G, F>(f: F, g: G) -> Box<dyn Fn(A) -> C + 'a>\n\twhere F: 'a + Fn(A) -> B, G: 'a + Fn(B) -> C\n{\n\tBox::new(move |x| g(f(x)))\n}" |
303 | 303 | }, |
304 | 304 | { |
305 | 305 | "id": 51, |
|
309 | 309 | }, |
310 | 310 | { |
311 | 311 | "id": 52, |
312 | | - "length": 149, |
| 312 | + "length": 147, |
313 | 313 | "source": "First-class function : generic composition - programming-idioms.org", |
314 | | - "text": "fn compose<'a, A, B, C, G, F>(f: F, g: G) -> Box<dyn Fn(A) -> C + 'a>\n\t\twhere F: 'a + Fn(A) -> B, G: 'a + Fn(B) -> C\n{\n\t\tBox::new(move |x| g(f(x)))\n}" |
| 314 | + "text": "fn compose<'a, A, B, C, G, F>(f: F, g: G) -> Box<dyn Fn(A) -> C + 'a>\n\twhere F: 'a + Fn(A) -> B, G: 'a + Fn(B) -> C\n{\n\tBox::new(move |x| g(f(x)))\n}" |
315 | 315 | }, |
316 | 316 | { |
317 | 317 | "id": 53, |
|
459 | 459 | }, |
460 | 460 | { |
461 | 461 | "id": 77, |
462 | | - "length": 129, |
| 462 | + "length": 128, |
463 | 463 | "source": "Launch 1000 parallel tasks and wait for completion - programming-idioms.org", |
464 | | - "text": "use std::thread;\nlet threads: Vec<_> = (0..1000).map(|i| thread::spawn(move || f(i))).collect();\nfor t in threads {\n\t\tt.join();\n}" |
| 464 | + "text": "use std::thread;\nlet threads: Vec<_> = (0..1000).map(|i| thread::spawn(move || f(i))).collect();\nfor t in threads {\n\tt.join();\n}" |
465 | 465 | }, |
466 | 466 | { |
467 | 467 | "id": 78, |
|
813 | 813 | }, |
814 | 814 | { |
815 | 815 | "id": 136, |
816 | | - "length": 108, |
| 816 | + "length": 107, |
817 | 817 | "source": "Iterate over map entries, ordered by values - programming-idioms.org", |
818 | | - "text": "use itertools::Itertools;\nfor (k, x) in mymap.iter().sorted_by_key(|x| x.1) {\n\t\tprintln!(\"[{},{}]\", k, x);\n}" |
| 818 | + "text": "use itertools::Itertools;\nfor (k, x) in mymap.iter().sorted_by_key(|x| x.1) {\n\tprintln!(\"[{},{}]\", k, x);\n}" |
819 | 819 | }, |
820 | 820 | { |
821 | 821 | "id": 137, |
|
939 | 939 | }, |
940 | 940 | { |
941 | 941 | "id": 157, |
942 | | - "length": 497, |
| 942 | + "length": 482, |
943 | 943 | "source": "Breadth-first traversing in a graph - programming-idioms.org", |
944 | | - "text": "use std::rc::{Rc, Weak};\nuse std::cell::RefCell;\nstruct Vertex<V> {\n\t\tvalue: V,\n\t\tneighbours: Vec<Weak<RefCell<Vertex<V>>>>,\n}\n// ...\nfn bft(start: Rc<RefCell<Vertex<V>>>, f: impl Fn(&V)) {\n\t\tlet mut q = vec![start];\n\t\tlet mut i = 0;\n\t\twhile i < q.len() {\n\t\t\tlet v = Rc::clone(&q[i]);\n\t\t\ti += 1;\n\t\t\t(f)(&v.borrow().value);\n\t\t\tfor n in &v.borrow().neighbours {\n\t\t\t\tlet n = n.upgrade().expect(\"Invalid neighbour\");\n\t\t\t\tif q.iter().all(|v| v.as_ptr() != n.as_ptr()) {\n\t\t\t\t\tq.push(n);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n}" |
| 944 | + "text": "use std::rc::{Rc, Weak};\nuse std::cell::RefCell;\nstruct Vertex<V> {\n\tvalue: V,\n\tneighbours: Vec<Weak<RefCell<Vertex<V>>>>,\n}\n// ...\nfn bft(start: Rc<RefCell<Vertex<V>>>, f: impl Fn(&V)) {\n\tlet mut q = vec![start];\n\tlet mut i = 0;\n\twhile i < q.len() {\n\t\tlet v = Rc::clone(&q[i]);\n\t\ti += 1;\n\t\t(f)(&v.borrow().value);\n\t\tfor n in &v.borrow().neighbours {\n\t\t\tlet n = n.upgrade().expect(\"Invalid neighbour\");\n\t\t\tif q.iter().all(|v| v.as_ptr() != n.as_ptr()) {\n\t\t\t\tq.push(n);\n\t\t\t}\n\t\t}\n\t}\n}" |
945 | 945 | }, |
946 | 946 | { |
947 | 947 | "id": 158, |
948 | | - "length": 465, |
| 948 | + "length": 450, |
949 | 949 | "source": "Depth-first traversing in a graph - programming-idioms.org", |
950 | | - "text": "use std::rc::{Rc, Weak};\nuse std::cell::RefCell;\nstruct Vertex<V> {\n\t\tvalue: V,\n\t\tneighbours: Vec<Weak<RefCell<Vertex<V>>>>,\n}\n// ...\nfn dft_helper(start: Rc<RefCell<Vertex<V>>>, f: &impl Fn(&V), s: &mut Vec<*const Vertex<V>>) {\n\t\ts.push(start.as_ptr());\n\t\t(f)(&start.borrow().value);\n\t\tfor n in &start.borrow().neighbours {\n\t\t\t\tlet n = n.upgrade().expect(\"Invalid neighbor\");\n\t\t\t\tif s.iter().all(|&p| p != n.as_ptr()) {\n\t\t\t\t\t\tSelf::dft_helper(n, f, s);\n\t\t\t\t}\n\t\t}\n}" |
| 950 | + "text": "use std::rc::{Rc, Weak};\nuse std::cell::RefCell;\nstruct Vertex<V> {\n\tvalue: V,\n\tneighbours: Vec<Weak<RefCell<Vertex<V>>>>,\n}\n// ...\nfn dft_helper(start: Rc<RefCell<Vertex<V>>>, f: &impl Fn(&V), s: &mut Vec<*const Vertex<V>>) {\n\ts.push(start.as_ptr());\n\t(f)(&start.borrow().value);\n\tfor n in &start.borrow().neighbours {\n\t\tlet n = n.upgrade().expect(\"Invalid neighbor\");\n\t\tif s.iter().all(|&p| p != n.as_ptr()) {\n\t\t\tSelf::dft_helper(n, f, s);\n\t\t}\n\t}\n}" |
951 | 951 | }, |
952 | 952 | { |
953 | 953 | "id": 159, |
|
1005 | 1005 | }, |
1006 | 1006 | { |
1007 | 1007 | "id": 168, |
1008 | | - "length": 141, |
| 1008 | + "length": 127, |
1009 | 1009 | "source": "Check if string contains only digits - programming-idioms.org", |
1010 | | - "text": "let chars_are_numeric: Vec<bool> = s.chars()\n\t\t\t\t\t\t\t\t.map(|c|c.is_numeric())\n\t\t\t\t\t\t\t\t.collect();\nlet b = !chars_are_numeric.contains(&false);" |
| 1010 | + "text": "let chars_are_numeric: Vec<bool> = s.chars()\n\t.map(|c|c.is_numeric())\n\t.collect();\nlet b = !chars_are_numeric.contains(&false);" |
1011 | 1011 | }, |
1012 | 1012 | { |
1013 | 1013 | "id": 169, |
|
1347 | 1347 | }, |
1348 | 1348 | { |
1349 | 1349 | "id": 225, |
1350 | | - "length": 63, |
| 1350 | + "length": 60, |
1351 | 1351 | "source": "Filter and transform list - programming-idioms.org", |
1352 | | - "text": "let y = x.iter()\n\t\t.filter(P)\n\t\t.map(T)\n\t\t.collect::<Vec<_>>();" |
| 1352 | + "text": "let y = x.iter()\n\t.filter(P)\n\t.map(T)\n\t.collect::<Vec<_>>();" |
1353 | 1353 | }, |
1354 | 1354 | { |
1355 | 1355 | "id": 226, |
|
1377 | 1377 | }, |
1378 | 1378 | { |
1379 | 1379 | "id": 230, |
1380 | | - "length": 129, |
| 1380 | + "length": 127, |
1381 | 1381 | "source": "Get a list of lines from a file - programming-idioms.org", |
1382 | | - "text": "use std::io::prelude::*;\nuse std::io::BufReader;\nlet lines = BufReader::new(File::open(path)?)\n\t\t.lines()\n\t\t.collect::<Vec<_>>();" |
| 1382 | + "text": "use std::io::prelude::*;\nuse std::io::BufReader;\nlet lines = BufReader::new(File::open(path)?)\n\t.lines()\n\t.collect::<Vec<_>>();" |
1383 | 1383 | }, |
1384 | 1384 | { |
1385 | 1385 | "id": 231, |
|
1431 | 1431 | }, |
1432 | 1432 | { |
1433 | 1433 | "id": 239, |
1434 | | - "length": 74, |
| 1434 | + "length": 73, |
1435 | 1435 | "source": "Formula with arrays - programming-idioms.org", |
1436 | | - "text": "for i in range 0..a.len() {\n\t\ta[i] = e*(a[i] + b[i] + c[i] + d[i].cos())\n}" |
| 1436 | + "text": "for i in range 0..a.len() {\n\ta[i] = e*(a[i] + b[i] + c[i] + d[i].cos())\n}" |
1437 | 1437 | }, |
1438 | 1438 | { |
1439 | 1439 | "id": 240, |
1440 | | - "length": 131, |
| 1440 | + "length": 121, |
1441 | 1441 | "source": "Type with automatic deep deallocation - programming-idioms.org", |
1442 | | - "text": "struct T {\n\t\ts: String,\n\t\tn: Vec<usize>,\n}\nfn main() {\n\t\tlet v = T {\n\t\t\t\ts: \"Hello, world!\".into(),\n\t\t\t\tn: vec![1,4,9,16,25]\n\t\t};\n}" |
| 1442 | + "text": "struct T {\n\ts: String,\n\tn: Vec<usize>,\n}\nfn main() {\n\tlet v = T {\n\ts: \"Hello, world!\".into(),\n\tn: vec![1,4,9,16,25]\n\t};\n}" |
1443 | 1443 | }, |
1444 | 1444 | { |
1445 | 1445 | "id": 241, |
|
1473 | 1473 | }, |
1474 | 1474 | { |
1475 | 1475 | "id": 246, |
1476 | | - "length": 138, |
| 1476 | + "length": 136, |
1477 | 1477 | "source": "Pad a string on both sides - programming-idioms.org", |
1478 | | - "text": "use std::iter;\nlet s2 = iter::repeat(c).take((m + 1) / 2).collect::<String>()\n\t\t+ &s\n\t\t+ &iter::repeat(c).take(m / 2).collect::<String>();" |
| 1478 | + "text": "use std::iter;\nlet s2 = iter::repeat(c).take((m + 1) / 2).collect::<String>()\n\t+ &s\n\t+ &iter::repeat(c).take(m / 2).collect::<String>();" |
1479 | 1479 | }, |
1480 | 1480 | { |
1481 | 1481 | "id": 247, |
|
1539 | 1539 | }, |
1540 | 1540 | { |
1541 | 1541 | "id": 257, |
1542 | | - "length": 101, |
| 1542 | + "length": 99, |
1543 | 1543 | "source": "for else loop - programming-idioms.org", |
1544 | | - "text": "if let None = items.iter().find(|&&item| item == \"rockstar programmer\") {\n\t\tprintln!(\"NotFound\");\n\t};" |
| 1544 | + "text": "if let None = items.iter().find(|&&item| item == \"rockstar programmer\") {\n\tprintln!(\"NotFound\");\n};" |
1545 | 1545 | }, |
1546 | 1546 | { |
1547 | 1547 | "id": 258, |
|
0 commit comments