Scala Puzzlers

Scala Puzzlers

Andrew Phillips

Language: English

Pages: 238

ISBN: 0981531679

Format: PDF / Kindle (mobi) / ePub

Scala Puzzlers

Andrew Phillips

Language: English

Pages: 238

ISBN: 0981531679

Format: PDF / Kindle (mobi) / ePub


Scala Puzzlers is a collection of enigmatic Scala code examples that behave highly contrary to expectations and will entertain and enlighten even the most accomplished Scala developer. Challenge your Scala intuition, then treat yourself to a revealing explanation of the reasons underlying the code's actual behavior that will provide important and often fundamental insights into Scala. Get ready for a fun journey that will take you further down the road to Scala mastery!

Sinatra: Up and Running

Beautiful Code: Leading Programmers Explain How They Think

More iPhone Development with Swift: Exploring the iOS SDK

Dreaming in Code: Two Dozen Programmers, Three Years, 4,732 Bugs, and One Quest for Transcendent Software

JavaServer Faces in Action

iOS 5 Essentials

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

equal to all the subsequent elements in that half. What happens in the case of the second array in the code example? First, stableSort recursively sorts 7.89, 1.23, and Double.NaN, 4.56, resulting in the sorted subarrays 1.23, 7.89, and Double.NaN, 4.56. The second subarray is indeed correctly sorted according to the IEEE specification, since Ordering.lt(4.56, Double.NaN) is false, i.e., Double.NaN <= 4.56. 2 See 3A SI-5104, “(Double.NaN min 0.0) yields 0.0, should be NaN.” [Dou] sorting

from s.type to ?{def toInt: ?} implicit def stringToInt(s: String): Int = s.toInt ˆ The short answer is that function vals take precedence over defs during implicit search. The point is that this behavior is not justified by the language specification, which specifies static overloading resolution as the algorithm for determining implicit precedence. Yet overloading resolution treats “regular” defs and function vals equivalently and does not exhibit the preference for function vals that is

infer it. Usually, it is fairly obvious—the element type of the resulting collection is the same as the element type of the source collection: scala> List("1", "2").toSet res2: scala.collection.immutable.Set[String] = Set(1, 2) In this case, however, the additional set of parentheses forces the compiler to reason differently. Because toSet cannot be invoked with parentheses, the compiler can only interpret the parentheses as an attempt to invoke apply on the result of calling toSet.

Overview · Contents · Discuss · Suggest · Index 189 Puzzler 32 · Set the Record Straight scala> val set = list.toSet set: scala.collection.immutable.Set[String] = Set(1, 2) scala> val result = set() :9: error: not enough arguments for method apply: (elem:String)Boolean in trait GenSetLike. Unspecified value parameter elem. val result = set() ˆ The last statement here fails to compile because the type widening that turns Set[String] into Set[Any] does not take place. That is, the

· Contents · Discuss · Suggest · Index Puzzler 5 · The Missing List Possibilities 1. Prints: Int = 4 Int = 4 2. Prints: Int = 4 Int = 2 3. Prints: Int = 2 Int = 4 4. Prints: Int = 2 Int = 2 Explanation You may wonder whether sumSizes is somehow counting the number of collections rather than their sizes, which would cause both statements to print 2, as in answer 4. As it happens, though, the correct answer is number 2: scala> sumSizes(List(Set(1, 2), List(3, 4))) res5: Int = 4 scala>

Download sample

Download