This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def createList(listSize: Int): List[String] = { | |
Seq.fill[Int](listSize)(Random.nextInt(sourceWords.size)) | |
.map(sourceWords(_)) | |
.toList | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val sourceWords: List[String] = | |
Source.fromFile("words.txt").getLines().toList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def createList(listSize: Int): List[String] = { | |
Seq.fill[Int](listSize)(Random.nextInt(sourceWords.length)) | |
.map(sourceWords(_)) | |
.toList | |
} | |
def allWords: List[String] = { | |
sourceWords.toList | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val sourceWords: Array[String] = | |
Source.fromFile("words.txt").getLines().toArray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<String> createList(int listSize) { | |
return | |
new Random() | |
.ints(listSize, 0, sourceWords.size()) | |
.mapToObj(sourceWords::get) | |
.collect(Collectors.toList()); | |
} |
NewerOlder