当前位置: 首页>>代码示例>>Scala>>正文


Scala Source类代码示例

本文整理汇总了Scala中scala.io.Source的典型用法代码示例。如果您正苦于以下问题:Scala Source类的具体用法?Scala Source怎么用?Scala Source使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Source类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: ProcessorUtil

//设置package包名称以及导入依赖的类
package processor

import data.Line

import scala.io.Source


object ProcessorUtil {

  def readFromLines(input: Source, skipHeader: Boolean = true): Iterator[Option[Line]] = {
    val lines = if (skipHeader) input.getLines().drop(1) else input.getLines()
    lines map (ln => {
      if (ln == ",,,,,") {
        // Ignore
        None
      } else if (ln == "Id,name,time_of_start,Obs.,,") {
        // Ignore
        None
      } else {
        val Array(id, name, time, _, _, _) = ln.split(",", -1)
        if (id == null || id.isEmpty) {
          // Ignore
          None
        } else {
          Option(Line(id, name, time))
        }

      }
    })
  }

} 
开发者ID:JohnnyCosta,项目名称:dataconsumer,代码行数:33,代码来源:ProcessorUtil.scala

示例2: SubstringSearch

//设置package包名称以及导入依赖的类
import scala.io.Source

object SubstringSearch{

    def hasSubstring(text: Array[Char], pattern: Array[Char]) = {
        val txtLen = text.length
        val patternLen = pattern.length
        val table = Array.ofDim[Int](patternLen)

        def populateTable() = {
            table(0) = -1
            def populateTableInternal(pos: Int, cnd: Int):Unit = pos match {
                case `patternLen` => ()
                case _ =>  if (pattern(pos-1) == pattern(cnd)) {
                                table(pos) = cnd + 1
                                populateTableInternal(pos + 1, cnd + 1)}
                           else if (cnd > 0){
                               table(pos) = 0
                               populateTableInternal(pos, table(cnd))}
                           else {
                               table(pos) = 0
                               populateTableInternal(pos + 1, cnd)}

            }
            if (patternLen > 1) {table(1) = 0; populateTableInternal(2, 0)}
        }

        def hasSubstringInternal(m:Int, i:Int):String = (m, i) match {
            case (_, `patternLen`) => "YES"
            case (`txtLen`, _) => "NO"
            case (_, _) if m + i >= txtLen => "NO"
            case (_, _) => if (text(m + i) == pattern(i)) hasSubstringInternal(m, i+1)
                           else if (table(i) > -1) hasSubstringInternal(m+i- table(i), table(i))
                           else hasSubstringInternal(m + 1, 0)
        }
        populateTable()
        println("done!")
        hasSubstringInternal(0, 0)
      }

    def main(args: Array[String]) {
        val currentDirectory = new java.io.File(".").getCanonicalPath
        val fileName = "test_SubstringSearch.txt"
        val filePath = s"$currentDirectory/src/main/resources/$fileName"

        val lines = Source.fromFile(filePath).getLines()
        val inputArray = for(i <- 1 to lines.next.toInt) yield (lines.next, lines.next)
        inputArray.foreach(x=> println(hasSubstring(x._1.toArray, x._2.toArray)))
    }

} 
开发者ID:siddhartha-chandra,项目名称:Solved-in-Scala,代码行数:52,代码来源:SubstringSearch.scala

示例3: getWeatherData

//设置package包名称以及导入依赖的类
import scala.io.Source
import scala.xml._

def getWeatherData(city: String) = {
  val url = "http://api.openweathermap.org/data/2.5/weather"

  val response = Source.fromURL(s"$url?q=$city&units=imperial&mode=xml")
  val xmlResponse = XML.loadString(response.mkString)
  val cityName = (xmlResponse \\ "city" \ "@name").text
  val temperature = (xmlResponse \\ "temperature" \ "@value").text
  val condition = (xmlResponse \\ "weather" \ "@value").text
  (cityName, temperature, condition)
}  

def printWeatherData(weatherData: (String, String, String)) = {
  val (cityName, temperature, condition) = weatherData

  println(f"$cityName%-15s $temperature%-6s $condition")
}

def timeSample(getData: List[String] => List[(String, String, String)]) = {
  val cities = List("Houston,us", "Chicago,us", "Boston,us", "Minneapolis,us", 
    "Oslo,norway", "Tromso,norway", "Sydney,australia", "Berlin,germany", 
    "London,uk", "Krakow,poland", "Rome,italy", "Stockholm,sweden", 
    "Bangalore,india", "Brussels,belgium", "Reykjavik,iceland")

  val start = System.nanoTime
  getData(cities) sortBy { _._1 } foreach printWeatherData
  val end = System.nanoTime
  println(s"Time taken: ${(end - start)/1.0e9} sec")
}

println("//" + "START:SEQUENTIAL_OUTPUT")
timeSample { cities => cities map getWeatherData }
println("//" + "END:SEQUENTIAL_OUTPUT")

println("//" + "START:PARALLEL_OUTPUT")
timeSample { cities => (cities.par map getWeatherData).toList }
println("//" + "END:PARALLEL_OUTPUT") 
开发者ID:ReactivePlatform,项目名称:Pragmatic-Scala,代码行数:40,代码来源:weather.scala

示例4: fileAndLine

//设置package包名称以及导入依赖的类
import java.io.File
import scala.io.Source

val dir = args(0)

def fileAndLine = for (
  file <- new File(dir).listFiles if file.isFile && file.canRead;
  line <- Source.fromFile(file).getLines.toList
) yield (file, line)

val longestLine = fileAndLine
  .reduceLeft(
    (l, r) =>
      if (getPrefix(l._1, l._2).length > getPrefix(r._1, r._2).length) l else r
  )

val longestPrefix = getPrefix(longestLine._1, longestLine._2).length

fileAndLine.foreach(f => println(padString(getPrefix(f._1, f._2), longestPrefix) + " " + f._2))

def padString(string: String, max: Int, char: Char = ' ') = char.toString * (max - string.length) + string
def getPrefix(file: File, line: String) = file.getName + ":" + line.length 
开发者ID:mhotchen,项目名称:programming-in-scala,代码行数:23,代码来源:length-of-lines-in-dir.scala

示例5: calcularAnchoTamLinea

//设置package包名称以及导入依赖的类
import scala.io.Source

def calcularAnchoTamLinea(s: String) = s.length.toString.length

if (args.length > 0){
	for(linea <- Source.fromFile(args(0)).getLines())
		println(linea.length+" "+linea)

	val lineas = Source.fromFile(args(0)).getLines().toList
	var maximoAnchoTam = 0
	println("-----Forma2-----")
	println("Calcular lo que le falta de sangrado a cada linea")
	for(linea <- lineas){
		maximoAnchoTam = maximoAnchoTam.max(calcularAnchoTamLinea(linea))
		println(maximoAnchoTam+" "+linea)
	}

	println("-----Forma3-----")
	println("Lo mismo pero mas elegante")
	val lineaMasLarga = lineas.reduceLeft((a,b) => if(a.length > b.length) a else b)
	maximoAnchoTam = calcularAnchoTamLinea(lineaMasLarga)
	println(maximoAnchoTam)
	// finalmente se muestra el contenido debidamente formateado
	for(linea <- lineas){
		// Se calcula el numero de espacios en blanco para iugalar  con el maximo tamaño ancho
		val numeroEspacios = maximoAnchoTam - calcularAnchoTamLinea(linea)

		// Se construye ña cadena de blancos de relleno
		val relleno = " "*numeroEspacios

		// Se muestra la liena
		println(relleno+linea.length+"|"+linea)
	}
}
else
	Console.err.println("Introduzca nombre de archivo") 
开发者ID:romanarranz,项目名称:NTP,代码行数:37,代码来源:leeArchivo.scala

示例6: calcularAnchoTamLinea

//设置package包名称以及导入依赖的类
import scala.io.Source

def calcularAnchoTamLinea(s : String) = s.length.toString.length

if (args.length > 0){
   val lineas=Source.fromFile(args(0)).getLines().toList
   val lineaMasLarga = lineas.reduceLeft(
       (a,b) => if(a.length > b.length) a else b)

   val maximoAnchoTam = calcularAnchoTamLinea(lineaMasLarga)

   for(linea <- lineas){
      // Se calcula el numero de espacios en blanco para igualar
      // con el maximo tamaño de ancho
      val numeroEspacios=maximoAnchoTam-calcularAnchoTamLinea(linea)
     
      // Se construye la cadena de blancos de relleno
      val relleno=" "*numeroEspacios

      // Se muestra la linea
      println(relleno + linea.length + "|" + linea)
   }
}
else
   Console.err.println("Introduzca nombre de archivo") 
开发者ID:romanarranz,项目名称:NTP,代码行数:26,代码来源:archivo3.scala

示例7: Main

//设置package包名称以及导入依赖的类
package eu.tznvy.jancy.modulesgen

import eu.tznvy.jancy.modulesgen.helpers.Filesystem
import eu.tznvy.jancy.modulesgen.discovery.{MetadataFilesDiscoverer, MetadataReader}
import eu.tznvy.jancy.modulesgen.codegeneration.{FilesLayout, HandlebarsRenderer, ModuleClassFactory}
import resource._
import java.nio.file.Paths

import scala.io.Source

object Main {

  def main(args: Array[String]): Unit = {

    val ansibleModulesPaths = List(
        "submodules/ansible/lib/ansible/modules")

    val filesLayout = new FilesLayout(
      Paths.get("jancy-modules/src/main/java/"))

    ansibleModulesPaths
      .flatMap(Filesystem.getFilesRecursively)
      .filter({ f =>
        managed(Source.fromFile(f))
          .map(MetadataFilesDiscoverer.isAnsibleModuleFile(f.getName, _))
          .opt
          .getOrElse(false)
       })
      .map(MetadataReader.readModuleMetadata)
      .map({ m => (m, HandlebarsRenderer.render(ModuleClassFactory.build(m))) })
      .foreach((filesLayout.saveModuleSource _).tupled)
  }
} 
开发者ID:brthanmathwoag,项目名称:jancy,代码行数:34,代码来源:Main.scala

示例8: userPreferencesFileContents

//设置package包名称以及导入依赖的类
package com.github.madoc.create_sbt_project.io

import java.nio.charset.Charset
import java.nio.file.{FileSystem, FileSystems, Files}
import java.util.concurrent.atomic.AtomicReference

import com.github.madoc.create_sbt_project.action.framework.ActionEnvironment
import com.github.madoc.create_sbt_project.io.Write.WriteToAppendable

import scala.io.Source

trait FileSystemSupport extends ActionEnvironment {
  def userPreferencesFileContents:Option[String]
  def writeToStandardOutput:Write
  def writeToErrorOutput:Write
}
object FileSystemSupport {
  val default:FileSystemSupport = new Default(FileSystems getDefault)
  val main:AtomicReference[FileSystemSupport] = new AtomicReference[FileSystemSupport](default)

  class Default(val fs:FileSystem) extends FileSystemSupport {
    def userPreferencesFileContents = {
      val prefsPath = fs getPath userPreferencesFilePath
      if(Files.exists(prefsPath) && Files.isRegularFile(prefsPath) && Files.isReadable(prefsPath)) {
        val source = Source.fromInputStream(Files newInputStream prefsPath, "utf-8")
        try Some(source mkString) finally source.close()
      }
      else None
    }

    val writeToErrorOutput = new WriteToAppendable(systemErr)
    val writeToStandardOutput = new WriteToAppendable(systemOut)
    def fileExists(path:String) = Files exists (fs getPath path)
    def isFileWritable(path:String) = Files isWritable (fs getPath path)
    def isDirectory(path:String) = Files isDirectory (fs getPath path)
    def mkdirs(path:String) = Files createDirectories (fs getPath path)
    def outputToFile[A](contents:A, path:String, charset:Charset)(implicit output:Output[A]) = {
      val writer = Files.newBufferedWriter(fs getPath path, charset)
      try output(contents)(new WriteToAppendable(writer)) finally writer close
    }

    protected def systemErr:Appendable = System err
    protected def systemOut:Appendable = System out
    protected def userHomeDirectory:String = System.getProperty("user.home")
    protected def userPreferencesFilePath:String =
      if(userHomeDirectory endsWith "/") userHomeDirectory + ".create-sbt-project.json"
      else userHomeDirectory + "/.create-sbt-project.json"
  }
} 
开发者ID:Madoc,项目名称:create-sbt-project,代码行数:50,代码来源:FileSystemSupport.scala

示例9: Server

//设置package包名称以及导入依赖的类
package foobar

import foobar.page.{Contact, Index}
import org.http4s.MediaType.`text/html`
import org.http4s.dsl._
import org.http4s.headers.`Content-Type`
import org.http4s.server.ServerApp
import org.http4s.server.blaze._
import org.http4s.{HttpService, Response, StaticFile}

import scala.io.Source
import scala.util.Try
import scalatags.Text.TypedTag
import scalaz.concurrent.Task

object Server extends ServerApp {

  def page(p: TypedTag[String]): Task[Response] =
    Ok(p.render).withContentType(Some(`Content-Type`(`text/html`)))

  val service = HttpService {
    case GET -> Root              => page(Index.page)
    case GET -> Root / "contact"  => page(Contact.page)
    case req @ GET -> Root / path =>
      println("file: " + Try(Source.fromFile(path).getLines().mkString))
      StaticFile.fromResource(path.toString, Some(req)).fold(NotFound())(Task.now)
  }

  def server(args: List[String]) =
    BlazeBuilder.bindHttp(8080)
      .mountService(service, "/")
      .start
} 
开发者ID:julien-truffaut,项目名称:FooBar,代码行数:34,代码来源:Server.scala

示例10: Mailer

//设置package包名称以及导入依赖的类
package org.kirhgoff.lastobot

import java.util.Properties
import javax.mail.{Message, Session}
import javax.mail.internet.{InternetAddress, MimeMessage}

import scala.io.Source


object Mailer {
  val host = "smtp.gmail.com"
  val port = "587"

  val address = "[email protected]"
  val username = "lastobot"
  val password = Source.fromFile(System.getProperty("user.home")
    + "/.lastobot/.mail").getLines.mkString

  def sendMail(text:String, subject:String) = {
    val properties = new Properties()
    properties.put("mail.smtp.port", port)
    properties.put("mail.smtp.auth", "true")
    properties.put("mail.smtp.starttls.enable", "true")

    val session = Session.getDefaultInstance(properties, null)
    val message = new MimeMessage(session)
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(address));
    message.setSubject(subject)
    message.setContent(text, "text/html")

    val transport = session.getTransport("smtp")
    transport.connect(host, username, password)
    transport.sendMessage(message, message.getAllRecipients)
  }

  def main(args:Array[String]) = {
    sendMail("aaaa", "bbb")
  }
} 
开发者ID:kirhgoff,项目名称:lastobot,代码行数:40,代码来源:Mailer.scala

示例11: TempasWaybackSpec

//设置package包名称以及导入依赖的类
package de.l3s.archivespark.specific.warc.tempas

import java.net.URLEncoder

import de.l3s.archivespark.dataspecs.DataSpec
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClients
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD

import scala.io.Source

class TempasWaybackSpec private (query: String, from: Option[Int] = None, to: Option[Int] = None, pages: Int, resultsPerPage: Int) extends DataSpec[TempasYearResult, TempasWaybackRecord] {
  import TempasWaybackSpec._

  def searchUrl(page: Int): String = {
    val queryEncoded = URLEncoder.encode(query, "UTF-8")
    var url = TempasSearchUrl.replace("$r", resultsPerPage.toString).replace("$p", page.toString).replace("$q", queryEncoded)
    if (from.isDefined) url += "&from=" + from.get
    if (to.isDefined) url += "&to=" + to.get
    url
  }

  override def load(sc: SparkContext, minPartitions: Int): RDD[TempasYearResult] = {
    sc.parallelize(1 to pages, minPartitions).flatMap { page =>
      @transient val client = HttpClients.createDefault
      @transient val get = new HttpGet(searchUrl(page))
      get.setHeader("Accept", AcceptType)
      val in = client.execute(get).getEntity.getContent
      try {
        Source.fromInputStream(in).getLines().toList.flatMap { line =>
          TempasYearResult.resultsFromTsv(line)
        }
      } finally {
        in.close()
      }
    }.repartition(minPartitions)
  }

  override def parse(result: TempasYearResult): Option[TempasWaybackRecord] = {
    Some(new TempasWaybackRecord(result))
  }
}

object TempasWaybackSpec {
  val TempasSearchUrl = "http://tempas.l3s.de/v2/query?resultsPerPage=$r&page=$p&q=$q"
  val DefaultResultsPerPage = 100
  val DefaultPages = 100
  val AcceptType = "text/tab-separated-values"

  def apply(query: String, from: Int = -1, to: Int = -1, pages: Int = DefaultPages, resultsPerPage: Int = DefaultResultsPerPage): TempasWaybackSpec = {
    val fromOpt = if (from < 0) None else Some(from)
    val toOpt = if (to < 0) None else Some(to)
    new TempasWaybackSpec(query, fromOpt, toOpt, pages, resultsPerPage)
  }
} 
开发者ID:helgeho,项目名称:Tempas2ArchiveSpark,代码行数:57,代码来源:TempasWaybackSpec.scala

示例12: TelephoneNumbers

//设置package包名称以及导入依赖的类
package cz.letal.progfun.toying

import scala.io.Source

object TelephoneNumbers extends App {

  def loadFile(path: String): Seq[Word] =
    Source.fromFile(path)
      .getLines()
      .map(_.toLowerCase.filter(_.isLetter))
      .toSeq

  val names: Seq[String] = loadFile("/usr/share/dict/propernames") ++ loadFile("/usr/share/dict/connectives")

  val mnem: Map[Char, String] = Map(
    '2' -> "abc",
    '3' -> "def",
    '4' -> "ghi",
    '5' -> "jkl",
    '6' -> "mno",
    '7' -> "pqrs",
    '8' -> "tuv",
    '9' -> "wxyz"
  ).withDefaultValue("")

  val inverseMnem: Map[Char, Char] = mnem.flatMap {
    case (num, str) => str.map(c => (c, num))
  }

  private val namesByNumberRepr: Map[String, Seq[String]] = names
    .groupBy(_.map(inverseMnem))

  type Word = String
  type Sentence = List[Word]

  def toSentence(number: Word): List[Sentence] = {
    if (number.isEmpty)
      return List(Nil)

    for {
      len <- (1 to number.length).toList
      (head, tail) = number.splitAt(len) if namesByNumberRepr contains head
      word <- namesByNumberRepr(head)
      rest <- toSentence(tail)
    } yield word :: rest
  }

  val number = namesByNumberRepr.keysIterator.take(5).mkString
  val numberValid = number.filter(c => mnem.contains(c))

  println(number + " - " + toSentence(number).map(_.mkString(" ")).mkString("\n"))


} 
开发者ID:letalvoj,项目名称:progfun_assignments,代码行数:55,代码来源:TelephoneNumbers.scala

示例13: Modules

//设置package包名称以及导入依赖的类
package com.bob.reservefund.scala

import com.bob.reservefund.scala.Service.UserService
import com.bob.reservefund.scala.Util.EnvironmentContext
import com.google.gson.JsonParser
import com.google.inject.{Provides, Singleton}
import com.twitter.finatra.annotations.Flag
import com.twitter.inject.TwitterModule
import com.typesafe.config.ConfigFactory

import scala.collection.JavaConversions._
import scala.io.Source

object Modules extends TwitterModule {

  val config = ConfigFactory.load()
  val test = config.getString("config.testurl")
  val default = config.getString("config.defaulturl")

  val env = System.getProperty("active.environment", "default")
  val url = env match {
    case "test" => test
    case _ => default
  }

  val jsons = Source.fromURL(url).mkString
  val parser = new JsonParser()
  val jsonobj = parser.parse(jsons).getAsJsonObject.getAsJsonArray("propertySources").get(0).getAsJsonObject.getAsJsonObject("source")
  jsonobj.entrySet().foreach(x => {
    flag(x.getKey, x.getValue.getAsString, "")
    EnvironmentContext.put(x.getKey, x.getValue.getAsString)
  })

  flag("dbusername", "root", "the username of the database")
  flag("active.environment", env, "which environment now is run")

  @Singleton
  @Provides
  def providesUserService(@Flag("dbusername") dbusername: String): UserService = {
    new UserService(dbusername)
  }

} 
开发者ID:bobxwang,项目名称:ReserveFundService,代码行数:44,代码来源:Modules.scala

示例14: DB

//设置package包名称以及导入依赖的类
package storage

import akka.actor.ActorSystem
import com.google.inject.AbstractModule
import java.sql.Connection
import javax.inject.{ Inject, Singleton }
import models.user.UserService
import models.user.Roles
import org.jooq.impl.DSL
import org.jooq.{ SQLDialect, DSLContext }
import play.api.db.Database
import scala.collection.JavaConversions._
import scala.concurrent.{ ExecutionContext, Future }
import scala.io.Source

object DB {
  
  val CURRENT_SQLDIALECTT = SQLDialect.POSTGRES_9_4

}


  private def initDB(connection: Connection) = {
    
    // Splitting by ; is not 100% robust - but should be sufficient for our own schema file
    val statement = connection.createStatement

    Source.fromFile("conf/schema.sql", "UTF-8")
      .getLines().map(_.trim)
      .filter(line => !(line.startsWith("--") || line.isEmpty))
      .mkString(" ").split(";")
      .foreach(s => {
        statement.addBatch(s + ";")
      })

    statement.executeBatch()
    statement.close()    
    
    val f = for {
      _ <- userService.insertUser("recogito", "[email protected]", "recogito")
      _ <- userService.insertUserRole("recogito", Roles.Admin)
    } yield()
    
    f.recover { case t: Throwable => t.printStackTrace() }
  } 
  
} 
开发者ID:pelagios,项目名称:recogito2,代码行数:48,代码来源:DB.scala

示例15: DataReader

//设置package包名称以及导入依赖的类
package org.hpi.esb.datasender

import org.hpi.esb.commons.util.Logging

import scala.io.Source

class DataReader(val source: Source, columns: List[String], columnDelimiter: String, dataColumnStart: Int, readInRam: Boolean)
  extends Logging {

  private var dataIterator: Iterator[String] = if(readInRam) recordList.toIterator else source.getLines
  private val sendWholeLine: Boolean = columns.size == 1
  private val delimiter = Option(columnDelimiter).getOrElse("")
  private lazy val recordList = source.getLines.toList

  def hasRecords: Boolean = dataIterator.hasNext

  def readRecords: Option[List[String]] = {
    if (dataIterator.hasNext) {
      retrieveRecords()
    } else {
      resetIterator()
      retrieveRecords()
    }
  }

  def retrieveRecords(): Option[List[String]] = {
    val line = dataIterator.next()
    if (sendWholeLine) {
      Option(List(line))
    }
    else {
      val multipleRecords = split(line)
      multipleRecords.map(_.drop(dataColumnStart))
    }
  }

  def resetIterator(): Unit = {
    if(readInRam) {
      dataIterator = recordList.toIterator
    } else {
      dataIterator = source.reset().getLines()
    }
  }

  def split(line: String): Option[List[String]] = {
    val splits = line.split(delimiter).toList
    if (columns.length > splits.length) {
      logger.error(s"There are less values available (${splits.length}) than columns defined (${columns.length}) - ignoring record")
      None
    }
    else if (columns.length < splits.length) {
      logger.error(s"There are less topics defined (${columns.length}) than values available (${splits.length}) - ignoring record")
      None
    }
    else {
      Option(splits)
    }
  }

  def close(): Unit = source.close
} 
开发者ID:BenReissaus,项目名称:EnterpriseStreamingBenchmark,代码行数:62,代码来源:DataReader.scala


注:本文中的scala.io.Source类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。