list

Android: How to convert a list of strings or objects to a single string alvin August 2, 2017 - 4:39pm

Android FAQ: How do I convert a list of strings (or a list of objects) to a single, combined string?

In Android, if you want to convert a list of strings to a String, I just saw this approach:

Ubuntu ‘apt-get’ list of commands (cheat sheet)

I have a couple of Ubuntu Linux systems, including Raspberry Pi systems, test servers, and production servers. It seems like every time I have to use an apt-get or other apt command, I always have to search for the command I need. To put an end to that, I’m creating this “apt-get reference page.” It’s very terse, as I’ve just written it for myself, but I hope it’s also helpful for others.

How to populate a Scala List with sample data (examples)

As a quick note, I was just reminded that you can populate a Scala List using a Range, like this:

scala> (1 to 5).toList
res0: List[Int] = List(1, 2, 3, 4, 5)

scala> (1 to 10 by 2).toList
res1: List[Int] = List(1, 3, 5, 7, 9)

scala> (5 to 11).toList
res2: List[Int] = List(5, 6, 7, 8, 9, 10, 11)

scala> ('d' to 'h').toList
res3: List[Char] = List(d, e, f, g, h)

Those are just a few examples. For many more ways to populate Scala lists with sample data, see How to populate Scala collections with a Range, How to generate random numbers, characters, and sequences in Scala, and Different ways to create and populate Lists in Scala.

How to create multiline strings in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.2, “How to Create Multiline Strings in Scala.”

Problem

You want to create multiline strings within your Scala source code, like you can with the “heredoc” syntax of other languages.

Solution

In Scala you create multiline strings by surrounding your text with three double quotes:

A “Minority Report” Monte Carlo simulation in Scala


“The Precogs are never wrong. But occasionally they do disagree.”
~ Minority Report
 

This article shares the source code for a Monte Carlo simulation that I wrote in Scala. It was inspired by the movie Minority Report, as well as my own experience.

Background

For the purposes of this simulation, imagine that you have three people that are each “right” roughly 80% of the time. For instance, if they take a test with 100 questions, each of the three individuals will get 80 of the questions right, although they may not get the same questions right or wrong. Given these three people, my question to several statisticians was, “If two of the people have the same answer to a given question, what are the odds that they are correct? Furthermore, if all three of them give the same answer to a question, what are the odds that they are right?”

Recursion: How Recursive Function Calls Work

An important point to understand about recursive function calls is that just as they “wind up” as they are called repeatedly, they “unwind” rapidly when the function’s end condition is reached.

In the case of the sum function, the end condition is reached when the Nil element in a List is reached. When sum gets to the Nil element, this pattern of the match expression is matched:

Recursion: How to Write a ‘sum’ Function

With all of the images of the previous lesson firmly ingrained in your brain, let’s write a sum function using recursion!

Sketching the sum function signature

Given a List of integers, such as this one:

val list = List(1, 2, 3, 4)

let’s start tackling the problem in the usual way, by thinking, “Write the function signature first.”

Using Scala Methods As If They Were Functions (Eta Expansion) alvin May 28, 2017 - 6:44pm

“The owls are not what they seem.”

From the television series, Twin Peaks

Goals

Have you noticed that the Scaladoc for the List class map method clearly shows that it takes a function?

But despite that, you can somehow pass it a method, and it still works, as shown in this code: