As a little note to self, here are some examples of how to create ArrayList instances in Kotlin while explicitly assigning their types:
As a quick note today, if you ever need some examples of how the Kotlin collections methods work, I hope these examples are helpful.
First, here’s some sample data:
val a = listOf(10, 20, 30, 40, 10) val names = listOf("joel", "ed", "chris", "maurice")
I just found some notes from when I first began working with Scala, and I was working with the yield keyword in for loops. If you haven't worked with something like yield before, it will be helpful to know how it works. Here's a statement of how the yield keyword works in for loops, based on the documentation in the book, Programming in Scala:
Table of Contents
- Background: What is a Cons cell?
- What it might look like in Scala
- Starting to create my own Cons class
- My second effort
- Defining my nil value
- Defining Cons
- Replacing the NilCons method bodies
- Adding a toString method to Cons
- The complete code at this point
- I’d really like a :: method
- See also
For some examples in my new book on functional programming in Scala I needed to create a collection class of some sort. Conceptually an immutable, singly-linked list is relatively easy to grok, so I decided to create my own Scala list from scratch. This tutorial shows how I did that.Back to top
Background: What is a Cons cell?
The first time I learned about linked lists was in a language named Lisp. In Lisp, a linked list is created as a series of “Cons” cells. A cons cell is simple, it contains only two things:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 19.6, “How to define a collection whose element are all of some base type.”Back to top
You want to specify that a class or method takes a type parameter, and that parameter is limited so it can only be a base type, or a subtype of that base type.Back to top
Define the class or method by specifying the type parameter with an upper bound. To demonstrate this, create a simple type hierarchy:Back to top
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 19.5, “How (and why) to make immutable collections covariant.”
You want to create a collection whose elements can’t be changed (they’re immutable), and want to understand how to specify it.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 19.4, “How to make mutable collections invariant in Scala.”
You want to create a collection whose elements can be mutated, and want to know how to specify the generic type parameter for its elements.
When creating a collection of elements that can be changed (mutated), its generic type parameter should be declared as
[A], making it invariant.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 17.1, “How to go to and from Java collections in Scala.”
You’re using Java classes in a Scala application, and those classes either return Java collections, or require Java collections in their method calls.
Use the methods of Scala’s
JavaConversions object to make the conversions work.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 16.6, “How to update documents in a MongoDB collection with Casbah.”
You want to update one or more documents in a MongoDB collection.
Use either the
update methods from the Casbah
MongoCollection class, as shown in this example:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 16.4, “How to insert documents into MongoDB with insert, save, and +=.”
You want to save documents to a MongoDB collection from a Scala application.
+= methods of the Casbah