Skip to content

Instantly share code, notes, and snippets.

View jmaciasluque's full-sized avatar

Juan Macias jmaciasluque

View GitHub Profile
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) {
def createList(listSize: Int): List[String] = {
Seq.fill[Int](listSize)(Random.nextInt(sourceWords.size))
.map(sourceWords(_))
.toList
}
val sourceWords: List[String] =
Source.fromFile("words.txt").getLines().toList
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"))
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"))
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
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())
def createList(listSize: Int): List[String] = {
Seq.fill[Int](listSize)(Random.nextInt(sourceWords.length))
.map(sourceWords(_))
.toList
}
def allWords: List[String] = {
sourceWords.toList
}
val sourceWords: Array[String] =
Source.fromFile("words.txt").getLines().toArray
public List<String> createList(int listSize) {
return
new Random()
.ints(listSize, 0, sourceWords.size())
.mapToObj(sourceWords::get)
.collect(Collectors.toList());
}