Search Results

Search found 972 results on 39 pages for 'scala'.

Page 11/39 | < Previous Page | 7 8 9 10 11 12 13 14 15 16 17 18  | Next Page >

  • Scala match question

    - by javier
    Hello to everyone. I came across with an error on my Scala code that I cannot solve by myself (I am new at Scala). I have the following code: def myFunction(list: List[Any]): String = { var strItems : String = ""; list.foreach(item => { strItems += item match { case x:JsonSerializable => x.toJson() case y:String => ("\"" + y + "\"") case _ => item.toString } if(item != list.last) strItems += ","; }) strItems; } The error I am getting is: error: pattern type is incompatible with expected type; found : String required: Unit case y:String = ("\"" + y + "\"") Any idea why? PS: is there a more performant way to code myFunction

    Read the article

  • What's the (hidden) cost of lazy val? (Scala)

    - by Jesper
    One handy feature of Scala is lazy val, where the evaluation of a val is delayed until it's necessary (at first access). Ofcourse a lazy val must have some overhead - somewhere Scala must keep track of whether the value has already been evaluated and the evaluation must be synchronized, because multiple threads might try to access the value for the first time at the same time. What exactly is the cost of a lazy val - is there a hidden boolean flag associated with a lazy val to keep track if it has been evaluated or not, what exactly is synchronized and are there any more costs? And a follow-up question: Suppose I do this: class Something { lazy val (x, y) = { ... } } Is this the same as having two separate lazy vals x and y or do I get the overhead only once, for the pair (x, y)?

    Read the article

  • Scala and the Java Memory Model

    - by Ben Lings
    The Java Memory Model (since 1.5) treats final fields differently to non-final fields. In particular, provided the this reference doesn't escape during construction, writes to final fields in the constructor are guaranteed to be visible on other threads even if the object is made available to the other thread via a data race. (Writes to non-final fields aren't guaranteed to be visible, so if you improperly publish them, another thread could see them in a partially constructed state.) Is there any documentation on how/if the Scala compiler creates final (rather than non-final) backing fields for classes? I've looked through the language specification and searched the web but can't find any definitive answers. (In comparison the @scala.volatile annotation is documented to mark a field as volatile)

    Read the article

  • Stub web calls in Scala

    - by Dennis Laumen
    I'm currently writing a wrapper of the Spotify Metadata API to learn Scala. Everything's fine and dandy but I'd like to unit test the code. To properly do this I'll need to stub the Spotify API and get consistent return values (stuff like popularity of tracks changes very frequently). Does anybody know how to stub web calls in Scala, the JVM in general or by using some external tool I could hook up into my Maven setup? PS I'm basically looking for something like Ruby's FakeWeb... Thanks in advance!

    Read the article

  • logging in scala

    - by IttayD
    In Java, the standard idiom for logging is to create a static variable for a logger object and use that in the various methods. In Scala, it looks like the idiom is to create a Logging trait with a logger member and mixin the trait in concrete classes. This means that each time an object is created it calls the logging framework to get a logger and also the object is bigger due to the additional reference. Is there an alternative that allows the ease of use of "with Logging" while still using a per-class logger instance? EDIT: My question is not about how one can write a logging framework in Scala, but rather how to use an existing one (log4j) without incurring an overhead of performance (getting a reference for each instance) or code complexity. Also, yes, I want to use log4j, simply because I'll use 3rd party libraries written in Java that are likely to use log4j.

    Read the article

  • Automated Java to Scala source code conversion?

    - by Alex R
    (Yes I know I can call Java code from Scala; but that is pointless; I want to DELETE the Java code, not keep it around and have to look at it and maintain it forever!) Are there any utilities out there to convert Java source to Scala source? I believe theoretically it should be possible to accomplish with minimal lossage. I have found this but it seems inactive, probably buggy/incomplete... http://sourceforge.net/projects/java2scala/ Any alternatives?

    Read the article

  • Regex and Pattern Matching in Scala

    - by Bruce Ferguson
    I am not strong in regex, and pretty new to Scala. I would like to be able to find a match between the first letter of a word, and one of the letters in a group such as "ABC". In pseudocode, this might look something like: case Process(word) => word.firstLetter match { case([a-c][A-C]) => case _ => } } but I don't know how to grab the first letter in Scala instead of Java, how to express the regular expression properly, nor if it's possible to do this within a case class. Any suggestions? Thanks in advance. Bruce

    Read the article

  • Scala: Matching optional Regular Expression groups

    - by Brian Heylin
    I'm trying to match on an option group in Scala 2.8 (beta 1) with the following code: import scala.xml._ val StatementPattern = """([\w\.]+)\s*:\s*([+-])?(\d+)""".r def buildProperty(input: String): Node = input match { case StatementPattern(name, value) => <propertyWithoutSign /> case StatementPattern(name, sign, value) => <propertyWithSign /> } val withSign = "property.name: +10" val withoutSign = "property.name: 10" buildProperty(withSign) // <propertyWithSign></propertyWithSign> buildProperty(withoutSign) // <propertyWithSign></propertyWithSign> But this is not working. What is the correct way to match optional regex groups?

    Read the article

  • Why is Scala very complex?

    - by Anantha Kumaran
    I am a student. I learned java during the 2nd year. Now i am in fourth year. I got bored with java and i started to learn Scala. As i learn it, i found it being very complex (although i love it). My question may apply to all new complex language. Why scala is complex? is it because we need to create complex softwares? or i am the only one who thinks it is complex?

    Read the article

  • Scala downwards or decreasing for loop?

    - by Felix
    In scala, you often use an iterator to do a for loop in an increasing order like: for(i <- 1 to 10){ code } How would you do it so it goes from 10 to 1? I guess 10 to 1 gives an empty iterator (like usual range mathematics)? I made a scala script which solves it by calling reverse on the iterator, but it's not nice in my opinion, is this the way to go: def nBeers(n:Int) = n match { case 0 => ("No more bottles of beer on the wall, no more bottles of beer."+ "\nGo to the store and buy some more, "+ "99 bottles of beer on the wall.\n") case _ => (n+" bottles of beer on the wall, "+n +" bottles of beer.\n"+"Take one down and pass it around, "+ (if((n-1)==0) "no more" else (n-1))+ " bottles of beer on the wall.\n") } for(b <- (0 to 99).reverse)println(nBeers(b)) ?? Any comments/suggestions?

    Read the article

  • How do i change the Scala version that sbt works with?

    - by ashy_32bit
    Firing up the SBT console it reads : [info] Building project AYLIEN 1.0 against Scala 2.8.1 [info] using MyProject with sbt 0.7.4 and Scala 2.7.7 How can I make it use MyProject with sbt 0.7.4 and Scala 2.8.1 ? Please pay attenetion that I'm not asking about the Scala version that is used to build my project (it is the 2.8.1 as you can see), but I rather want to make sbt use MyProject with Scala 2.8.1. Apparently sbt uses it's own scala version to work with project definition (MyProject here) which is different than one it uses to actually build the project! or perhaps I'm missing something ... ?

    Read the article

  • Scala: "Parameter type in structural refinement may not refer to an abstract type defined outside th

    - by raichoo
    Hi, I'm having a problem with scala generics. While the first function I defined here seems to be perfectly ok, the compiler complains about the second definition with: error: Parameter type in structural refinement may not refer to an abstract type defined outside that refinement def >>[B](a: C[B])(implicit m: Monad[C]): C[B] = { ^ What am I doing wrong here? trait Lifter[C[_]] { implicit def liftToMonad[A](c: C[A]) = new { def >>=[B](f: A => C[B])(implicit m: Monad[C]): C[B] = { m >>= (c, f) } def >>[B](a: C[B])(implicit m: Monad[C]): C[B] = { m >> a } } } IMPORTANT: This is NOT a question about Monads, it's a question about scala polymorphism in general. Regards, raichoo

    Read the article

  • Remove redundant entries, scala way

    - by andersbohn
    Edit: Added the fact, that the list is sorted, and realizing 'duplicate' is misleading, replaced that with 'redundant' in the title. I have a sorted list of entries stating a production value in a given interval. Entries stating the exact same value at a later time adds no information and can safely be left out. case class Entry(minute:Int, production:Double) val entries = List(Entry(0, 100.0), Entry(5, 100.0), Entry(10, 100.0), Entry(20, 120.0), Entry(30, 100.0), Entry(180, 0.0)) Experimenting with the scala 2.8 collection functions, so far I have this working implementation: entries.foldRight(List[Entry]()) { (entry, list) => list match { case head :: tail if (entry.production == head.production) => entry :: tail case head :: tail => entry :: list case List() => entry :: List() } } res0: List[Entry] = List(Entry(0,100.0), Entry(20,120.0), Entry(30,100.0), Entry(180,0.0)) Any comments? Am I missing out on some scala magic?

    Read the article

  • What are the exact versions of stuff you have to install in order to be able to step-debug a Scala p

    - by Alex R
    How do YOU debug a Scala program? I mean YOU as in the person posting the Answer :) Please answer only from personal experience, not from stuff you've heard or read on the Internet. You should not believe everything you read on the Internet, especially tales of complex open-source software configurations that actually work :-) The are many Java tools which claim to support Scala in some way or another, but I have so far struck out in trying to get any one of them to actually let me set a breakpoint in Scala code and step through it. These are big, major open-source IDEs I'm talking about here. The main problem in getting a debugger to work seems to be the "version hell" with fast-changing IDEs, Plug-Ins, JDKs, and the Scala language itself. Hence, the more detailed re-statement of the question is appropriate: What is the exact version number of the IDE, Plug-In, JDK, Scala, and even Operating System, that you are successfully using? My question is related to this one, but wider in scope: http://stackoverflow.com/questions/2272705/how-to-debug-scala-code-when-outside-of-an-ide Thanks

    Read the article

  • make a lazy var in scala

    - by ayvango
    Scala does not permit to create laze vars, only lazy vals. It make sense. But I've bumped on use case, where I'd like to have similar capability. I need a lazy variable holder. It may be assigned a value that should be calculated by time-consuming algorithm. But it may be later reassigned to another value and I'd like not to call first value calculation at all. Example assuming there is some magic var definition lazy var value : Int = _ val calc1 : () => Int = ... // some calculation val calc2 : () => Int = ... // other calculation value = calc1 value = calc2 val result : Int = value + 1 This piece of code should only call calc2(), not calc1 I have an idea how I can write this container with implicit conversions and and special container class. I'm curios if is there any embedded scala feature that doesn't require me write unnecessary code

    Read the article

  • scala xml rewrite rule (or, simple pattern help)

    - by williamstw
    I'm missing some fairly simple syntax I gather. I'm trying to rewrite an element label to something else and keep everything else intact. object htmlRule extends RewriteRule { override def transform(n: Node): Seq[Node] = n match { case Elem(prefix, "document", attribs, scope, child@_*) => Elem(prefix, "html", attribs, scope, child) case other => other } } Now, I ask for an explanation of two things: 1) What exactly does "child@_*" mean in plain English? 2) How can I capture the value of "child@_*" and just let it pass right through to the new element? Currently, I get the following error, which makes sense. [error] found : Seq[scala.xml.Node] [error] required: scala.xml.Node [error] Elem(prefix, "html", attribs, scope, child) I'm not wedded to this either, so if there's a better way to simply change the element name of a specific node, let's here it... Thanks, --tim

    Read the article

  • what good orm api will work well with scala or erlang

    - by Emotu Balogun
    I'm considering taking up scala programming but i'm really concerned about what will become of my ORM based applications. I currently use hibernate as my ORM and i find it a really reliable tool. I'd like to know if there's any ORM tool as efficient but written in scala, or will hibernate work seamlessly with it. i don't want to have to start writing endless sql queries again (like the days of JDBC). I also have the same thought about erlang. is there a good orm out there for erlang?? and can i use erlang with other DBMS like oracle and mysql with ORM

    Read the article

  • Remove duplicates in entries, scala way

    - by andersbohn
    I have a list of entries stating a production value in a given interval. Entries stating the exact same value at a later time adds no information and can safely be left out. case class Entry(minute:Int, production:Double) val entries = List(Entry(0, 100.0), Entry(5, 100.0), Entry(10, 100.0), Entry(20, 120.0), Entry(30, 100.0), Entry(180, 0.0)) Experimenting with the scala 2.8 collection functions, so far I have this working implementation: entries.foldRight(List[Entry]()) { (entry, list) => list match { case head :: tail if (entry.production == head.production) => entry :: tail case head :: tail => entry :: list case List() => entry :: List() } } res0: List[Entry] = List(Entry(0,100.0), Entry(20,120.0), Entry(30,100.0), Entry(180,0.0)) Any comments? Am I missing out on some scala magic?

    Read the article

  • Generic object load function for scala

    - by Isaac Oates
    I'm starting on a Scala application which uses Hibernate (JPA) on the back end. In order to load an object, I use this line of code: val addr = s.load(classOf[Address], addr_id).asInstanceOf[Address]; Needless to say, that's a little painful. I wrote a helper class which looks like this: import org.hibernate.Session class DataLoader(s: Session) { def loadAddress(id: Long): Address = { return s.load(classOf[Address], id).asInstanceOf[Address]; } ... } So, now I can do this: val dl = new DataLoader(s) val addr = dl loadAddress(addr_id) Here's the question: How do I write a generic parametrized method which can load any object using this same pattern? i.e val addr = dl load[Address](addr_id) (or something along those lines.) I'm new to Scala so please forgive anything here that's especially hideous.

    Read the article

  • What are the exact versions of stuff you had to install in order to be able to step-debug a Scala pr

    - by Alex R
    How do YOU debug a Scala program? I mean YOU as in the person posting the Answer :) Please answer only from personal experience, not from stuff you've heard or read on the Internet. You should not believe everything you read on the Internet, especially tales of complex open-source software configurations that actually work :-) The are many Java tools which claim to support Scala in some way or another, but I have so far struck out in trying to get any one of them to actually let me set a breakpoint in Scala code and step through it. These are big, major open-source IDEs I'm talking about here. The main problem in getting a debugger to work seems to be the "version hell" with fast-changing IDEs, Plug-Ins, JDKs, and the Scala language itself. Hence the second part of my question, which is really the most important part: What is the exact version number of the IDE, Plug-In, JDK, Scala, and even Operating System, that you are successfully using? My question is related to this one, but wider in scope: http://stackoverflow.com/questions/2272705/how-to-debug-scala-code-when-outside-of-an-ide Thanks

    Read the article

  • Can I reproduce Scala's behavior for == ?

    - by JPP
    In Programming in Scala, I can read that the == operator behaves as if it was defined like this: final def == (that: Any): Boolean = if (null eq this) {null eq that} else {this equals that} But there must actually be compiler magic to avoid null pointer exceptions, right? Is there any way for me to replicate this behavior with pure Scala; i.e., have an operator/method return one thing if the receiver is null and another one if it isn't? What I mean is an actual implementation of null eq this. I suppose I can write a "pimp" and then define the method on the wrapper class, but is there a more direct way to do this?

    Read the article

< Previous Page | 7 8 9 10 11 12 13 14 15 16 17 18  | Next Page >