Changeset 7415 for lang/scala

Show
Ignore:
Timestamp:
03/03/08 21:40:54 (6 years ago)
Author:
ryugate
Message:

POI,Format,Http:改行コードの修正
Timecode:前面改定

Location:
lang/scala/sandbox/src/jp/ryugate
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • lang/scala/sandbox/src/jp/ryugate/apache/POI.scala

    r6890 r7415  
    1818    = n.foldLeft(List[T]()){(acc, i) => f(i) :: acc}.reverse 
    1919     
    20   private def eachWithIndex[T](lst:List[T], f2:(T,int) => Unit) 
     20  private def eachWithIndex[T,V](lst:List[T], f2:(T,int) => V) 
    2121    = lst.zipWithIndex.foreach{case (row,idx) => f2(row,idx)} 
    2222 
     
    3636    } 
    3737 
    38     def withSheet(index:int  )(f:Sheet => Unit):Unit = f(sheet(index)) 
    39     def withSheet(name:String)(f:Sheet => Unit):Unit = f(sheet(name)) 
     38    def withSheet[V](index:int  )(f:Sheet => V) = f(sheet(index)) 
     39    def withSheet[V](name:String)(f:Sheet => V) = f(sheet(name)) 
    4040 
    4141    def sheets(idxs:int*  ) = makeSomeList(sheet, idxs:_*) 
    4242    def sheets(range:Range) = makeSomeList(sheet, range:_*) 
    4343 
    44     def eachSheets(idxs:int*  )(f:Sheet => Unit):Unit = sheets(idxs:_* ).foreach{f(_)} 
    45     def eachSheets(range:Range)(f:Sheet => Unit):Unit = sheets(range:_*).foreach{f(_)} 
     44    def eachSheets[V](idxs:int*  )(f:Sheet => V) = sheets(idxs:_* ).foreach{f(_)} 
     45    def eachSheets[V](range:Range)(f:Sheet => V) = sheets(range:_*).foreach{f(_)} 
    4646     
    47     def eachSheetsWithIndex(idxs:int*  )(f:(Sheet,int) => Unit) = eachWithIndex(sheets(idxs:_*), f) 
    48     def eachSheetsWithIndex(range:Range)(f:(Sheet,int) => Unit) = eachWithIndex(sheets(range:_*), f) 
     47    def eachSheetsWithIndex[V](idxs:int*  )(f:(Sheet,int) => V) = eachWithIndex(sheets(idxs:_*), f) 
     48    def eachSheetsWithIndex[V](range:Range)(f:(Sheet,int) => V) = eachWithIndex(sheets(range:_*), f) 
    4949 
    50     def each(f:Sheet => Unit):Unit = eachSheets(1 to hssf_book.getNumberOfSheets)(f) 
     50    def each[V](f:Sheet => V) = eachSheets(1 to hssf_book.getNumberOfSheets)(f) 
    5151 
    5252    //-------------------------------------------- 
     
    6565    def cell(ri:int,ci:int) = row(ri).apply(ci) 
    6666 
    67     def withRow(ri:int)(f:Row => Unit):Unit = f(row(ri)) 
    68     def withCol(ci:int)(f:Col => Unit):Unit = f(col(ci)) 
     67    def withRow[V](ri:int)(f:Row => V) = f(row(ri)) 
     68    def withCol[V](ci:int)(f:Col => V) = f(col(ci)) 
    6969 
    7070    def rows(rowIdxs:int*) = makeSomeList(row, rowIdxs:_*) 
     
    7373    def cols(range:Range ) = makeSomeList(col, range:_*) 
    7474 
    75     def eachRows(rowIdxs:int*)(f:Row => Unit) = rows(rowIdxs:_*).foreach{f(_)} 
    76     def eachCols(colIdxs:int*)(f:Col => Unit) = cols(colIdxs:_*).foreach{f(_)} 
    77     def eachRows(range:Range )(f:Row => Unit) = rows(range     ).foreach{f(_)} 
    78     def eachCols(range:Range )(f:Col => Unit) = cols(range     ).foreach{f(_)} 
     75    def eachRows[V](rowIdxs:int*)(f:Row => V) = rows(rowIdxs:_*).foreach{f(_)} 
     76    def eachCols[V](colIdxs:int*)(f:Col => V) = cols(colIdxs:_*).foreach{f(_)} 
     77    def eachRows[V](range:Range )(f:Row => V) = rows(range     ).foreach{f(_)} 
     78    def eachCols[V](range:Range )(f:Col => V) = cols(range     ).foreach{f(_)} 
    7979 
    80     def eachRowsWithIndex(idxs:int*  )(f:(Row,int) => Unit) = eachWithIndex(rows(idxs:_*), f) 
    81     def eachColsWithIndex(idxs:int*  )(f:(Col,int) => Unit) = eachWithIndex(cols(idxs:_*), f) 
    82     def eachRowsWithIndex(range:Range)(f:(Row,int) => Unit) = eachWithIndex(rows(range:_*), f) 
    83     def eachColsWithIndex(range:Range)(f:(Col,int) => Unit) = eachWithIndex(cols(range:_*), f) 
     80    def eachRowsWithIndex[V](idxs:int*  )(f:(Row,int) => V) = eachWithIndex(rows(idxs:_*), f) 
     81    def eachColsWithIndex[V](idxs:int*  )(f:(Col,int) => V) = eachWithIndex(cols(idxs:_*), f) 
     82    def eachRowsWithIndex[V](range:Range)(f:(Row,int) => V) = eachWithIndex(rows(range:_*), f) 
     83    def eachColsWithIndex[V](range:Range)(f:(Col,int) => V) = eachWithIndex(cols(range:_*), f) 
    8484 
    85     def each(f:Row => Unit):Unit = eachRows(0 to getLastRowNum)(f) 
     85    def each[V](f:Row => V) = eachRows(0 to getLastRowNum)(f) 
    8686     
    8787    //-------------------------------------------- 
     
    115115    def cells(range:Range) = makeSomeList(apply, range:_*) 
    116116 
    117     def eachCells(idxs:int*  )(f:Cell => Unit) = cells(idxs:_* ).foreach{f(_)} 
    118     def eachCells(range:Range)(f:Cell => Unit) = cells(range:_*).foreach{f(_)} 
     117    def eachCells[V](idxs:int*  )(f:Cell => V) = cells(idxs:_* ).foreach{f(_)} 
     118    def eachCells[V](range:Range)(f:Cell => V) = cells(range:_*).foreach{f(_)} 
    119119  
    120     def eachCellsWithIndex(idxs:int*  )(f:(Cell,int) => Unit) = eachWithIndex(cells(idxs:_*), f) 
    121     def eachCellsWithIndex(range:Range)(f:(Cell,int) => Unit) = eachWithIndex(cells(range:_*), f) 
     120    def eachCellsWithIndex[V](idxs:int*  )(f:(Cell,int) => V) = eachWithIndex(cells(idxs:_*), f) 
     121    def eachCellsWithIndex[V](range:Range)(f:(Cell,int) => V) = eachWithIndex(cells(range:_*), f) 
    122122       
    123     def each(f:Cell => Unit):Unit = eachCells(0 to hssf_row.getLastCellNum)(f) 
     123    def each[V](f:Cell => V) = eachCells(0 to hssf_row.getLastCellNum)(f) 
    124124 
    125125    //-------------------------------------------- 
     
    151151    def cells(range:Range) = makeSomeList(apply, range:_*) 
    152152     
    153     def eachCells(idxs:int*  )(f:Cell => Unit) = cells(idxs:_* ).foreach{f(_)} 
    154     def eachCells(range:Range)(f:Cell => Unit) = cells(range:_*).foreach{f(_)} 
     153    def eachCells[V](idxs:int*  )(f:Cell => V) = cells(idxs:_* ).foreach{f(_)} 
     154    def eachCells[V](range:Range)(f:Cell => V) = cells(range:_*).foreach{f(_)} 
    155155  
    156     def eachCellsWithIndex(idxs:int*  )(f:(Cell,int) => Unit) = eachWithIndex(cells(idxs:_*), f) 
    157     def eachCellsWithIndex(range:Range)(f:(Cell,int) => Unit) = eachWithIndex(cells(range:_*), f) 
     156    def eachCellsWithIndex[V](idxs:int*  )(f:(Cell,int) => V) = eachWithIndex(cells(idxs:_*), f) 
     157    def eachCellsWithIndex[V](range:Range)(f:(Cell,int) => V) = eachWithIndex(cells(range:_*), f) 
    158158       
    159     def each(f:Cell => Unit):Unit = eachCells(0 to sheet.getLastRowNum)(f) 
     159    def each[V](f:Cell => V) = eachCells(0 to sheet.getLastRowNum)(f) 
    160160  } 
    161161   
  • lang/scala/sandbox/src/jp/ryugate/apache/POISpecification.scala

    r6890 r7415  
    33import org.specs._ 
    44import org.specs.matcher._ 
    5 import org.specs.matcher.AnyMatchers 
    6 import org.specs.matcher.PatternMatchers.CaseMatcher 
    75 
    86object POISpecification extends Specification { 
     
    1210 
    1311  "POI module" should { 
    14     val book = new Book("test.xls") 
     12    val book = new Book("testdata/test.xls") 
    1513    val sheet1 = book.sheet(1) 
    1614 
  • lang/scala/sandbox/src/jp/ryugate/net/Http.scala

    r6269 r7415  
     1//      if (_proxy != null) { 
     2//        val proxy_addr = new InetSocketAddress(PROXY_HOST, PROXY_PORT) 
     3//        val proxy      = new Proxy(Proxy.Type.HTTP, proxy_addr) 
     4//      } 
     5 
    16package jp.ryugate.net 
    27 
     
    1015    def get(resource:String):String = { 
    1116      val url = new URL("http://" + host + resource) 
     17      val br = new BufferedReader(new InputStreamReader(getInputStream(url))) 
     18      try { 
     19        readContents("", (br.readLine _)) 
     20      } finally { 
     21        br.close 
     22      } 
     23    } 
     24 
     25    def getInputStream(url: URL) = { 
    1226      val con = url.openConnection.asInstanceOf[HttpURLConnection] 
    1327      if (_read_timeout > 0) con.setReadTimeout(_read_timeout) 
    1428      con.setRequestMethod("GET") 
    15       val br = new BufferedReader(new InputStreamReader(con.getInputStream)) 
    16       readContents("", (br.readLine _)) 
     29      con.getInputStream 
    1730    } 
    1831 
    19     def readContents(acc:String, reader:()=>String):String = reader() match{ 
     32    def readContents(acc:String, reader:()=>String):String = reader() match { 
    2033      case null => acc 
    2134      case s => readContents(acc + s, reader) 
     
    2336     
    2437    def read_timeout:int = _read_timeout 
    25     def read_timeout_=(timeout:int) { _read_timeout = timeout} 
     38    def read_timeout_=(timeout:int) { _read_timeout = timeout } 
     39 
     40//    def proxy_host:String = _proxy_host 
     41//    def proxy_host_=(proxy_host:String) { _proxy_host = proxy_host } 
    2642  } 
    2743 
     
    3046    f(http) 
    3147  } 
     48   
     49//  def get(url:String) 
    3250} 
    3351   
  • lang/scala/sandbox/src/jp/ryugate/text/RegexpSpecification.scala

    r6122 r7415  
    6363    } 
    6464 
    65     "=~" in { 
    66       ("piyohoge" =~ "hoge") must beTrue 
     65//    "=~" in { 
     66//      ("piyohoge" =~ "hoge") must beTrue 
     67//    } 
     68     
     69    "with Pattern match" in { 
     70      var ans = "" 
     71       
     72      val lst = List("hoge", "piyo", "foo", "hogepiyo") 
     73      lst.foreach {item => 
     74        item match { 
     75          case s if s =~ "h..e" =>    ans += s 
     76          case s if s =~ "p[iy]+o" => ans += s 
     77          case _ => null 
     78        } 
     79      } 
     80      ans must be_==("hogepiyohogepiyo") 
    6781    } 
     82     
    6883  } 
    6984} 
  • lang/scala/sandbox/src/jp/ryugate/video/Timecode.scala

    r6119 r7415  
    33import jp.ryugate.text.Format 
    44 
    5 object Timecode { 
    6   class TimecodeFactory(rate:Any) { 
    7     def make(hh:int, mm:int, ss:int, ff:int) = rate match { 
    8       case "ntsc" => 
    9         val minutes = hh * 60 + mm 
    10         new Timecode(17982 * (minutes/10) + 1798 * (minutes%10) + ss * 30 + ff) 
    11       case r:Int => 
    12         val mrate = r * 60 
    13         val hrate = mrate * 60 
    14         new Timecode(hh * hrate + mm * mrate + ss * r + ff) 
    15     } 
     5class Timecode(val frames:Int, val rate:Any) { 
     6  def this(hh:Int, mm:Int, ss:Int, ff:Int, rate:Any) = this( rate match { 
     7    case "ntsc" => 
     8      val minutes = hh * 60 + mm 
     9      17982 * (minutes/10) + 1798 * (minutes%10) + ss * 30 + ff 
     10    case r:Int => 
     11      val mrate = r * 60 
     12      val hrate = mrate * 60 
     13      hh * hrate + mm * mrate + ss * r + ff 
     14  }, rate) 
     15 
     16  def changeRate(r:Any) = new Timecode(frames, r) 
     17 
     18  override def toString = rate match { 
     19    case "ntsc" => 
     20      val hh = frames / 17982 / 6 
     21      val mm = frames / 17982 % 6 * 10 + (frames % 17982 - 2) / 1798 
     22      val ss = ((frames % 17982 - 2) % 1798 + 2) / 30 
     23      val ff = ((frames % 17982 - 2) % 1798 + 2) % 30 
     24      sprIntf("%02d:%02d:%02d;%02d", hh,mm,ss,ff) 
     25    case r:Int => 
     26      val m = r * 60 
     27      val h = m * 60 
     28      val hh = frames / h 
     29      val mm = (frames % h) / m 
     30      val ss = ((frames % h) % m) / r 
     31      val ff = ((frames % h) % m) % r 
     32      sprIntf("%02d:%02d:%02d:%02d", hh,mm,ss,ff) 
    1633  } 
    1734 
    18   class Timecode(frames:int) { 
    19     class Tcode(hh:int, mm:int, ss:int, ff:int) { 
    20       def hour = hh 
    21       def minutes = mm 
    22       def second = ss 
    23       def frame = ff 
    24    
    25       override def toString() = Format.sprintf("%02d:%02d:%02d.%02d", hh,mm,ss,ff) 
    26     } 
     35  def +(add:Int) = new Timecode(frames + add, rate) 
     36  def -(sub:Int) = new Timecode(frames - sub, rate) 
    2737 
    28     def ntsc = 
    29       new Tcode(frames / 17982 / 6, 
    30                 frames / 17982 % 6 * 10 + (frames % 17982 - 2) / 1798, 
    31                 ((frames % 17982 - 2) % 1798 + 2) / 30, 
    32                 ((frames % 17982 - 2) % 1798 + 2) % 30 
    33               ) 
     38  private def sprIntf(fmt:String, args:Any*) = String.format(fmt, args.map(e => e.asInstanceOf[AnyRef]).toArray) 
     39} 
    3440 
    35     def tc:Tcode = tc(30) 
    36     def tc(rate:int):Tcode = { 
    37       val m = rate * 60 
    38       val h = m * 60 
    39       new Tcode(frames / h, 
    40                 (frames % h) / m, 
    41                 ((frames % h) % m) / rate, 
    42                 ((frames % h) % m) % rate 
    43               ) 
    44     } 
    45   } 
     41object TimecodeTest extends Application { 
     42  println("DROP") 
     43  val t1 = new Timecode(0,0,59,27,"ntsc") 
     44  (1 to 5).foreach {i => println(t1 + i) } 
     45 
     46  println("NONDROP") 
     47  val t2 = t1.changeRate(30) 
     48  (1 to 5).foreach {i => println(t2 + i) } 
     49 
     50  println("DROP 10min") 
     51  val t4 = new Timecode(0,9,59,27, "ntsc") 
     52  (1 to 5).foreach {i => println(t4 + i) } 
     53 
     54  println("NONDROP 10min") 
     55  val t3 = new Timecode(0,9,59,27, 30) 
     56  (1 to 5).foreach {i => println(t3 + i) } 
    4657}