I want to create lazy sequence to read values from input stream in Scala.
import java.util._
object Main {
val input = new Scanner(System.in)
def main(args: Array[String]) {
val it = new MyIt().take(5)
}
class MyIt extends Iterator[Int] {
def hasNext = true
// def next = 29
def next = input.nextInt
}
}
When I changed next = 29 to next = input.nextInt, it would not compile any more complaining that MyIt has no member take. It looks I am completely misunderstanding something. Could you please give me hint (or perhaps link to good article on lazy sequences - there are a lot of results of google, but too much trash, seems - so I am getting lost)
import java.util._ overrides scala.collection.Iterator (which available by default via type alias in scala package object) with java.util.Iterator. Just change your import to import java.util.Scanner
Related
I have a situation where I'm trying to use implicit resolution on a singleton type. This works perfectly fine if I know that singleton type at compile time:
object Main {
type SS = String with Singleton
trait Entry[S <: SS] {
type out
val value: out
}
implicit val e1 = new Entry["S"] {
type out = Int
val value = 3
}
implicit val e2 = new Entry["T"] {
type out = String
val value = "ABC"
}
def resolve[X <: SS](s: X)(implicit x: Entry[X]): x.value.type = {
x.value
}
def main(args: Array[String]): Unit = {
resolve("S") //implicit found! No problem
}
}
However, if I don't know this type at compile time, then I run into issues.
def main(args: Array[String]): Unit = {
val string = StdIn.readLine()
resolve(string) //Can't find implicit because it doesn't know the singleton type at runtime.
}
Is there anyway I can get around this? Maybe some method that takes a String and returns the singleton type of that string?
def getSingletonType[T <: SS](string: String): T = ???
Then maybe I could do
def main(args: Array[String]): Unit = {
val string = StdIn.readLine()
resolve(getSingletonType(string))
}
Or is this just not possible? Maybe you can only do this sort of thing if you know all of the information at compile-time?
If you knew about all possible implementations of Entry in compile time - which would be possible only if it was sealed - then you could use a macro to create a map/partial function String -> Entry[_].
Since this is open to extending, I'm afraid at best some runtime reflection would have to scan the whole classpath to find all possible implementations.
But even then you would have to embed this String literal somehow into each implementations because JVM bytecode knows nothing about mappings between singleton types and implementations - only Scala compiler does. And then use that to find if among all implementations there is one (and exactly one) that matches your value - in case of implicits if there are two of them at once in the same scope compilation would fail, but you can have more than one implementation as long as the don't appear together in the same scope. Runtime reflection would be global so it wouldn't be able to avoid conflicts.
So no, no good solution for making this compile-time dispatch dynamic. You could create such dispatch yourself by e.g. writing a Map[String, Entry[_]] yourself and using get function to handle missing pices.
Normally implicits are resolved at compile time. But val string = StdIn.readLine() becomes known at runtime only. Principally, you can postpone implicit resolution till runtime but you'll be able to apply the results of such resolution at runtime only, not at compile time (static types etc.)
object Entry {
implicit val e1 = ...
implicit val e2 = ...
}
import scala.reflect.runtime.universe._
import scala.reflect.runtime
import scala.tools.reflect.ToolBox
val toolbox = ToolBox(runtime.currentMirror).mkToolBox()
def resolve(s: String): Any = {
val typ = appliedType(
typeOf[Entry[_]].typeConstructor,
internal.constantType(Constant(s))
)
val instanceTree = toolbox.inferImplicitValue(typ, silent = false)
val instance = toolbox.eval(toolbox.untypecheck(instanceTree)).asInstanceOf[Entry[_]]
instance.value
}
resolve("S") // 3
val string = StdIn.readLine()
resolve(string)
// 3 if you enter S
// ABC if you enter T
// "scala.tools.reflect.ToolBoxError: implicit search has failed" otherwise
Please notice that I put implicits into the companion object of type class in order to make them available in the implicit scope and therefore in the toolbox scope. Otherwise the code should be modified slightly:
object EntryImplicits {
implicit val e1 = ...
implicit val e2 = ...
}
// val instanceTree = toolbox.inferImplicitValue(typ, silent = false)
// should be replaced with
val instanceTree =
q"""
import path.to.EntryImplicits._
implicitly[$typ]
"""
In your code import path.to.EntryImplicits._ is import Main._.
Load Dataset from Dynamically generated Case Class
I use double to encode boolean value in a configuration file. PureConfig does not find a way to cast it while reading the configuration.
Initial question (see below for edit).
Here is some code to reproduce the behavior.
import com.typesafe.config.ConfigFactory
import pureconfig.ConfigReader
import pureconfig.generic.auto._
object Main {
def main(args: Array[String]): Unit = {
println(pureconfig.loadConfig[BooleanTest](ConfigFactory.parseString("a = 1")))
}
}
case class BooleanTest(a: Boolean)
object ConfigImplicits {
implicit val myBooleanReader: ConfigReader[Boolean] = ConfigReader[Double].map { n => n > 0}
}
Here, I expect my code to print an instance of BooleanTest.
Instead, I got a ConvertFailure:
Left(ConfigReaderFailures(ConvertFailure(WrongType(NUMBER,Set(BOOLEAN)),None,a),List()))
One way to fix this, is to add import ConfigImplicits._ just before calling the loadConfig function.
However, as you can suppose, my code is actually part of a bigger project, and adding the import in the real project does not fix my error.
Do you have any hint on what can be wrong?
Kind,
Alexis.
Edit:
After comments from Thilo it appears logic to add the import statement.
Below is an updated version of the code which include the import statement but still produce the same error...
Change the main function to:
def main(args: Array[String]): Unit = {
println(ConfigUtils.loadConfig[BooleanTest]("a = 1"))
}
And declare a ConfigUtils object as follow:
object ConfigUtils {
def loadConfig[A : ConfigReader](str: String) : ConfigReader.Result[A] = {
import ConfigImplicits._
val config = ConfigFactory.parseString(str)
pureconfig.loadConfig[A](config)
}
}
Run the code and you get the same error as previously:
ConvertFailure(WrongType(NUMBER,Set(BOOLEAN))
Why does pureconfig not use my implicit myBooleanReader to parse this configuration?
Kind, Alexis.
I have the following code
sealed trait DomainValidation {
def errorMessage: String
}
type ValidationResult[A] = ValidatedNel[DomainValidation, A]
val ai:ValidationResult[String] = "big".validNel
val bi:ValidationResult[String] = "leboski".validNel
val l = List(ai,bi)
I want to convert l to ValidationResult[List[String]]. I came across sequence functionality but I am unable to use cats sequence as some implicit has to be there which knows how to handle ValidationResult[A]. But I am unable figure out what exactly is needed. I wrote the following
object helper {
implicit class hello[A](l: List[ValidationResult[A]]) {
def mysequence: ValidationResult[List[A]] = {
val m = l.collect { case Invalid(a) => Invalid(a) }
if (m.isEmpty) l.map { case Valid(a) => a }.validNel
else /* merge the NonEmpty Lists */
}
}
}
I am able to do l.mysequence. But how do I use cats sequence.
PS: I am a scala beginner. Having a hard time learning :). Forgive for any incorrect mentions.
The following should work as expected on Scala 2.12:
import cats.data.ValidatedNel, cats.syntax.validated._
// Your code:
sealed trait DomainValidation {
def errorMessage: String
}
type ValidationResult[A] = ValidatedNel[DomainValidation, A]
val ai:ValidationResult[String] = "big".validNel
val bi:ValidationResult[String] = "leboski".validNel
val l = List(ai,bi)
And then:
scala> import cats.instances.list._, cats.syntax.traverse._
import cats.instances.list._
import cats.syntax.traverse._
scala> l.sequence
res0: ValidationResult[List[String]] = Valid(List(big, leboski))
You don't show your code or explain what's not working, so it's hard to diagnose your issue, but it's likely to be one of the following problems:
You're on Scala 2.11, where .sequence requires you to enable -Ypartial-unification in your compiler options. If you're using sbt, you can do this by adding scalacOptions += "-Ypartial-unification" to your build.sbt (assuming you're on 2.11.9+).
You've omitted one of the necessary imports. You need at least the Traverse instance for List and the syntax for Traverse. The example code above includes the two imports you need, or you can just import cats.implicits._ and make your life a little easier.
If it's not one of these two things, you'll probably need to include more detail in your question for us to be able to help.
I would like to save a Type or TypeTag in a val for later use. At this time, I am having to specify a type in several locations in a block of code. I do not need to parameterize the code because only one type will be used. This is more of a curiosity than a necessity.
I tried using typeOf, classOf, getClass, and several other forms of accessing the class and type. The solution is likely simple but my knowledge of Scala typing or type references is missing this concept.
object Example extends App {
import scala.reflect.runtime.universe._
object TestClass { val str = "..." }
case class TestClass() { val word = ",,," }
def printType[A: TypeTag](): Unit = println(typeOf[A])
printType[List[Int]]() //prints 'List[Int]'
printType[TestClass]() //prints 'Example.TestClass'
val typeOfCompanion: ??? = ??? //TODO what goes here?
val typeOfCaseClass: ??? = ??? //TODO what goes here?
printType[typeOfCompanion]() //TODO should print something like 'Example.TestClass'
printType[typeOfCaseClass]() //TODO should print something like 'Example.TestClass'
}
The solution should be able to save a Type or TypeTag or what the solution is. Then, pass typeOfCompanion or typeOfCaseClass like printTypetypeOfCompanion for printing. Changing the printing portion of the code may be required; I am not certain.
You have to be more explicit here
import scala.reflect.runtime.universe._
def printType(a: TypeTag[_]): Unit = println(a)
val typeOfCompanion = typeTag[List[Int]]
printType(typeOfCompanion)
def printType[A: TypeTag](): Unit = println(typeOf[A])
is exactly the same as
def printType[A]()(implicit a: TypeTag[A]): Unit = println(typeOf[A])
(except for the parameter name). So it can be called as
val listTypeTag /* : TypeTag[List[Int]] */ = typeTag[List[Int]]
printType()(listTypeTag)
(you can remove the empty parameter list from printType if you want).
For the companion, you need to use a singleton type:
val companionTag = typeTag[TestClass.type]
val caseClassTag = typeTag[TestClass]
Given an scala XML object, can I perform a xpath string query like "//entries[#title='scala']" ?
Ideally, it would be like:
<a><b name='n1'></b></a>.xpath("//b[#name='n1']")
I can't manually convert all the xpath queries to scala's internal xpath-ish method calls as my program will is dynamically accepting xpath queries.
Additionally, the built-in java xml library is very verbose so I would like to avoid it.
Your best bet is (and always was, even in Java) to use JDOM. I've pimped JDom with the following library to be a bit more scala friendly:
import org.jdom._
import org.jdom.xpath._
import scala.collection.JavaConversions
import java.util._
import scala.collection.Traversable
package pimp.org.jdom{
object XMLNamespace{
def apply(prefix:String,uri:String) = Namespace.getNamespace(prefix,uri)
def unapply(x:Namespace) = Some( (x.getPrefix, x.getURI) )
}
object XMLElement{
implicit def wrap(e:Element) = new XMLElement(e)
def unapply(x:Element) = Some( (x.getName, x.getNamespace) )
}
class XMLElement(underlying:Element){
def attributes:java.util.List[Attribute] =
underlying.getAttributes.asInstanceOf[java.util.List[Attribute]]
def children:java.util.List[Element] =
underlying.getChildren.asInstanceOf[java.util.List[Element]]
def children(name: String): java.util.List[Element] =
underlying.getChildren(name).asInstanceOf[java.util.List[Element]]
def children(name: String, ns: Namespace): java.util.List[Element] =
underlying.getChildren(name, ns).asInstanceOf[java.util.List[Element]]
}
}
package pimp.org.jdom.xpath{
import pimp.org.jdom._
//instances of these classes are not thread safe when xpath variables are used
class SingleNodeQuery[NType](val expression:String)(implicit namespaces:Traversable[Namespace]=null){
private val compiled=XPath.newInstance(expression)
if (namespaces!=null){
for ( ns <- namespaces ) compiled.addNamespace(ns.getPrefix,ns.getURI)
}
def apply(startFrom:Any,variables:(String,String)*)={
variables.foreach{ x=> compiled.setVariable(x._1,x._2)}
compiled.selectSingleNode(startFrom).asInstanceOf[NType]
}
}
class NodesQuery[NType](val expression:String)(implicit namespaces:Traversable[Namespace]=null){
private val compiled=XPath.newInstance(expression)
if (namespaces!=null){
for ( ns <- namespaces ) compiled.addNamespace(ns.getPrefix,ns.getURI)
}
def apply(startFrom:Any,variables:(String,String)*)={
variables.foreach{ x=> compiled.setVariable(x._1,x._2)}
compiled.selectNodes(startFrom).asInstanceOf[java.util.List[NType]]
}
}
class NumberValueQuery(val expression:String)(implicit namespaces:Traversable[Namespace]=null){
private val compiled=XPath.newInstance(expression)
if (namespaces!=null){
for ( ns <- namespaces ) compiled.addNamespace(ns.getPrefix,ns.getURI)
}
def apply(startFrom:Any,variables:(String,String)*)={
variables.foreach{ x=> compiled.setVariable(x._1,x._2)}
compiled.numberValueOf(startFrom).intValue
}
}
class ValueQuery(val expression:String)(implicit namespaces:Traversable[Namespace]=null){
private val compiled=XPath.newInstance(expression)
if (namespaces!=null){
for ( ns <- namespaces ) compiled.addNamespace(ns.getPrefix,ns.getURI)
}
def apply(startFrom:Any,variables:(String,String)*)={
variables.foreach{ x=> compiled.setVariable(x._1,x._2)}
compiled.valueOf(startFrom)
}
}
}
My idea when I wrote this was that in general, you want to compile each XPath query in advance (so that it can be reused more than once), and that you want to specify the type returned by the query at the point where you specify the text of the query (not like JDOM's XPath class does which is to pick one of four methods to call at execution time).
Namespaces should be passed around implicitly (so you can specify them once and then forget about them), and XPath variable binding should be available at query time.
You'd use the library like this: (Explicit type annotations can be inferred -- I've included them for illustration only.)
val S = XMLNamespace("s","http://www.nist.gov/speech/atlas")
val XLink = XMLNamespace("xlink", "http://www.w3.org/1999/xlink")
implicit val xmlns= List(S, XLink)
private val anchorQuery=new ValueQuery("s:AnchorRef[#role=$role]/#xlink:href")
val start:String=anchorQuery(region,"role"->"start")
val end:String=anchorQuery(region,"role"->"end")
//or
private val annotationQuery=new NodesQuery[Element]("/s:Corpus/s:Analysis/s:AnnotationSet/s:Annotation")
for(annotation:Element <- annotationQuery(doc)) {
//do something with it
}
I guess I should come up with some way of releasing this to the public.
kantan.xpath does just that. Here's something I just typed in the REPL:
import kantan.xpath._
import kantan.xpath.ops._
"<a><b name='n1'></b></a>".evalXPath[Node]("//b[#name='n1']")
, where the Node type parameter describes the type one expects to extract from the XML document. A perhaps more explicit example would be:
new URI("http://stackoverflow.com").evalXPath[List[URI]]("//a/#href")
This would download the stackoverflow homepage, evaluate it as an XML Document (there's a NekoHTML module for HTML sanitisation) and extract the target of all links.