MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/q9spcd/prefer_to_use_iterator_functions_they_said/
r/badcode • u/made_with_mematic69 • Oct 17 '21
12 comments sorted by
94
use std::collections::HashMap; use itertools::Itertools; fn parse_encoded_string(enc_str: &str) -> HashMap<&str, &str> { enc_str .split(',') .tuples() .collect() } fn main() { let v = parse_encoded_string("1,83,2,30"); dbg!(v); // {"1": "83", "2": "30"} }
Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f6fb7cdaeb727da4c6c4e2c78541bc9b
29 u/nuuren Oct 17 '21 I wouldn't say OP code is terribly bad. It's a naive implementation at best, but yours is so damn good 13 u/Mancobbler Oct 17 '21 Damn that’s good! 7 u/[deleted] Oct 17 '21 edited Oct 17 '21 -- meanwhile in Haskell parseEncodedString :: String -> HashMap String String parseEncodedString s = fromList $ map tuplify $ chunksOf 2 $ splitOn "," s where tuplify [a, b] = (a, b)
29
I wouldn't say OP code is terribly bad. It's a naive implementation at best, but yours is so damn good
13
Damn that’s good!
7
-- meanwhile in Haskell parseEncodedString :: String -> HashMap String String parseEncodedString s = fromList $ map tuplify $ chunksOf 2 $ splitOn "," s where tuplify [a, b] = (a, b)
30
super hard to read this code, but the example helped. but I'm sure I can make an easier to read revision of this with less code too.
12
Image Transcription: Code
// Example: converts "1,83,2,30" into {"1": "83", "2": "30"} fn parse_encoded_string( enc_str: String ) -> HashMap<String, String> { enc_str .match_indices(",") .map(|x| x.0 + 1) .chain(iter::once(enc_str.len())) .enumerate() .filter(|x| x.0 % 2 == 1) .scan( (0, 0), |tp, item| { *tp = (tp.1, item.1) Some(tp.clone()) }) .map(|x| enc_str[x.0 .. x.1].split(",").collect()) .map(|x: Vec<_>| (x[0].into(), x[1].into())) .collect() }
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
5 u/Sechan9 Oct 17 '21 Good human
5
Good human
6
Better than using imperative loops btw
1
cleanly in ruby, and not so cleanly, also in ruby
essentially u/0lach's solution
def parse_encoded_str(st) st.split(",").each_slice(2).to_h end ## not so clean, recursive /w lambda to_ha = ->(a){ a.is_a?(String) ? to_ha.call(a.split(",")) : a[1] ? {a[0]=> a[1]}.merge(to_ha.call(a[2..])) : {}} to_ha.call(st) #to call
Evil and ugly virgin imperative code vs chad good and beautiful functional programming
const parse = (s: string) => { const arr = s.split(","); return arr.reduce((acc, v, i) => i % 2 === 0 ? { ...acc, [v]: arr[i + 1], } : acc, {}); }
0
Not gonna lie, a lot of my code looks like this
94
u/0lach Oct 17 '21 edited Oct 17 '21
Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f6fb7cdaeb727da4c6c4e2c78541bc9b
30
u/Scud000 Oct 17 '21
super hard to read this code, but the example helped. but I'm sure I can make an easier to read revision of this with less code too.
12
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Oct 17 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
6
u/davi_suga Oct 17 '21
Better than using imperative loops btw
1
u/Skwidz Oct 18 '21 edited Oct 18 '21
cleanly in ruby, and not so cleanly, also in ruby
essentially u/0lach's solution
1
u/Kasufert Nov 04 '21
Evil and ugly virgin imperative code vs chad good and beautiful functional programming
1
u/Stephen2Aus Oct 17 '21
0
u/zerofox2189 Oct 17 '21
Not gonna lie, a lot of my code looks like this