Skip to content

Instantly share code, notes, and snippets.

@monapasan
Created February 1, 2017 00:08
Show Gist options
  • Save monapasan/11aca6dda66f1d7959d52407f3090fb7 to your computer and use it in GitHub Desktop.
Save monapasan/11aca6dda66f1d7959d52407f3090fb7 to your computer and use it in GitHub Desktop.
package list
case class Cons (val head:Int, val tail:IntList) extends IntList{
def isEmpty=false
def nth(index:Int):Int= index match{
case 0 => head
case i => tail.nth(i-1)
}
def contains(elem:Int):Boolean= elem match{
case y if (y==head) => true
case _ => tail.contains(elem)
}
def insert(X:Int):IntList= new Cons(X,this)
def insertS(elem:Int):IntList= {
if(head >= elem)
new Cons(elem, this)
else
new Cons(head, tail.insertS(elem))
}
// def delete(elem:Int):IntList= elem match {
// case el if(this.head == el) => Cons(tail.head, tail.tail)
// case _ => Cons(head, tail.delete(elem))
// }
def delete(elem:Int):IntList= {
if(elem==head) tail
else Cons(head, tail.delete(elem))
}
def prefix(list1: IntList): IntList = list1.isEmpty match {
case true => this
case false => new Cons(list1.head, this.prefix(this.prefix(list1.tail)))
}
def deleteAll(elem:Int):IntList = elem match {
case el if(this.head == el) => tail.deleteAll(elem)
case _ => new Cons(head, tail.delete(elem))
}
def map(f:Int=> Int): IntList = {
Cons(f(head), tail.map(f))
}
def filter(f:Int=>Boolean): IntList = {
if(f(head)) Cons(head, tail.filter(f))
else tail.filter(f)
}
// def reduce(f:(Int,Int)=> Int, base:Int): Int = {
// f(head,tail.reduce(f, base))
// }
// (1 to 10) map(x=> x*x) === (1 to 10) yield x*x
for(x <- 1 to 10) yield x*x
for(x <- 1 to 10 if(x%2==0)) yield x*x
// deklarativ
for(x<- List(1,2,3); y<-List('a','b','c')) yield(x,y)
// funktional
List(1,2,3).map(x=> List('a','b','c').map(y=>(x,y))).toList.flatten
List(1,2,3).flatMap(x=> List('a','b','c').map(y=>(x,y))).toList
Array(1,2,3).zip(Array(4,5,6)).map(x=>x._1 + x._2) // Array(5, 7, 9)
Array(1,2,3).zip(Array(4,5,6)).map(x=>x._1 + x._2).reduce((x:Int,y:Int)=>x+y) // 21
val m = Map('a'->1,'b'->2,'c'->3)
m.getOrElse('q',0) // 0
m.getOrElse('a',0) // 1
m.get('q') match{
case None => 'Nichts'
case Some(x) => x.toString
}
}
package list
case object Empty extends IntList{
def isEmpty=true
def head:Int= throw new Error("head.nil")
def tail:IntList= throw new Error("tail.nil")
def contains(elem:Int):Boolean=false
def nth(index:Int)= throw new Error("IndexOutOfBound")
def insert(X:Int):IntList= new Cons(X,this)
def prefix(list1: IntList): IntList = this
def insertS(elem:Int):IntList= new Cons(elem, this)
def delete(elem:Int):IntList= this
def deleteAll(elem:Int):IntList = this
def map(f:Int=> Int): IntList = this
def filter(f:Int=>Boolean): IntList = null
// def reduce(f:(Int,Int)=> Int, base:Int): Int = base
}
package list
abstract class IntList {
def isEmpty:Boolean
def head:Int
def tail:IntList
def nth(index:Int):Int
def contains(elem:Int):Boolean
def insert(elem:Int):IntList
def insertS(elem:Int):IntList
def delete(elem:Int):IntList
def deleteAll(elem:Int):IntList
def prefix(list1: IntList): IntList
def insertSO(elem:Int):IntList= {
if(head >= elem)
new Cons(elem, this)
else
new Cons(head, tail.insertS(elem))
}
def map(f:Int=> Int): IntList
def insertionSort:IntList= {
if(isEmpty)
this
else
tail.insertionSort.insertS(head)
}
def filter(f:Int=>Boolean): IntList
def reduce(f:(Int,Int)=> Int): Int = {
if(tail.isEmpty){
head
}else{
f(head,tail.reduce(f))
}
}
def flatten(l: List[Any]):List[Any] = l match{
case Nil => Nil
// _ is a synonym for Any
case (x:List[_])::xs => flatten(x) :: flatten(xs)
case x::xs => x::flatten(xs)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment