read-eval-print-loop

Setting package in Scala REPL

故事扮演 提交于 2019-11-30 20:49:19
Is there a way in the Scala REPL to set the "active" package scope ? Say I have a package com.package with class A , I want to be able to type new A() instead of new com.package.A() without explicitly doing import com.package.A . There might be a number of other classes in that package I'm interested into and I don't want to polute my REPL's global namespace by doing import com.package._ . Even better, I'd like to define class A without typing its fully qualified name. Something like: package com.package // do this once class A class B val a = new A() val b = new B() I'm aware of the :paste

Relation between REPL, interpreter and compiler

巧了我就是萌 提交于 2019-11-30 17:25:09
问题 From Wikipedia: The REPL is commonly misnamed an interpreter. This is a misnomer—many programming languages that use compilation (including bytecode compilation) have REPLs, such as Common Lisp and Python. From a reply to this post Interactive interpreters use REPLs. An interpreter is not required to have one. You can run Python, for example, in non-interactive mode (on a file) and it will not use a read-eval-print loop. I was wondering if REPL [del] always exists [/del] exists only for an

How can I access the last result in Scala REPL?

 ̄綄美尐妖づ 提交于 2019-11-30 17:17:59
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? 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 You can refer to the previous output as res N for some N . You've probably noticed that in the Scala REPL, results are printed in the form res N : Type = value : Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24). Type in

handy ways to show linearization of a class?

十年热恋 提交于 2019-11-30 14:11:03
I'll bet there are some snazzy ways to show linearization in the repl via the new reflection libraries and/or repl power mode. What are they? In my particular case, I'm trying to understand the linearization of ArrayBuffer instances. (I'm trying to make a similar class that's a Buffer and an IndexedSeqOptimized, and I'm finding complaints from the compiler that overriding method seq has incompatible type) The rules for linearization of scala classes are described in the scala spec section 5.1.2. As I understand it, method overrides go in linearization order, and initialization goes in reverse

How does orElse work on PartialFunctions

筅森魡賤 提交于 2019-11-30 13:09:25
I am getting very bizarre behavior (at least it seems to me) with the orElse method defined on PartialFunction It would seem to me that: val a = PartialFunction[String, Unit] { case "hello" => println("Bye") } val b: PartialFunction[Any, Unit] = a.orElse(PartialFunction.empty[Any, Unit]) a("hello") // "Bye" a("bogus") // MatchError b("bogus") // Nothing b(true) // Nothing makes sense but this is not how it is behaving and I am having a lot of trouble understanding why as the types signatures seem to indicate what I exposed above. Here is a transcript of what I am observing with Scala 2.11.2:

How to save REPL session?

假如想象 提交于 2019-11-30 12:43:22
Is it possible to save a REPL session in a file? Is there a minimum version of Scala requiered to do this? I remember having seen someone do it, but I can't fine it in :help or in the documentation. som-snytt This is possible as of Scala 2.11. Example usage: scala> 1 res0: Int = 1 scala> 2 res1: Int = 2 scala> 3 res2: Int = 3 scala> :save xxx scala> :load xxx Loading xxx... res3: Int = 1 res4: Int = 2 res5: Int = 3 You can :reset before a :load to get correct references to results: scala> 1 res0: Int = 1 scala> res0 + 1 res1: Int = 2 scala> :save xxx later that day... scala> 7 res0: Int = 7

Scala REPL in Gradle

旧巷老猫 提交于 2019-11-30 12:42:12
At the moment Gradle's scala integration does not offer REPL functionality. How to ergonomically run a Scala REPL from Gradle with the appropriate classpath? Dominykas Mostauskis Minimal build.gradle : apply plugin: 'scala' repositories{ mavenCentral() } dependencies{ compile "org.scala-lang:scala-library:2.11.7" compile "org.scala-lang:scala-compiler:2.11.7" } task repl(type:JavaExec) { main = "scala.tools.nsc.MainGenericRunner" classpath = sourceSets.main.runtimeClasspath standardInput System.in args '-usejavacp' } Credit to this answer for explaining how to direct stdin with standardInput

REPL for interpreter using Flex/Bison

对着背影说爱祢 提交于 2019-11-30 09:15:00
I've written an interpreter for a C-like language, using Flex and Bison for the scanner/parser. It's working fine when executing full program files. Now I'm trying implement a REPL in the interpreter for interactive use. I want it to work like the command line interpreters in Ruby or ML: Show a prompt Accept one or more statements on the line If the expression is incomplete display a continuation prompt allow the user to continue entering lines When the line ends with a complete expression echo the result of evaluating the last expression show the main prompt My grammar starts with a top_level

node js interact with shell application

梦想与她 提交于 2019-11-30 07:29:26
There are plenty of node js examples on online about how to spawn a child process and then catch the result as a string for your own processing. But... I want to 'interact' with a child process. For example, how would I write a node js application than starts by calling ' python ' and then types a statement ' 1+1 ', lets me catch the result ' 2 ', before proceeding to type another arbitrary statement ' 4+4 '? (And by 'type' I'm assuming it will require streaming data to the stdin that the process uses). var child = require('child_process'); var ps = child.spawn('python', ['-i']); ps.stdout

scala.tools.nsc.IMain within Play 2.1

柔情痞子 提交于 2019-11-30 07:13:07
问题 I googled a lot and am totally stuck now. I know, that there are similar questions but please read to the end. I have tried all proposed solutions and none did work. I am trying to use the IMain class from scala.tools.nsc within a Play 2.1 project (Using Scala 2.10.0). Controller Code This is the code, where I try to use the IMain in a Websocket. This is only for testing. object Scala extends Controller { def session = WebSocket.using[String] { request => val interpreter = new IMain() val