background preloader

SCALA crazy research

Facebook Twitter

Wiki - Cheat Sheet. Scalacheat. Cheat sheet for scala syntax. Working with Lists. Chapter 16 of Programming in Scala, First EditionWorking with Listsby Martin Odersky, Lex Spoon, and Bill VennersDecember 10, 2008 Lists are probably the most commonly used data structure in Scala programs.

Working with Lists

This chapter explains lists in detail. It presents many common operations that can be performed on lists. It also teaches some important design principles for programs working on lists. 16.1 List literals [link] Scala - Difference between MutableList and ListBuffer. Create a List, foreach, for comprehension. By Alvin Alexander.

Create a List, foreach, for comprehension

Last updated: Feb 5, 2014. Building A Simple Scala List From Scratch « Matt Malone’s Old-Fashioned Software Development Blog. In Java you don’t see a lot of linked lists, and if you do it’s almost always java.util.LinkedList.

Building A Simple Scala List From Scratch « Matt Malone’s Old-Fashioned Software Development Blog

People never write their own lists. They don’t really need to, I suppose. The one from java.util is fine. Plenty of people are leading fulfilling software careers never having implemented their own linked list. But it’s kind of a shame. Scala: How to convert tuple elements to lists. Is there a scala list operation that makes tuples from lists.

SCALA by Martin Oderskey Course Related

Scala Tutorial - Tuples, Lists, methods on Lists and Strings. Preface This is the second in a planned series of tutorials on programming in Scala for first-time programmers, with specific reference to my Fall 2011 course Introduction to Computational Linguistics.

Scala Tutorial - Tuples, Lists, methods on Lists and Strings

You can see the other tutorials here on this blog; they are also listed on the course’s links page. This tutorial focuses on Tuples and Lists, which are two constructs for working with groups of elements. You won’t get much done without the latter, and the former are so incredibly useful you probably find yourself using them a lot. Tuples We saw in the previous tutorial how a single value can be assigned to a variable and then used in various contexts. The elements of a Tuple can be recovered in a few different ways. Scala> val (first, second) = twoInts first: Int = 3 second: Int = 9 scala> val (numTimes, thingToSay, price) = mixedUp numTimes: Int = 1 thingToSay: java.lang.String = hello price: Double = 1.16 Scala peels off the values and assigns them to each of the single variables. Scala for Loops. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Scala for Loops

There are various forms of for loop in Scala which are described below: The for Loop with Ranges The simplest syntax of a for loop in Scala is: for( var x <- Range ){ statement(s);} Here, the Range could be a range of numbers and that is represented as i to j or sometime like i until j. Example: Following is the example of for loop with range using i to j syntax: When the above code is compiled and executed, it produces the following result: Performance with Scala Arrays and Lists. As I continue to tinker with Scala, I was wondering about the performance differences between an Array and List.

Performance with Scala Arrays and Lists

This post will detail what I've found, but as always YMMV and I could be doing it all wrong. If there's a better (in this case, better == faster) way to do this in Scala, please let me know. My application performs a lot of collection iteration as it combines the values of two collections into a new collection by addition. For instance, I need to combine [1,2] and [3,4] into [4,6]. I wanted to find out if the collections should be an Array or List. Intuition tells me that the Array will perform better, but this is Scala, and Lists reign supreme. For each test, I wanted to write a function that combined the two collections using tail recursion. Code Examples for Programming in Scala. 99 Problems in Scala. Placement Profile. Converting Array to List in Scala. Now, this has to have a built-in somewhere in Scala, because it just seems too common.

Converting Array to List in Scala

LISTS IN SCALA - Complete. As with most functional languages, lists play a big roll in Scala.

LISTS IN SCALA - Complete

Lists contains, among others, the following operations. // List in Scala or homogenous, they are declared as List[T] val names: List[String] = List("Arnold", "George", "Obama") // Lists are constructed from two building blocks :: and Nil assert(names == "Arnold" :: "George" :: "Obama" :: Nil) // Gets the first element of a list assert(names.head == "Arnold") // Gets the rest of the list assert(names.tail == "George" :: "Obama" :: Nil) // Checks if the list is empty assert(List().isEmpty) Instead of using head and tail, pattern matching is commonly used.

From these simple functions a flora of functions is built. Eclipse Shortcuts. Digg Man, I’m such an impatient guy.

Eclipse Shortcuts

I cringe whenever I see somebody squint and frown, looking for a JSP file in Eclipse by browsing painfully through the gazillion JSPs in multiple folders in the Package Explorer. I squirm whenever I see somebody looking for a Java class by clicking through packages, one by one, backtracking if it’s the wrong package, and so on, until he sees the correct Java class. I mean, any resource in the workspace is literally seconds away. Ditto to classes (and interfaces, and members, and so on). Why waste time and brain cycles to wade through countless lines in countless files? So without further ado, let’s say you want to: Open any file quickly without browsing for it in the Package Explorer: Ctrl + Shift + R. Open a type (e.g.: a class, an interface) without clicking through interminable list of packages: Ctrl + Shift + T. Go directly to a member (method, variable) of a huge class file, especially when a lot of methods are named similarly: Ctrl + O. Generate random numbers.

To generate a series of random numbers as a unit, you need to use a singleRandom object - do not create a new Random object for each new random number.

Generate random numbers

Other alternatives are: SecureRandom, a cryptographically strong subclass of Random ThreadLocalRandom, intended for multi-threaded cases Here are some examples using Random. Example 1 Example run of this class: Generating 10 random integers in range 0..99. Exceptions and pattern matching. 28 Jan 2008 So far, we’ve been examining the similarities between Scala and Java. Areas where Scala syntax is so similar to Java’s as to be almost identical. Of course, this doesn’t take full advantage of the language, but it allows developers to utilize the cleaner Scala syntax within the restrictions of a familiar environment. From this point on, things are going to get a bit weird. A number of the “more advanced” features in the Scala language are clear departures from Java.

Pattern Matching. SCALA Futures. ShadajProgramming's channel. Lazy val. How to add elements to a List in Scala.