Skip to content

Commit c432a57

Browse files
author
Artem Tyurin
committed
Add example: Collections
1 parent ef059fd commit c432a57

File tree

6 files changed

+114
-1
lines changed

6 files changed

+114
-1
lines changed

examples/collections/collections.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
enum class Food {
2+
vegetable,
3+
fruit,
4+
fastfood
5+
}
6+
7+
val farm = hashMapOf(
8+
Food.vegetable to setOf("🍅", "🌽"),
9+
Food.fruit to setOf("🍌", "🍏")
10+
)
11+
12+
fun main() {
13+
val vegetables = farm[Food.vegetable]
14+
if (vegetables != null) {
15+
if (vegetables.contains("🍅")) {
16+
println("We have tomatoes!")
17+
}
18+
}
19+
}

examples/collections/collections.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::collections::HashMap;
2+
use std::collections::HashSet;
3+
4+
#[derive(Hash, PartialEq, Eq)]
5+
enum Food {
6+
Vegetable,
7+
Fruit,
8+
FastFood,
9+
}
10+
11+
fn main() {
12+
let mut vegetables = HashSet::new();
13+
vegetables.insert("🍅");
14+
vegetables.insert("🌽");
15+
16+
let mut fruits = HashSet::new();
17+
fruits.insert("🍌");
18+
fruits.insert("🍏");
19+
20+
let mut farm = HashMap::new();
21+
farm.insert(Food::Vegetable, vegetables);
22+
farm.insert(Food::Fruit, fruits);
23+
24+
if let Some(vegetables) =
25+
farm.get(&Food::Vegetable)
26+
{
27+
if vegetables.contains("🍅") {
28+
println!("We have tomatoes!");
29+
}
30+
}
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
enum Food {
2+
case vegetable
3+
case fruit
4+
case fastfood
5+
}
6+
7+
var farm: [Food:Set<String>] = [
8+
.vegetable: Set(["🍅", "🌽"]),
9+
.fruit: Set(["🍌", "🍏"])
10+
]
11+
12+
if let vegetables = farm[.vegetable] {
13+
if (vegetables.contains("🍅")) {
14+
print("We have tomatoes!")
15+
}
16+
}

examples/collections/collections.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
enum Food {
2+
vegetable,
3+
fruit,
4+
fastfood
5+
}
6+
7+
const farm = new Map([
8+
[Food.vegetable, new Set(["🍅", "🌽"])],
9+
[Food.fruit, new Set(["🍌", "🍏"])]
10+
]);
11+
12+
const vegetables = farm.get(Food.vegetable);
13+
if (vegetables) {
14+
if (vegetables.has("🍅")) {
15+
console.log("We have tomatoes!");
16+
}
17+
}

src/examples.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,32 @@ export const examples: Example[] = [
4545
)
4646
}
4747
},
48+
{
49+
key: "collections",
50+
title: "Collections",
51+
code: {
52+
swift: readFileSync(
53+
__dirname +
54+
"/../examples/collections/collections.swift",
55+
"utf-8"
56+
),
57+
kotlin: readFileSync(
58+
__dirname +
59+
"/../examples/collections/collections.kt",
60+
"utf-8"
61+
),
62+
typescript: readFileSync(
63+
__dirname +
64+
"/../examples/collections/collections.ts",
65+
"utf-8"
66+
),
67+
rust: readFileSync(
68+
__dirname +
69+
"/../examples/collections/collections.rs",
70+
"utf-8"
71+
)
72+
}
73+
},
4874
{
4975
key: "higher-order-functions",
5076
title: "Higher-order functions",

src/playground.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import * as LZString from "lz-string";
33
export const playground = {
44
kotlin: (code: string) =>
55
`https://play.kotlinlang.org/#${btoa(
6-
JSON.stringify({ code })
6+
unescape(
7+
encodeURIComponent(
8+
JSON.stringify({ code })
9+
)
10+
)
711
)}`,
812

913
swift: (code: string) =>

0 commit comments

Comments
 (0)