Skip to content

Instantly share code, notes, and snippets.

@berngp
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berngp/10199642 to your computer and use it in GitHub Desktop.
Save berngp/10199642 to your computer and use it in GitHub Desktop.
Scala Scripts Exercises.
val width = 5
//width: Int = 5
val tTop = ( 0 until width/2 ) map ( n => 2 * n + 1 )
//tTop: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 3)
val tBottom = tTop.reverse
//tBottom: scala.collection.immutable.IndexedSeq[Int] = Vector(3, 1)
val diamond = tTop union Seq(width) union tBottom
//diamond: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 3, 5, 3, 1)
val offset = 3
//offset: Int = 3
val d = diamond map ( n => ( offset + (width - n)/2, n) )
//d: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((5,1),
//(4,3), (3,5), (4,3), (5,1))
def printDiamond( s:Seq[(Int,Int)] ) = {
s.foreach{ t =>
( 0 until t._1 ).foreach( _ => print(".") )
( 0 until t._2 ).foreach( _ => print("*") )
print("\n")
}
}
//printDiamond: (s: Seq[(Int, Int)])Unit
printDiamond(d)
//.....*
//....***
//...*****
//....***
//.....*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment