Learning to love the unfamiliar

Click here to load reader

  • date post

    15-Apr-2017
  • Category

    Technology

  • view

    195
  • download

    3

Embed Size (px)

Transcript of Learning to love the unfamiliar

  • @brian_pearce

    LEARNING TO

    THE UNFAMILIARLove

    with @brian_pearce

  • @brian_pearce

  • @brian_pearce

  • @brian_pearce

  • @brian_pearce

    LEARNING TO

    THE UNFAMILIARLove

    with @brian_pearce

  • @brian_pearce

    Steep learning curve at the beginning

    1 in 4 People

    - State of Rust Survey 2016

  • @brian_pearce

  • @brian_pearce

    def read_file()with open('my_file.txt') as file:

    for line in file: process(line)

    def process(line)if is_a_pun(line):

    tell_a_friend()

    Python

  • @brian_pearce

    fn read_file() {let mut data = String::new();let mut f = File::open("my_file.txt").unwrap();f.read_to_string(&mut data).unwrap();for line in data.lines() {

    process(line);}

    }

    fn process(line: &str) {if is_a_pun(line) { tell_a_friend();}

    }Rust

  • @brian_pearce

    def read_file(argv)with open('my_file.txt') as file:

    for line in file: process(line)

    fn read_file() {let mut data = String::new();let mut f = File::open("my_file.txt").unwrap();f.read_to_string(&mut data).unwrap();for line in data.lines() {

    process(line);}

    }

    Python

    Rust

  • @brian_pearce

    def read_file(argv)with open('my_file.txt') as file:

    for line in file: process(line)

    fn read_file() {let mut data = String::new();let mut f = File::open("my_file.txt").unwrap();f.read_to_string(&mut data).unwrap();for line in data.lines() {

    process(line);}

    }

    Python

    Rust

  • @brian_pearce

    Introducing:

    The Maybe type*star wipe*

  • @brian_pearce

    Maybes in the form of:

    Option & Result

  • @brian_pearce

    enum Option { Some(T), None}

    Option:

  • @brian_pearce

    Ruby

    irb> '7'.to_i

  • @brian_pearce

    Ruby

    irb> '7'.to_i=> 7

  • @brian_pearce

    Ruby

    irb> '7'.to_i=> 7

    irb> ''.to_i

  • @brian_pearce

    Ruby

    irb> '7'.to_i=> 7

    irb> ''.to_i=> 0

  • @brian_pearce

    Ruby

    irb> '7'.to_i=> 7

    irb> ''.to_i=> 0

    irb> '4520000011112222'.to_i=> 4520000011112222

  • @brian_pearce

    Ruby

    irb> '7'.to_i=> 7

    irb> ''.to_i=> 0

    irb> '4520000011112222'.to_i=> 4520000011112222

    irb> '4520not_a_num2'.to_i

  • @brian_pearce

    irb> '7'.to_i=> 7

    irb> ''.to_i=> 0

    irb> '4520000011112222'.to_i=> 4520000011112222

    irb> '4520not_a_num2'.to_i=> 4520

    Ruby

  • @brian_pearce

    Rust

    rust> '7.to_digit(...)

  • @brian_pearce

    Rust

    rust> '7.to_digit(...)=> Some(7)

  • @brian_pearce

    Rust

    rust> '7.to_digit(...)=> Some(7)

    rust> ''.to_digit(...)

  • @brian_pearce

    Rust

    rust> '7.to_digit(...)=> Some(7)

    rust> ''.to_digit(...)=> None

  • @brian_pearce

    Rust

    rust> '7.to_digit(...)=> Some(7)

    rust> ''.to_digit(...)=> None

    rust> 4520000011112222.to_digit(...)=> Some(4520000011112222)

  • @brian_pearce

    Rust

    rust> '7.to_digit(...)=> Some(7)

    rust> ''.to_digit(...)=> None

    rust> 4520000011112222.to_digit(...)=> Some(4520000011112222)

    rust> 4520not_a_num2.to_digit(...)

  • @brian_pearce

    rust> '7.to_digit(...)=> Some(7)

    rust> ''.to_digit(...)=> None

    rust> 4520000011112222.to_digit(...)=> Some(4520000011112222)

    rust> 4520not_a_num2.to_digit(...)=> None

    Rust

  • @brian_pearce

    Rust

    rust> '7.to_digit(...).unwrap()

  • @brian_pearce

    Rust

    rust> '7.to_digit(...).unwrap()=> 7

  • @brian_pearce

    Rust

    rust> '7.to_digit(...).unwrap()=> 7

    rust> ''.to_digit(...).unwrap()

  • @brian_pearce

    rust> '7.to_digit(...).unwrap()=> 7

    rust> ''.to_digit(...).unwrap()=> thread 'main' panicked at 'called `Option::unwrap()` on a `None` value'

    Rust

  • @brian_pearce

    puts "Enter a number"num = gets.chomp.to_i

    if num.is_a?(Numeric) puts "Great number choice: #{num}"else puts "That wasn't a number"end

    Ruby

  • @brian_pearce

    Ruby

    puts "Enter a number"num = gets.chomp.to_i

    if num.is_a?(Numeric) puts "Great number choice: #{num}"else puts "That wasn't a number"end

  • @brian_pearce

    Rust

    fn main() {println!("Enter a number");let mut input = String::new();io::stdin().

    read_line(&mut num).unwrap();

    let int = num.to_digit(10).unwrap();println!("Great number choice: {}", int);

    }

  • @brian_pearce

    fn main() {println!("Enter a number");let mut input = String::new();io::stdin().

    read_line(&mut num).unwrap();

    let maybe = num.to_digit(10);

    if maybe.is_some() {let i = maybe.unwrap();println!("Great number choice: {}", i);

    } else if maybe.is_none() {println!("That wasn't a number");

    };} Rust

  • @brian_pearce

    fn main() {println!("Enter a number");let mut input = String::new();io::stdin().

    read_line(&mut num).unwrap();

    let maybe = num.to_digit(10);

    if maybe.is_some() {let i = maybe.unwrap();println!("Great number choice: {}", i);

    } else if maybe.is_none() {println!("That wasn't a number");

    };} Rust

  • @brian_pearce

    fn main() {println!("Enter a number");let mut input = String::new();io::stdin().

    read_line(&mut num).unwrap();

    let maybe = num.to_digit(10);

    match maybe { Some(n) => {

    println!("Great number choice: {}", n);},None => {

    println!("That wasn't a number");}

    }; Rust

  • @brian_pearce

    thread '' panicked at 'cannot access a TLS value during or after it is destroyed', ../src/libcore/option.rs:330fatal runtime error: Could not unwind stack, error = 5th

    read

    '' p

    anick

    ed at

    'can

    not a

    cces

    s a TL

    S valu

    e dur

    ing or

    afte

    r it i

    s des

    troye

    d', ../

    src/

    libco

    re/o

    ption

    .rs:33

    0fa

    tal r

    untim

    e erro

    r: Co

    uld no

    t unw

    ind st

    ack,

    erro

    r = 5

    Error Handling

  • @brian_pearce

    try { } catch {

    }

    Java

  • @brian_pearce

    begin

    rescue

    end

    Ruby

  • @brian_pearce

    try:

    expect:

    Python

  • @brian_pearce

    try!(my_func())

  • @brian_pearce

    enum Result { Ok(T), Err(E)}

    Result

  • @brian_pearce

    def read_file(argv)try:

    with open('my_file.txt') as file:for line in file: process(line)

    except:# Failure

    Python

  • @brian_pearce

    Rust

    fn read_file() {let mut data = String::new();let mut f = File::open("my_file.txt").unwrap()f.read_to_string(&mut data).unwrap();for line in data.lines() {

    process(line);};

    }

  • @brian_pearce

    Rust

    fn read_file() {let mut data = String::new();let mut f = File::open("my_file.txt").unwrap()

    f.read_to_string(&mut data).unwrap();for line in data.lines() {

    process(line);};

    }

  • @brian_pearce

    Rust

    fn read_file() {let mut data = String::new();let mut f = File::open("my_file.txt").unwrap()

    f.read_to_string(&mut data).unwrap();for line in data.lines() {

    process(line);};

    }

  • @brian_pearce

    Rust

    fn read_file() -> Result {let mut data = String::new();let mut f = try!(File::open("my_file.txt"));

    f.read_to_string(&mut data).unwrap();for line in data.lines() {

    process(line);};

    }

  • @brian_pearce

    Rust

    fn read_file() -> Result {let mut data = String::new();let mut f = try!(

    File::open("my_file.txt").map_err(|e| e ));

    f.read_to_string(&mut data).unwrap();for line in data.lines() {

    process(line);};

    }

  • @brian_pearce

    fn read_file() -> Result {let mut data = String::new();let mut f = try!(

    File::open("my_file.txt").map_err(|e| e ));

    f.read_to_string(&mut data).unwrap();for line in data.lines() {

    process(line);};

    Ok(Success)}

    Rust

  • @brian_pearce

    fn my_func() { }

    #[test]fn my_funct_test { }

    Testing

  • @brian_pearce

    bash

  • @brian_pearce

    def setup @dir = '/tmp/rb_tests' Dir.mkdir(@dir) unless File.directory?(@dir)end

    def teardown FileUtils.rm_r(@dir)end

    def test_file_write_one file_name = "#{@dir}/test_one" File.open(file_name, 'w') { |f| f.write "I failed a google code interview" } assert File.exists?(file_name)end

    def test_file_write_two file_name = "#{@dir}/test_two" File.open(file_name, 'w') { |f| f.write "had a mixup with the interviewer" } assert File.exists?(file_name)end

    def test_file_write_three file_name = "#{@dir}/test_three" File.open(file_name, 'w') { |f| f.write "a mis-understanding of sorts" }end

    Ruby

  • @brian_pearce

    def setup @dir = '/tmp/rb_tests' Dir.mkdir(@dir) unless File.directory?(@dir)end

    def teardown FileUtils.rm_r(@dir)end

    def test_file_write_one file_name = "#{@dir}/test_one" File.open(file_name, 'w') { |f| f.write "I failed a google code interview" } assert File.exists?(file_name)end

    def test_file_write_two file_name = "#{@dir