سؤال

حسنا، هذا السؤال يبدو أن الغباء حقا واحدة، ولكن وجهة نظري هو أنه إذا كنت نلقي نظرة على سكالا 2.7.6 API، التي قطعتها طريقة mappingToString إهمال. ولذلك، ينبغي أن يكون هناك بديل أكثر أناقة للطباعة خريطة بتنسيق المخصصة. منذ ما يقرب من أي غرض، وجود طريقة معادلة mkString في الخريطة هو مفيد حقا.

وماذا يا رفاق التفكير في الامر؟ ما هو قصاصة الترميز الخاص بك لطباعة خريطة باستثناء println؟

هل كانت مفيدة؟

المحلول

وكانت تستخدم هذه الطريقة mappingToString لتغيير الطريقة التي ترجمت كل زوج من مفتاح / قيمة إلى سلسلة، الذي كان آنذاك والتي يستخدمها طريقة toString.

وأعتقد أن هذا نوبة رديء. وتضيف التحولية إلى بنية البيانات غير قابل للتغيير إلا لشيء واحد. إذا كان لديك متطلبات الطباعة محددة، ثم ربما كنت أفضل حالا وضعها في فئة أخرى.

نصائح أخرى

وكان mappingToString الخاصة Map.

مع إطار مجموعات جديدة في Scala2.8، وMap يمكن كرر بأي IterableLike، الذي يمتد على TraversableLike .

وأسلوب <لأ href = "http://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/library/scala/collection/TraversableLike.scala#L766" يختلط = "نوفولو noreferrer" > mkstring (وبالفعل هناك في 2.7 ل <لأ href = "http://lampsvn.epfl.ch/trac/scala/browser/scala/tags/R_2_7_7_RC2/src/library/scala/Iterable.scala؟view = الترميز # L411 "يختلط =" نوفولو noreferrer "> Iterable) ينبغي تستخدم بعد ذلك.

بلوق وظيفة "سلاسل" جيسي ، ل 2.7 أمثلة 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

هل يمكن الجمع بين أيضا Iterator.map() مع mkString()، على سبيل المثال لإنشاء استعلام سلسلة من map[String,String]:

val queryString = updatedMap.map(pair => pair._1+"="+pair._2).mkString("?","&","")
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top