来自http://martintrojer.github.io/clojure/2013/07/07/coreasync-and-blocking-io/:
为了更具体一点,让我们看看当我们尝试使用core.async发出一些HTTP GET请求时会发生什么.让我们从天真的解决方案开始,通过clj-http使用阻塞IO.
Run Code Online (Sandbox Code Playgroud)(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 )))在这里,我们尝试使用go块(并阻塞IO)获取90个代码片段(并行).这需要很长时间,这是因为go块线程被长时间运行的IO操作"阻塞"了.通过将go块切换到普通线程可以改善这种情况.
Run Code Online (Sandbox Code Playgroud)(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 …
这是代码:
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) 我在遗传学包中找到了一行,如下所示:
P <- D <- Dprime <- nobs <- chisq <- p.value <- corr <- R.2 <- P
Run Code Online (Sandbox Code Playgroud)
注意P是在开头和结尾.这是什么意思?
我正在为命令行工具编写一个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)",它只完成当前工作路径中的文件名.有什么建议?
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)
他们看起来几乎一样.
从21世纪C书:
静态变量,甚至是函数内部的静态变量,在程序启动时,在main之前初始化,因此您无法使用非恒定值初始化它们.
Run Code Online (Sandbox Code Playgroud)//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));
我不明白宏有什么区别.预处理后它不是完全一样吗?
从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) 我试过这个:
#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) 我使用包中的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) r ×4
macros ×2
autocomplete ×1
bash ×1
c ×1
clojure ×1
core.async ×1
intellij-15 ×1
java ×1
javafx-8 ×1
rcpp ×1
rust ×1
sbt ×1
shell ×1