我们应该知道,Clojure map可以应用于一个序列:
(map #(* %1 %1) [1 2 3]) ; (1)
Run Code Online (Sandbox Code Playgroud)
..或多个,以这种方式:
(map vector [0 1] [2 1]) ; (2)
;=> ([0 2] [1 1])
Run Code Online (Sandbox Code Playgroud)
现在我想获得与(2)相同的结果,但我将参数存储在序列中.换句话说,以下内容未给出所需的结果:
(map vector [[0 1] [2 1]]) ; (3)
;=> ([[0 1]] [[2 1]])
Run Code Online (Sandbox Code Playgroud)
所以我写了这个简单的宏,其中umap代表"unsplice map":
(defmacro umap [fun args-list]
"umap stands for unspliced map.
Let args-list be a list of args [a1 a2 ... an].
umap is the same of (map fun a1 a2 .. an)"
`(map ~fun ~@args-list))
Run Code Online (Sandbox Code Playgroud)
显然它就像一个魅力:
(umap vector [[0 …Run Code Online (Sandbox Code Playgroud) 我正在玩一些Haskell代码.我已经定义了两个函数:
count :: [a] -> Int
count [] = 0
count (x:xs) = 1 + (count xs)
-- 03. Write a function that computes the mean of a list, i.e., the sum of all
-- elements in the list divided by its length. (You may need to use the
-- fromIntegralfunction to convert the length of the list from an integer
-- into a floating-point number.)
-- I've guessed this type definition, but it's incorrect:
-- listMean :: [a] -> …Run Code Online (Sandbox Code Playgroud) 我正在评估在Erlang中绑定我的C++项目.我的项目大量使用模板和方法重载,所以有这样的事情并不罕见:
template <typename T, class Iterator = BufferIterator<T> >
class Buffer
{
public:
[...]
private:
[...]
};
Run Code Online (Sandbox Code Playgroud)
我听说Erlang中的计算模型与"传统"编程语言略有不同.在Erlang中,节点似乎是第一类组件,其中消息从Node流向另一个节点.在这种情况下,有可能,例如:"这是一个int列表.将它发送到C++节点,它将它转换为Buffer <int>对象,对它执行一些操作,然后将结果转换回一个新的Erlang列表"?我在网上看到过一些像tinch ++这样的项目,看起来很有希望,但根本不稳定.每种提示,链接或代码片段都会非常受欢迎.
提前谢谢,A.
我对malloc有一个奇怪的问题.我有这个typedef:
typedef struct buffer {
int D;
int T;
unsigned int current_size;
unsigned int max_size;
pthread_mutex_t mutex;
pthread_cond_t non_pieno;
pthread_cond_t non_vuoto;
msg_t** messages;
struct buffer* (*buffer_init)(unsigned int);
void (*buffer_destroy)(struct buffer*);
} buffer_t;
Run Code Online (Sandbox Code Playgroud)
这是buffer_init和buffer_destroy函数:
buffer_t* buffer_init(unsigned int maxsize)
{
buffer_t* new_buffer = (buffer_t*)malloc(sizeof(buffer_t));
msg_t** new_messages = (msg_t**)calloc(maxsize, sizeof(msg_t**));
pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t np = PTHREAD_COND_INITIALIZER;
pthread_cond_t nv = PTHREAD_COND_INITIALIZER;
new_buffer->T = 0;
new_buffer->D = 0;
new_buffer->current_size = 0;
new_buffer->max_size = maxsize;
new_buffer->messages = new_messages;
new_buffer->mutex = mx;
new_buffer->non_pieno = np; …Run Code Online (Sandbox Code Playgroud)