Skip to content

Instantly share code, notes, and snippets.

@sujeet100
sujeet100 / ScalaSyntax.scala
Created March 23, 2016 06:50
Scala syntax
// 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)
@sujeet100
sujeet100 / MapDiff.groovy
Created January 13, 2017 06:42
Groovy script to find out the difference between two maps. Can be useful to debug failing unit tests when the objects in comparison are big jsons.
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) {
@sujeet100
sujeet100 / Book.java
Last active February 15, 2017 08:47
Collections in Scala, Groovy and Java 8
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;
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));
}
};
//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(
//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
//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))));
@sujeet100
sujeet100 / TupleQuery.scala
Last active November 14, 2017 04:16
Quill: Tail recursive liftQuery macro
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.
[{"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.
[{"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,