idiomatic

Golang and inheritance

空扰寡人 提交于 2019-11-30 01:37:30
I want to provide a base struct with methods in my library that can be 'extended'. The methods of this base struct rely on methods from the extending struct. This is not directly possible in Go, because struct methods only have acces to the structs own fields, not to parent structs. The point is to have functionality that I do not have to repeat in each extending class. I have come up with this pattern, which works fine, but looks quite convoluted due to it's cyclical structure. I have never found anything like it in other Go code. Is this very un-go? What different approach could I take? type

Multiple constructors: the Pythonic way? [duplicate]

人盡茶涼 提交于 2019-11-29 20:33:37
This question already has an answer here: How to overload __init__ method based on argument type? 10 answers I have a container class that holds data. When the container is created, there are different methods to pass data. Pass a file which contains the data Pass the data directly via arguments Don't pass data; just create an empty container In Java, I would create three constructors. Here's how it would look like if it were possible in Python: class Container: def __init__(self): self.timestamp = 0 self.data = [] self.metadata = {} def __init__(self, file): f = file.open() self.timestamp = f

Read a file and get an array of strings

风格不统一 提交于 2019-11-29 17:09:37
问题 I want to read a file and get back a vector of String s. The following function works, but is there a more concise or idiomatic way? use std::fs::File; use std::io::Read; fn lines_from_file(filename: &str) -> Vec<String> { let mut file = match File::open(filename) { Ok(file) => file, Err(_) => panic!("no such file"), }; let mut file_contents = String::new(); file.read_to_string(&mut file_contents) .ok() .expect("failed to read!"); let lines: Vec<String> = file_contents.split("\n") .map(|s:

How can I better learn to “not pay for what you don't use”?

♀尐吖头ヾ 提交于 2019-11-29 16:18:50
I've just gotten answers to this question which, at the bottom line, tell me: "Doing X doesn't make sense since it would make you pay for things you might not use." I find this maxim difficult to follow; my instincts lean more towards seeing what I consider clear semantics, with things defined "in their place". More generally, it's not immediate for me to realize what the hidden costs and secret tariffs would be for a particular design choice?. Is this covered by (non-reference) books on C++? Is there someplace relevant online to better enlighten myself on following this principle? In the case

scala: union of two maps whose key type is the same and whose value type is a collection of elements, but whose types are different

我只是一个虾纸丫 提交于 2019-11-29 12:53:50
I would like to create a union of two maps whose key type is the same and whose value type is a collection of elements, but whose types are different. Consider the following contrived example: case class Child(name: String) val peopleToChildren: Map[String, Seq[Child]] = Map("max" -> Seq(Child("a"), Child("b")), "yaneeve" -> Seq(Child("y"), Child("d"))) case class Pet(name: String) val peopleToPets: Map[String, Seq[Pet]] = Map("max" -> Seq(Pet("fifi")), "jill" -> Seq(Pet("bobo"), Pet("jack"), Pet("Roger rabbit"))) val peopleToChildrenAndDogs: Map[String, (Seq[Child], Seq[Pet])] = { // people

Get the first element of a list idiomatically in Groovy

青春壹個敷衍的年華 提交于 2019-11-29 10:44:26
问题 Let the code speak first def bars = foo.listBars() def firstBar = bars ? bars.first() : null def firstBarBetter = foo.listBars()?.getAt(0) Is there a more elegant or idiomatic way to get the first element of a list, or null if it's not possible? (I wouldn't consider a try-catch block elegant here.) 回答1: Not sure using find is most elegant or idiomatic, but it is concise and wont throw an IndexOutOfBoundsException. def foo foo = ['bar', 'baz'] assert "bar" == foo?.find { true } foo = [] assert

Multiple constructors: the Pythonic way? [duplicate]

柔情痞子 提交于 2019-11-28 16:35:25
问题 This question already has an answer here: How to overload __init__ method based on argument type? 10 answers I have a container class that holds data. When the container is created, there are different methods to pass data. Pass a file which contains the data Pass the data directly via arguments Don't pass data; just create an empty container In Java, I would create three constructors. Here's how it would look like if it were possible in Python: class Container: def __init__(self): self

How to idiomatically iterate one half of an array and modify the structure of the other?

牧云@^-^@ 提交于 2019-11-28 14:09:55
What is the idiomatic way to iterate (read) over the first half of the vector and change the structure of the second half of the vector depending on the first? This is very abstract but some algorithms could be boiled down to this problem. I want to write this simplified C++ example in Rust: for (var i = 0; i < vec.length; i++) { for (var j = i + 1 ; j < vec.length; j++) { if (f(vec[i], vec[j])) { vec.splice(j, 1); j--; } } } red75prime An idiomatic solution of this generic problem will be the same for Rust and C, as there's no constraints which would allow simplification. We need to use

scala: union of two maps whose key type is the same and whose value type is a collection of elements, but whose types are different

梦想与她 提交于 2019-11-28 06:25:35
问题 I would like to create a union of two maps whose key type is the same and whose value type is a collection of elements, but whose types are different. Consider the following contrived example: case class Child(name: String) val peopleToChildren: Map[String, Seq[Child]] = Map("max" -> Seq(Child("a"), Child("b")), "yaneeve" -> Seq(Child("y"), Child("d"))) case class Pet(name: String) val peopleToPets: Map[String, Seq[Pet]] = Map("max" -> Seq(Pet("fifi")), "jill" -> Seq(Pet("bobo"), Pet("jack"),

Seeking constructive criticism on monad implementation

断了今生、忘了曾经 提交于 2019-11-28 06:16:08
I'm learning monads, this is my first working one (aside from the trivial monad). Feel free to criticize everything in it ruthlessly. I'm especially interested in "more idiomatic" and "more elegant" kind of responses. This monad counts the number of binds performed. data C a = C {value :: a, count :: Int} deriving (Show) instance Monad C where (>>=) (C x c) f = C (value $ f x) (c + 1) return x = C x 0 add :: (Num a) => a -> a -> C a add x y = return $ x + y -- Simpler way to do this? foldM is obviously something different. mysum [x] = return x mysum (x:xs) = mysum xs >>= add x Stylistically