Question

D'accord, cette question semble être vraiment stupide, mais ce que je veux dire, c'est que si vous regardez l'API Scala 2.7.6, ils ont rendu la méthode mappingToString obsolète. Par conséquent, il devrait exister une alternative plus élégante pour l'impression de cartes au format personnalisé. Avoir la méthode d’équivalence de mkString dans Map est très pratique dans presque tous les cas.

Qu'est-ce que vous en pensez? Quel est votre bout de code pour imprimer une carte sauf println?

Était-ce utile?

La solution

La méthode mappingToString a été utilisée pour modifier la traduction de chaque paire clé / valeur en chaîne, qui a ensuite été utilisée par la méthode toString .

Je pense que c'est un ajustement moche. Cela ajoute une mutabilité à une structure de données par ailleurs immuable. Si vous avez des besoins d'impression spécifiques, vous feriez probablement mieux de les placer dans une autre classe.

Autres conseils

mappingToString était spécifique à Map .

Avec le nouveau framework de collections dans Scala2.8, un Map peut être itéré par tout IterableLike , qui étend TraversableLike .

La méthode mkstring (déjà disponible dans la version 2.7 pour Iterable ) doit ensuite être utilisé.

Voir cet article de blog " Strings " par Jesse , pour 2.7 exemples mkstring () :

/*
   Making use of raw strings to create a multi line string
   I add a | at the beginning of each line so that we can line up the quote nicely 
   in source code then later strip it from the string using stripMargin
*/
scala> val quote = """|I  don-t consider myself a pessimist.                                                                                                 
     |                |I think of a pessimist as someone who is waiting for it to rain.
     |                |And I feel soaked to the skin.
     | 
     |                |Leonard Cohen"""
quote: java.lang.String = 
|I don-t consider myself a pessimist. 
                      |I think of a pessimist as someone who is waiting for it to rain.
                      |And I feel soaked to the skin.

                      |Leonard Cohen

// capilize the first character of each line
scala> val capitalized = quote.lines.
     |                         map( _.trim.capitalize).mkString("\n")
capitalized: String = 
|I don-t consider myself a pessimist.
|I think of a pessimist as someone who is waiting for it to rain.
|And I feel soaked to the skin.

|Leonard Cohen

// remove the margin of each line
scala> quote.stripMargin        
res1: String = 
I don-t consider myself a pessimist. 
I think of a pessimist as someone who is waiting for it to rain.
And I feel soaked to the skin.

Leonard Cohen

// this is silly.  I reverse the order of each word but keep the words in order
scala> quote.stripMargin.         
     |       lines.               
     |       map( _.split(" ").   
     |              map(_.reverse).
     |              mkString (" ")).
     |      mkString("\n")
res16: String = 
I t-nod redisnoc flesym a .tsimissep
I kniht fo a tsimissep sa enoemos ohw si gnitiaw rof ti ot .niar
dnA I leef dekaos ot eht .niks

dranoeL nehoC

Vous pouvez également combiner Iterator.map () avec mkString () , par exemple pour créer une chaîne de requête à partir d'un map [String, String] :

val queryString = updatedMap.map(pair => pair._1+"="+pair._2).mkString("?","&","")
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top