Last active
April 2, 2018 09:54
-
-
Save jleeothon/3eb6a4cf09adaed4d9025abb0644e0c5 to your computer and use it in GitHub Desktop.
Example lazy evaluation in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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