# array

A “Minority Report” Monte Carlo simulation in Scala alvin August 13, 2019 - 6:47pm

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?”

How to convert a Java array into a Stream alvin March 1, 2019 - 7:32pm

If you ever need to convert a Java array into a Stream, there are at least two ways to do it.

## 1) Converting an array to a Stream

First, to convert the entire array to a `Stream`, use the `Stream.of` static method like this:

Scala: Generating a sequence/list of all ASCII printable characters alvin February 7, 2019 - 5:11pm

I ran into a couple of interesting things today when trying to generate random alphanumeric strings in Scala, which can be summarized like this. I won’t get into the “random” stuff I was working on, but here are a couple of examples of how to generate lists of alphanumeric/ASCII characters in Scala:

```scala> val chars = ('a' to 'Z').toList
chars: List[Char] = List()

scala> val chars = ('A' to 'z').toList
chars: List[Char] =
List(A, B, C, D, E, F, G, H, I, J, K, L,
M, N, O, P, Q, R, S, T, U, V, W, X,
Y, Z, [, \, ], ^, _, `, a, b, c, d,
e, f, g, h, i, j, k, l, m, n, o, p,
q, r, s, t, u, v, w, x, y, z)

scala> val chars = (' ' to 'z').toList
chars: List[Char] =
List( , !, ", #, \$, %, &, ', (, ), *, +,
,, -, ., /, 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, :, ;, <, =, >, ?, @, A, B, C,
D, E, F, G, H, I, J, K, L, M, N, O,
P, Q, R, S, T, U, V, W, X, Y, Z, [,
\, ], ^, _, `, a, b, c, d, e, f, g,
h, i, j, k, l, m, n, o, p, q, r, s,
t, u, v, w, x, y, z)```

## Kotlin sortedBy syntax and examples

As a quick note, here’s an example of the Kotlin `sortedBy` syntax. Given this list of strings:

``val names = listOf("kim", "julia", "jim", "hala")``

the comments after these examples show how the Kotlin `sortedBy` function works:

## Kotlin: Convert a list to a String (joinToString syntax/example)

Here’s a little Kotlin `joinToString` example. First, a sample list to work with:

``val nums = listOf(1,2,3,4,5)``

Then here’s the `joinToString` example:

``````nums.joinToString(
separator = ", ",
prefix = "[",
postfix = "]",
limit = 3,
truncated = "there’s more ..."
)``````

When you put all of that code in the Kotlin REPL you’ll see this result: