ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决clojure - How can I access the last result in Scala REPL?

itPublisher 分享于

2020腾讯云10周年活动,优惠非常大!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1040

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
地址https://www.aliyun.com/minisite/goods

In python REPL I can do things like:

>>> [1,2,3,4]
[1, 2, 3, 4]
>>> sum(_)
10

In clojure REPL I can do this:

user=> "Hello!"
"Hello!"

user=> *1
"Hello!"

Is there is something like this in Scala REPL?

scala clojure read-eval-print-loop
|
  this question
edited Jul 22 '16 at 21:50 Alex Miller 46.8k 23 87 137 asked Aug 15 '12 at 15:30 om-nom-nom 48.5k 9 137 178

 | 

3 Answers
3

解决方法

Yes, you can use dot notation to refer to the last result:

scala> List(1,2,3,4)
res0: List[Int] = List(1, 2, 3, 4)

scala> .sum
res1: Int = 10

|
  this answer
answered Aug 15 '12 at 15:30 om-nom-nom 48.5k 9 137 178

 | 

You can refer to the previous output as resN for some N. You've probably noticed that in the Scala REPL, results are printed in the form resN: Type = value:

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.

scala> List(1,2,3,4)
res0: List[Int] = List(1, 2, 3, 4)

scala> "Hello!"
res1: java.lang.String = Hello!

Well, that resN is a real variable name. In this example, you can refer to the list as res0 and the string as res1 for (at least as far as I know) as long as the REPL is open:

scala> (res0.toString + res1) toLowerCase
res2: java.lang.String = list(1, 2, 3, 4)hello!

|
  this answer
answered Aug 15 '12 at 18:27 Antal Spector-Zabusky 27k 5 54 121

 | 

I normally just hit the ↑ key to bring back the last line of code and carry on typing. This has the advantage of keeping the whole expression together for easy cutting-and-pasting or editing later.


|
  this answer
answered Aug 15 '12 at 22:50 Luigi Plinge 33.1k 16 80 150

 | 


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。