将非负数Integer转换为其数字列表通常如下所示:
import Data.Char
digits :: Integer -> [Int]
digits = (map digitToInt) . show
Run Code Online (Sandbox Code Playgroud)
我试图找到一种更直接的方式来执行任务,而不涉及字符串转换,但我无法想出更快的东西.
到目前为止我一直在尝试的事情:
基线:
digits :: Int -> [Int]
digits = (map digitToInt) . show
Run Code Online (Sandbox Code Playgroud)
从StackOverflow上的另一个问题得到这个:
digits2 :: Int -> [Int]
digits2 = map (`mod` 10) . reverse . takeWhile (> 0) . iterate (`div` 10)
Run Code Online (Sandbox Code Playgroud)
试着自己动手:
digits3 :: Int -> [Int]
digits3 = reverse . revDigits3
revDigits3 :: Int -> [Int]
revDigits3 n = case divMod n 10 of
(0, digit) -> [digit]
(rest, …Run Code Online (Sandbox Code Playgroud) 在Java中,"二进制代码"是否与"Java字节码"相同?
这是Java中的流程吗?
Java文件(.java) - > [javac] - > ByteCode文件(.class) - > [JVM/Java解释器] - >运行它(首先将其转换为特定于机器的二进制代码)
谢谢!
我的代码如下:
CATransition *transition = [CATransition animation];
transition.duration = duration
Run Code Online (Sandbox Code Playgroud)
我希望得到CATransition/Animation的结束事件.可能吗?
试图实现一个shell,主要是管道.我已经编写了这个测试用例,我希望将其简单地传递给wc ......它肯定不能按预期工作.它将ls打印到终端然后打印耗尽的内存.我很失落如何解决这个问题并让它发挥作用.find_path适用于我的所有测试.
编辑 - 我必须使用execv为项目,它是一个类的东西,但我已经尝试使用execvp以防万一,它完全相同的事情.这也只是一个例子,一个测试,看看为什么它不起作用,我为fork和waitpid调用fork两次,因为我没有别的事可做.
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
int find_path(char* execname, char** dst)
{
char *path = getenv("PATH");
path = strdup(path);
char *pos;
path = strtok_r(path, ":", &pos);
char *originalpath = path;
do
{
char* test = (char*)calloc(strlen(path) + strlen(execname) + 2, sizeof(char));
test = strcpy(test, path);
int testlen = strlen(test);
(*(test+testlen)) = '/';
strcpy(test + testlen + 1,execname);
struct stat buf;
int result = stat(test, &buf);
if (result == 0) …Run Code Online (Sandbox Code Playgroud) 使用Raphael JavaScript库时,如何找到纸张/画布的绝对位置?
例如,假设最小工作示例如下:
<html>
<head>
<script type="text/javascript" src="raphael.js"></script>
<script>
window.onload = function() {
var size = 600;
var paper = new Raphael(document.getElementById("canvas_container"),
size, size);
var c = paper.circle(100, 100, 50).attr({fill: "#00f"});
var x = 0; // get the paper's absolute x coordinate
var y = 0; // get the paper's absolute y coordinate
document.getElementById("coordinates").innerHTML = x + " " + y;
};
</script>
<style type="text/css">
#canvas_container {
width: 600px;
margin-left: auto;
margin-right: auto;
border: 1px solid #aaa;
}
</style>
</head> …Run Code Online (Sandbox Code Playgroud) 可能是为时已晚,但我发现至少好奇以下几行似乎导致了分段错误,当且仅当使用gcc的优化编译时,甚至是"-O1"!
settings_dialog = gtk_dialog_new_with_buttons("gatotray Settings"
, NULL, 0, GTK_STOCK_CANCEL, FALSE, GTK_STOCK_SAVE, TRUE, 0);
g_signal_connect(G_OBJECT(settings_dialog), "response", G_CALLBACK(gtk_widget_destroy), NULL);
g_signal_connect(G_OBJECT(settings_dialog), "destroy", G_CALLBACK(settings_destroyed), NULL);
GtkWidget *vb = gtk_dialog_get_content_area(GTK_DIALOG(settings_dialog));
GtkWidget *hb = gtk_hbox_new(FALSE, 3);
gtk_container_add(GTK_CONTAINER(hb), gtk_label_new("Background:"));
GtkWidget *cb = gtk_color_button_new();
gtk_container_add(GTK_CONTAINER(hb), cb);
gtk_container_add(GTK_CONTAINER(vb), hb);
Run Code Online (Sandbox Code Playgroud)
这是回溯:
(gdb) backtrace
#0 0x00007ffff4d88052 in ?? () from /lib/libc.so.6
#1 0x00007ffff5304112 in g_strdup () from /lib/libglib-2.0.so.0
#2 0x00007ffff5bc799d in ?? () from /usr/lib/libgobject-2.0.so.0
#3 0x00007ffff5ba826c in g_object_new_valist ()
from /usr/lib/libgobject-2.0.so.0
#4 0x00007ffff5ba84f1 in g_object_new () from /usr/lib/libgobject-2.0.so.0
#5 0x00007ffff78502d5 …Run Code Online (Sandbox Code Playgroud) 我已经意识到这已经被问了好几百次,但是,我似乎无法理解"为什么"JavaScript中的原型是正确的,因为它模仿了类(是的,我知道JavaScript是一种基于原型的语言 - 我'我收集了很多).
像许多其他人一样努力使JavaScript成为我使用的日常语言,我习惯于常规的OOP类风格,因为我在Java中玩过(并且在ActionScript和PHP中使用过类).然而,虽然我认为我理解原型是如何工作的,但我似乎无法理解为什么需要原型.
这是我目前在JavaScript中理解原型的示例脚本:
var Apple = function() {
// An apple?
};
Apple.prototype.color = "red";
Apple.prototype.changeColor = function(new_color) {
this.color = new_color;
};
Apple.prototype.getColor = function() {
alert('color: '+this.color);
};
var apple1 = new Apple();
var apple2 = new Apple();
apple2.changeColor("green");
apple1.getColor();
apple2.getColor();
Run Code Online (Sandbox Code Playgroud)
...我曾经假设原型可能意味着它共享同一个对象,而不是每次只创建一个新对象 - 但是,显然不是这样,因为apple1和apple2都有不同的颜色,仍然(运行后说)脚本).
然后我用更多面向对象的脚本编写了它:
var Apple = function() {
this.color = "red";
this.changeColor = function(new_color) {
this.color = new_color;
};
this.getColor = function() {
alert('color: '+this.color);
};
};
var apple1 = new Apple(); …Run Code Online (Sandbox Code Playgroud) 我觉得这可以改善(红宝石的常见感觉).我正试图根据值unq一系列哈希.在这个例子中,我想要元素的颜色.苔藓和雪是冒名顶替者.
# remove unique array of hashes based on a hash value
a = [
{ :color => "blue", :name => "water" },
{ :color => "red", :name => "fire" },
{ :color => "white", :name => "wind" },
{ :color => "green", :name => "earth" },
{ :color => "green", :name => "moss" },
{ :color => "white", :name => "snow" }
]
# remove moss and snow
uniques = []
a.each_with_index do |r, i|
colors = uniques.collect {|e| …Run Code Online (Sandbox Code Playgroud) 有一个带有以下控件的表单(它是一个上传图像控件)
<FileControl(profile_image=<No files added>)>
Run Code Online (Sandbox Code Playgroud)
我应该把什么放在"??????????"中:
br = mechanize.Browser()
br.open(mywebsite)
br.select_form(nr=1)
br.form['profile_image'] = ??????????
br.submit()
Run Code Online (Sandbox Code Playgroud)
我试过了
br.form['profile_image'] = open("img.jpg")
Run Code Online (Sandbox Code Playgroud)
但得到错误
File "/usr/local/lib/python2.6/dist-packages/mechanize-0.2.4-py2.6.egg/mechanize/_form.py", line 2784, in __setitem__
raise ValueError(str(e))
ValueError: value attribute is readonly
Run Code Online (Sandbox Code Playgroud)