Skip to content

Instantly share code, notes, and snippets.

View sassman's full-sized avatar
🦀
rust ❤️

Sven Kanoldt sassman

🦀
rust ❤️
View GitHub Profile
@sassman
sassman / yaml2json
Created December 7, 2018 15:33
Yaml to json converter
#!/usr/bin/env python
import sys, yaml, json
try:
# included in standard lib from Python 2.7
from collections import OrderedDict
except ImportError:
# try importing the backported drop-in replacement
# it's available on PyPI
@sassman
sassman / alphabet_position_spec.rs
Created September 8, 2019 22:18
rust tdd: test/alphabet_position_spec.rs
// test/alphabet_position_spec.rs
use dev_challenge_47::alphabet_position;
use speculate::speculate;
speculate! { // <-- it's a macro but who cares
describe "alphabet_position" {
it "should replace 'a' with 1" {
assert_eq!(alphabet_position("a"), "1");
}
it "should replace 'A' with 1" {
@sassman
sassman / alphabet_position_test.rs
Last active September 8, 2019 22:23
rust tdd: alphabet_position_test.rs
// test/alphabet_position_test.rs
#[test]
fn it_should_replace_the_sentence() {
let replaced = alphabet_position("The sunset sets at twelve o' clock.");
assert_eq!(
replaced,
"20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11"
);
}
@sassman
sassman / lib.rs
Created September 8, 2019 22:24
rust tdd: src/lib.rs
// src/lib.rs
pub fn alphabet_position(s: &str) -> String {
s.to_lowercase()
.chars()
.filter(|x| x.is_alphabetic()) // <-- adding this line here
.map(|x| -> u8 { x as u8 - 'a' as u8 + 1 })
.map(|x| -> String { x.to_string() })
.collect::<Vec<String>>()
.join(" ")
@sassman
sassman / alphabet_position_test.rs
Created September 8, 2019 22:25
rust tdd: test/alphabet_position_test.rs
// test/alphabet_position_test.rs
#[test]
fn it_should_ignore_non_characters() {
let replaced = alphabet_position("'a a. 2");
assert_eq!(replaced, "1 1");
}
@sassman
sassman / lib.rs
Last active September 8, 2019 22:26
rust tdd: src/lib.rs
// src/lib.rs
pub fn alphabet_position(s: &str) -> String {
s.to_lowercase() // <-- adding this line
.chars()
.map(|x| -> u8 { x as u8 - 'a' as u8 + 1 })
.map(|x| -> String { x.to_string() })
.collect::<Vec<String>>()
.join(" ")
}
@sassman
sassman / alphabet_position_test.rs
Created September 8, 2019 22:27
rust tdd: test/alphabet_position_test.rs
// test/alphabet_position_test.rs
#[test]
fn it_should_replace_the_capital_a_with_1() {
let replaced = alphabet_position("A");
assert_eq!(replaced, "1");
}
@sassman
sassman / lib.rs
Created September 8, 2019 22:28
rust tdd: src/lib.rs
// src/lib.rs
pub fn alphabet_position(s: &str) -> String {
s.chars() // <-- get an iterator over all chars
.map(|x| -> u8 { x as u8 - 'a' as u8 + 1 }) // <-- substract the ascii value of 'a'
.map(|x| -> String { x.to_string() }) // <-- convert the char to a String
.collect::<Vec<String>>() // <-- collect a Vector of String
.join(" ") // <-- join the Strings by whitespace
}
@sassman
sassman / lib.rs
Created September 8, 2019 22:29
rust tdd: src/lib.rs
// src/lib.rs
pub fn alphabet_position(s: &str) -> String {
String::from("Hello")
}
@sassman
sassman / alphabet_position_test.rs
Created September 8, 2019 22:29
rust tdd: test/alphabet_position_test.rs
// test/alphabet_position_test.rs
use dev_challenge_47::alphabet_position; // <-- we don't have this function yet
#[test] // <-- how to define a test in rust
fn it_should_replace_the_a_with_1() { // <-- we express the requirement as name
let replaced = alphabet_position("a");
assert_eq!(replaced, "1"); // <-- we assert that the both are equal
}