Skip to content

Commit d1169eb

Browse files
committed
Merge remote-tracking branch 'upstream/main'
2 parents b823c74 + b227621 commit d1169eb

30 files changed

+753
-372
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ANCHOR: all
2+
enum Coin {
3+
Penny: (),
4+
Nickel: (),
5+
Dime: (),
6+
Quarter: (),
7+
}
8+
9+
// ANCHOR: function
10+
fn value_in_cents(coin: Coin) -> felt252 {
11+
match coin {
12+
Coin::Penny(_) => 1,
13+
Coin::Nickel(_) => 5,
14+
Coin::Dime(_) => 10,
15+
Coin::Quarter(_) => 25,
16+
}
17+
}
18+
// ANCHOR_END: function
19+
// ANCHOR_END: all
20+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[derive(Drop)]
2+
enum UsState {
3+
Alabama: (),
4+
Alaska: (),
5+
}
6+
7+
#[derive(Drop)]
8+
enum Coin {
9+
Penny: (),
10+
Nickel: (),
11+
Dime: (),
12+
Quarter: (UsState, ),
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use option::OptionTrait;
2+
use debug::PrintTrait;
3+
4+
fn plus_one(x: Option<u8>) -> Option<u8> {
5+
match x {
6+
Option::Some(val) => Option::Some(val + 1),
7+
Option::None(_) => Option::None(()),
8+
}
9+
}
10+
11+
fn main() {
12+
let five: Option<u8> = Option::Some(5);
13+
let six: Option<u8> = plus_one(five);
14+
six.unwrap().print();
15+
let none = plus_one(Option::None(()));
16+
none.unwrap().print();
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ANCHOR: all
2+
// ANCHOR: enum_example
3+
#[derive(Drop)]
4+
enum Direction {
5+
North: (),
6+
East: (),
7+
South: (),
8+
West: (),
9+
}
10+
// ANCHOR_END: enum_example
11+
12+
fn main() {
13+
// ANCHOR: here
14+
let direction = Direction::North(());
15+
// ANCHOR_END: here
16+
}
17+
// ANCHOR_END: all
18+
19+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// ANCHOR: all
2+
use debug::PrintTrait;
3+
// ANCHOR: message
4+
#[derive(Drop)]
5+
enum Message {
6+
Quit: (),
7+
Echo: felt252,
8+
Move: (u128, u128),
9+
}
10+
// ANCHOR_END: message
11+
12+
// ANCHOR: trait_impl
13+
trait Processing {
14+
fn process(self: Message);
15+
}
16+
17+
impl ProcessingImpl of Processing {
18+
fn process(self: Message) {
19+
match self {
20+
Message::Quit(()) => {
21+
'quitting'.print();
22+
},
23+
Message::Echo(value) => {
24+
value.print();
25+
},
26+
Message::Move((x, y)) => {
27+
'moving'.print();
28+
},
29+
}
30+
}
31+
}
32+
// ANCHOR_END: trait_impl
33+
fn main() {
34+
// ANCHOR: main
35+
let msg: Message = Message::Quit(());
36+
msg.process();
37+
// ANCHOR_END: main
38+
}
39+
//ANCHOR_END: all
40+
41+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use array::ArrayTrait;
2+
use debug::PrintTrait;
3+
use option::OptionTrait;
4+
fn find_value_recursive(arr: @Array<felt252>, value: felt252, index: usize) -> Option<usize> {
5+
if index >= arr.len() {
6+
return Option::None(());
7+
}
8+
9+
if *arr.at(index) == value {
10+
return Option::Some(index);
11+
}
12+
13+
find_value_recursive(arr, value, index + 1)
14+
}
15+
16+
fn find_value_iterative(arr: @Array<felt252>, value: felt252) -> Option<usize> {
17+
let length = arr.len();
18+
let mut index = 0;
19+
let mut found: Option<usize> = Option::None(());
20+
loop {
21+
if index < length {
22+
if *arr.at(index) == value {
23+
found = Option::Some(index);
24+
break ();
25+
}
26+
} else {
27+
break ();
28+
}
29+
index += 1;
30+
};
31+
return found;
32+
}
33+
34+
#[test]
35+
#[available_gas(999999)]
36+
fn test_increase_amount() {
37+
let mut my_array = ArrayTrait::new();
38+
my_array.append(3);
39+
my_array.append(7);
40+
my_array.append(2);
41+
my_array.append(5);
42+
43+
let value_to_find = 7;
44+
let result = find_value_recursive(@my_array, value_to_find, 0);
45+
let result_i = find_value_iterative(@my_array, value_to_find);
46+
47+
match result {
48+
Option::Some(index) => {
49+
if index == 1 {
50+
'it worked'.print();
51+
}
52+
},
53+
Option::None(()) => {
54+
'not found'.print();
55+
},
56+
}
57+
match result_i {
58+
Option::Some(index) => {
59+
if index == 1 {
60+
'it worked'.print();
61+
}
62+
},
63+
Option::None(()) => {
64+
'not found'.print();
65+
},
66+
}
67+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use debug::PrintTrait;
2+
enum Coin {
3+
Penny: (),
4+
Nickel: (),
5+
Dime: (),
6+
Quarter: (),
7+
}
8+
9+
// ANCHOR: main
10+
fn value_in_cents(coin: Coin) -> felt252 {
11+
match coin {
12+
Coin::Penny(_) => {
13+
('Lucky penny!').print();
14+
1
15+
},
16+
Coin::Nickel(_) => 5,
17+
Coin::Dime(_) => 10,
18+
Coin::Quarter(_) => 25,
19+
}
20+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use debug::PrintTrait;
2+
3+
// ANCHOR: enum_def
4+
#[derive(Drop)]
5+
enum UsState {
6+
Alabama: (),
7+
Alaska: (),
8+
}
9+
10+
#[derive(Drop)]
11+
enum Coin {
12+
Penny: (),
13+
Nickel: (),
14+
Dime: (),
15+
Quarter: (UsState, ),
16+
}
17+
// ANCHOR_END: enum_def
18+
19+
// ANCHOR: function
20+
fn value_in_cents(coin: Coin) -> felt252 {
21+
match coin {
22+
Coin::Penny(_) => 1,
23+
Coin::Nickel(_) => 5,
24+
Coin::Dime(_) => 10,
25+
Coin::Quarter(state) => {
26+
state.print();
27+
25
28+
},
29+
}
30+
}
31+
// ANCHOR_END: function
32+
33+
// ANCHOR: print_impl
34+
impl UsStatePrintImpl of PrintTrait<UsState> {
35+
fn print(self: UsState) {
36+
match self {
37+
UsState::Alabama(_) => ('Alabama').print(),
38+
UsState::Alaska(_) => ('Alaska').print(),
39+
}
40+
}
41+
}
42+
// ANCHOR_END: print_impl
43+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use debug::PrintTrait;
2+
3+
// ANCHOR: here
4+
fn did_i_win(nb: felt252) {
5+
match nb {
6+
0 => ('You won!').print(),
7+
_ => ('You lost...').print(),
8+
}
9+
}
10+
// ANCHOR_END: here
11+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
mod front_of_house {
2+
mod hosting {
3+
fn add_to_waitlist() {}
4+
5+
fn seat_at_table() {}
6+
}
7+
8+
mod serving {
9+
fn take_order() {}
10+
11+
fn serve_order() {}
12+
13+
fn take_payment() {}
14+
}
15+
}

0 commit comments

Comments
 (0)