Skip to content

Instantly share code, notes, and snippets.

@steveklabnik
Created March 23, 2015 21:50
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveklabnik/690683d4cb8cbd933736 to your computer and use it in GitHub Desktop.
Save steveklabnik/690683d4cb8cbd933736 to your computer and use it in GitHub Desktop.
a little wc-like in rust
#![feature(collections)]
use std::io::prelude::*;
use std::fs::File;
use std::io::BufReader;
fn main() {
let args: Vec<String> = std::env::args().collect();
let filename = args[1].clone();
let file = File::open(&filename).ok().expect("I couldn't open that file, sorry :(");
let reader = BufReader::new(file);
let words = reader.split(b' ')
.map(|x| x.ok().expect("There was an IO error."))
.count();
println!("words: {}", words);
let file = File::open(&filename).ok().expect("I couldn't open that file, sorry :(");
let newlines = file.bytes()
.map(|x| x.ok().expect("There was an IO error."))
.filter(|x| *x == b'\n')
.count();
println!("newlines: {}", newlines)
}
@oleschoenburg
Copy link

I'm wondering why we can access the array at an arbitrary position in line 9. What would happen if the program hadn't received any arguments?

@oleschoenburg
Copy link

And why do we have to open the same file twice?

@brushbox
Copy link

I couldn't resist playing golf with that. This is less clear, and less "functional" but is possibly? more efficient and concise:

#![feature(collections)]

use std::io::prelude::*;
use std::fs::File;
use std::io::BufReader;

fn main() {
    let args: Vec<String> = std::env::args().collect();
    let filename = args[1].clone();

    let file = File::open(&filename).ok().expect("I couldn't open that file, sorry :(");

    let reader = BufReader::new(file);

    let mut words = 0;
    let newlines = reader.split(b'\n')
        .map(|x| {
            let line = x.ok().expect("There was an IO error");
            words = words + line.split(|x| *x == b' ').filter(|x| x.len() > 0).count();
            line
        })
        .count();

    println!("words: {}", words);
    println!("newlines: {}", newlines)
}

@brushbox
Copy link

I'd love to see a Reactive stream version of that (Rx stuff) ...

  • one stage to turn the file into lines (map)
    • and a count for that
    • and a second stage that turns the lines into words concat_map (I'm really not sure, I'm just dabbling with Rx so far).
      • and a count for that.

It would be a single pass and would be "functional" (as far as the client code goes).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment