如何使用以下代码将我输出的记录数限制为仅3条记录:
User.rb
def workouts_on_which_i_commented
comments.map{|x|x.workout}.uniq
end
def comment_stream
workouts_on_which_i_commented.map do |w|
w.comments
end.flatten.sort{|x,y| y.created_at <=> x.created_at}
end
Run Code Online (Sandbox Code Playgroud)
html.erb文件
<% current_user.comment_stream.each do |comment| %>
...
<% end %>
Run Code Online (Sandbox Code Playgroud)
更新:
我正在使用Rails 2.3.9
我想在-10到10之间得到20个随机整数,我想在matlab中使用rand函数.
我想到了10的myltiplying,然后找到了一种只获得-10到10之间的方法,并对超出限制[-10,10]的每个其他数字使用迭代来获得一个新的数字在限制范围内.
有更好,更快的方式吗?
我想分配一个2.9GB的char数组
database = (char*) malloc((2900 * 1000000 * sizeof(char)));
Run Code Online (Sandbox Code Playgroud)
这给出了整数溢出警告和malloc返回NULL.的
malloc参数的类型的size_t根据文档其是类型的
unsigned int.
所以最大应该UINT_MAX是至少2.9GB.但是,如果我尝试分配超过MAX_INT了malloc失败.这是否意味着
size_t我的系统是int类型?我该如何检查?我看了看
/usr/include/stdlib.h
Run Code Online (Sandbox Code Playgroud)
和
./lib/gcc/x86_64-redhat-linux/4.1.1/include/stddef.h
Run Code Online (Sandbox Code Playgroud)
但找不到定义size_t.非常感谢
我有一些方法,我正在尝试单元测试.这些方法使用Spring JdbcTemplates对DB2 v8运行正常(在生产中).
每个SQL通过在语句中附加"WITH UR"来允许"脏读".此外,它们中的一些通过添加例如"FETCH FIRST 1 ROWS ONLY"来使用"限制".
这对于真正的DB2工作正常,但我想对内存数据库进行单元测试 - 这就是H2进入的地方.
如果我删除"WITH UR"和"FETCH FIRST ..",一切正常,但我不想更改方法,只需更改后端数据库.
据我所知,这不是直接使用H2,因为语法不同(虽然我使用MODE = DB2).
现在,该怎么办?我应该/可以使用另一个内存数据库吗?我不想改变方法,也不想添加"测试"功能/黑客,所以这是不行的.
想法和同样非常感谢!
编辑
我不确定它是我还是什么,但我得到以下错误.注意,我使用的是Spring 3.1和H2 1.3.166,数据库的url是"jdbc:h2:〜/ testdb; MODE = DB2".我不知道它为什么会失败,因为sql在H2控制台中运行时起作用,但不是来自我的单元测试(猜测它不是H2坏了):
Tests run: 3, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.109 sec <<< FAILURE!
getAdvisor(impl.AdvisorServiceDaoImplTest) Time elapsed: 0.078 sec <<< ERROR!
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT * FROM ADVISOR FETCH FIRST 1 ROWS ONLY ]; nested exception is org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT * FROM ADVISOR FETCH[*] …Run Code Online (Sandbox Code Playgroud) 我有一个包含3个字段的表:
id
note
created_at
Run Code Online (Sandbox Code Playgroud)
有没有办法在SQL语言特别是Postgres中我可以选择最后一个的值note而不必LIMIT 1?
正常查询:
select note from table order by created_at desc limit 1
Run Code Online (Sandbox Code Playgroud)
我对避免限制的事情感兴趣,因为我需要它作为子查询.
我需要完成的工作很简单 - 将段落元素长度的限制设置为60个字符.我写的jQuery脚本如下:
$('p.classname').each(function(){
var paragraph = $(p.description).text();
var strlength = paragraph.length;
var maxlength = 60;
strlength < maxlength;
});
Run Code Online (Sandbox Code Playgroud)
我仍然可以在元素中包含超过60个字符.我怎么能把它限制在60?
我正在使用libgdx开发一款针对Android的小游戏,并希望将fps限制为30以节省电池.问题是它不起作用.fps从60下降到56.
这是代码的一部分:(它在渲染部分的末尾)
System.out.print("\nFPS: " + Gdx.graphics.getFramesPerSecond() + "\n");
if(Gdx.graphics.getDeltaTime() < 1f/30f)
{
System.out.print("DeltaTime: " + Gdx.graphics.getDeltaTime() + " s\n");
float sleep = (1f/30f-Gdx.graphics.getDeltaTime())*1000;
System.out.print("sleep: " + sleep + " ms\n");
try
{
Thread.sleep((long) sleep);
}
catch (InterruptedException e)
{
System.out.print("Error...");
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
FPS: 56
DeltaTime: 0.014401722 s
sleep: 18.931612 ms
FPS: 56
DeltaTime: 0.023999143 s
sleep: 9.334191 ms
FPS: 56
DeltaTime: 0.010117603 s
sleep: 23.215733 ms
Run Code Online (Sandbox Code Playgroud)
提前致谢...
我使用多对多的关系;
用户实体;
/**
* @ORM\ManyToMany(targetEntity="Conversation", inversedBy="users")
*/
protected $conversations;
Run Code Online (Sandbox Code Playgroud)
会话实体;
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="conversations")
* @ORM\JoinTable(name="user_conversation")
*/
protected $users;
Run Code Online (Sandbox Code Playgroud)
什么时候,我工作这个功能;
$user->getConversations();
Run Code Online (Sandbox Code Playgroud)
Symfony在后台运行这个sql代码;
SELECT
t0.id AS id1,
t0.conversationid AS conversationid2
FROM
Conversation t0
INNER JOIN user_conversation ON t0.id = user_conversation.conversation_id
WHERE
user_conversation.user_id = ?
Run Code Online (Sandbox Code Playgroud)
并选择所有对话.这将是性能问题.所以,我使用存储库类.但是,我不能一起做多对多和限制功能.我该怎么办?我写的什么到存储库类?
我在NVIDIA文档(http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#features-and-technical-specifications,table#12)中读到每个线程的本地内存量我的GPU是512 Ko(GTX 580,计算能力2.0).
我尝试用CUDA 6.5检查Linux上的这个限制是不成功的.
这是我使用的代码(它的唯一目的是测试本地内存限制,它不会进行任何有用的计算):
#include <iostream>
#include <stdio.h>
#define MEMSIZE 65000 // 65000 -> out of memory, 60000 -> ok
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=false)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if( abort )
exit(code);
}
}
inline void gpuCheckKernelExecutionError( const char *file, int line)
{
gpuAssert( cudaPeekAtLastError(), file, line);
gpuAssert( cudaDeviceSynchronize(), file, line);
}
__global__ void kernel_test_private(char *output)
{
int c = …Run Code Online (Sandbox Code Playgroud) 如何随机生成所有数字limit [m,n].要从...生成所有数字6 to 12,序列必须如此[7 12 11 9 8 10 6].
r = randi([6 12],1,7);
Run Code Online (Sandbox Code Playgroud)
但这给出了结果:
[12 11 12 7 9 10 12]
Run Code Online (Sandbox Code Playgroud)
这里重复数字,序列不包含所有数字6 to 12.
limit ×10
java ×2
matlab ×2
random ×2
android ×1
c ×1
cuda ×1
dirtyread ×1
frame-rate ×1
h2 ×1
javascript ×1
jquery ×1
libgdx ×1
malloc ×1
many-to-many ×1
memory ×1
mysql ×1
numbers ×1
paragraph ×1
postgresql ×1
relationship ×1
repository ×1
ruby ×1
size-t ×1
spring ×1
sql ×1
symfony ×1