-
-
Save giovannicandido/79b360711526d282e9d2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
object Main extends App { | |
val nomes = Seq("Twing","Twing","Twofi") | |
def imprimirSeq(seq: Seq[String], filtro: Filtro){ | |
val filtrados = filtro.filtrar(seq) | |
filtrados.foreach(println) | |
} | |
imprimirSeq(nomes, new FiltrarPorNome("Twing")) | |
imprimirSeq(nomes, new FiltrarPorComecarCom("Tw")) | |
imprimirSeq(nomes, new Filtro { | |
override def filtrar(valores: Seq[String]) = { | |
nomes.filter(n => n.endsWith("fi")) | |
} | |
}) | |
def imprimirSeqFun(seq: Seq[String], filtro: Seq[String] => Seq[String]){ | |
val filtrados = filtro(seq) | |
filtrados.foreach(println) | |
} | |
def imprimirSeqFun2(seq: Seq[String], filtro: String => Boolean) { | |
val filtrados = seq.filter(filtro) | |
filtrados.foreach(println) | |
} | |
def filtrarPorNome(nome: String)(nomes: Seq[String]) = { | |
nomes.filter(n => n.equals(nome)) | |
} | |
def filtrarPorComecarCom(nome: String)(nomes: Seq[String]) = { | |
nomes.filter(n => n.startsWith(nome)) | |
} | |
imprimirSeqFun(nomes, filtrarPorNome("Twing") ) | |
imprimirSeqFun(nomes, filtrarPorComecarCom("Tw")) | |
imprimirSeqFun(nomes, n => n.filter(_.equals("Twofi"))) | |
imprimirSeqFun2(nomes, n => n.equals("Twofi")) | |
imprimirSeqFun2(nomes, _.equals("Twofi")) | |
} | |
abstract class Filtro { | |
def filtrar(lista: Seq[String]): Seq[String] | |
} | |
class FiltrarPorNome(val nome: String) extends Filtro { | |
override def filtrar(lista: Seq[String]): Seq[String] = { | |
import scala.collection.mutable.ListBuffer | |
var novaLista: ListBuffer[String] = ListBuffer.empty | |
for(valor <- lista) { | |
if(valor.equals(nome)) | |
novaLista += valor | |
} | |
novaLista.toSeq | |
} | |
} | |
class FiltrarPorComecarCom(val nome: String) extends Filtro { | |
override def filtrar(lista: Seq[String]): Seq[String] = { | |
import scala.collection.mutable.ListBuffer | |
var novaLista: ListBuffer[String] = ListBuffer.empty | |
for(valor <- lista) { | |
if(valor.startsWith(nome)) | |
novaLista += valor | |
} | |
novaLista.toSeq | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment