▲ 198 ▼ LISP is ugly (lemmy.ml) submitted 2 years ago by yogthos@lemmy.ml to c/programmerhumor@lemmy.ml 26 comments fedilink hide all child comments source https://rakhim.org/honestly-undefined/10/
[+] thejevans@lemmy.ml 23 points 2 years ago* (last edited 10 months ago) (2 children) [deleted] permalink fedilink source hideshow 4 child comments replies: [–] DWin@sh.itjust.works 1 point 2 years ago* (1 child) Just use this syntax let myResultObject = getResult() let item = match myResultObject { Ok(item) => item, Err(error) => { return; } }; permalink fedilink source parent hideshow 2 child comments replies: [+] thejevans@lemmy.ml 2 points 2 years ago* (last edited 10 months ago) (1 child) [deleted] permalink fedilink source parent hideshow 2 child comments replies: [–] DWin@sh.itjust.works 1 point 2 years ago (1 child) Oh sorry, I misread what you typed and went on a tangent and just idly typed that in. One thing you could do for your situation if you're planning on iterating over some array or vector of items is to use the inbuilt iterators and the chaining syntax. It could look like this let output_array = array.into_iter() .map(|single_item| { // match here if you want to handle exceptions }) .collect(); The collect also only collects results that are Ok(()) leaving you to match errors specifically within the map. This chaining syntax also allows you to write code that transverses downwards as you perform each operation, rather than transversing to the right via indentation. It's not perfect and sometimes it's felt a bit confusing to know exactly what's happening at each stage, particularly if you're trying to debug with something mid way through a chain, but it's prettier than having say 10 levels of nesting due to iterators, matching results, matching options, ect. permalink fedilink source parent hideshow 2 child comments replies: [+] thejevans@lemmy.ml 1 point 2 years ago* (last edited 10 months ago) (1 child) [deleted] permalink fedilink source parent hideshow 2 child comments replies: [–] DWin@sh.itjust.works 2 points 2 years ago* (1 child) It feels like maybe this could be a code structure issue, but within your example what about something like this? fn main(){ let mut counter = 0; let output_array = array.into_iter() .map(|single_item| { // breaks the map if the array when trying to access an item past 5 if single_item > 5 { break; } }) .collect() .map(|single_item| { // increment a variable outside of this scope that's mutable that can be changed by the previous run counter += 1; single_item.function(counter); }) .collect(); } Does that kinda syntax work for your workflow? Maybe it'll require you to either pollute a single map (or similar) with a bunch of checks that you can use to trigger a break though. Most of the time I've been able to find ways to re-write them in this syntax, but I also think that rusts borrowing system although fantastic for confidence in your code makes refactoring an absolute nightmare so often it's too much of a hassle to rewrite my code with a better syntax. permalink fedilink source parent hideshow 2 child comments replies: [+] thejevans@lemmy.ml 2 points 2 years ago* (last edited 10 months ago) [deleted] permalink fedilink source parent [–] darcy@sh.itjust.works 1 point 2 years ago the loop or match statement could possibly be extracted to another function, depending on the situation. rustc will most likely inline it so its zero cost permalink fedilink source parent
[–] DWin@sh.itjust.works 1 point 2 years ago* (1 child) Just use this syntax let myResultObject = getResult() let item = match myResultObject { Ok(item) => item, Err(error) => { return; } }; permalink fedilink source parent hideshow 2 child comments replies: [+] thejevans@lemmy.ml 2 points 2 years ago* (last edited 10 months ago) (1 child) [deleted] permalink fedilink source parent hideshow 2 child comments replies: [–] DWin@sh.itjust.works 1 point 2 years ago (1 child) Oh sorry, I misread what you typed and went on a tangent and just idly typed that in. One thing you could do for your situation if you're planning on iterating over some array or vector of items is to use the inbuilt iterators and the chaining syntax. It could look like this let output_array = array.into_iter() .map(|single_item| { // match here if you want to handle exceptions }) .collect(); The collect also only collects results that are Ok(()) leaving you to match errors specifically within the map. This chaining syntax also allows you to write code that transverses downwards as you perform each operation, rather than transversing to the right via indentation. It's not perfect and sometimes it's felt a bit confusing to know exactly what's happening at each stage, particularly if you're trying to debug with something mid way through a chain, but it's prettier than having say 10 levels of nesting due to iterators, matching results, matching options, ect. permalink fedilink source parent hideshow 2 child comments replies: [+] thejevans@lemmy.ml 1 point 2 years ago* (last edited 10 months ago) (1 child) [deleted] permalink fedilink source parent hideshow 2 child comments replies: [–] DWin@sh.itjust.works 2 points 2 years ago* (1 child) It feels like maybe this could be a code structure issue, but within your example what about something like this? fn main(){ let mut counter = 0; let output_array = array.into_iter() .map(|single_item| { // breaks the map if the array when trying to access an item past 5 if single_item > 5 { break; } }) .collect() .map(|single_item| { // increment a variable outside of this scope that's mutable that can be changed by the previous run counter += 1; single_item.function(counter); }) .collect(); } Does that kinda syntax work for your workflow? Maybe it'll require you to either pollute a single map (or similar) with a bunch of checks that you can use to trigger a break though. Most of the time I've been able to find ways to re-write them in this syntax, but I also think that rusts borrowing system although fantastic for confidence in your code makes refactoring an absolute nightmare so often it's too much of a hassle to rewrite my code with a better syntax. permalink fedilink source parent hideshow 2 child comments replies: [+] thejevans@lemmy.ml 2 points 2 years ago* (last edited 10 months ago) [deleted] permalink fedilink source parent
[+] thejevans@lemmy.ml 2 points 2 years ago* (last edited 10 months ago) (1 child) [deleted] permalink fedilink source parent hideshow 2 child comments replies: [–] DWin@sh.itjust.works 1 point 2 years ago (1 child) Oh sorry, I misread what you typed and went on a tangent and just idly typed that in. One thing you could do for your situation if you're planning on iterating over some array or vector of items is to use the inbuilt iterators and the chaining syntax. It could look like this let output_array = array.into_iter() .map(|single_item| { // match here if you want to handle exceptions }) .collect(); The collect also only collects results that are Ok(()) leaving you to match errors specifically within the map. This chaining syntax also allows you to write code that transverses downwards as you perform each operation, rather than transversing to the right via indentation. It's not perfect and sometimes it's felt a bit confusing to know exactly what's happening at each stage, particularly if you're trying to debug with something mid way through a chain, but it's prettier than having say 10 levels of nesting due to iterators, matching results, matching options, ect. permalink fedilink source parent hideshow 2 child comments replies: [+] thejevans@lemmy.ml 1 point 2 years ago* (last edited 10 months ago) (1 child) [deleted] permalink fedilink source parent hideshow 2 child comments replies: [–] DWin@sh.itjust.works 2 points 2 years ago* (1 child) It feels like maybe this could be a code structure issue, but within your example what about something like this? fn main(){ let mut counter = 0; let output_array = array.into_iter() .map(|single_item| { // breaks the map if the array when trying to access an item past 5 if single_item > 5 { break; } }) .collect() .map(|single_item| { // increment a variable outside of this scope that's mutable that can be changed by the previous run counter += 1; single_item.function(counter); }) .collect(); } Does that kinda syntax work for your workflow? Maybe it'll require you to either pollute a single map (or similar) with a bunch of checks that you can use to trigger a break though. Most of the time I've been able to find ways to re-write them in this syntax, but I also think that rusts borrowing system although fantastic for confidence in your code makes refactoring an absolute nightmare so often it's too much of a hassle to rewrite my code with a better syntax. permalink fedilink source parent hideshow 2 child comments replies: [+] thejevans@lemmy.ml 2 points 2 years ago* (last edited 10 months ago) [deleted] permalink fedilink source parent
[–] DWin@sh.itjust.works 1 point 2 years ago (1 child) Oh sorry, I misread what you typed and went on a tangent and just idly typed that in. One thing you could do for your situation if you're planning on iterating over some array or vector of items is to use the inbuilt iterators and the chaining syntax. It could look like this let output_array = array.into_iter() .map(|single_item| { // match here if you want to handle exceptions }) .collect(); The collect also only collects results that are Ok(()) leaving you to match errors specifically within the map. This chaining syntax also allows you to write code that transverses downwards as you perform each operation, rather than transversing to the right via indentation. It's not perfect and sometimes it's felt a bit confusing to know exactly what's happening at each stage, particularly if you're trying to debug with something mid way through a chain, but it's prettier than having say 10 levels of nesting due to iterators, matching results, matching options, ect. permalink fedilink source parent hideshow 2 child comments replies: [+] thejevans@lemmy.ml 1 point 2 years ago* (last edited 10 months ago) (1 child) [deleted] permalink fedilink source parent hideshow 2 child comments replies: [–] DWin@sh.itjust.works 2 points 2 years ago* (1 child) It feels like maybe this could be a code structure issue, but within your example what about something like this? fn main(){ let mut counter = 0; let output_array = array.into_iter() .map(|single_item| { // breaks the map if the array when trying to access an item past 5 if single_item > 5 { break; } }) .collect() .map(|single_item| { // increment a variable outside of this scope that's mutable that can be changed by the previous run counter += 1; single_item.function(counter); }) .collect(); } Does that kinda syntax work for your workflow? Maybe it'll require you to either pollute a single map (or similar) with a bunch of checks that you can use to trigger a break though. Most of the time I've been able to find ways to re-write them in this syntax, but I also think that rusts borrowing system although fantastic for confidence in your code makes refactoring an absolute nightmare so often it's too much of a hassle to rewrite my code with a better syntax. permalink fedilink source parent hideshow 2 child comments replies: [+] thejevans@lemmy.ml 2 points 2 years ago* (last edited 10 months ago) [deleted] permalink fedilink source parent
[+] thejevans@lemmy.ml 1 point 2 years ago* (last edited 10 months ago) (1 child) [deleted] permalink fedilink source parent hideshow 2 child comments replies: [–] DWin@sh.itjust.works 2 points 2 years ago* (1 child) It feels like maybe this could be a code structure issue, but within your example what about something like this? fn main(){ let mut counter = 0; let output_array = array.into_iter() .map(|single_item| { // breaks the map if the array when trying to access an item past 5 if single_item > 5 { break; } }) .collect() .map(|single_item| { // increment a variable outside of this scope that's mutable that can be changed by the previous run counter += 1; single_item.function(counter); }) .collect(); } Does that kinda syntax work for your workflow? Maybe it'll require you to either pollute a single map (or similar) with a bunch of checks that you can use to trigger a break though. Most of the time I've been able to find ways to re-write them in this syntax, but I also think that rusts borrowing system although fantastic for confidence in your code makes refactoring an absolute nightmare so often it's too much of a hassle to rewrite my code with a better syntax. permalink fedilink source parent hideshow 2 child comments replies: [+] thejevans@lemmy.ml 2 points 2 years ago* (last edited 10 months ago) [deleted] permalink fedilink source parent
[–] DWin@sh.itjust.works 2 points 2 years ago* (1 child) It feels like maybe this could be a code structure issue, but within your example what about something like this? fn main(){ let mut counter = 0; let output_array = array.into_iter() .map(|single_item| { // breaks the map if the array when trying to access an item past 5 if single_item > 5 { break; } }) .collect() .map(|single_item| { // increment a variable outside of this scope that's mutable that can be changed by the previous run counter += 1; single_item.function(counter); }) .collect(); } Does that kinda syntax work for your workflow? Maybe it'll require you to either pollute a single map (or similar) with a bunch of checks that you can use to trigger a break though. Most of the time I've been able to find ways to re-write them in this syntax, but I also think that rusts borrowing system although fantastic for confidence in your code makes refactoring an absolute nightmare so often it's too much of a hassle to rewrite my code with a better syntax. permalink fedilink source parent hideshow 2 child comments replies: [+] thejevans@lemmy.ml 2 points 2 years ago* (last edited 10 months ago) [deleted] permalink fedilink source parent
[+] thejevans@lemmy.ml 2 points 2 years ago* (last edited 10 months ago) [deleted] permalink fedilink source parent
[–] darcy@sh.itjust.works 1 point 2 years ago the loop or match statement could possibly be extracted to another function, depending on the situation. rustc will most likely inline it so its zero cost permalink fedilink source parent