A Dart function to return a random element from a list
If you need to get a random element from a list in Dart, I can confirm this this getRandomListElement
method works:
If you need to get a random element from a list in Dart, I can confirm this this getRandomListElement
method works:
Here’s a little example of a Flutter time preferences widget, something I’ll be using in the new version of my Just Be app. Probably most important thing about this source code is that it shows an example of how to use the Flutter RadioListTile
:
Summary: This blog post shows one way to drop/filter the first matching element from a Scala sequence (Seq, List, Vector, Array, etc.). I don’t claim that the algorithm is efficient, but it does work.
While creating some Scala test code earlier today I had an immutable list of toppings for a pizza, and I got into a situation where I wanted to remove the first instance of a topping.
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.
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?”
As a brief note today, here’s a Scala method that writes the strings in a list — more accurately, a Seq[String]
— to a file:
def writeFile(filename: String, lines: Seq[String]): Unit = {
val file = new File(filename)
val bw = new BufferedWriter(new FileWriter(file))
for (line <- lines) {
bw.write(line)
}
bw.close()
}
As a brief note today, I was working on a Java/Android application recently, and I needed a “tail” function when I was working on a Java list. What I mean by that is that Scala has a tail
function that returns all elements of the list except for the head element, like this:
scala> val x = List(1,2,3,4)
x: List[Int] = List(1, 2, 3, 4)
scala> x.tail
res1: List[Int] = List(2, 3, 4) //head element removed
and I wanted the same thing in Java.
As a little note today, if you ever need to extract a subset of a Java list or array, here are some examples of the Java subList
method:
If for some reason you ever need a list of people’s given names for testing your Java/Scala/Kotlin/JVM code, here’s a Java class with a sorted, static list of over 5,000 male and female given (first) names:
As a brief note, here’s an example that shows how to create an RxJava 2 Observable from a Java List:
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)