Skip to content

Commit 95443fe

Browse files
committed
Added dict comp to rust
Tidyed java dict comp (what was I thinking)
1 parent b16fd6d commit 95443fe

3 files changed

Lines changed: 88 additions & 19 deletions

File tree

teachprogramming/static/language_reference/languages/java/Java.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ Java SE (Standard Edition) JDK (Java Development Kit) // V
1717

1818
import java.io.BufferedWriter; //Add at top of file // VER: file_write
1919
import java.io.FileWriter; // VER: file_write
20-
20+
2121
import java.util.List; // VER: list_comprehension
2222
import java.util.Arrays; // VER: list_comprehension
2323
import java.util.ArrayList; // VER: list_comprehension
2424

2525
import java.util.stream.Collectors; // VER: list_comprehension,dict_comprehension
2626
import java.util.Map; // VER: dict_comprehension
2727
import java.util.HashMap; // VER: define_map
28-
import static java.util.Map.entry; // VER: dict_comprehension
28+
import static java.util.Map.entry; // VER: dict_comprehension,define_map
2929

3030
import java.util.Set; // VER: define_set
3131
import java.util.HashSet; // VER: define_set
@@ -286,14 +286,11 @@ void split_strings() {
286286

287287
void list_comprehension() {
288288
// int[] i = new int[]{1,2,3,4}; Arrays.stream(i).collect(toList());
289-
List<Integer> data1 = new ArrayList<>(Arrays.asList(new Integer[]{1,2,3,4,5,6})); // VER: list_comprehension
290-
List<Integer> data2 = data1.stream( // VER: list_comprehension
291-
).filter( // VER: list_comprehension
292-
(i) -> i >= 3 // VER: list_comprehension
293-
).map( // VER: list_comprehension
294-
(i) -> i * 2 // VER: list_comprehension
295-
).collect(Collectors.toList()); // VER: list_comprehension
296-
289+
List<Integer> data1 = new ArrayList<>(Arrays.asList(new Integer[]{1,2,3,4,5,6})); // VER: list_comprehension
290+
List<Integer> data2 = data1.stream() // VER: list_comprehension
291+
.filter((i) -> i >= 3) // VER: list_comprehension
292+
.map((i) -> i * 2) // VER: list_comprehension
293+
.collect(Collectors.toList()); // VER: list_comprehension
297294
System.out.println(data2);
298295
}
299296

@@ -303,13 +300,10 @@ void dict_comprehension() {
303300
entry("b", 2), // VER: dict_comprehension
304301
entry("c", 3) // VER: dict_comprehension
305302
); // VER: dict_comprehension
306-
Map<Integer, String> data4 = data3.entrySet().stream( // VER: dict_comprehension
307-
).filter( // VER: dict_comprehension
308-
(e) -> e.getValue() >= 2 // VER: dict_comprehension
309-
).map( // VER: dict_comprehension
310-
(e) -> entry(e.getValue() + 10, e.getKey()) // VER: dict_comprehension
311-
).collect(Collectors.toMap((e)->e.getKey(), (e)->e.getValue())); // VER: dict_comprehension
312-
303+
Map<Integer, String> data4 = data3.entrySet().stream() // VER: dict_comprehension
304+
.filter((e) -> e.getValue() >= 2) // VER: dict_comprehension
305+
.map ((e) -> entry(e.getValue() + 10, e.getKey())) // VER: dict_comprehension
306+
.collect(Collectors.toMap((e)->e.getKey(), (e)->e.getValue())); // VER: dict_comprehension
313307
System.out.println(data4);
314308
}
315309

@@ -318,7 +312,7 @@ void define_2d_arrays() {
318312
Integer width = 3;
319313
Integer height = 3;
320314
Integer value = 1;
321-
315+
322316
Integer[][] grid1 = new Integer[width][height]; // VER: define_2d_arrays_with_nested_arrays
323317
for (var row: grid1) {Arrays.fill(row, value);} // VER: define_2d_arrays_with_nested_arrays
324318
grid1[2][1] = 5; // VER: define_2d_arrays_with_nested_arrays

teachprogramming/static/language_reference/languages/python/python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def define_set():
368368
print(aa - cc) # VER: define_set
369369
print(cc in aa) # VER: define_set
370370
aa.add(5) # VER: define_set
371-
371+
372372

373373
def json_data():
374374
data = [ # VER: json_data

teachprogramming/static/language_reference/languages/rust/rust.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use std::collections::HashMap; // VER: define_map
2+
use std::collections::HashSet; // VER: define_set
3+
24

35
//fn main() { // VER: hello_world
46
fn hello_world() {
@@ -58,6 +60,49 @@ fn if_statement() {
5860
} // VER: if_statement
5961
}
6062

63+
fn for_loop() {
64+
let data = [5,6,7]; // VER: for_loop
65+
for i in 0..data.len() { // VER: for_loop
66+
println!("{}", data[i]); // VER: for_loop
67+
} // VER: for_loop
68+
}
69+
70+
fn while_loop() {
71+
let mut count = 0; // VER: while_loop
72+
while count < 10 { // VER: while_loop
73+
println!("Count is {}", count); // VER: while_loop
74+
count = count + 2; // VER: while_loop
75+
} // VER: while_loop
76+
}
77+
78+
fn for_each_loop() {
79+
let ff = ["a","b","c"]; // VER: for_each_loop
80+
for f in ff { // VER: for_each_loop
81+
println!("{}", f); // VER: for_each_loop
82+
} // VER: for_each_loop
83+
}
84+
85+
86+
fn sayHello() { // VER: function
87+
println!("Hello"); // VER: function
88+
println!("Goodbye"); // VER: function
89+
} // VER: function
90+
fn function() {
91+
sayHello(); // VER: function
92+
}
93+
94+
fn biggest(a: i32, b: i32) -> i32 { // VER: function_with_return_value
95+
if a > b { // VER: function_with_return_value
96+
return a; // VER: function_with_return_value
97+
} // VER: function_with_return_value
98+
else { // VER: function_with_return_value
99+
return b; // VER: function_with_return_value
100+
} // VER: function_with_return_value
101+
} // VER: function_with_return_value
102+
fn function_with_return_value() -> () {
103+
println!("{}", biggest(1,2)); // VER: function_with_return_value
104+
}
105+
61106

62107
fn define_fixed_array() {
63108
let mut aa: [&str; 3] = [""; 3]; // VER: define_fixed_array
@@ -131,6 +176,29 @@ fn split_strings() {
131176
println!("{}", csv_line_test2); // VER: split_strings
132177
}
133178

179+
fn dict_comprehension() {
180+
let data3 = HashMap::from([ // VER: dict_comprehension
181+
("a", 1), // VER: dict_comprehension
182+
("b", 2), // VER: dict_comprehension
183+
("c", 3), // VER: dict_comprehension
184+
]); // VER: dict_comprehension
185+
let data4 = data3.iter() // VER: dict_comprehension
186+
.filter(|(&k,&v)| v>=2) // VER: dict_comprehension
187+
.map(|(&k,&v)| (v+10,k)) // VER: dict_comprehension
188+
.collect::<HashMap<_,_>>(); // VER: dict_comprehension
189+
println!("{:?}", data4); // VER: dict_comprehension
190+
}
191+
192+
fn define_set() {
193+
let mut aa: HashSet<_> = vec![1,2,3].into_iter().collect(); // VER: define_set
194+
let bb: HashSet<_> = vec![2,3,4].into_iter().collect(); // VER: define_set
195+
let cc: HashSet<_> = vec![1,2].into_iter().collect(); // VER: define_set
196+
println!("{:?}", aa.union(&bb).collect::<Vec<_>>()); // VER: define_set
197+
println!("{:?}", aa.intersection(&bb).collect::<Vec<_>>()); // VER: define_set
198+
println!("{:?}", aa.difference(&bb).collect::<Vec<_>>()); // VER: define_set
199+
println!("{:?}", cc.is_subset(&aa)); // VER: define_set
200+
aa.insert(5); // VER: define_set
201+
}
134202

135203

136204
fn main() {
@@ -139,9 +207,16 @@ fn main() {
139207
define_constants();
140208
arithmetic();
141209
if_statement();
210+
for_loop();
211+
while_loop();
212+
for_each_loop();
213+
function();
214+
function_with_return_value();
142215
define_fixed_array();
143216
define_list();
144217
define_map();
145218
string_concatenation();
146219
split_strings();
220+
dict_comprehension();
221+
define_set();
147222
}

0 commit comments

Comments
 (0)