Skip to content

Instantly share code, notes, and snippets.

@jleeothon
Last active April 2, 2018 09:54
Show Gist options
  • Save jleeothon/3eb6a4cf09adaed4d9025abb0644e0c5 to your computer and use it in GitHub Desktop.
Save jleeothon/3eb6a4cf09adaed4d9025abb0644e0c5 to your computer and use it in GitHub Desktop.
Example lazy evaluation in Ruby
# Note .lazy
txt_files = file_names.lazy.filter { |file_name| file_name.end_with? '.txt' }
file_contents = txt_files = { |file_name| File.read(file_name) }
second_to_last_chars = file_contents.map { |text| text[-2] }
concatenated_characters = second_to_last_chars.reduce { |c, result| result + c }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment