小编Aad*_*hah的帖子

使用自定义原型实例化JavaScript函数

我使用以下函数从一组参数创建JavaScript中的函数实例:

var instantiate = function (instantiate) {
    return function (constructor, args, prototype) {
        "use strict";

        if (prototype) {
            var proto = constructor.prototype;
            constructor.prototype = prototype;
        }

        var instance = instantiate(constructor, args);
        if (proto) constructor.prototype = proto;
        return instance;
    };
}(Function.prototype.apply.bind(function () {
    var args = Array.prototype.slice.call(arguments);
    var constructor = Function.prototype.bind.apply(this, [null].concat(args));
    return new constructor;
}));
Run Code Online (Sandbox Code Playgroud)

使用上面的函数,您可以按如下方式创建实例(请参阅小提琴):

var f = instantiate(F, [], G.prototype);

alert(f instanceof F); // false
alert(f instanceof G); // true

f.alert(); // F

function F() { …
Run Code Online (Sandbox Code Playgroud)

javascript prototype instantiation

6
推荐指数
1
解决办法
849
查看次数

基于寄存器+堆栈的虚拟机如何工作?

我知道如何基于寄存器以及基于堆栈的虚拟机如何独立工作.我知道两者的优点和缺点.我想知道的是,有没有人试图合并这两者?

我试图在网上搜索这种虚拟机的存在,但无济于事.我得到的最好结果是一篇关于混合虚拟机(HyVM)的文章.如果确实为编程语言创建了这样的虚拟机,我将有兴趣查看其源代码以了解它是如何工作的.

也许有人可以指出我找到这样一个虚拟机的正确方向,或者将我链接到本主题中详述的文章或博客文章.

register-allocation stack-based cpu-registers ssa vm-implementation

6
推荐指数
1
解决办法
586
查看次数

Haskell中的传感器和单态限制

我在Haskell中实现了传感器,如下所示:

{-# LANGUAGE RankNTypes #-}

import Prelude hiding (foldr)
import Data.Foldable

type Reducer b a = a -> b -> b
type Transducer a b = forall t. Reducer t b -> Reducer t a

class Foldable c => Collection c where
    insert :: a -> c a -> c a
    empty  :: c a

reduce :: Collection c => Transducer a b -> c a -> c b
reduce f = foldr (f insert) empty

mapping :: (a -> …
Run Code Online (Sandbox Code Playgroud)

haskell transducer monomorphism-restriction higher-rank-types

6
推荐指数
1
解决办法
289
查看次数

具有Flow类型注释的Mixins

我正在项目中使用ES6和Flow类型检查器.

假设我有两个类型别名,仅根据期望的方法定义(如Java接口):

type Airplane = {
    takeOff: (() => void);
    land: (() => void);
};

type Car = {
    drive: ((speed: number) => void);
};
Run Code Online (Sandbox Code Playgroud)

我如何定义一个类FlyingCar来向类型检查器演示它既是a Car又是Airplane?我正在使用ECMAScript 6类.

对于我怀疑它看起来像这样的类型:

type FlyingCar = (Airplane & Car);
Run Code Online (Sandbox Code Playgroud)

但是,我似乎无法调和我想要的类语法,因为它似乎与ES6的类语法联系在一起.

javascript ecmascript-6 flowtype

6
推荐指数
1
解决办法
935
查看次数

Haskell中命名字段的令人惊讶的类型推断

请考虑以下GHCi的转录本,版本8.2.2:

GHCi, version 8.2.2: http://www.haskell.org/ghc/  :? for help
Prelude> :set -XRankNTypes
Prelude> data Functor f = Functor { fmap :: forall a b. (a -> b) -> f a -> f b }
Prelude> :t fmap
fmap :: Functor f1 -> (a -> b) -> f2 a -> f2 b
Prelude> :t Functor map
Functor map :: Functor []
Prelude> :t fmap (Functor map)
fmap (Functor map) :: (a -> b) -> [a] -> [b]
Run Code Online (Sandbox Code Playgroud)

如您所见,fmap推断的类型为Functor f1 …

haskell type-inference

6
推荐指数
1
解决办法
74
查看次数

如何配置gcc默认使用-no-pie?

我想在Linux上编译以下程序:

    .global _start
    .text
_start:
    mov $1,   %rax
    mov $1,   %rdi
    mov $msg, %rsi
    mov $13,  %rdx
    syscall
    mov $60,  %rax
    xor %rdi, %rdi
    syscall
msg:
    .ascii "Hello World!\n"
Run Code Online (Sandbox Code Playgroud)

但是,它给了我以下链接器错误:

$ gcc -nostdlib hello.s
/usr/bin/ld: /tmp/ccMNQrOF.o: relocation R_X86_64_32S against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

我认为它不起作用的原因是 gcc-pie默认使用生成共享对象。因此,使用-no-pie修复它:

$ gcc -no-pie -nostdlib hello.s
$ ./a.out …
Run Code Online (Sandbox Code Playgroud)

configuration gcc gnu-assembler ld position-independent-code

6
推荐指数
1
解决办法
2万
查看次数

伪经典继承相对于功能继承(工厂函数)有哪些技术优势?

替代标题:"为什么这么多流行的JavaScript库使用伪经典继承而不是功能继承(工厂函数)?"

JavaScript:Good Parts建议使用工厂功能,以便您获得真正的方法和属性隐私.这是有道理的,所以我想知道为什么这么多现代JS库仍然使用伪造的经典继承(使用new关键字).与工厂功能相比,这条路线有一些技术优势吗?如果没有,它只是一种风格选择吗?

编辑:这不是一个基于意见的帖子.我不是问哪个更好,我问的是伪古典继承有哪些技术优势超过功能,以了解为什么有人会选择那种风格.

编辑2:伪古典的几个优点我可以看到:

  1. 当您console.log是原型的实例时,它会显示它所实例化的对象的名称.
  2. 在相关的说明中,您可以使用instanceof查看实例是否来自特定对象,这是工厂函数无法实现的.

javascript inheritance prototype

6
推荐指数
1
解决办法
100
查看次数

使用宏在Scheme中柯里化函数

我正在学习Scheme 中的宏系统,我认为实现柯里化函数将是一个好的开始。这是我煮的:

(define-syntax function
    (syntax-rules ()
        ((_ () body ...) (lambda () body ...))
        ((_ (param) body ...) (lambda (param) body ...))
        ((_ (param_1 param_2 params ...) body ...) (lambda (param_1 . rest)
            (let ((k (function (param_2 params ...) body ...)))
                (if (null? rest) k (apply k rest)))))
        ((_ name params body ...) (define name (function params body ...)))))
Run Code Online (Sandbox Code Playgroud)

这段代码按预期工作。例如我可以定义一个add函数如下:

(function add (x y) (+ x y))
Run Code Online (Sandbox Code Playgroud)

然后我就可以正常调用它了:

(add 2 3) ; => 5
Run Code Online (Sandbox Code Playgroud)

此外,我可以轻松地部分应用它:

(map (add 10) …
Run Code Online (Sandbox Code Playgroud)

lisp macros scheme define-syntax currying

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

使用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
查看次数