This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use crate::RGBError::WrongLength; | |
use std::num::ParseIntError; | |
use std::str::FromStr; | |
#[derive(Debug)] | |
struct RGB { | |
red: u8, | |
green: u8, | |
blue: u8, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use crate::InvalidIsbn::{BadChecksum, TooLong, TooShort}; | |
use std::str::FromStr; | |
// check digit calculation for ISBN-13 | |
#[derive(Debug)] | |
struct Isbn { | |
raw: String, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::iter::Sum; | |
fn sum_with_missing<T: Sum + Copy>(vec: &Vec<Option<T>>) -> T { | |
vec.into_iter().filter_map(|x| *x).sum() | |
} | |
fn main() { | |
let mut v = vec![Some(1), Some(2), Some(3), None, Some(5)]; | |
println!("{}", sum_with_missing(&v)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use lazy_static::lazy_static; | |
use std::collections::HashMap; | |
// Ace is worth 11, except if total >21, then it is equal to 1 | |
#[derive(Debug, PartialEq, Eq, Hash)] | |
enum Card { | |
SmallAce, | |
Ace, | |
Two, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fmt::Formatter; | |
trait MorseCode { | |
fn to_morse_code(&self) -> Message; | |
} | |
impl MorseCode for String { | |
fn to_morse_code(&self) -> Message { | |
self.chars() | |
.flat_map(|c| c.to_ascii_lowercase().to_morse_code()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn insensitive_sort<T: AsRef<str>>(users: &mut Vec<T>) { | |
users.sort_by_cached_key(|a: &T| a.as_ref().to_lowercase()); | |
} | |
fn main() { | |
let mut users: Vec<String> = vec![String::from("zzz"), String::from("xxx")]; | |
insensitive_sort(&mut users); | |
println!("{:?}", users); | |
let mut users: Vec<&str> = vec!["Todd", "amy"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashSet; | |
use std::hash::Hash; | |
fn unique_keep_order<T: Eq + Hash + Clone>(vec: Vec<T>) -> Vec<T> { | |
let mut seen = HashSet::new(); | |
let mut output = Vec::with_capacity(vec.len()); | |
for x in vec.into_iter() { | |
if seen.insert(x.clone()) { | |
output.push(x); // Use the original x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.effect.{IO, IOApp} | |
import org.http4s.{Headers, MediaType, Method, Request} | |
import org.http4s.ember.client.EmberClientBuilder | |
import org.http4s.headers.Accept | |
import org.http4s.implicits.http4sLiteralsSyntax | |
import org.typelevel.log4cats.LoggerFactory | |
import org.typelevel.log4cats.slf4j.Slf4jFactory | |
object EmberClientCall extends IOApp.Simple { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.effect.{IO, IOApp} | |
// Emits intro text, then prompts for integers, keeping a running sum. Will exit upon 'exit' | |
object Repl2 extends IOApp.Simple { | |
def parseInput(input: String): Int = | |
scala.util.Try(input.toInt).toOption.getOrElse(0) | |
def repl(total: Int): IO[Int] = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.effect.{IO, IOApp} | |
// Emits intro text, then prompts for some command. Will exit upon 'exit' | |
// Note, these programs are values, as expected | |
object Repl extends IOApp.Simple { | |
val repl: IO[Unit] = { | |
for { | |
input <- IO.println(">>> ") *> IO.readLine | |
_ <- IO.println(s"You entered: $input") *> (if (input == "exit") IO.unit else repl) |