Skip to content

Instantly share code, notes, and snippets.

@dkoontz
Created December 28, 2018 23:29
Show Gist options
  • Save dkoontz/ec716a36a38f9bd319a2458835dbe2db to your computer and use it in GitHub Desktop.
Save dkoontz/ec716a36a38f9bd319a2458835dbe2db to your computer and use it in GitHub Desktop.
// #![feature(proc_macro_hygiene, decl_macro)]
// #[macro_use]
// extern crate rocket;
// #[get("/hello/<name>/<age>")]
// fn hello(name: String, age: u8) -> String {
// format!("Hello, {} year old named {}!", age, name)
// }
// fn main() {
// rocket::ignite().mount("/", routes![hello]).launch();
// }
fn main() {
let out1 = fizz(1);
let out2 = looop2(15);
println!("Fizz {:?}", out2);
println!("{}", apply(add_one, 2))
}
fn add_one(i: u32) -> u32 {
i + 1
}
// fn apply<A, B>(f: Fn(A) -> B, a: A) -> B {
fn apply<F, A, B>(f: F, a: A) -> B
where
F: Fn(A) -> B,
{
f(a)
}
fn looop2(end: u32) -> Vec<String> {
(1..=end).map(|i| fizz(i)).collect()
}
fn looop(end: u32) -> String {
(1..=end)
.map(|i| fizz2(i))
// .fold(String::new(), |acc, v| format!("{}\n{}", acc, v))
.fold(String::new(), |acc, v| acc + &v)
}
fn fizz(i: u32) -> String {
if i % 15 == 0 {
return "FizzBuzz".to_string();
} else if i % 3 == 0 {
return "Fizz".to_string();
} else if i % 5 == 0 {
return "Buzz".to_string();
} else {
return i.to_string();
}
}
fn fizz2(i: u32) -> String {
match i {
n if n % 15 == 0 => "FizzBuzz".to_string(),
n if n % 3 == 0 => "Fizz".to_string(),
n if n % 5 == 0 => "Buzz".to_string(),
n => n.to_string(),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment