Instantly share code, notes, and snippets.

View Gotcha1NoTerminalOperation.java
package me.juanmacias;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class Gotcha1NoTerminalOperation {
public static void main(String[] args) {
View Lesson3_Part1_createList_inefficient.scala
def createList(listSize: Int): List[String] = {
Seq.fill[Int](listSize)(Random.nextInt(sourceWords.size))
.map(sourceWords(_))
.toList
}
View Lesson3_Part1_constructor_inefficient.scala
val sourceWords: List[String] =
Source.fromFile("words.txt").getLines().toList
View Lesson3_Part3.scala
def processWords(wordList: List[String], parallel: Boolean): List[String] = {
maybeParallel(parallel, wordList.sorted)
.map(_.toLowerCase)
.map(_.toUpperCase)
.filter(!_.startsWith("a"))
.filter(!_.startsWith("A"))
.filter(!_.startsWith("m"))
.filter(!_.startsWith("M"))
.filter(!_.startsWith("z"))
View Lesson3_Part3.java
static List<String> processWords(List<String> wordList, boolean parallel) {
Supplier<Stream<String>> streamSupplier = parallel ? wordList::parallelStream : wordList::stream;
return
streamSupplier.get()
.sorted()
.map(String::toLowerCase)
.map(String::toUpperCase)
.filter(word -> !word.startsWith("a"))
View Lesson3_Part2.scala
def computeLevenshtein(wordList: List[String], parallel: Boolean): Array[Array[Int]] = {
maybeParallel(parallel, wordList).map((a) =>
maybeParallel(parallel, wordList).map((b) =>
Levenshtein.lev(a, b)).toArray
).toArray
}
def maybeParallel(parallel: Boolean, wordList: List[String]): GenSeq[String] with Immutable = {
if(parallel)
wordList.par
View Lesson3_Part2.java
static int[][] computeLevenshtein(List<String> wordList, boolean parallel) {
final int LIST_SIZE = wordList.size();
int[][] distances = new int[LIST_SIZE][LIST_SIZE];
Supplier<Stream<String>> streamSupplier = parallel ? wordList::parallelStream : wordList::stream;
streamSupplier.get()
.map(a -> streamSupplier.get()
.mapToInt(b -> Levenshtein.lev(a, b))
.toArray())
View Lesson3_Part1_createList.scala
def createList(listSize: Int): List[String] = {
Seq.fill[Int](listSize)(Random.nextInt(sourceWords.length))
.map(sourceWords(_))
.toList
}
def allWords: List[String] = {
sourceWords.toList
}
View Lesson3_Part1_constructor.scala
val sourceWords: Array[String] =
Source.fromFile("words.txt").getLines().toArray
View Lesson3_Part1_createList.java
public List<String> createList(int listSize) {
return
new Random()
.ints(listSize, 0, sourceWords.size())
.mapToObj(sourceWords::get)
.collect(Collectors.toList());
}