Created
February 9, 2024 12:18
-
-
Save berkes/75a202eaa1d7d97e98dd6cd6024f2b0f to your computer and use it in GitHub Desktop.
One-line to multiline block iterators
This file contains hidden or 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
from itertools import chain, repeat | |
# Define the inventory as a dictionary | |
inventory = { | |
"banana": 3, | |
"apple": 2, | |
"pear": 1, | |
"berry": 4, | |
} | |
# Use itertools.chain and itertools.repeat to replicate the fruit names | |
inventory_list = list(chain.from_iterable(repeat(fruit.upper(), quantity) for fruit, quantity in inventory.items())) | |
print(inventory_list) | |
## INTO Version with logging | |
from itertools import chain, repeat | |
# Define the inventory as a dictionary | |
inventory = { | |
"banana": 3, | |
"apple": 2, | |
"pear": 1, | |
"berry": 4, | |
} | |
def log_and_uppercase(fruit): | |
print(f"Processing: {fruit}") # Log statement before uppercasing | |
return fruit.upper() | |
# Use a generator expression with a function call for logging and uppercasing | |
inventory_list = list(chain.from_iterable(repeat(log_and_uppercase(fruit), quantity) for fruit, quantity in inventory.items())) | |
print(inventory_list) |
This file contains hidden or 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
inventory = { | |
'banana' => 3, | |
'apple' => 2, | |
'pear' => 1, | |
'berry' => 4 | |
} | |
inventory_list = inventory.flat_map { |fruit, quantity| [fruit.upcase] * quantity } | |
puts inventory_list.inspect | |
## Version with logging. | |
inventory = { | |
'banana' => 3, | |
'apple' => 2, | |
'pear' => 1, | |
'berry' => 4 | |
} | |
inventory_list = inventory.flat_map do |fruit, quantity| | |
puts "Processing: #{fruit}" # Log statement before uppercasing | |
[fruit.upcase] * quantity | |
end | |
puts inventory_list.inspect |
This file contains hidden or 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
use std::collections::HashMap; | |
fn main() { | |
let inventory = HashMap::from([("banana", 3), ("apple", 2), ("pear", 1), ("berry", 4)]); | |
// Transform and repeat each fruit name in uppercase based on its quantity | |
let inventory_list: Vec<String> = inventory | |
.iter() | |
.flat_map(|(fruit, &quantity)| std::iter::repeat(fruit.to_uppercase()).take(quantity)) | |
.collect(); | |
println!("{:?}", inventory_list); | |
} | |
//into: | |
use std::collections::HashMap; | |
fn main() { | |
let inventory = HashMap::from([ | |
("banana", 3), | |
("apple", 2), | |
("pear", 1), | |
("berry", 4), | |
]); | |
let inventory_list: Vec<String> = inventory.iter() | |
.flat_map(|(fruit, &quantity)| { | |
std::iter::repeat_with(|| { | |
println!("Processing: {}", fruit); // Log statement before uppercasing | |
fruit.to_uppercase() | |
}) | |
.take(quantity) | |
}) | |
.collect(); | |
println!("{:?}", inventory_list); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment