How to split a string in Haskell?

Is there a standard way to split a string in Haskell? lines and words work great from splitting on a space or newline, but surely there is a standard way to split on a comma? I couldn’t find it on Hoogle. To be specific, I’m looking for something where split “,” “my,comma,separated,list” returns [“my”,”comma”,”separated”,”list”]. 14 … Read more

Python: most idiomatic way to convert None to empty string?

What is the most idiomatic way to do the following? def xstr(s): if s is None: return ” else: return s s = xstr(a) + xstr(b) update: I’m incorporating Tryptich’s suggestion to use str(s), which makes this routine work for other types besides strings. I’m awfully impressed by Vinay Sajip’s lambda suggestion, but I want … Read more

Why is it discouraged to accept a reference to a String (&String), Vec (&Vec), or Box (&Box) as a function argument?

I wrote some Rust code that takes a &String as an argument: fn awesome_greeting(name: &String) { println!(“Wow, you are awesome, {}!”, name); } I’ve also written code that takes in a reference to a Vec or Box: fn total_price(prices: &Vec<i32>) -> i32 { prices.iter().sum() } fn is_even(value: &Box<i32>) -> bool { **value % 2 == … Read more

How do I concatenate strings in Swift?

How to concatenate string in Swift? In Objective-C we do like NSString *string = @”Swift”; NSString *resultStr = [string stringByAppendingString:@” is a new Programming Language”]; or NSString *resultStr=[NSString stringWithFormat:@”%@ is a new Programming Language”,string]; But I want to do this in Swift-language. 21 Answers 21

What is the equivalent of the join operator over a vector of Strings?

I wasn’t able to find the Rust equivalent for the “join” operator over a vector of Strings. I have a Vec<String> and I’d like to join them as a single String: let string_list = vec![“Foo”.to_string(),”Bar”.to_string()]; let joined = something::join(string_list,”-“); assert_eq!(“Foo-Bar”, joined); Related: What’s an idiomatic way to print an iterator separated by spaces in Rust? … Read more

Finding index of character in Swift String

It’s time to admit defeat… In Objective-C, I could use something like: NSString* str = @”abcdefghi”; [str rangeOfString:@”c”].location; // 2 In Swift, I see something similar: var str = “abcdefghi” str.rangeOfString(“c”).startIndex …but that just gives me a String.Index, which I can use to subscript back into the original string, but not extract a location from. … Read more