我需要在sqlite数据库中计算欧氏距离.
除了为数学函数编写和加载动态库之外,有谁知道如何在sqlite中计算平方根?
我接近于在http://en.wikipedia.org/wiki/Fast_inverse_square_root中使用快速反平方根算法,尽管它现在可能变得比我需要的更有趣.
作为旁注,最好弄明白如何做电源(这是一个普遍的问题,而且编码比自己乘以一个数字更清晰).
谢谢,
西蒙娜
我知道我可以toString()在一个对象上创建一个函数,这样每次打印或处理它就像一个字符串时,它将首先使用该函数对该对象进行字符串化.
有可能直接这样做,所以我可以在对象上使用String对象函数吗?
var SomeObject = function(a, b){
this.a = a;
this.b = b
}
SomeObject.prototype.toString = function(){
return [ this.a, this.b ].join(' ')
}
var objInstance = new SomeObject('this', 'that');
console.log(objInstance + '') // This that
console.log(("" + objInstance).split('')) // [ 't', 'h', 'i', 's', ' ', 't', 'h', 'a', 't' ]
console.log(objInstance.split()) // Error
Run Code Online (Sandbox Code Playgroud)
当调用String函数时,是否可以这样做,使对象"行为"像一个字符串?
换句话说,我希望得到与之objInstance.split()相同的结果("" + objInstance).split(''),objInstance.length或者objInstance.match(/something/)等等.
我想用d3附加一个文字SVG元素.
所以不要写作
svg.selectAll("circle")
.data(data)
.enter()
.append("circle") // etc etc
Run Code Online (Sandbox Code Playgroud)
我想这样做:
svg.selectAll("circle")
.data(data)
.enter()
.append('<circle cx="158.9344262295082" cy="200" r="16" fill="red"></circle>')
Run Code Online (Sandbox Code Playgroud)
这样我就可以在别处创建一个复杂的模板(例如用把手),然后用数据编译并附加它.
在DBIx :: Class中查找类的表名是很简单的,就像这样
my $s = DBIx::Class::Schema::Loader->connect('dbi:SQLite:foo.db');
$s->class($class_name)->table;
Run Code Online (Sandbox Code Playgroud)
但是我怎么能做相反的事情,并从数据库中的表名获取类名?
我正在尝试构建一个bookmarklet,将当前url作为参数提供给另一个url.
但是我发现了这一点
javascript:(function(){window.open("http://www.somesi.te/some/thing?url="+encodeURIComponent(window.location.href))})()
Run Code Online (Sandbox Code Playgroud)
这不起作用
javascript:(function(){window.location.href = "http://www.somesi.te/some/thing?url="+encodeURIComponent(window.location.href)})()
Run Code Online (Sandbox Code Playgroud)
确实.除了window.open打开另一个窗口的明显区别,并window.location.href改变位置,为什么后者工作,而前者只是打开另一个窗口到原始位置?
这是在Firefox上.有趣的是,在Chrome上工作正常.
这是安全的事吗?
我想从模板内部访问Mojolicious中的模板名称以进行调试,就像Template Toolkit一样(参见这里)
变量__FILE__工作得很整齐,但它引用的是当前文件,而不是顶级模板,这意味着它在布局模板中无用.
我也试过了
<%= app->renderer->template_name %>
Run Code Online (Sandbox Code Playgroud)
但没有结果
在Mojolicious有可能吗?
在angular我发现你可以将模板绑定到一个返回数组的函数,如下所示:
<div class="cal_row" id ="id_{{task.id}}" ng-repeat="task in calendar.filtered()">
<div class="id">{{task.id}}</div>
<div class="task">{{task.task}}</div>
<div class="start">{{task.start}}</div>
<div class="finish">{{task.finish}}</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这很酷,因为这样我可以避免为了维护数据的过滤版本而不得不保留变量.
但是,我也松开了与原始数据的绑定:当底层数据发生变化时,我似乎无法获得angular.js来发现更改,并更新视图.
有没有办法做到这一点?我试图在文档中找到任何东西,但不能
非常感谢
为什么我得到的图像数据是WebGLRenderingContext.readPixels()颠倒的?
我尝试执行以下操作:
var gl = renderer.domElement.getContext("webgl")
var pixels = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4);
gl.readPixels(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
var imageData = new ImageData(Uint8ClampedArray.from(pixels), gl.drawingBufferWidth, gl.drawingBufferHeight);
ctx.putImageData(imageData, 0, 0);
Run Code Online (Sandbox Code Playgroud)
但结果是沿 x 轴镜像的图像(即:倒置)。
我也试过在ctx.putImageData这样之后使用比例:
ctx.scale(1, -1);
Run Code Online (Sandbox Code Playgroud)
但没有结果。反转像素也不起作用。
现在我明白 putImageData() 使用从左上角开始的坐标,而 readPixels() 从左下角开始。
有没有人有关于如何翻转图像或完全避免问题的建议?
我可以从这个答案看到,如果我这样做
sub match_all_positions {
my ($regex, $string) = @_;
my @ret;
while ($string =~ /$regex/g) { push @ret, $-[0] }
return @ret
}
print join ',', match_all_positions('0{3}', '001100010000');
Run Code Online (Sandbox Code Playgroud)
我明白了
4,8
Run Code Online (Sandbox Code Playgroud)
我需要做什么来获得所有匹配的索引,即使重叠,例如上面示例中的位置8和9?
我可以
sub match_all_positions_b {
my ($substr, $string) = @_;
return unless index($string, $substr) > 0;
my @res;
my $i = 0;
while ($i <= (length($string) - $length)) {
$i = index($string, $substr, $i);
last if $i < 0;
push @res, $i++;
}
return @res;
} …Run Code Online (Sandbox Code Playgroud) 我希望能够像这样使用一个函数:
my @grouped = split_at {
$a->{time}->strftime("%d-%b-%Y %H") ne
$b->{time}->strftime("%d-%b-%Y %H")
} @files;
Run Code Online (Sandbox Code Playgroud)
wheresplit_at根据函数将数组拆分为arrayrefs数组,如下所示:
sub split_at(&@) {
# split an array into an arrayrefs based on a function
my $cb = shift;
my @input = @_;
my @retval = ( [ ] );
$i = 0;
while ($i <= $#input) {
push @{$retval[$#retval]}, $input[$i];
$i++;
if (($i < $#input) && $cb->($input[$i-1], $input[$i])) { push @retval, [] }
}
pop @retval unless @{$retval[$#retval]};
return @retval;
}
Run Code Online (Sandbox Code Playgroud)
现在我只能这样称呼它:
my @grouped …Run Code Online (Sandbox Code Playgroud) javascript ×4
perl ×4
angularjs ×1
canvas ×1
d3.js ×1
dbix-class ×1
firefox ×1
math ×1
mojolicious ×1
regex ×1
sql ×1
sqlite ×1
svg ×1
webgl ×1