Repository: incubator-toree Updated Branches: refs/heads/master e87c0859b -> 9ee665d68
[TOREE-466] Properly recognize higher order functions Closes #153 Project: http://git-wip-us.apache.org/repos/asf/incubator-toree/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-toree/commit/9ee665d6 Tree: http://git-wip-us.apache.org/repos/asf/incubator-toree/tree/9ee665d6 Diff: http://git-wip-us.apache.org/repos/asf/incubator-toree/diff/9ee665d6 Branch: refs/heads/master Commit: 9ee665d68cce8994d39ed4633c2cbe379d26526c Parents: e87c085 Author: Luciano Resende <lrese...@apache.org> Authored: Thu Oct 4 20:38:32 2018 -0400 Committer: Luciano Resende <lrese...@apache.org> Committed: Fri Oct 5 09:49:22 2018 -0400 ---------------------------------------------------------------------- .../scala/ScalaInterpreterSpecific.scala | 17 +++++++++++++---- .../interpreter/scala/ScalaInterpreter.scala | 6 ++++++ .../scala-2.11/scala/ScalaInterpreterSpec.scala | 15 +++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/9ee665d6/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala ---------------------------------------------------------------------- diff --git a/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala b/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala index 1bde367..902bb1d 100644 --- a/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala +++ b/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala @@ -247,10 +247,19 @@ trait ScalaInterpreterSpecific extends SettingsProducerLike { this: ScalaInterpr override def read(variableName: String): Option[AnyRef] = { require(iMain != null) - iMain.eval(variableName) match { - case null => None - case str: String if str.isEmpty => None - case res => Some(res) + try { + iMain.eval(variableName) match { + case null => None + case str: String if str.isEmpty => None + case res => Some(res) + } + } catch { + // if any error returns None + case e: Throwable => { + logger.debug(s"Error reading variable name: ${variableName}", e) + clearLastException() + None + } } } http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/9ee665d6/scala-interpreter/src/main/scala/org/apache/toree/kernel/interpreter/scala/ScalaInterpreter.scala ---------------------------------------------------------------------- diff --git a/scala-interpreter/src/main/scala/org/apache/toree/kernel/interpreter/scala/ScalaInterpreter.scala b/scala-interpreter/src/main/scala/org/apache/toree/kernel/interpreter/scala/ScalaInterpreter.scala index d8a937a..cf763d8 100644 --- a/scala-interpreter/src/main/scala/org/apache/toree/kernel/interpreter/scala/ScalaInterpreter.scala +++ b/scala-interpreter/src/main/scala/org/apache/toree/kernel/interpreter/scala/ScalaInterpreter.scala @@ -204,6 +204,11 @@ class ScalaInterpreter(private val config:Config = ConfigFactory.load) extends I val text = new StringBuilder interpreterOutput.split("\n").foreach { + + case HigherOrderFunction(name, func, funcType) => + + definitions.append(s"$name: $func$funcType").append("\n") + case NamedResult(name, vtype, value) if read(name).nonEmpty => val result = read(name) @@ -409,6 +414,7 @@ class ScalaInterpreter(private val config:Config = ConfigFactory.load) extends I object ScalaInterpreter { + val HigherOrderFunction: Regex = """(\w+):\s+(\(\s*.*=>\s*\w+\))(\w+)\s*.*""".r val NamedResult: Regex = """(\w+):\s+([^=]+)\s+=\s*(.*)""".r val Definition: Regex = """defined\s+(\w+)\s+(.+)""".r val Import: Regex = """import\s+([\w\.,\{\}\s]+)""".r http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/9ee665d6/scala-interpreter/src/test/scala-2.11/scala/ScalaInterpreterSpec.scala ---------------------------------------------------------------------- diff --git a/scala-interpreter/src/test/scala-2.11/scala/ScalaInterpreterSpec.scala b/scala-interpreter/src/test/scala-2.11/scala/ScalaInterpreterSpec.scala index fd0cf20..9d024f1 100644 --- a/scala-interpreter/src/test/scala-2.11/scala/ScalaInterpreterSpec.scala +++ b/scala-interpreter/src/test/scala-2.11/scala/ScalaInterpreterSpec.scala @@ -438,6 +438,21 @@ class ScalaInterpreterSpec extends FunSpec interpreter.stop() } + it("should properly handle higher order functions") { + interpreter.start() + doReturn("myFunction: (x: Int, foo: Int => Int)Int").when(mockSparkIMain).eval("myFunction") + + // Results that match + interpreter.prepareResult("myFunction: (x: Int, foo: Int => Int)Int") should be( + (None, + Some("myFunction: (x: Int, foo: Int => Int)Int\n"), + None)) + + + interpreter.stop() + + } + it("should truncate res results that have tuple values") { //val t: (String, Int) = ("hello",1) ==> t: (String, Int) = (hello,1) interpreter.start()