Skip to content

Instantly share code, notes, and snippets.

View martinmroz's full-sized avatar

Martin Mroz martinmroz

  • San Francisco, CA
View GitHub Profile

Keybase proof

I hereby claim:

  • I am martinmroz on github.
  • I am martinmroz (https://keybase.io/martinmroz) on keybase.
  • I have a public key ASAzlqHwmUQVK6z6SrEAFQhkWMoUThrk1qvLQbyPRpjdWQo

To claim this, I am signing this object:

@martinmroz
martinmroz / SimpleViewController.swift
Last active June 15, 2018 22:04
Medium - Flow Controller Demo - Simple View Controller
public protocol SimpleViewControllerDelegate: class {
func simpleViewControllerAction(_ viewController: SimpleViewController)
}
public final class SimpleViewController {
// MARK: - Public Properties
public weak var delegate: SimpleViewControllerDelegate? = nil
@martinmroz
martinmroz / RootFlow.swift
Last active November 21, 2018 01:18
Medium - Flow Controller Demo - Simple Flow
public final class RootFlow {
// MARK: - Private Properties
private let navigationController: UINavigationController
private var presented: Bool = false
// MARK: - Private Properties - View Controllers
private lazy var simpleViewController = SimpleViewController(delegate: self)
@martinmroz
martinmroz / main.rs
Created October 27, 2019 01:18
The dumbest thing that could possibly work
fn main() {
let target_path = std::env::args().nth(1).expect("No file path specified");
// Count the bytes, words and lines in the specified file.
let whole_file_string = std::fs::read_to_string(&target_path).expect("Unable to read file");
let bytes = whole_file_string.len();
let words = whole_file_string.split_whitespace().count();
let lines = whole_file_string.lines().count();
@martinmroz
martinmroz / performance_1.csv
Created October 27, 2019 01:31
Performance of the dumbest thing that could possibly work
file wc time wc mem wc_rs time wc_rs mem
empty.txt 0.00s 671kB 0.00s 753kB
big.txt 0.02s 1.72MB 0.02s 7.27MB
huge.txt 0.32s 1.72MB 0.45s 99.73MB
@martinmroz
martinmroz / performance_2.csv
Created October 27, 2019 02:14
Performance of something slightly less dumb.
file wc time wc mem wc_rs time wc_rs mem
empty.txt 0.00s 671kB 0.00s 761kB
big.txt 0.02s 1.72MB 0.02s 1.29MB
huge.txt 0.32s 1.72MB 0.35s 1.29MB
@martinmroz
martinmroz / flux_type.rs
Created October 27, 2019 02:17
Creating a Monoid?
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
struct Flux {
/// The type of the left-most character in the chunk.
pub leftmost_char_type: CharType,
/// The number of words in the chunk.
pub words: usize,
/// The number of lines in the chunk.
pub lines: usize,
/// The type of the right-most character in the chunk.
pub rightmost_char_type: CharType,
@martinmroz
martinmroz / main.rs
Last active October 27, 2019 02:20
Processing a file one piece at a time.
let mut last_char_type = CharType::IsSpace;
'buffer_loop: loop {
let buffer = input.fill_buf()?;
let length = buffer.len();
if length == 0 {
break 'buffer_loop;
}
// Update the byte counter from the buffer.
@martinmroz
martinmroz / character.rs
Last active October 27, 2019 02:25
Mapping a single character onto a Flux Monoid
impl From<u8> for Flux {
fn from(other: u8) -> Self {
if other.is_ascii_whitespace() {
// A line-feed is considered an ASCII whitespace character by `is_ascii_whitespace`.
let lines = if other == ('\n' as u8) { 1 } else { 0 };
Flux::new(CharType::IsSpace, 0, lines, CharType::IsSpace)
} else {
Flux::new(CharType::NotSpace, 1, 0, CharType::NotSpace)
}
}
@martinmroz
martinmroz / span.rs
Created October 27, 2019 02:32
Span joining two Flux instances together
fn span(self, rhs: Flux) -> Self {
let words = {
// If the span is formed along a non-space to non-space boundary the word count is one less than the sum.
if let (CharType::NotSpace, CharType::NotSpace) = (self.rightmost_char_type, rhs.leftmost_char_type) {
self.words + rhs.words - 1
} else {
self.words + rhs.words
}
};
Flux::new(self.leftmost_char_type, words, self.lines + rhs.lines, rhs.rightmost_char_type)