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
import System.IO | |
import Data.String | |
import Data.Map(Map,empty,insertWith',toList) | |
import Data.List(foldl',sortBy) | |
import Data.Ord(comparing) | |
main = do | |
inh <- openFile "sample.txt" ReadMode | |
inpStr <- hGetContents inh |
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
val timeSections = List((0,3), (3,6), (6,9), (9,12), (12,15), (15,18), (18,21), (21,24)) | |
scala> val hour = Calendar.getInstance.get(Calendar.HOUR_OF_DAY) | |
hour: Int = 12 | |
//doesn't work | |
scala> timeSections.filter((start,end) => start <= hour && end > hour) | |
<console>:11: error: wrong number of parameters; expected = 1 | |
timeSections.filter((start,end) => start <= hour && end > hour) |
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
scala> def gen[T](count:Int, elem:T): List[T] = List.fill(count)(elem) | |
gen: [T](count: Int, elem: T)List[T] | |
scala> gen[Char](4, 'a') | |
res21: List[Char] = List(a, a, a, a) | |
scala> gen(4, 'a') | |
res22: List[Char] = List(a, a, a, a) | |
scala> gen[String](4, 'a') |
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
class AA { | |
def /: (s: String) = { | |
println("s = " + s) | |
} | |
def :/ (s: String) = { | |
println("s = " + s) | |
} | |
} |
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
val anyList = List[AnyRef](1:java.lang.Integer) | |
val strList = List[String]("abc") | |
def workToAny(xs: List[AnyRef]) = "ok,works" | |
----------------------------------------------- | |
scala> val anyList = List[AnyRef](1:java.lang.Integer) | |
anyList: List[AnyRef] = List(1) | |
scala> val strList = List[String]("abc") |
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
case class Rectangle(width:Float,height:Float) | |
scala> (1 to 10) map (Rectangle(10, _)) | |
res1: scala.collection.immutable.IndexedSeq[Rectangle] = Vector(Rectangle(10.0,1.0), Rectangle(10.0,2.0), Rectangle(10.0,3.0), Rectangle(10.0,4.0), Rectangle(10.0,5.0), Rectangle(10.0,6.0), Rectangle( | |
10.0,7.0), Rectangle(10.0,8.0), Rectangle(10.0,9.0), Rectangle(10.0,10.0)) |
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
// oo style | |
trait OShape { | |
def area: Double | |
} | |
class OCircle(radius: Double) extends OShape { | |
def area = radius * radius * Math.Pi | |
} | |
class OSquare(length:Double) extends OShape { | |
def area = length * length |
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 Bf { | |
def bf = (x:Int) => (y:Int) => (z:Int) => x + y + z | |
def bf1(x:Int)(y:Int)(z:Int) = x + y + z | |
def bf2(x:Int,y:Int,z:Int) = x + y + z | |
} | |
------------- | |
E:\source\scala\learn>scalac -print bf.scala | |
[[syntax trees at end of cleanup]]// Scala source: bf.scala |
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
scala> def csum(x:Int)(y:Int)(z:Int) = x + y + z | |
csum: (x: Int)(y: Int)(z: Int)Int | |
scala> def psum(x:Int, y:Int, z:Int) = x + y + z | |
psum: (x: Int, y: Int, z: Int)Int | |
scala> :t csum(1) _ | |
Int => Int => Int | |
scala> :t psum(1,_:Int,_:Int) |
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
def add(a:Int)(b:Int) = a + b | |
def plus(a:Int,b:Int) = a + b | |
val add1 = add(1) _ | |
val plus1 = plus(1,_:Int) | |
scala> plus1(2) | |
res17: Int = 3 | |
scala> add1(2) |