我有一个名为Cell的案例类,它有无参数的方法,可以向上,向下,向左,向右移动单元格......
case class Cell(topLeft: Coordinate, botRight: Coordinate) {
def up: Cell = {
Cell(
Coordinate(topLeft.x + 0, topLeft.y - 1)
, Coordinate(botRight.x + 0, botRight.y - 1))
}
}
Run Code Online (Sandbox Code Playgroud)
这种向上操作应该是一个实例方法,并且这样调用是正确的:
val cell = Cell(x,y)
cell.up
Run Code Online (Sandbox Code Playgroud)
但是,如果我使这些操作属于一个伴随对象的静态函数,就像这样,
object Cell{
def up(cell: Cell): Cell = {
Cell(
Coordinate(cell.topLeft.x + 0, cell.topLeft.y - 1)
, Coordinate(cell.botRight.x + 0, cell.botRight.y - 1))
}
...
}
Run Code Online (Sandbox Code Playgroud)
然后他们似乎更容易组合.现在我可以向上,向下,向左或向右传递,作为Cell => Cell类型的参数.作为无参数实例方法,它等价于一个值,因此不能作为函数传递.
请参阅下面的两条注释行.
private def move(move: Cell …Run Code Online (Sandbox Code Playgroud) 这里有两个代码用于解决项目euler中的问题6:为什么在我将数字设置得更大之前它们会给出类似的答案?(100,000)
前十个自然数的平方和是,
1 2 + 2 2 + ... + 10 2 = 385
前十个自然数之和的平方是,
(1 + 2 + ... + 10)2 = 55 2 = 3025
因此,前十个自然数的平方和与总和的平方之差为3025-385 = 2640.
找出前100个自然数的平方和与总和的平方之间的差异.
代码1:
public class Problem_Six_V2 {
public static void main(String[] args) {
long limit = 100000;
long sum = (limit * (limit + 1)) / 2;
long sumOfSqr = (long)((((2*limit)*limit)+((2*limit)*1)+(1*limit)+(1*1))*limit)/6;
System.out.println(Math.pow(sum, 2) +" "+ sumOfSqr);
System.out.println(Math.pow(sum, 2) - sumOfSqr);
}
}
Run Code Online (Sandbox Code Playgroud)
^^^输出= 2.500016666416665E19
这是代码二:
public class Problem_Six {
public …Run Code Online (Sandbox Code Playgroud) 我编写了以下程序,该程序是匿名的,并从字符串中删除所有元音,但是当我调用它时,我收到一个错误:我已经按照类似帖子中给出的建议,但它没有帮助:Oracle PLS-00363:表达式''不能用作赋值目标
SQL> CREATE OR REPLACE PROCEDURE disemvowel (string IN OUT NVARCHAR2)
2 IS
3 BEGIN
4 DBMS_OUTPUT.PUT_LINE(translate(string,'euioa',''));
5 END disemvowel;
6 /
Procedure created.
Run Code Online (Sandbox Code Playgroud)
到目前为止很好,但现在我称之为:
SQL> BEGIN
2 disemvowel('hahahahaha');
3 END;
4 /
Run Code Online (Sandbox Code Playgroud)
错误消息说:
disemvowel('hahahahaha');
*
ERROR at line 2:
ORA-06550: line 2, column 12:
PLS-00363: expression 'hahahahaha' cannot be used as an assignment target
ORA-06550: line 2, column 1:
PL/SQL: Statement ignored
Run Code Online (Sandbox Code Playgroud) 我读过男人和其他地方,但我正在努力解决这个问题.子进程始终是唯一的,但在分叉的任何例子中,我发现孩子的PID值必须为0.如果有aremany孩子,他们不能全为0,否则就不是唯一的?
我正在为 REST 负载主体创建一个 json 主体,如下所示:
>>> j = json.loads('["foo", {"bar": ["to_be_replaced", 1.1, 1.0, 2]}]')
>>> text = "aaaa" + "\\" + "bbbbb" + "\\" + "cccc"
>>> j[1]["bar"][0] = text
>>> j
['foo', {'bar': ['aaaa\\bbbbb\\cccc', 1.1, 1.0, 2]}]
Run Code Online (Sandbox Code Playgroud)
烦人的是,另一边期望的格式是这样的
"aaaa\bbbb\cccc".
Run Code Online (Sandbox Code Playgroud)
我知道这是一个糟糕的主意。
我已经尝试了所有方法,并且开始相信根本不可能将这种格式的文本存储在 json 对象中。有办法吗?或者我是否需要让 Web 服务的开发人员选择一个更合理的分隔符。
我知道这实际上是一个反斜杠,如果我打印,就会得到一个反斜杠
>>> print(text)
aaaa\bbbbb\cccc
Run Code Online (Sandbox Code Playgroud)
但这并不能帮助我将其放入 json 对象中。
我有这个函数来功能性地遍历图形:
private def dfs(current: RCell, rCellsMovedWithEdges: Vector[RCell], acc: Vector[RCell] = Vector()): Vector[RCell] = {
current.edges.foldLeft(acc) {
(results, next) =>
if (results.contains(rCellsMovedWithEdges(next))) results
else dfs(rCellsMovedWithEdges(next), rCellsMovedWithEdges, results :+ current)
} :+ current
}
Run Code Online (Sandbox Code Playgroud)
这很好,但我担心最后的“:+ current”会使其成为非尾递归。
我把它改成这样:
private def dfs(current: RCell, rCellsMovedWithEdges: Vector[RCell]): Vector[RCell] = {
@annotation.tailrec
def go(current: RCell, rCellsMovedWithEdges: Vector[RCell], acc: Vector[RCell] = Vector()): Vector[RCell] = {
current.edges.foldLeft(acc) {
(results, next) =>
if (results.contains(rCellsMovedWithEdges(next))) results
else go(rCellsMovedWithEdges(next), rCellsMovedWithEdges, results :+ current)
}
}
go(current, rCellsMovedWithEdges) :+ …Run Code Online (Sandbox Code Playgroud)