Algebraic data types break encapsulation by exposing the internal representation of the type publicly. When you take a functional programming point of view with regards to your design, then mutable state is not something that is a concern normally. Therefore, exposing the internal representation is not really a big deal...
Scala maps are Invariant in the key type, thus Map[String, _] has no type relation to Map[Any, _] Map documentation trait Map[A, +B] Note that there is no variance marker on A, therefore it's invariant. You could parameterize it: abstract class MultipleOutputWriter[K, V <: OutputStream] { protected def writers: collection.mutable.Map[K,...
Simply do: array1.zip(array2).zipWithIndex.map { case ((a, b), i) => (a, b, i) } ...
I guess you are using the RegexParsers (just note that it skips white spaces by default). I'm assuming that it ends with "\n\n--open--" instead (if you can change that otherwise I'll show you how to modify the repsep parser). With this change we see that the text has the following...
yyy is not a function, it's a method. You have to either convert it to a function using η-expansion yyy _ or use a function in the first place val yyy = (c: Char) => c.toUpper // or val yyy: Char => Char = c => c.toUpper // or val...
One of them is probably a java.lang.Float. You probably would need to val aPtr: Pointer[java.lang.Float] = allocateFloats(n) If it is not obvious from the allocateFloats documentation, you can find out what type it is by doing something like allocateFloats(0).getClass.getName which will give you the full name with a [L before...
See the answer to How to set main class in build? You'll need something like: mainClass in (Compile, run) := Some("com.abhi.Main1") ...
scala> val RecipeItem = """(\S+)\s+(\S+)\s+(.*)""".r RecipeItem: scala.util.matching.Regex = (\S+)\s+(\S+)\s+(.*) scala> val RecipeItem(qty, unit, ingredient) = "1/4 cup chopped green onion" qty: String = 1/4 unit: String = cup ingredient: String = chopped green onion ...
When you use typeOf[m.type] in the foreach, you are getting the type of the instance of m, which is of type Symbol, so that isn't what you want. You can use the info method on Symbol to get the Type instance you are looking for: typeOf[BusinessFacade.type].members.filter(_.isModule).foreach { m => println(m.name...
A is covariant so a Stack[C] is a subtype of Stack[B] since C is a subtype of B. The signature of push means that if you push a supertype of A, you will receive a Stack[Supertype]. This means pushing a B onto a Stack[C] will return a Stack[B]: val s...
scala,asynchronous,akka,future
You don't want to block, by waiting on the response of the actor, so you are using Future correctly. The code in the onComplete function is executed, when your actor responds with the list. And since you don't want to block and handle it asynchronously, your last println statement is...
pure for zip lists repeats the value forever, so it's not possible to define a zippy applicative instance for Scala's List (or for anything like lists). Scalaz does provide a Zip tag for Stream and the appropriate zippy applicative instance, but as far as I know it's still pretty broken....
Following the reading of Effective Akka by Jamie Allen, I am going to attempt to apply his "Cameo" pattern suggestion. Slideshare: http://www.slideshare.net/shinolajla/effective-akka-scalaio Github: https://github.com/jamie-allen/effective_akka I think what I created will work, but doesn't sound like the best approach based on Jamie's comments in his talks. I will update / edit...
I would recommend you to use directly the UI that spark provides. It provides a lot of information and metrics regarding time, steps, network usage, etc... You can check more about it here: https://spark.apache.org/docs/latest/monitoring.html Also, in the new Spark version (1.4.0) there is a nice visualizer to understand the steps...
You can compose PartialFunctions like Receive with PartialFunction.orElse: class TransformationFrontend extends Actor { // ... def myReceive: Receive = { case job: TransformationJob => // ... // ... } def defaultRoute: Route = get { // ... } override def receive: Receive = runRoute(defaultRoute) orElse myReceive } That said, it...
The %% in the dependency automatically appends a _2.XX scala version to your artifact id. It makes scala dependencies easier to manage, but you can't use it with java dependencies like apache httpcomponents. Instead just use %: "org.apache.httpcomponents" % "httpclient" % "4.5" ...
This has nothing to do with IntelliJ, but is simply a limitation of the Scala compiler. The warning does not appear whenever a nontrivial if clause is present. You can even try this in the REPL: Option(1) match { case Some(x) => ??? } //warning Option(1) match { case Some(x)...
Fairly straightforward using a for-comprehension and some pattern matching to destructure things: val in = List((5, Map ( "ABCD" -> Map("3200" -> 3, "3350.800" -> 4, "200.300" -> 3))), (1, Map ("DEF" -> Map("1200" -> 32, "1320.800" -> 4, "2100" -> 3)))) case class Thing(a:Int, b:String, c:String, d:Int) for {...
The code you have provided does not compile. this() is not valid because there is no default constructor for the class A. this.p1 = somevalue is wrong, because there is not member p1. Correctly it might look like this: class A(p1: Type1, p2: Type2) { def this(p1: Type1) { this(p1,...
You could use the Numeric type class def round[T](input: T, scale: Int, f: BigDecimal => T)(implicit n: Numeric[T]): T = { f(BigDecimal(n.toDouble(input)).setScale(scale, RoundingMode.HALF_UP)) } Which can be used as: round(5.525, 2, _.doubleValue) res0: Double = 5.53 round(123456789L, -5, _.longValue) res1: Long = 123500000 Another way might be to create a...
Here below is my solution: def localHost()(implicit request: RequestHeader = null): Host = { def loadFromConfig = { val ssl = config.getBoolean("ssl").getOrElse(false) val host = config.getString("host").getOrElse(EndPoint.DefaultHostName) val port = Play.isTest match { case false => System.getProperty("http.port", null) match { case port if port != null => parse[Int](port).getOrElse(EndPoint.DefaultPort) case _ =>...
scala,apache-spark,spark-graphx
triangleCount counts number of triangles per vertex and returns Graph[Int,Int], so you have to extract vertices: scala> graph.triangleCount().vertices.collect() res0: Array[(org.apache.spark.graphx.VertexId, Int)] = Array((1,1), (3,1), (2,1)) ...
You can take your solution, @Samlik, and effectively zip in the currentElement variable, but then map it out when you're done with it. sequence.take(1) ++ sequence.zip(sequence.drop(1)). takeWhile({case (a, b) => a < b}).map({case (a, b) => b}) Also works with infinite sequences: val sequence = Seq(1, 2, 3).toStream ++ Stream.from(1)...
The issue is that you are missing the LocalTime => Time conversion for ResultSet -> Scala Collection conversion: MappedColumnType.base[java.sql.Time, org.joda.time.LocalTime] ( time => new LocalTime(time.getTime), localTime => new Time(localTime.toDateTimeToday().getMillis())) ...
UPDATE Answer: Start in your Project File the activator Then start the web App with run and open the browser with http://localhost:9000 This loads all the dependencies and compiles the Scala Play Application. This should correct your IDEA Ide Problems about missing Dependencies. In Scala Play 2.4 you can choose...
java,scala,ews,exchange-server-2010,ewsjavaapi
Apparently there is a difference between a meeting and an appointment. Replacing this line: new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Appointment, 0x03, MapiPropertyType.Binary), with new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Meeting, 0x03, MapiPropertyType.Binary) works....
I'm not sure if such a thing is possible directly with lists, possibly because you could create a potential infinite number of values. That's why you need to provide a len parameter. But yes, you can create a Stream that generates values followed by a takeWhile. If you know that...
json,scala,playframework,scala-macros
For doing that I should define an implicit object like this: implicit object StatusFormat extends Format[Status] { def reads(json: JsValue) = json match { case JsString("Edited") => JsSuccess(Edited) case JsString("NotEdited") => JsSuccess(NotEdited) case _ => JsError("cannot parse it") } def writes(stat: Status) = JsString(stat.toString) } ...
scala,functional-programming,pattern-matching
You can use a custom extractor to abstract the matching part away from the logic part: object Leafed { def unapply(tree: Tree) = tree match { case Node(Leaf(_, _), parent, qux) => Some((parent, qux)) case Node(parent, Leaf(_, _), qux) => Some((parent, qux)) case _ => None } } And then...
Several points: The first problem at (1) is that you don't handle the case where findLookupId returns None. You need to decide what to do in this case. Fail the whole process? Exclude that file from the list? The second problem at (1) is that findIssues will itself return a...
eclipse,scala,maven,intellij-idea,sbt
It should work out of box for dependencies, which are imported to the project as modules, no additional settings needed. At least for Java. Just do not run a Maven goal, that would use dependencies from the repository. ...
You don't need to describe how to combine errors—that's taken care of by ValidationNel's applicative functor via the Semigroup instance for NonEmptyList. The argument to the applicative builder should instead indicate how to create people: def makePerson(name: String, age: Int, gender: Gender): Validation[NonEmptyList[InvalidPersonError], Person] = (validateName(name) |@| validateAge(age))(Person(_, _, gender))...
regex,scala,parsing,lexical-analysis
In a double quoted string backslash is an escape character. If you mean to use the literal backslash in a double quotes string you must escape it, thus "\d" should be "\\d". Furthermore you do not need to escape the regex dot within a character class, since dot has no...
scala,apache-spark,bigdata,distributed-computing
shuffle=true and shuffle=false aren't going to have any practical differences in the resulting output since they are both going down to a single partition. However, when you set it to true you will do a shuffle which isn't of any use. With shuffle=true the output is evenly distributed amongst the...
to run as scala application, you need to create Scala App and not class In eclipse, package explorer select project/src/package right click new>scala app inform Name e.g. Test and click "finish" select Test.scala right click "run as Scala Application" see results in console window....
java,scala,intellij-idea,sbt,intellij-plugin
Okay, it seems that this question has been answered here: https://devnet.jetbrains.com/message/5546566#5546566...
I'm only going to comment on findStart for now. There are two things wrong with findStart: findStart is recursively called on every adjacent cell. Unfortunately, the neighbouring cell of any neighbour is the cell itself. The function never checks if you can actually walk on a given cell (I assume...
scala,sbt,akka,spray,microservices
The issue as it seems transitive dependency of the dependency is resulting with two different versions of metrics-core. The best thing to do would be to used the right library dependency so that you end up with a single version of this library. Please use https://github.com/jrudolph/sbt-dependency-graph , if it is...
var balance = Some(0) is inferred to be of type Some[Int], when you need to tell this explicitly that it's of type Option[Int]: var balance: Option[Int] = Some(0) Then balance will be able to take in either Some(0) or None. By the way, it's sometimes a good practice to always...
Try to write your last line as def map(tree:Tree[Int])(f:Int=>Int) : Tree[Int] = fold(tree , EmptyTree:Tree[Int])((l,x,r) => Node(f(x),l,r)) Scala's type inference is very limited compared to haskell, in this case it tries to infere type of fold from it's arguments left to right, and incorectly decides that result type of fold...
Your question is unclear, but I'll take a shot. To go from: val x = Array("a","x,y","b") to "a:x,y:b" You can use mkString: x.mkString(":") ...
algorithm,scala,priority-queue
If your elements are Ints, the easiest way to define such an Ordering is by taking a negative of the elements which should be ordered in reverse. You can create the Ordering, using methods provided by the object Ordering, and pass it to the PriotityQueue either explicitly: // I'm using...
The issue is simply that isAllergicTo is implemented incorrectly. If you fix it, you won't need to change score at all. As a hint: think about binary representation of score.
The problem is delayed evaluation from Stream/Iterable. This is very easy to see if you break it up into smaller pieces. scala> Stream("A", "").toIterable.map(_.head) res7: Iterable[Char] = Stream(A, ?) Mapping the Stream to _.head means we're going to treat the strings as collections, and the empty string as a collection...
The problem is the Java type erasure. classOf[List[MyClass]] at runtime is the same as classOf[List[_]]. That is why Jackson cannot know, which types of the elements to create. Luckily Jackson does support parsing with the JavaType, which describes the types themselves. Here a simple sample in Java: JavaType type =...
So, lots of problems. All that stuff you are doing? It's getting done in the constructor of Book, and redone for every instance. Your main method? That's gets compiled to instance method of Book, not a static method, so it does not serve an an entry point for an executable...
scala,apache-spark,scala-collections,spark-graphx
You're looking for the groupBy function followed by mapValues to process each group. pairs groupBy {_._1} mapValues { groupOfPairs => doSomething(groupOfPairs) } ...
Yep, Scalaz provides monad instances for tuples (up to Tuple8): import scalaz.std.anyVal._, scalaz.std.tuple._, scalaz.syntax.monad._ scala> type IntTuple[A] = (Int, A) defined type alias IntTuple scala> pair >>= (a => (a+1).point[IntTuple]) res0: (Int, String) = (2,as1) scala> for (p <- pair) yield (p + 1) res1: (Int, String) = (2,as1) (Note...
scala,playframework,playframework-2.3,playframework-2.4
Have a look at play documentation: Writing your own field constructor. You can check on errors with @if(elements.hasErrors) within the template of your custom field constructor. <div class="input-with-label text-left @if(elements.hasErrors){field-error}"> ... Edit: You can pass the error state of your field via the args parameter to your input. From the...
This enabled basic run time checks: trait RangeBound type Probability = Double with RangeBound implicit def makeProb(p: Double): Probability = { assert (p >= 0.0 && p <= 1.0) p.asInstanceOf[Probability] } implicit val probabilityOrdering = Ordering.Double.asInstanceOf[Ordering[Probability]] ...
sequential is not variable; It is a method call. Take a look at the specs2 code base: def sequential: Boolean = execute.sequential And why wouldn't it compile? specs2 is mature testing framework....
Your code should be okay provided you have the right implicits in scope. If you have an implicit FlowMaterializer in scope then things should work as expected as this code that compiles shows: import akka.http.scaladsl.server.Route import akka.actor.ActorSystem import akka.stream.ActorFlowMaterializer import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives._ import akka.stream.FlowMaterializer implicit val system = ActorSystem("test")...
You could, for example, move it to a trait like this: trait DateColumnMapper extends HasDatabaseConfig[JdbcProfile] { protected val dbConfig: DatabaseConfig[JdbcProfile] import driver.api._ implicit val dateColumnType = MappedColumnType.base[Date, Long]( d => d.getTime, d => new Date(d) ) } Then you can include this trait in whatever DAO or db component you...
scala,pattern-matching,scala-2.11
The equivalent non-infix version is: xs match { case List(x, _, _) => "yes" case _ => "no" } Scala specification says: An infix operation pattern p;op;q is a shorthand for the constructor or extractor pattern op(p,q). The precedence and associativity of operators in patterns is the same as in...
Using the tuple functionality in shapeless you could do: import shapeless._ import syntax.std.tuple._ case class Foo(a: Int, b: String) val hlist = 1 :: "a" :: 2 :: "b" :: HNil Foo.tupled(hlist.take(2).tupled) ...
scala,recursion,case,frequency
Cons operator (::) is an infix operator so if you want to get a type of List[T] and not List[List[T]] then you should write freq(c, y.filter(_ == c),(count(c,y),c)) :: list) ...
Yes, and it is implemented as this, like in Java or C++. You don't have to pass it explicitly at the method call, like in python. Then what you're creating in your example are instance variables, also called members. They belong to instances of the class, i.e. they are not...
as written in the manual, you can customize the sources (or source directories) pretty freely. by default, sbt will expect to have scala and java sources under a source directory. you can customize that too. depending on your exact use case, maybe you want these sources under a different configuration?...
You can make a custom serialiser for your class as it is described in here: https://github.com/json4s/json4s#serializing-non-supported-types Unless you really need it I wouldn't advice to make it part of your toString though; I'd rather advice to have some other object perform the transformation as it will make things a bit...
A simple singly linked list will provide O(1) for the append/discard portions of what you describe. Worst case, if you need to reverse the list at the end before processing, that would be an O(n) operation paid once. Note that if you go down this road, during the accumulation phase...
You're probably looking for the toSet method on TraversableOnce: def toSet[B >: A]: immutable.Set[B] Converts this traversable or iterator to a set. Note: will not terminate for infinite-sized collections. returns a set containing all elements of this traversable or iterator. You can use that together with the values or valuesIterator...
This is a topic discussed in Plugins Best Practices, specifically in the Configuration advices section. Provide raw settings and configured settings If your plugin is ObfuscatePlugin, provide baseObfuscateSettings that's not scoped in any configuration: lazy val baseObfuscateSettings: Seq[Def.Setting[_]] = Seq( obfuscate := Obfuscate((sources in obfuscate).value), sources in obfuscate := sources.value...
java,image,scala,image-processing
Before you write it out with ImageIO, create a BufferedImage first. It can be as simple as using the setRGB methods, and has the added benefit of allowing you to observe the image before writing it out.
json,scala,playframework,playframework-2.0,jsonpath
You can do: val text = (json \\ "name").asOpt[String] .getOrElse((json \\ "title").as[String]) ...
Any operator with a : on its right side has its operands flipped. There's other operators that make use of this to (can't think of any examples off the top of my head though).
The conventional way to write a factory in Scala is to define an apply method on the companion object. Here's an example using Either (because null is never/rarely used in Scala, and exceptions are ugly): class A private (n: Int) { override def toString = s"A($n)" } object A {...
The JavaTokenParsers does not implement the Scanners trait. So you would need to extends also from this trait (or a trait that extends it) in order to have access to this class. Unless your expr parser accepts the Reader as a parameter (not from its apply method), you'd need to...
It looks like the json parsing itself doesn't work. The No usable value for $outer message indicates that reflection might not work with Direction as an inner class of the Specification. So you should move it outside as a top-level class.
In this case you can use mapPartitions with the preservesPartitioning attribute. x.map((it => it.map { case (k,rr) => (k, someFun(rr, k)) }), preservesPartitioning = true) You just have to make sure you are not changing the partitioning, i.e. don't change the key....
The slf4j library is really an interface to some underlying logging implementation. You would have log4j, logback or some other logging implementation do the heavy lifting, with an adapter jar, as explained in the slf4j documentation. You would then provide the details in the properties file for log4j for instance,...
actorOf is used to create new actors by supplying their Props objects. actorSelection is a "pointer" to a path in actor tree. By using resolveOne you will get actorRef of already existing actor under that path - but that actorRef takes time to resolve, hence the Future. Here's more detailed...
You can overcome this by passing a function that calls mergesort to generalizedMergeSort. This call will capture the implicit Ordering: def mergesort[A: Ordering](as: List[A]): List[A] = { generalizedMergeSort(as, mergesort(_: List[A])) } mergesort(_: List[A]) is a closure function of type List[A] => List[A], which calls mergesort with its argument, and the...
scala,shapeless,type-level-computation
You're very close. The problem is that Scala isn't going to propagate implicit requirements up the call chain automatically for you. If you need a Generic[A, T] instance to call convert, then you'll have to make sure that one's in scope every time you call convert convert. If A and...
This will be not a very rigours analysis, but the problem seems to be with the BasicTransformer's transform(Seq[Node]) method[1]. The child transform method will be called twice for a node which is changed. Specifically in your example all the nodes will be called twice for this reason. And you are...
You are calling the as method on the wrong object. It should look as follows: Ok(bytOfImage).as("image/jpg") ...
The first problem with your code is that you need to forward from the master actor to the child so that the sender is properly propagated and available for the child to respond to. So change this (in RedisActor): summaryActor ! msg To: summaryActor forward msg That's the primary issue....
scala,gradle,akka,minecraft,minecraft-forge
I figured it out. application.conf had: provider = "akka.cluster.ClusterActorRefProvider" cluster isn't part of akka-actor its part of akka-cluster. I've switched to provider = "akka.actor.LocalActorRefProvider" That works now. The other option is to add akka-cluster to the dependancies list. If your actually trying to use ClusterActorRefProvider...
scala,testing,netty,unfiltered
Easy answer: replace your Unfiltered Netty server with a HTTP4S Blaze server. var server: org.http4s.server.Server = null val go: Task[Server] = org.http4s.server.blaze.BlazeBuilder .bindHttp(mockServicePort) .mountService(mockService) .start before { server = go.run } after { server.shutdown.run } There's also an awaitShutdown that blocks until the server shuts down. But since shutdown is...
I definitely think it is a compiler bug. def test1[T:ClassTag](v:In[T]) = { val t1 = classTag[T] == classTag[T1] val t2 = classTag[T] == classTag[T2] println(v match { case x : VIn[[email protected]] if t1 => "T1" case y : VIn[[email protected]] if t2 => "T2" }) v match { case x:In[T1] if...
scala,f#,functional-programming,tail-recursion,continuation-passing
The second call to go on line 4 is not in tail position, it is wrapped inside an anonymous function. (It is in tail position for that function, but not for go itself.) For continuation passing style you need Proper Tail Calls, which Scala unfortunately doesn't have. (In order to...
The code that is there now needs two minor changes: The Message trait must be sealed, or otherwise, Shapeless will not provide a Generic.Aux[Message, SomeCoproduct] instance. The call to Codec.coproduct[Message] must be after all the subtypes are defined. Moving the companion to the end of the file is sufficient. With...
You can call flatMap with the identity function to 'flatten' the structure of your RDD. rdd.flatMap(identity) ...
arrays,string,scala,split,scala-collections
Use split with -1 argument. string.split(",",-1) This behavior comes from Java (since Scala uses Java Strings). Here is the documentation directly out of Javadoc. ...
string,scala,scala-collections,scala-string
You can use the \bth\w* pattern to look for words that begin with th followed by other word characters, and then replace all matches with "123" scala> "this is the example, that we think of, anne hathaway".replaceAll("\\bth\\w*", "123") res0: String = 123 is 123 example, 123 we 123 of, anne...
The T: ClassTag syntax is called a context bound. It is syntactic sugar for an implicit parameter of type ClassTag[T]. In other words, the following class signatures are equivalent: class Test[T : ClassTag](val value : T) class Test[T](val value: T)(implicit val ev: ClassTag[T]) So the constructor you're looking for expects...
The thing is that we have to submit the class file to the spark cluster whom we want to execute or will take use as a supporting file, so follow these steps - Create a jar file of this class -> In eclipse you can export this class as a...
scala,formatting,messageformat
Since we don't like nulls I wrapped your possibly-null value into an Option. If the option have a value I create the array from it and then pass it into the formatter. If it didn't have a value I give it the value "". val szPhoneFrmt = new MessageFormat("1 {0}{1}-{2}")...
If you'll crate one actor per job, you'll get parallel processing of messages, while if you'll create actor on initialization, you messages of the same type will be processed one by one. Usually, you shouldn't use actors for parallel execution of your programm, their task is to process common resources,...
scala,functional-programming,higher-order-functions
You can use a filter and then map to get the index : scala> val s = "10010010" s: String = 10010010 scala> s.zipWithIndex.withFilter(_._1 == '1').map(_._2) res0: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 3, 6) Note: I'm using withFilter and not filter to avoid creating a temporary collection. Or you can use collect,...
Can you post your Status class definitation .If code is type column[Int] your code should be giving error as like works on column[string]. The below snippet works for doing a like on integer field. class Coffees(tag: Tag) extends Table[(String, Int)](tag, "COFFEES") { def name = column[String]("NAME") def status = column[Int]("STATUS")...