On Tue, Oct 15, 2013 at 7:05 PM, Jose A. Lopes <[email protected]> wrote:
> > > > > > > > +-- | Groups instance information calls heading out to the same > nodes. > > > > +consoleParamsToCalls :: [InstanceConsoleInfoParams] > > > > + -> [(Node, RpcCallInstanceConsoleInfo)] > > > > +consoleParamsToCalls params = > > > > + let sortedParams = sortBy > > > > + (comparing (instPrimaryNode . instConsInfoParamsInstance)) > > > params > > > > + groupedParams = groupBy compareParamsByNode sortedParams > > > > + in map (\x -> case x of > > > > + [] -> error "Programmer error: group must have one or > more > > > members" > > > > > > This function 'consoleParamsToCalls' is being called from within the > > > IO monad. I'm sure we can handle this error better. Perhaps > > > 'exitIfBad' in the IO monad? > > > > > > > > This is truly a case of a programmer error - the groupBy function > produces > > groups containing at least one element. > > Haskell cannot infer this, and so it complains if the empty list case is > > not handled. > > The head function would perhaps be ideal, but hlint complains about it. > > What is 'params' is empty? Shouldn't 'consoleParamsToCalls' return an > empty list? > What about the following? > > mapMaybe (\x -> case x of > [] -> Nothing > ... -> Just ...) > > Well, if params is empty, the grouping produces an empty list, mapped to an empty list, which is completely correct. Although the solution you suggested is quite elegant, and I will probably want to use it in the future, I do not think it fits well here. It simply masks an erroneous case in which an empty list is delivered to a function expecting at least one value in the list. Should that happen, I want to know about it and not ignore it. As the code is now, it should never happen - the group function splits a list into sublists based on equality, and in any sane implementation this will mean that the sublists have at least one element. However, if anyone tinkers with the code, I want to signal that this is a prerequisite, and I cannot enforce this using only the Haskell type system. I would really like to signal this at compile time and still get a pure function, but I do not think I can. Correctness > purity? Hrvoje Ribicic Ganeti Engineering Google Germany GmbH Dienerstr. 12, 80331, München Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg Geschäftsführer: Graham Law, Christine Elizabeth Flores Steuernummer: 48/725/00206 Umsatzsteueridentifikationsnummer: DE813741370
