小编Aad*_*hah的帖子

使用errback有什么好处?

程序员似乎对如何异步通知错误感到分歧.

一些程序员喜欢使用带有两个参数的回调:一个值和一个布尔值,它告诉该值是否错误.这样做的好处在于它看起来像一个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)

javascript callback

5
推荐指数
1
解决办法
1600
查看次数

Lambda微积分(λa.b)((λx.xx)(λx.xx))

我正在寻找一个弱正规化lambda项的例子.我是否正确地说以下内容:

(?a.b)((?x.xx)(?x.xx))
Run Code Online (Sandbox Code Playgroud)

减少到:

b
Run Code Online (Sandbox Code Playgroud)

要么:

没有终止(如果你试图减少(?x.xx)(?x.xx))

我不确定第一次减少是否正确所以只需要一些澄清,谢谢.

lambda haskell functional-programming lambda-calculus

5
推荐指数
1
解决办法
431
查看次数

使用MVars在Haskell中实现事件流

我想将以下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)

javascript concurrency haskell

5
推荐指数
1
解决办法
202
查看次数

Math.random 产生多少熵?

我想生成一个非常大的随机数。我不需要这个数字是加密安全的。因此,我没有使用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 位长的:

IEEE 754 双精度二进制浮点格式:binary64

在我看来,我们至少应该能够恢复尾数的所有 52 位。我们可以从Math.randomJavaScript生成的数字中提取多少位熵,以及如何提取?

javascript random precision

5
推荐指数
1
解决办法
1172
查看次数

整数除法仅使用加法,乘法,减法和最大值

假设我们有一种编程语言ℤ,它具有以下语法:

? := 0 | 1 | (+ ? ?) | (* ? ?) | (- ? ?) | (max ? ?)
Run Code Online (Sandbox Code Playgroud)

为方便起见,我们可以使用以下语言定义新的绑定表单:

  1. (not x) = (- 1 x)
  2. (abs x) = (- (max 0 (+ x x)) x)
  3. (min x y) = (- 0 (max (- 0 x) (- 0 y)))
  4. (nil x) = (not (min 1 (abs x)))

这种语言足以表达分支和比较运算符:

  1. (if x y z) = (+ (* x y) (* (not x) z))
  2. (eq x y) = (nil (- …

algorithm math logic integer lambda-calculus

5
推荐指数
1
解决办法
205
查看次数

如何在 IO 异常处理程序中保留 monad 堆栈的状态?

考虑以下程序。

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)

haskell exception state-monad io-monad

5
推荐指数
1
解决办法
76
查看次数

这个语法怎么模棱两可?

我正在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)

javascript parsing bison parser-generator jison

4
推荐指数
1
解决办法
713
查看次数

在Jison中调试

我正在使用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)

javascript parsing bison parser-generator jison

4
推荐指数
1
解决办法
882
查看次数

生成星空的算法

我昨天刚刚了解了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的倍数,我最终获得了这样的图像:

标志星空由160个同心圆生成,半径增加30倍.

这真是令人惊讶.乍一看,它看起来像一张真实的夜空照片,它让我思考 - 是否有一种绘制星空的通用算法?

顺便说一句,如果你仔细观察,你可以看到30 x 30像素正方形的网格.边界是黑色的,所以有点难以注意到.

javascript algorithm graphics canvas logo-lang

4
推荐指数
2
解决办法
1440
查看次数

功能珍珠:在JavaScript中实现跟踪

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

4
推荐指数
1
解决办法
157
查看次数