Filter text in vim using scala - scala

Suppose you have the following text content in vim editor:
a b c
d e f
a b c
d e f
a b c
d e f
a b c
d e f
a b c
d e f
And you visually select all and do !tail -n 1, then the text is pipe through the tail command and only the last line is left.
Now suppose I have a snippet in scala like this:
object HelloWorld extends App {
println("Hello")
}
HelloWorld.main(Array(""))
How can I filter it so that it becomes "Hello"?
The same technique can be applied in intellij with ideavim enabled.
I have tried !scala but the result is a mess of course:
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_51).
Type in expressions to have them evaluated.
Type :help for more information.
scala> object HelloWorld extends App {
| println("Hello")
| }
defined object HelloWorld
scala> HelloWorld.main(Array(""))
Hello
scala> :quit

! xargs -0 scala -e
scala -e executes its argument as Scala code, and xargs -0 turns the piped input into an argument for the given command.

Related

Right not working with for comprehension Scala

Background
I am trying to use comprehensions with Scala with the Either type, namely using a Right.
However, despite my efforts, I get an error and nothing works.
Code
I am using scala's repl to make a few tests. This is the easiest use case I could come up with:
scala> for {
| x <- Right(1)
| y <- Right(2)
| z <- Right(3)
| } yield x + y + z
You will see it is basically a copy of this page:
https://www.scala-lang.org/api/2.12.7/scala/util/Either.html
Problem
However, this fails with the following error:
<console>:13: error: value flatMap is not a member of scala.util.Right[Nothing,Int]
x <- Right(1)
^
<console>:14: error: value flatMap is not a member of scala.util.Right[Nothing,Int]
y <- Right(2)
^
<console>:15: error: value map is not a member of scala.util.Right[Nothing,Int]
z <- Right(3)
^
I am using using the following version of scala:
Welcome to Scala 2.11.12 (OpenJDK 64-Bit Server VM, Java 11.0.13).
Type in expressions for evaluation. Or try :help.
I understand some changes were made to Either, so it became Right biased, but I don't see how such changes could affect this example.
What am I missing?
In scala 2.11 Either is not a Monad. Combinators like flatMap and map are missing from it. Instead, you call .right or .left to get a RightProjection or LeftProjection which does have the combinators. You need to project your Either as right. The code below will return Right(6).
for {
x <- Right(1).right
y <- Right(2).right
z <- Right(3).right
} yield x + y + z

Purescript - How to define function signature in REPL?

When I type, say:
f :: Int -> Int
The REPL complaints with:
The type declaration for f should be followed by its definition.
The REPL expects each single line to be a complete program, and since a lonely signature is not a complete program, you get an error.
In order to make the REPL ingest several lines before trying to compile them, use the :paste command to begin the block and then hit Ctrl+D to end it:
> :paste
… f :: Int -> Int
… f x = x + 42
… ^D
> f 1
43
This and other commands can be discovered by typing :?, as the REPL invites you to do at the beginning of the session:
PSCi, version 0.13.6
Type :? for help

Using vim editor to create a Scala script within REPL

Version of Scala I am using is Scala 2.12.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_121) and the Jline Library present is 2.14.3 .
This could sound silly but I am trying to figure out the problem when trying to create a scala file using editor cmd line vi or vim during the Scala REPL mode its throwing error. Below is my error .. Could you please let me know if there is any specific Scala Terminal console that I am suppose to use or am I doing something wrong ?
scala> vi test1.scala
<console>:1: error: ';' expected but '.' found.
vi test1.scala
I am able to do a VI and VIM as well in my system without the SCALA REPL mode but when I am in REPL I am not able to create a scala script file and execute it . What could be wrong ? Is there any settings that needs to be enabled for this ?
For saving your REPL history, use :save file.
There is limited support for using an external editor. The result of the edit is run immediately. After a reset, only the edited lines are in session history, so save will save only those lines.
$ EDITOR=gedit scala
Welcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_111).
Type in expressions for evaluation. Or try :help.
scala> val x = 42
x: Int = 42
scala> println(x)
42
scala> :edit -2
+val x = 17
+println(x)
17
x: Int = 17
scala> :hi 3
1896 val x = 17
1897 println(x)
1898 :hi 3
scala> :reset
Resetting interpreter state.
Forgetting this session history:
val x = 42
println(x)
val x = 17
println(x)
Forgetting all expression results and named terms: $intp, x
scala> :ed 1896+2
+val x = 5
+println(x)
5
x: Int = 5
scala> :save sc.sc
scala> :load sc.sc
Loading sc.sc...
x: Int = 5
5

How to clear all variables in Scala REPL

Is there a quick command? I don't want to Ctrl+d and run Scala everytime I want to clear all variables. reset, clear and clean don't work and :help doesn't have anything listed
You can use :reset
Welcome to Scala version 2.10.0-RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val a = 1
a: Int = 1
scala> val b = 3
b: Int = 3
scala> :reset
Resetting interpreter state.
Forgetting this session history:
val a = 1
val b = 3
Forgetting all expression results and named terms: $intp, a, b
scala>

Playframework, scala case class and property not found

I've got very strange behaviour when I run playframework in scala. I used anorm as database access layer thus I've started doing some code and I saw very strange scala compiler behavoiur.
This is code which is working:
case class P_Page_Control(Control_ID:Int,
Client_ID:String,
cContent: String,
Page_ID: Int,
Language_ID: Int,
InsertTime: Date,
ChangeTime: Option[Date],
IsDeleted: Boolean)
and:
object P_Page_Control { val parser = {
get[Int]("Control_ID") ~
get[String]("Client_ID") ~
get[String]("Content") ~
get[Int]("Page_ID") ~
get[Int]("Language_ID") ~
get[Date]("InsertTime") ~
get[Option[Date]]("ChangeTime") ~
get[Boolean]("IsDeleted") map {
case a ~ b ~ c ~ d ~ e ~ f ~ g ~ h =>
P_Page_Control(a, b, c, d, e, f, g, h)
}}}
For this moment no compilation error. Works fine.
But when I change property name I got errors:
object P_Page_Control { val parser = {
get[Int]("Control_ID") ~
get[String]("Client_ID") ~
get[String]("Content") ~
get[Int]("Page_ID") ~
get[Int]("Language_ID") ~
get[Date]("InsertTime") ~
get[Option[Date]]("ChangeTime") ~
get[Boolean]("IsDeleted") map {
case A_B ~ b ~ c ~ d ~ e ~ f ~ g ~ h =>
P_Page_Control(A_B, b, c, d, e, f, g, h)
}}}
As I'm a totally new to Scala I thought _ is some magic keyword or other magic stuff.
So I changed property name to aBB_AccAd but there was no compilation errors.
ooops...
Next funny thing: I renamed this to AAbbdddsadasdasAAFFFFeeee and I saw again compilation errors.
So what motivates Scala to throw compilation error for some set of literals?
Is this a bug or feature ? :-)
Names in patterns, which start with a capital letter, are interpreted as variable or object names that refer to an extractor (an object with an unapply or unapplySeq method). Since you haven't declared such a variable or object, Scala can't find it and throws an error.