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
// Sum of an array | |
def f(arr:List[Int]):Int = arr.reduceLeft(_+_) | |
//sum of odd numbers in an array | |
//reduceLeft does not take inital value | |
def f(arr:List[Int]):Int = arr.foldLeft(0)((a, b)=> if(b%2==1) a+b else 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 mapDiff(Map m1, Map m2, String path="") { m1.each{k,v-> | |
if(m2[k] != m1[k]) { | |
if(m1[k] in Map) { | |
mapDiff(m1[k] as Map, m2[k] as Map, "${path}${k}.") | |
} | |
else { | |
println("${path}${k} ->"); | |
println("\texpected: ${m1[k]}") | |
println("\tactual: ${m2[k]}") | |
if (m1[k] in List) { |
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 class Book { | |
private String name; | |
private String author; | |
private int numberOfPages; | |
public Book(String name, String author, int numberOfPages) { | |
this.name = name; | |
this.author = author; | |
this.numberOfPages = numberOfPages; |
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
List<Book> books = new ArrayList<Book>() { | |
{ | |
add(new Book("Clean Code", "Bob", 100)); | |
add(new Book("Refactoring", "Martin", 300)); | |
add(new Book("Extreme Programming", "Bob", 200)); | |
add(new Book("TDD", "Kent", 250)); | |
} | |
}; |
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
//Scala | |
assert(books.filter((book: Book) => book.author == "Bob") == List(new Book("Clean Code", "Bob", 100), new Book("Extreme Programming", "Bob", 200))) | |
//Or | |
assert(books.filter(book => book.author == "Bob") == List(new Book("Clean Code", "Bob", 100), new Book("Extreme Programming", "Bob", 200))) | |
//Or | |
assert(books.filter(_.author == "Bob") == List(new Book("Clean Code", "Bob", 100), new Book("Extreme Programming", "Bob", 200))) | |
//Java | |
assertThat( |
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
//Scala | |
assert(books.find(_.name == "TDD") == Option(new Book("TDD", "Kent", 250))) | |
//Java 8 | |
assertThat(books.stream() | |
.filter(book -> book.getName().equals("TDD")) | |
.findFirst(), | |
is(Optional.of(new Book("TDD", "Kent", 250)))); | |
//Groovy |
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
//Scala | |
assert(books.filter{_.numberOfPages > 100}.take(2) == List(new Book("Refactoring", "Martin", 300), new Book("Extreme Programming", "Bob", 200))) | |
//Java 8 | |
assertThat(books.stream() | |
.filter(book -> book.getNumberOfPages() > 100) | |
.limit(2) | |
.collect(Collectors.toList()) | |
, is(Arrays.asList(new Book("Refactoring", "Martin", 300), new Book("Extreme Programming", "Bob", 200)))); |
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
trait TupleQuery { | |
this: io.getquill.context.Context[_, _] => | |
def liftTuples[T1: Encoder, T2: Encoder](l: List[(T1, T2)]): Quoted[Query[(T1, T2)]] = | |
liftTuples(l, (t: (T1, T2)) => infix"(${lift(t._1)}, ${lift(t._2)})", null) | |
private def liftTuples[T, U](l: List[T], f: T => Quoted[Any], acc: Quoted[Query[U]]): Quoted[Query[U]] = { | |
l match { | |
case Nil => infix"".as[Query[U]] | |
case head :: Nil => infix"${f(head)}, $acc".as[Query[U]] |
This file has been truncated, but you can view the full file.
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
[{"productKey":374,"productName":"childlike-studio-miss","buyingPrice":4,"currentPrice":83,"recommendedPrice":20,"compName":"messy-toys-soothsay","compPrice":45,"children":[{"productKey":374,"productName":"childlike-studio-miss","buyingPrice":4,"currentPrice":83,"recommendedPrice":20,"compName":"messy-toys-soothsay","compPrice":45,"region":"EAST"},{"productKey":374,"productName":"childlike-studio-miss","buyingPrice":4,"currentPrice":83,"recommendedPrice":20,"compName":"messy-toys-soothsay","compPrice":45,"region":"WEST"},{"productKey":374,"productName":"childlike-studio-miss","buyingPrice":4,"currentPrice":83,"recommendedPrice":20,"compName":"messy-toys-soothsay","compPrice":45,"region":"NORTH"},{"productKey":374,"productName":"childlike-studio-miss","buyingPrice":4,"currentPrice":83,"recommendedPrice":20,"compName":"messy-toys-soothsay","compPrice":45,"region":"SOUTH"}]},{"productKey":880,"productName":"flowery-cobweb-target","buyingPrice":22,"currentPrice":3,"recommendedPrice":21,"compName":"noiseless-desk- |
This file has been truncated, but you can view the full file.
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
[{"productKey":149039373,"productName":"homely-seat-scribble","buyingPrice":10,"currentPrice":32,"recommendedPrice":65,"compName":"loose-seed-warn","compPrice":14,"region":"EAST"},{"productKey":149039373,"productName":"homely-seat-scribble","buyingPrice":10,"currentPrice":32,"recommendedPrice":65,"compName":"loose-seed-warn","compPrice":14,"region":"WEST"},{"productKey":149039373,"productName":"homely-seat-scribble","buyingPrice":10,"currentPrice":32,"recommendedPrice":65,"compName":"loose-seed-warn","compPrice":14,"region":"NORTH"},{"productKey":149039373,"productName":"homely-seat-scribble","buyingPrice":10,"currentPrice":32,"recommendedPrice":65,"compName":"loose-seed-warn","compPrice":14,"region":"SOUTH"},{"productKey":709644620,"productName":"mellow-lawyer-inventory","buyingPrice":25,"currentPrice":67,"recommendedPrice":44,"compName":"awake-marble-wind","compPrice":53,"region":"EAST"},{"productKey":709644620,"productName":"mellow-lawyer-inventory","buyingPrice":25,"currentPrice":67,"recommendedPrice":44, |
OlderNewer