小编qed*_*qed的帖子

在core.async中阻止vs线程

来自http://martintrojer.github.io/clojure/2013/07/07/coreasync-and-blocking-io/:

为了更具体一点,让我们看看当我们尝试使用core.async发出一些HTTP GET请求时会发生什么.让我们从天真的解决方案开始,通过clj-http使用阻塞IO.

(defn blocking-get [url]
  (clj-http.client/get url))


(time
   (def data
     (let [c (chan)
           res (atom [])]
       ;; fetch em all
       (doseq [i (range 10 100)]
         (go (>! c (blocking-get (format "http://fssnip.net/%d" i)))))
       ;; gather results
       (doseq [_ (range 10 100)]
         (swap! res conj (<!! c)))
       @res
       )))
Run Code Online (Sandbox Code Playgroud)

在这里,我们尝试使用go块(并阻塞IO)获取90个代码片段(并行).这需要很长时间,这是因为go块线程被长时间运行的IO操作"阻塞"了.通过将go块切换到普通线程可以改善这种情况.

(time
   (def data-thread
     (let [c (chan)
           res (atom [])]
       ;; fetch em all
       (doseq [i (range 10 100)]
         (thread (>!! c (blocking-get (format "http://fssnip.net/%d" i)))))
       ;; gather results
       (doseq …
Run Code Online (Sandbox Code Playgroud)

multithreading clojure core.async

8
推荐指数
1
解决办法
2042
查看次数

JavaFX ObservableList的回调和提取器

这是代码:

package sample;

import javafx.beans.Observable;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.util.Callback;

import java.util.List;

/**
 * Created by IDEA on 28/07/15.
 */
public class ListUpdateTest {
    public static void main(String[] args) {
        Callback<IntegerProperty, Observable[]> extractor = (IntegerProperty p) -> {
            System.out.println("The extractor is called.");
            Observable[] res = new Observable[]{p};
            System.out.println("Result from extractor: " + res);
            return res;
        };
        ObservableList<IntegerProperty> list = FXCollections.observableArrayList(extractor);
        System.out.printf("Before adding");
        IntegerProperty p1 = new SimpleIntegerProperty(10);
        IntegerProperty p2 = new SimpleIntegerProperty(20);
        list.addAll(p1, …
Run Code Online (Sandbox Code Playgroud)

java javafx-8

8
推荐指数
1
解决办法
3941
查看次数

R中的循环分配

我在遗传学包中找到了一行,如下所示:

P <- D <- Dprime <- nobs <- chisq <- p.value <- corr <- R.2 <- P
Run Code Online (Sandbox Code Playgroud)

注意P是在开头和结尾.这是什么意思?

r

7
推荐指数
1
解决办法
125
查看次数

Bash Completion脚本用于在某些参数选项后完成文件路径

我正在为命令行工具编写一个bash完成脚本:

_plink()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="--1 --23file --a1-allele --a2-allele --adjust --aec"

    if [[ ${cur} == --*file ]] || [[ ${cur} == --out ]]; then
        COMPREPLY=( $(compgen -W "$(ls)" -- ${cur}) )
    elif [[ ${cur} == -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}
complete -F _plink plink1.9
Run Code Online (Sandbox Code Playgroud)

这个想法是,如果选项--*file--outbash之后应该自动完成文件路径.现在我正在使用" $(ls)",它只完成当前工作路径中的文件名.有什么建议?

bash shell autocomplete

7
推荐指数
1
解决办法
3692
查看次数

R中引号和表达式的区别

gettypes = function(x) {paramx = substitute(x); print(typeof(paramx)); print(class(paramx)); print(mode(paramx))}
gettypes(expression(sin(x+y)))
# [1] "language"
# [1] "call"
# [1] "call"
gettypes(quote(sin(x+y)))
# [1] "language"
# [1] "call"
# [1] "call"
expression(sin(x+y)) == quote(sin(x+y))
Run Code Online (Sandbox Code Playgroud)

错误expression(sin(x + y)) == quote(sin(x + y)):表达式不允许比较

identical(expression(sin(x+y)), quote(sin(x+y)))
# [1] FALSE
x = 1
y = 2
eval(expression(sin(x+y)))
# [1] 0.14112
eval(quote(sin(x+y)))
# [1] 0.14112
Run Code Online (Sandbox Code Playgroud)

他们看起来几乎一样.

r

7
推荐指数
1
解决办法
1244
查看次数

使用非恒定值初始化静态变量

从21世纪C书:

静态变量,甚至是函数内部的静态变量,在程序启动时,在main之前初始化,因此您无法使用非恒定值初始化它们.

//this fails: can't call gsl_vector_alloc() before main() starts
static gsl_vector *scratch = gsl_vector_alloc(20);
Run Code Online (Sandbox Code Playgroud)

这是一个烦恼,但很容易用宏从零开始解决并在首次使用时分配:

#define Staticdef(type, var, initialization) \ 
static type var = 0; \
if (!(var)) var = (initialization);

//usage:
Staticdef(gsl_vector*, scratch, gsl_vector_alloc(20));
Run Code Online (Sandbox Code Playgroud)

我不明白宏有什么区别.预处理后它不是完全一样吗?


c macros

7
推荐指数
1
解决办法
178
查看次数

什么是intellij中的root-build?

每当我在intellij中创建一个sbt项目时,我会得到这样的结果:

在此输入图像描述

什么在root-build这里,为什么它被标记为"源根"?

intellij-idea sbt intellij-15

7
推荐指数
1
解决办法
1405
查看次数

Iterator :: unzip中SizeHint的目的是什么?

从Rust标准库的实现unzip:

fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB) where
    FromA: Default + Extend<A>,
    FromB: Default + Extend<B>,
    Self: Sized + Iterator<Item=(A, B)>,
{
    struct SizeHint<A>(usize, Option<usize>, marker::PhantomData<A>);
    impl<A> Iterator for SizeHint<A> {
        type Item = A;

        fn next(&mut self) -> Option<A> { None }
        fn size_hint(&self) -> (usize, Option<usize>) {
            (self.0, self.1)
        }
    }

    let (lo, hi) = self.size_hint();
    let mut ts: FromA = Default::default();
    let mut us: FromB = Default::default();

    ts.extend(SizeHint(lo, hi, marker::PhantomData));
    us.extend(SizeHint(lo, hi, …
Run Code Online (Sandbox Code Playgroud)

rust

7
推荐指数
2
解决办法
754
查看次数

如何在Rcpp中向对象添加多个类?

我试过这个:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector difflag(NumericVector x, int lag) {
    int n = x.size();
    NumericVector out(n-lag);

    for(int i=0; i<(n-lag); i++) {
        out[i] = x[i+lag] - x[i];
    }

    out.attr("class") += "myclass";
    return out;
}
Run Code Online (Sandbox Code Playgroud)

它给了我一个错误:

all.cpp: In function ‘Rcpp::NumericVector difflag(Rcpp::NumericVector, int)’:
all.cpp:64:26: error: no match for ‘operator+=’ in ‘Rcpp::RObject::attr(const string&) const((* & std::basic_string<char>(((const char*)"class"), (*(const std::allocator<char>*)(& std::allocator<char>()))))) += "myclass"’
make: *** [all.o] Error 1
Run Code Online (Sandbox Code Playgroud)

r rcpp

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

在R中使用宏是否安全?

我使用包中的defmacro函数在R中编写了一些宏gtools:

#' IfLen macro
#' 
#' Check whether a object has non-zero length, and 
#' eval expression accordingly.
#' 
#' @param df An object which can be passed to \code{length}
#' @param body1 If \code{length(df)} is not zero, then this clause is evaluated, otherwise, body2 is evaluated.
#' @param body2 See above.
#' @importFrom gtools defmacro
#' 
#' @examples 
#' ifLen(c(1, 2), { print('yes!') }, {print("no!")})
#' 
#' @author kaiyin
#' @export
ifLen = gtools::defmacro(df, body1, …
Run Code Online (Sandbox Code Playgroud)

macros r

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