程序员似乎对如何异步通知错误感到分歧.
一些程序员喜欢使用带有两个参数的回调:一个值和一个布尔值,它告诉该值是否错误.这样做的好处在于它看起来像一个try catch声明:
asyncFunct(function (value, noError) {
if (noError) {
// success, do something with value
} else {
// value is the error which is thrown
}
});
Run Code Online (Sandbox Code Playgroud)
其他人更喜欢负面(即布尔值应该判断该值是否错误).他们的理由是,如果您知道异步函数永远不会抛出错误,那么您可以安全地省略第二个参数,如下所示:
asyncFunction(function (value, isErroneous) {
if (!isErrorneous) {
// success, do something with value
} else {
// value is the error which is thrown
}
});
asyncFunction(function (value) {
// success, do something with value
});
Run Code Online (Sandbox Code Playgroud)
然后有人提出单独的回调以成功执行异步函数和错误执行异步函数的错误.这允许程序员选择是否要处理回调,错误,两者或两者:
asyncFunction(function (value) {
// success, do something with value
}, function …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个弱正规化lambda项的例子.我是否正确地说以下内容:
(?a.b)((?x.xx)(?x.xx))
Run Code Online (Sandbox Code Playgroud)
减少到:
b
Run Code Online (Sandbox Code Playgroud)
要么:
没有终止(如果你试图减少(?x.xx)(?x.xx))
我不确定第一次减少是否正确所以只需要一些澄清,谢谢.
我想将以下JavaScript代码移植到Haskell:http://jsfiddle.net/mz68R/
这是我试过的:
import Control.Concurrent
import Data.IORef
type EventStream a = IORef [MVar a]
newEventStream :: IO (EventStream a)
newEventStream = newIORef []
setEvent :: EventStream a -> a -> IO ()
setEvent stream event = readIORef stream >>= mapM_ (`putMVar` event)
getEvent :: EventStream a -> (a -> IO b) -> IO ThreadId
getEvent stream listener = do
event <- newEmptyMVar
modifyIORef stream (++ [event])
forkIO $ loop (takeMVar event >>= listener)
loop :: Monad m => …Run Code Online (Sandbox Code Playgroud) 我想生成一个非常大的随机数。我不需要这个数字是加密安全的。因此,我没有使用crypto.getRandomValues. 目前,我生成的随机数如下:
const random = length =>
Math.floor(length * Math.random());
const padding = (length, character, string) =>
(new Array(length + 1).join(character) + string).slice(string.length);
const randomBits = bits =>
padding(bits, '0', random(Math.pow(2, bits)).toString(2));
const getRandom = bits =>
bits <= 32 ? randomBits(bits) : randomBits(32) + getRandom(bits - 32);
console.log(' 1 2 3 4 5 6');
console.log(getRandom(64));Run Code Online (Sandbox Code Playgroud)
然而,这似乎有点浪费,因为 JavaScript 中的数字是64 位长的:
![]()
在我看来,我们至少应该能够恢复尾数的所有 52 位。我们可以从Math.randomJavaScript生成的数字中提取多少位熵,以及如何提取?
假设我们有一种编程语言ℤ,它具有以下语法:
? := 0 | 1 | (+ ? ?) | (* ? ?) | (- ? ?) | (max ? ?)
Run Code Online (Sandbox Code Playgroud)
为方便起见,我们可以使用以下语言定义新的绑定表单:
(not x) = (- 1 x)(abs x) = (- (max 0 (+ x x)) x)(min x y) = (- 0 (max (- 0 x) (- 0 y)))(nil x) = (not (min 1 (abs x)))这种语言足以表达分支和比较运算符:
(if x y z) = (+ (* x y) (* (not x) z))(eq x y) = (nil (- …考虑以下程序。
import Control.Monad.State
import Control.Monad.Catch
ex1 :: StateT Int IO ()
ex1 = do
modify (+10)
liftIO . ioError $ userError "something went wrong"
ex2 :: StateT Int IO ()
ex2 = do
x <- get
liftIO $ print x
ex3 :: StateT Int IO ()
ex3 = ex1 `onException` ex2
main :: IO ()
main = evalStateT ex3 0
Run Code Online (Sandbox Code Playgroud)
当我们运行程序时,我们得到以下输出。
$ runhaskell Test.hs
0
Test.hs: user error (something went wrong)
Run Code Online (Sandbox Code Playgroud)
但是,我预计输出如下。
$ runhaskell Test.hs
10
Test.hs: user error (something …Run Code Online (Sandbox Code Playgroud) 我正在Jison中编写一个简单的表达式解析器.这是我的语法:
{
"operators": [
["left", "+", "-"],
["left", "*", "/", "%"]
],
"bnf": {
"program": [
["statement EOF", "return $1;"]
],
"statement": [
["expression NEWLINE", "$$ = $1 + ';';"]
],
"expression": [
["NUMBER", "$$ = yytext;"],
["expression binary expression", "$$ = $1 + $2 + $3;"]
],
"binary": [
["+", "$$ = ' + ';"],
["-", "$$ = ' - ';"],
["*", "$$ = ' * ';"],
["/", "$$ = ' / ';"],
["%", "$$ = ' % ';"], …Run Code Online (Sandbox Code Playgroud) 我正在使用Jison编写解析器.这是我的语法:
{
"program": [
["statements EOF", "return $1;"]
],
"statements": [
["statement", "$$ = $1;"],
["statements statement", "$$ = $1 + '\\n' + $2;"]
],
"statement": [
["expression NEWLINE", "$$ = $1 + ';';"]
],
"expression": [
["NUMBER", "$$ = yytext;"],
["expression expression", "$$ = $1 + ', ' + $2;"]
]
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我收到以下错误消息:
Conflict in grammar: multiple actions possible when lookahead token is NUMBER in
state 9
- reduce by rule: expression -> expression expression
- shift token (then …Run Code Online (Sandbox Code Playgroud) 我昨天刚刚了解了Logo.出生于九十年代,我以前从未见过它.所以我开始使用由Joshua Bell编写的在线Logo解释器,我决定编写一个circle函数来制作同心圆.这就是我写的:
cs
to circle
penup forward :radius right 90
pendown repeat 360 [forward 3.14 * :radius / 180 right 1]
penup left 90 back :radius pendown
end
make "radius 30
repeat 160 [circle make "radius :radius + 30]
Run Code Online (Sandbox Code Playgroud)
有趣的是,绘图画布是环形阵列.因此,圆圈最终重叠.通过绘制160个同心圆,半径增加到30的倍数,我最终获得了这样的图像:

这真是令人惊讶.乍一看,它看起来像一张真实的夜空照片,它让我思考 - 是否有一种绘制星空的通用算法?
顺便说一句,如果你仔细观察,你可以看到30 x 30像素正方形的网格.边界是黑色的,所以有点难以注意到.
Ross Paterson:Arrows and Computation介绍了该trace功能(第11页):
trace :: ((a, c) -> (b, c)) -> a -> b
trace f a = let (b, c) = f (a, c) in b
Run Code Online (Sandbox Code Playgroud)
该trace功能对于模拟循环程序中的魔术反馈步骤非常有用.例如,考虑Richard Bird的着名repmin函数,该函数查找树的最小叶值并创建一个相同的树,每个叶子值都替换为最小叶值,通过巧妙地使用惰性求值和局部递归(如提供者letrec):
data Tree = Leaf Int | Node Tree Tree deriving Show
repmin :: Tree -> Tree
repmin = trace repmin'
repmin' :: (Tree, Int) -> (Tree, Int)
-- put the minimum value m into the leaf …Run Code Online (Sandbox Code Playgroud) javascript haskell functional-programming lazy-evaluation tying-the-knot