如何将文本输入的字符串转换为Scala中的函数 [英] How to convert a string from a text input into a function in a Scala
问题描述
我正在使用Scala创建一个GUI,它应该根据算法播放一个信号,但是会放入textField中,例如。 SIN(2 * PI * 400 * T)。播放功能完美运行,但问题在于GUI的输入。 textField接受函数并将其转换为字符串,现在我想知道是否有一种简单的方法将字符串转换回函数。我正在考虑将输入与各种有效函数进行匹配,但是我不想限制可能的函数,因为我希望用户能够更多地创建
我仍然斯卡拉相对较新,希望你们中的一个有更好的主意。这里是我到目前为止的代码,它的基本设置侧重于功能。
对象PlayApp扩展SimpleGUIApplication {
def top = new MainFrame {
title =Play Function App
val label = new Label(输入要播放的函数)
对象函数扩展TextField = 10)
val label2 =新标签(输入播放时间的长度)
对象时间扩展TextField {columns = 10}
val按钮=新建按钮(播放)
content = new BoxPanel(Orientation.Vertical){
contents + = label
contents + = Function
contents + = label2
contents + = Time
contents + = button
border = Swing.EmptyBorder(50,50,20,50)
}
var f =sin(t)
$ b ListenTo(Function,Time,button)
reactions + = {
case EditDone(Function)=>
f = Function.text.toString
case EditDone(Time)=>
x = Time.text.toInt
case ButtonClicked(b)=>
play(t => f.toDouble,0,x)
}
}
}
您希望编译用户输入的代码行,并带有一些有用的前导码。
我没有这样做,所以我没有代码片段(但是尝试搜索),但是可以嵌入REPL,让它编译代码行,然后提取结果。
这是一个使用ILoop获得某些输出的代码片段,但您希望使用IMain来获取包含用户函数的结果类的结果。
import scala.tools.nsc.interpreter.ILoop
import java.io.StringReader
import java.io.StringWriter
import java.io.PrintWriter
import java.io.BufferedReader
import scala.tools.nsc.Settings
object FuncRunner extends App {
val line =sin(2 * Pi * 400 * t)
val lines =import scala.math._
| var t = 1。stripMargin
val in = new StringReader(lines +\\\
+ line +\\\
val f =(t:Int)=> + line)
val out = new StringWriter
val settings = new Settings
$ b $ val valoper = new ILoop(new BufferedReader(in),new PrintWriter ))
val res = looper进程设置
控制台println s[$ res] $ out
}
I am creating a GUI using Scala which should play a signal dependent on the algorithm but into the textField, eg. sin(2*Pi*400*t). The playing function works perfectly, however the issue is the input for the GUI. The textField takes the function and converts it to a string, now I was wondering is there a simple way to convert the string back into a function. I was considering matching the input to various valid functions, however I don't want to restrict the possible functions, as I want the user to be more create
I am still relatively new to Scala, hopefully one of you have a better idea. Here is the code I have so far, its a basic setup focusing on functionality.
object PlayApp extends SimpleGUIApplication {
def top = new MainFrame {
title = "Play Function App"
val label = new Label("Type in function to be played")
object Function extends TextField ( columns = 10)
val label2 = new Label("Type in length of time to play")
object Time extends TextField { columns = 10}
val button = new Button("Play")
contents = new BoxPanel(Orientation.Vertical) {
contents += label
contents += Function
contents += label2
contents += Time
contents += button
border = Swing.EmptyBorder(50, 50, 20, 50)
}
var f = "sin(t)"
var x = 0
listenTo(Function, Time, button)
reactions += {
case EditDone(Function) =>
f = Function.text.toString
case EditDone(Time) =>
x = Time.text.toInt
case ButtonClicked(b) =>
play(t => f.toDouble,0,x)
}
}
}
You want to compile the line of code that the user enters, with some useful preamble.
I haven't done this so I don't have a snippet handy (but try searching), but you can embed the REPL, have it compile the line of code, and then extract the result.
Here is a snippet that uses ILoop to get some output, but you want to use IMain to get a result with the result class that wraps the user function.
import scala.tools.nsc.interpreter.ILoop
import java.io.StringReader
import java.io.StringWriter
import java.io.PrintWriter
import java.io.BufferedReader
import scala.tools.nsc.Settings
object FuncRunner extends App {
val line = "sin(2 * Pi * 400 * t)"
val lines = """import scala.math._
|var t = 1""".stripMargin
val in = new StringReader(lines + "\n" + line + "\nval f = (t: Int) => " + line)
val out = new StringWriter
val settings = new Settings
val looper = new ILoop(new BufferedReader(in), new PrintWriter(out))
val res = looper process settings
Console println s"[$res] $out"
}
这篇关于如何将文本输入的字符串转换为Scala中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!