Skip to content

My solution #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/macrokata.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .zed/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Folder-specific settings
//
// For a full list of overridable settings, and general information on folder-specific settings,
// see the documentation: https://zed.dev/docs/configuring-zed#settings-files
{
"languages": {
"Rust": {
"show_edit_predictions": false
}
}
}
6 changes: 5 additions & 1 deletion exercises/01_my_first_macro/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ fn show_output() {
////////// DO NOT CHANGE ABOVE HERE /////////

// TODO: create `show_output!()` macro.

macro_rules! show_output {
() => {
show_output()
};
}
////////// DO NOT CHANGE BELOW HERE /////////

fn main() {
Expand Down
12 changes: 11 additions & 1 deletion exercises/02_numbers/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ fn print_result(num: i32) {
////////// DO NOT CHANGE ABOVE HERE /////////

// TODO: create `num!()` macro.

macro_rules! num {
(one) => {
1
};
(two) => {
2
};
(three) => {
3
};
}
////////// DO NOT CHANGE BELOW HERE /////////

fn main() {
Expand Down
8 changes: 8 additions & 0 deletions exercises/03_literal_variables/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ fn print_result(num: i32) {
////////// DO NOT CHANGE ABOVE HERE /////////

// TODO: create `math!()` macro.
macro_rules! math {
($a:literal plus $b:literal) => {
$a + $b
};
(square $a:literal) => {
$a * $a
};
}

////////// DO NOT CHANGE BELOW HERE /////////

Expand Down
8 changes: 8 additions & 0 deletions exercises/04_expression_variables/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ fn print_result(num: i32) {
////////// DO NOT CHANGE ABOVE HERE /////////

// TODO: create `math!()` macro.
macro_rules! math {
($a:expr, plus, $b: expr) => {
$a + $b
};
(square $a:expr) => {
$a * $a
};
}

////////// DO NOT CHANGE BELOW HERE /////////

Expand Down
16 changes: 15 additions & 1 deletion exercises/05_more_complex_example/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,21 @@ impl Coordinate {
////////// DO NOT CHANGE ABOVE HERE /////////

// TODO: Create `for_2d!` macro here.

macro_rules! for_2d {
(
$row: ident <$row_type: ty> in $row_items: expr,
$col: ident <$col_type: ty> in $col_items: expr,
$block: block
) => {
for $row in $row_items {
let $row: $row_type = $row;
for $col in $col_items {
let $col: $col_type = $col;
$block
}
}
};
}
////////// DO NOT CHANGE BELOW HERE /////////

fn main() {
Expand Down
6 changes: 5 additions & 1 deletion exercises/06_repetition/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ fn print_success() {
////////// DO NOT CHANGE ABOVE HERE /////////

// TODO: create `if_any!()` macro.

macro_rules! if_any {
($($predicate: expr),+; $block: block) => {
if $($predicate)||* $block
};
}
////////// DO NOT CHANGE BELOW HERE /////////

fn main() {
Expand Down
10 changes: 9 additions & 1 deletion exercises/07_more_repetition/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ fn print_hashmap(hashmap: &HashMap<&str, &str>) {
////////// DO NOT CHANGE ABOVE HERE /////////

// TODO: create `hashmap!()` macro.

macro_rules! hashmap {
($($key:literal => $value: expr),* $(,)*) => {
{
let mut hm = HashMap::new();
$(hm.insert($key, $value));*;
hm
}
};
}
////////// DO NOT CHANGE BELOW HERE /////////

fn main() {
Expand Down
13 changes: 12 additions & 1 deletion exercises/08_nested_repetition/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@ fn print_vec<V: std::fmt::Debug>(vec: &Vec<V>) {
////////// DO NOT CHANGE ABOVE HERE /////////

// TODO: create `graph!()` macro.

macro_rules! graph {
(
$($key:literal -> ($(
$val:expr
),*
));* $(;)?
) => {{
let mut vec = Vec::new();
$($(vec.push(($key, $val));)*)*
vec
}};
}
////////// DO NOT CHANGE BELOW HERE /////////

#[allow(clippy::vec_init_then_push)]
Expand Down
23 changes: 12 additions & 11 deletions exercises/09_ambiguity_and_ordering/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[allow(dead_code)]
////////// DO NOT CHANGE BELOW HERE /////////

/// This enum should represent what code the user wrote exactly.
Expand All @@ -24,29 +25,29 @@ impl NumberType {

// Sum together at least two expressions.
macro_rules! sum {
($($expr:expr),+ , $lastexpr:expr) => {
$($expr + )+ $lastexpr
}
($first:expr $(, $expr:expr)*) => {
$first$(+ $expr)*
};
}

macro_rules! get_number_type {
( $e:expr ) => {
NumberType::UnknownBecauseExpr($e)
};
( $block:block ) => {
NumberType::UnknownBecauseBlock($block)
(-$negative:literal ) => {
NumberType::NegativeNumber(-$negative)
};
( $positive:literal ) => {
NumberType::PositiveNumber($positive)
};
( -$negative:literal ) => {
NumberType::NegativeNumber(-$negative)
( $block:block ) => {
NumberType::UnknownBecauseBlock($block)
};
( $e:expr ) => {
NumberType::UnknownBecauseExpr($e)
};
}

////////// DO NOT CHANGE BELOW HERE /////////
fn main() {
// PositiveNumber
// // PositiveNumber
get_number_type!(5).show();

// NegativeNumber
Expand Down
8 changes: 7 additions & 1 deletion exercises/10_macros_calling_macros/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ macro_rules! digit {
////////// DO NOT CHANGE ABOVE HERE /////////

// TODO: create `number!()` macro.

macro_rules! number {
($($num: tt) +) => { // instead of tt, can use literal
concat!($(
digit!($num)
),+)
};
}
////////// DO NOT CHANGE BELOW HERE /////////

fn main() {
Expand Down
29 changes: 29 additions & 0 deletions exercises/11_macro_recursion/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
// #![recursion_limit = "256"]
// TODO: Create the `curry!()` macro.
macro_rules! curry {
(
($ident_arg:ident:$ident_type:ty)=>_,
$block: block
) => {
move |$ident_arg: $ident_type| {
print_curried_argument($ident_arg);
$block
}
};
(
($ident_arg:ident:$ident_type:ty)=>
$(
($ident_args:ident:$ident_types:ty) =>

)*
_,
$block: block
) => {
move |$ident_arg: $ident_type| {
print_curried_argument($ident_arg);
curry!(
$(($ident_args:$ident_types)=>)* _,
$block
)
}
};
}
////////// DO NOT CHANGE BELOW HERE /////////

fn print_numbers(nums: &Vec<i32>) {
Expand All @@ -16,6 +44,7 @@ fn print_curried_argument(val: impl std::fmt::Debug) {

fn main() {
println!("=== defining functions ===");

let is_between = curry!((min: i32) => (max: i32) => (item: &i32) => _, {
min < *item && *item < max
});
Expand Down
21 changes: 18 additions & 3 deletions exercises/12_hygienic_macros/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
macro_rules! coord {
($x:expr, $y:expr) => {};
($x:expr, $y:expr, $z:expr) => {};
($x:expr, $y:expr, $z:expr, $t:expr) => {};
($x:expr, $y:expr) => {
$crate::Coordinate { x: $x, y: $y }
};
($x:expr, $y:expr, $z:expr) => {
$crate::third_dimension::Coordinate {
x: $x,
y: $y,
z: $z,
}
};
($x:expr, $y:expr, $z:expr, $t:expr) => {
$crate::fourth_dimension::Coordinate {
x: $x,
y: $y,
z: $z,
t: $t,
}
};
}

////////// DO NOT CHANGE BELOW HERE /////////
Expand Down