Skip to content

Instantly share code, notes, and snippets.

View notyy's full-sized avatar

大魔头 notyy

View GitHub Profile
@notyy
notyy / gist:1516600
Created December 24, 2011 06:43
out of mem
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
@notyy
notyy / gist:1489145
Created December 17, 2011 04:12
why can't
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)
@notyy
notyy / gist:1394003
Created November 25, 2011 17:14
parameterized
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')
@notyy
notyy / gist:1388718
Created November 23, 2011 13:54
strange1
class AA {
def /: (s: String) = {
println("s = " + s)
}
def :/ (s: String) = {
println("s = " + s)
}
}
@notyy
notyy / gist:1388638
Created November 23, 2011 13:17
谁装了scala2.77?
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")
@notyy
notyy / gist:1373793
Created November 17, 2011 17:13
case class constructor
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))
@notyy
notyy / gist:1372878
Created November 17, 2011 10:40
oo style vs fp style
// 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
@notyy
notyy / gist:1372507
Created November 17, 2011 06:20
compiled
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
@notyy
notyy / gist:1366271
Created November 15, 2011 05:44
different
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)
@notyy
notyy / gist:1366221
Created November 15, 2011 05:14
what's benefet?
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)