Skip to content

Instantly share code, notes, and snippets.

View jzelayeta's full-sized avatar
💭
λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ

Julian Zelayeta jzelayeta

💭
λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ
View GitHub Profile
package model
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AsyncWordSpecLike
import java.text.SimpleDateFormat
import java.time.Instant
import java.util.Date
import java.util.concurrent.{ExecutorService, Executors}
import scala.concurrent.{Future, Promise}
package model
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AsyncWordSpecLike
import java.text.SimpleDateFormat
import java.time.Instant
import java.util.Date
import java.util.concurrent.{ExecutorService, Executors}
import scala.concurrent.{Future, Promise}
@jzelayeta
jzelayeta / Introduction2PatternMatching.scala
Created February 27, 2020 21:29
Introduction2PatternMatching
//Sum the elements of a list
def sum(list: List[Int]): Int = list match {
case Nil => 0
case head :: tail => head + sum(tail)
}
//Reverse a list
def reverse[A](list: List[A]): List[A] = list match {
case Nil => Nil
case head :: Nil => List(head)
{
"leader": "akka://Catalog-Domain@10.0.10.47:2551",
"members": [{
"node": "akka://Catalog-Domain@10.0.31.10:2551",
"nodeUid": "6963894071647843575",
"roles": [
"dc-default"
],
"status": "Leaving"
},
[{
"time": "2020-02-12T18:34:22.487Z",
"message": "Slf4jLogger started"
},
{
"time": "2020-02-12T18:34:23.587Z",
"message": "Remoting started with transport [Artery tcp]; listening on address [akka://Catalog-Domain@10.0.75.227:2551] with UID [-4725893541386128209]"
},
{
"time": "2020-02-12T18:34:23.614Z",
import scala.concurrent.{ExecutionContext, Future}
case class Post() // For sake of simplicity it hasn't attributes
def findPostsForUser(userId: Long): Future[List[Post]] = ???
def postsByUsers(userIds: List[Long])(implicit ec: ExecutionContext) =
for {
aUserId <- userIds
def sequence[A,B](seq: Seq[Either[A,B]]): Either[Seq[A], B] = seq.foldLeft(Left(Seq.empty[A]): Either[Seq[A], B]){
(acc, e) => e match {
case Left(exception) => acc.left.map(l => l :+ exception)
case Right(_) => acc
}}
def validate[A](sequence: Seq[A])(f: A => Either[Exception,A]): Seq[Either[Exception,A]] = sequence.map(f)
public List[Match] process(String csv) throws CSVMatchConstraintException{
List[String] rows = toRows(csv);
validator.validate(rows) //in case of invalid it will throw an exeption
return rows.stream()
.map(row => new Match(row(0), row(1), row(2), row(3)))
.collect(toList())
}
}
public class CSVMatchConstraintException extends RuntimeException {
public CSVMatchConstraintException(Int lineNumber, Throwable err) {
super("Error at line " + lineNumber, err)
}
}