@@ -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+ }
0 commit comments