Skip to content

Commit c960914

Browse files
committed
Donated with HOFs
1 parent ac94d0e commit c960914

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

fops/src/file_io_ops.rs

+50-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn write_games_to_file(games: &[Game]) -> io::Result<()> {
3232
let mut contents = String::new();
3333
for g in games {
3434
contents.push_str(&g.to_string());
35-
contents.push_str("\n");
35+
contents.push('\n');
3636
}
3737
let mut f = File::create("games.dat")?;
3838
f.write_all(contents.as_bytes())?;
@@ -145,3 +145,52 @@ pub fn append_game_to_file(path: &str, game: &Game) -> io::Result<()> {
145145
writeln!(file, "{}", game)?;
146146
Ok(())
147147
}
148+
149+
/*
150+
Aşğıdaki fonksiyon games.dat dosyasında okuma ve Game türünden vektöre dönüştürm işini
151+
iterasyon fonksiyonlarını kullanarak gerçekleştirir. Iterasyon fonksiyonları Higher- Order Functions
152+
olarak da ifade edilebilir. Rust'ın bu yetkinliği Zero Cost Abstraction da sağlar. Yani normal for döngüleri ile
153+
icra edilenler gibi herhangi bir çalışma zamanı performans kaybı söz konusu değildir.
154+
155+
Higher-Order Function'lar parametre olarak fonksiyon alan veya fonksiyon döndüren enstrümanlardır.
156+
Fonksiyonel dillerde yer alan önemli özelliklerden birisidir.
157+
*/
158+
pub fn read_games_to_vec_with_hof() -> io::Result<Vec<Game>> {
159+
read_games_from_file()?
160+
.into_iter()
161+
.map(|line| {
162+
let cols: Vec<&str> = line.split('|').collect();
163+
if cols.len() != 3 {
164+
return Err(io::Error::new(
165+
io::ErrorKind::InvalidData,
166+
format!("Beklenmeyen sütun sayısı: `{}`", line),
167+
));
168+
}
169+
170+
let title = cols[0].to_string();
171+
let year = cols[1]
172+
.parse::<u16>()
173+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
174+
let popularity = cols[2]
175+
.parse::<f32>()
176+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
177+
178+
Ok(Game {
179+
title,
180+
year,
181+
popularity,
182+
})
183+
})
184+
.collect()
185+
}
186+
187+
pub fn write_games_buffered_with_hof(path: &str, games: &[Game]) -> io::Result<()> {
188+
let file = File::create(path)?;
189+
let mut writer = BufWriter::new(file);
190+
191+
games
192+
.iter()
193+
.try_for_each(|game| writeln!(writer, "{}", game))?;
194+
195+
writer.flush()
196+
}

fops/src/main.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn main() -> std::io::Result<()> {
2727
println!("{}", game);
2828
}
2929

30-
println!("\n\n");
30+
println!("\nOyunları Vector İçerisine Okuma Sonuçları\n");
3131
let games = read_games_to_vec()?;
3232
for game in games {
3333
println!("{}", game);
@@ -40,12 +40,25 @@ fn main() -> std::io::Result<()> {
4040
println!("{}", game);
4141
}
4242

43-
println!("\n\n");
43+
let games = load_games();
44+
write_games_buffered_with_hof("newGames.dat", &games)?;
45+
let games = read_games_from_file()?;
46+
for game in games {
47+
println!("{}", game);
48+
}
49+
50+
println!("\nBufReader ile Okuma Sonuçları\n");
4451
let games = read_games_buffered_into_vec("newGames.dat")?;
4552
for game in games {
4653
println!("{}", game);
4754
}
4855

56+
println!("\nHOF Tekniği ile Okuma Sonuçları\n");
57+
let games = read_games_to_vec_with_hof()?;
58+
for game in games {
59+
println!("{}", game);
60+
}
61+
4962
let shogun = Game {
5063
title: "Shogun Shawdown".to_string(),
5164
year: 2024,

io.md

+57
Original file line numberDiff line numberDiff line change
@@ -391,4 +391,61 @@ pub fn append_game_to_file(path: &str, game: &Game) -> io::Result<()> {
391391
writeln!(file, "{}", game)?;
392392
Ok(())
393393
}
394+
```
395+
396+
### Iterator Fonksiyonlarını Kullanmak
397+
398+
Rust'ın güçlü özelliklerinden birisi de Zero Cost Abstraction sağlayan Higher-Order Function setidir. Genellikle
399+
fonksiyonel dillerde fonksiyonları parametre olarak alan veya döndüren fonksiyonlar yaygın olarak kullanılır. Rust'ın
400+
iterasyon metotları sonrasında gelen birçok fonksiyon bu tanıma uyar. Dolayısıyla Rust'ın da Higher-Order Function
401+
desteği sağladığını söyleyebiliriz.
402+
403+
Aşğıdaki fonksiyon games.dat dosyasından okuma ve Game türünden vektöre dönüştürme işlevini iterasyon fonksiyonlarını
404+
kullanarak gerçekleştirir. Örnekte bunlar map ve collect çağrılarıdır. Bu metotların çalışma zamanı maliyetleri yoktur.
405+
406+
```rust
407+
pub fn read_games_to_vec_with_hof() -> io::Result<Vec<Game>> {
408+
read_games_from_file()?
409+
.into_iter()
410+
.map(|line| {
411+
let cols: Vec<&str> = line.split('|').collect();
412+
if cols.len() != 3 {
413+
return Err(io::Error::new(
414+
io::ErrorKind::InvalidData,
415+
format!("Beklenmeyen sütun sayısı: `{}`", line),
416+
));
417+
}
418+
419+
let title = cols[0].to_string();
420+
let year = cols[1]
421+
.parse::<u16>()
422+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
423+
let popularity = cols[2]
424+
.parse::<f32>()
425+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
426+
427+
Ok(Game {
428+
title,
429+
year,
430+
popularity,
431+
})
432+
})
433+
.collect()
434+
}
435+
```
436+
437+
Bu örnekten hareketle file_io_ops modülündeki diğer metotlarda da benzer kullanımlar icra edilebilir. Örneğin yazma
438+
işlemini ele aldığımız fonksiyonu aşağıdaki gibi değiştirebiliriz.
439+
440+
```rust
441+
pub fn write_games_buffered_with_hof(path: &str, games: &[Game]) -> io::Result<()> {
442+
let file = File::create(path)?;
443+
let mut writer = BufWriter::new(file);
444+
445+
games
446+
.iter()
447+
.try_for_each(|game| writeln!(writer, "{}", game))?;
448+
449+
writer.flush()
450+
}
394451
```

0 commit comments

Comments
 (0)