鉴于这些F#类型声明......
type Message =
| MessageA
| MessageB
| MessageC
| MessageD
type State = {
Name:string
NextStateMap: Map<Message,State>
}
Run Code Online (Sandbox Code Playgroud)
...对这个特定的状态机有一个同样富有表现力的定义......
let rec state0 = { Name = "0"; NextStateMap = Map.ofList [ (MessageA,state1); (MessageB,state2)] }
and state1 = { Name = "1"; NextStateMap = Map.ofList [ (MessageB,state3)] }
and state2 = { Name = "2"; NextStateMap = Map.ofList [ (MessageA,state3)] }
and state3 = { Name = "3"; NextStateMap = Map.ofList [ (MessageC,state4)] }
and state4 = { Name …Run Code Online (Sandbox Code Playgroud) 在许多语言中,可以使用内联值列表,以及某种形式的代码类似于以下内容:
for x in [1,7,8,12,14,56,123]:
print x # Or whatever else you fancy doing
Run Code Online (Sandbox Code Playgroud)
在过去一年左右的时间里使用 SQL,我发现即使在 WHERE 中使用这样的数组也不是问题......
select *
from foo
where someColumn in (1,7,8,12,14,56,123) and someThingElse...
Run Code Online (Sandbox Code Playgroud)
...我还没有找到从内联数组获取数据的等效形式:
-- This is not working
select *
from (1,7,8,12,14,56,123)
where somethingElse ...
Run Code Online (Sandbox Code Playgroud)
在寻找解决方案时,我只发现有人建议联合汤:
select *
from (SELECT 1 UNION SELECT 1 UNION SELECT 7 UNION ...)
where somethingElse ...
Run Code Online (Sandbox Code Playgroud)
...这可以说是丑陋且冗长的。
我可以在编辑器 (VIM) 中通过几次按键快速从列表中生成 UNION soup,然后将其粘贴回我的数据库提示符 - 但我想知道我是否缺少其他一些方法来完成此操作。
另外,如果没有标准的方法来做到这一点,我仍然会对特定于数据库引擎的解决方案(Oracle、PostgreSQL 等)感兴趣
预先感谢您的任何指点。
在查看代码库时,我发现了一段特定的代码,它触发了关于“越界访问”的警告。查看代码后,我看不到报告访问的发生方式 - 并试图最小化代码以创建可重现的示例。然后,我使用我可以访问的两个商业静态分析器以及开源 Frama-C 检查了这个示例。
他们三个人都看到相同的“越界”访问权限。
我不。我们来看一下:
3 extern int checker(int id);
4 extern int checker2(int id);
5
6 int compute(int *q)
7 {
8 int res = 0, status;
9
10 status = checker2(12);
11 if (!status) {
12 status = 1;
13 *q = 2;
14 for(int i=0; i<2 && 0!=status; i++) {
15 if (checker(i)) {
16 res = i;
17 status=checker2(i);
18 }
19 }
20 }
21 if (!status)
22 *q = res;
23 return …Run Code Online (Sandbox Code Playgroud) 看看这个F#/ OCaml代码:
type AllPossible =
| A of int
| B of int*int
| ...
| Z of ...
let foo x =
....
match x with
| A(value) | B(value,_) -> (* LINE 1 *)
(* do something with the first (or only, in the case of A) value *)
...
(* now do something that is different in the case of B *)
let possibleData =
match x with
| A(a) -> bar1(a)
| B(a,b) -> bar2(a+b)
| …Run Code Online (Sandbox Code Playgroud) 似乎Arrays的模式匹配器不会抱怨丢失的情况.例如,如果我尝试从args字符串数组中读取...
def main(args: Array[String]) {
val limit = args match {
case Array(x, _*) => { println("Running for " + x) ; x.toInt }
// case _ => 1000
}
}
Run Code Online (Sandbox Code Playgroud)
...编译器是静默的,即使我没有匹配所有可能的数组情况(例如空的情况).但是,如果我将数组转换为List ...
def main(args: Array[String]) {
val limit = args.toList match {
case x :: xs => { println("Running for " + x) ; x.toInt }
// case _ => 1000
}
}
Run Code Online (Sandbox Code Playgroud)
...然后编译器捕获错误,并报告:
[warn] missedErrorCase.scala:38: match may not be exhaustive.
[warn] It would fail on the …Run Code Online (Sandbox Code Playgroud) 以下(一个小的C程序和一个调用它的python脚本)在各种Unix上表现不同.
在其中一些(例如Debian stable)中,C app获取信号,消息从信号处理程序打印得很好并且脚本完成.在其他人(例如,两年前的Ubuntu和OpenBSD)中,信号丢失,因此根本不打印消息,并且脚本永远等待...
信号总是传递,如果在python脚本中,我改变了这个......
mysub=subprocess.Popen("./cryInAbort", shell=True)
Run Code Online (Sandbox Code Playgroud)
进入这...
mysub=subprocess.Popen("./cryInAbort", shell=False)
Run Code Online (Sandbox Code Playgroud)
所以看起来在某些Unix中,中间shell"吃掉"SIGINT,而在其他Unix中,它将它转发给子进程(C程序).
我注意只在信号处理程序中调用重入函数,所以这似乎与我的C代码无关 - 它看起来好像是在"/ bin/sh"中的信号处理行为(Python使用的默认shell)产生的东西)在Unix上并不"稳定"......
难道我做错了什么?
编辑:如果你想知道为什么我使用"shell = True":这是因为在我的真实代码中,我不只是传递"./executable" - 我使用shell代码进行循环和通配符.
这是在获得SIGINT时打印和死亡的C代码:
#include <signal.h>
#include <unistd.h>
void my_handler()
{
static const char msg[] = "Goodbye - test was OK.\n";
write(1,msg,sizeof(msg));
fsync(1);
_exit (0);
}
int main()
{
(void) signal (SIGINT, my_handler);
while (1);
}
Run Code Online (Sandbox Code Playgroud)
这是通过发送SIGINT来测试它的Python脚本:
#!/usr/bin/env python
import subprocess,signal,time
mysub=subprocess.Popen("./cryInAbort", shell=True)
time.sleep(2)
mysub.send_signal(signal.SIGINT)
mysub.wait()
Run Code Online (Sandbox Code Playgroud) 在阅读了最近关于泊松分布应用程序的博客文章后,我尝试使用 Python 的“scipy.stats”模块以及 Excel/LibreOffice 的“POISSON”和“CHITEST”函数重现其发现。
对于文章中显示的期望值,我简单地使用了:
import scipy.stats
for i in range(8):
print(scipy.stats.poisson.pmf(i, 2)*31)
Run Code Online (Sandbox Code Playgroud)
这重现了博客文章中显示的表格 - 我还在 LibreOffice 中重新创建了它,使用第一列 A,在单元格 A1、A2、...、A8 中具有值 0 到 7,以及简单的公式 '=POISSON( A1, 2, 0)*31' 在 B 列的前 8 行中重复。
到目前为止一切顺利 - 现在是卡方 p 检验值:
在 LibreOffice 下,我只是在单元格 C1-C8 中记下观察到的值,并使用“=CHITEST(C1:C8, B1:B8)”来重现文章报告的 p 值 0.18。但是,在 scipy.stats 下,我似乎无法重现此值:
import numpy as np
import scipy.stats
obs = [4, 10, 7, 5, 4, 0, 0, 1]
exp = [scipy.stats.poisson.pmf(i, 2)*31 for i in range(8)]
# we …Run Code Online (Sandbox Code Playgroud) 多年来,我一直在追踪解决技术的问题 - 我还有一篇关于将它们应用于特定难题的博客文章 - "穿越梯子".
为了达到目的,我偶然发现了z3,并尝试将其用于特定问题.我使用了Python绑定,并写了这个:
$ cat laddersZ3.py
#!/usr/bin/env python
from z3 import *
a = Int('a')
b = Int('b')
c = Int('c')
d = Int('d')
e = Int('e')
f = Int('f')
solve(
a>0, a<200,
b>0, b<200,
c>0, c<200,
d>0, d<200,
e>0, e<200,
f>0, f<200,
(e+f)**2 + d**2 == 119**2,
(e+f)**2 + c**2 == 70**2,
e**2 + 30**2 == a**2,
f**2 + 30**2 == b**2,
a*d == 119*30,
b*c == 70*30,
a*f - 119*e + a*e == …Run Code Online (Sandbox Code Playgroud) 在使用 CodePeer 分析 Ada 代码库时,该工具在执行以下操作的行报告“数组索引检查可能失败”:
Param(Param'First) := ....
Run Code Online (Sandbox Code Playgroud)
Param 是传入的out参数 - 它是数组类型。
我的理解是 Ada 在运行时携带数组大小和有效索引范围(为了执行范围检查)......我看不出任何 Ada 数组如何通过访问 index 来违反其范围检查array'First。
我错过了什么?
我在空闲时间学习Scala - 作为一个学习练习,我将一些我在另一个StackOverflow问题中编写的 OCaml代码翻译成Scala.由于我是Scala的新手,我很欣赏一些建议......
但在提出我的问题之前 - 这是原始的OCaml代码:
let visited = Hashtbl.create 200000
let rec walk xx yy =
let addDigits number =
let rec sumInner n soFar =
match n with
| x when x<10 -> soFar+x
| x -> sumInner (n/10) (soFar + n mod 10) in
sumInner number 0 in
let rec innerWalk (totalSoFar,listOfPointsToVisit) =
match listOfPointsToVisit with
| [] -> totalSoFar
| _ ->
innerWalk (
listOfPointsToVisit
(* remove points that we've already seen *)
|> …Run Code Online (Sandbox Code Playgroud) 在Python中,我使用了一个名为的库futures,它允许我以简洁明了的方式对N个工作进程池进行处理:
schedulerQ = []
for ... in ...:
workParam = ... # arguments for call to processingFunction(workParam)
schedulerQ.append(workParam)
with futures.ProcessPoolExecutor(max_workers=5) as executor: # 5 CPUs
for retValue in executor.map(processingFunction, schedulerQ):
print "Received result", retValue
Run Code Online (Sandbox Code Playgroud)
(这processingFunction是CPU绑定的,所以这里的异步机器没有意义 - 这是关于简单的旧算术计算)
我现在正在寻找在Scala中做同样事情的最接近的方法.请注意,在Python中,为了避免GIL问题,我使用了进程(因此使用了ProcessPoolExecutor代替ThreadPoolExecutor) - 并且库自动地将workParam参数封送到每个执行的流程实例processingFunction(workParam)- 并且它将结果封送回主进程,执行者的map循环消费.
这适用于Scala和JVM吗?我的processingFunction原则上也可以从线程执行(根本没有全局状态) - 但是我有兴趣看到多处理和多线程的解决方案.
问题的关键部分是JVM世界中是否有任何与futures上面看到的Python一样明确的API ...我认为这是我见过的最好的SMP API之一 - 准备一个列表使用所有调用的函数参数,然后只有两行:创建poolExecutor和map处理函数,一旦工作人员生成结果就返回结果.一旦第一次调用processingFunction返回结果,结果就会立即开始,直到它们全部完成为止 - 此时for循环结束.
今天我对此感到惊讶:
\n#include <stdlib.h> // for the 'exit' call\n\nint foo() {\n // return 0;\n}\n\nint main() {\n int res = foo();\n exit(res);\n}\nRun Code Online (Sandbox Code Playgroud)\n我知道忘记返回 ; 中的预期整数值不是好形式foo。但你会期望这段代码出现段错误吗?
以下是 GCC7.5 中发生的情况:
\n(thanassis)$ g++ -O3 -Wall a.cc\na.cc: In function \xe2\x80\x98int foo()\xe2\x80\x99:\na.cc:5:5: warning: no return statement in function returning non-void [-Wreturn-type]\n }\n ^\n\n(thanassis)$ ./a.out \n\n(thanassis)$ gdb ./a.out\nGNU gdb (Ubuntu 10.2-0ubuntu1~18.04~2) 10.2\nCopyright (C) 2021 Free Software Foundation, Inc.\nLicense GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is free software: you are free …Run Code Online (Sandbox Code Playgroud)