我正在学习RxJ。我使用Angular2 rc3。以下流有效,但是它给了我太多的mousemove事件。我想使用时间(节流)或其他控制流来减慢它们的速度。我怎样才能做到这一点?
鼠标移动流而无节流
const mouseMove$ = Observable.fromEvent<MouseEvent>(document, 'mousemove');
mouseMove$.subscribe( x => console.log(x)); // works great, many {mouse position object} 's
Run Code Online (Sandbox Code Playgroud)
简单的解决方案:使用节流应该是这样的:
const mouseMove$ = Observable.fromEvent<MouseEvent>(document, 'mousemove');
const latestMouseEventEvery1Second$ = mouseMove$.sample(1000);
latestMouseEventEvery1Second$.subscribe( x => console.log(x)); // error
Run Code Online (Sandbox Code Playgroud)
- 我在这里采用了这个sample()运算符的形式:http://reactivex.io/documentation/operators/sample.html
但这在角度2-CLI项目中不起作用。给我这个错误:
***Argument of type 'number' is not assignable to parameter of type 'Observable<\any>'*** - notice i've put <\MouseEvent> when casting.
Run Code Online (Sandbox Code Playgroud)
我认为,实现相同结果的另一种更强大的方法可能是顺其自然:
如果我们可以mousemove item 根据从另一流接收到的项目发送最新消息,那将是很好的。任何流-由我们创建。
例如:
当我们从eachSecond$Stream(=我们的“控制流”)接收到一个新项目(1、2,3 ..)时,-我们发出donwstream(进入mouseMoveEachSecond$)-从mouseMove$stream中接收到的最新项目。 …
我正在为学校开发一个UWP应用程序,我正在尝试在应用程序中显示来自我的覆盆子pi的MJPEG流.所有可用的解码器似乎适用于Windows Phone 8.1,但不适用于新的UWP应用程序.
我可以做些什么来在我的应用程序中使用这些流?
如果没有,是否有一个工具可用于转换流并以正确的格式在另一个端口上流式传输?这可以是覆盆子或只是窗户.
提前致谢
当我这样定义时fib(1):
def fib(n: Int) = {
lazy val fibs: Stream[BigInt] = 0 #:: 1 #:: fibs.zip(fibs.tail).map{n => n._1 + n._2}
fibs.drop(n).head
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
scala> fib(1000000)
java.lang.OutOfMemoryError: Java heap space
Run Code Online (Sandbox Code Playgroud)
另一方面,这很好(2):
def fib = {
lazy val fibs: Stream[BigInt] = 0 #:: 1 #:: fibs.zip(fibs.tail).map{n => n._1 + n._2}
fibs
}
scala> fib.drop(1000000).head
res17: BigInt = 195328212...
Run Code Online (Sandbox Code Playgroud)
此外,如果我按以下方式更改流定义,我可以drop(n).head在函数内调用,也不会得到任何错误(3):
def fib(n: Int) = {
lazy val fibs: (BigInt, BigInt) => Stream[BigInt] = (a, b) => a #:: fibs(b, …Run Code Online (Sandbox Code Playgroud) 我在内存中有大量的二进制数据,我需要从随机访问的字节对齐地址进行读/写.但是,有时候我需要读/写8位字,有时候(大端)16位字,有时候(big-endian)32位字.
有一种天真的解决方案,即将数据表示为一个ByteArray并手动实现16/32位读/写:
class Blob (val image: ByteArray, var ptr: Int = 0) {
fun readWord8(): Byte = image[ptr++]
fun readWord16(): Short {
val hi = readWord8().toInt() and 0xff
val lo = readWord8().toInt() and 0xff
return ((hi shl 8) or lo).toShort()
}
fun readWord32(): Int {
val hi = readWord16().toLong() and 0xffff
val lo = readWord16().toLong() and 0xffff
return ((hi shl 16) or lo).toInt()
}
}
Run Code Online (Sandbox Code Playgroud)
(和writeWord8/ writeWord16/ 类似writeWord32).
有一个更好的方法吗?当Java本身已经在内部使用big-endian表示时,执行所有这些字节混洗似乎效率很低......
重申一下,我需要读取和写入访问,随机搜索 …
我正在尝试重新上传刚刚检索到的流.我使用AWS并不重要我相信......也许我对使用流的理解太有限了?:-)
我直接从AWS文档中使用以下方法来下载和上传流:
上传文件:
public bool UploadFile(string keyName, Stream stream)
{
using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1))
{
try
{
TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1));
fileTransferUtility.Upload(stream, bucketName, keyName);
return true;
}
catch (AmazonS3Exception amazonS3Exception)
{
[...]
}
}
}
Run Code Online (Sandbox Code Playgroud)
获取文件:
public Stream GetFile(string keyName)
{
using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast2))
{
try
{
GetObjectRequest request = new GetObjectRequest
{
BucketName = bucketName,
Key = keyName
};
GetObjectResponse response = client.GetObject(request);
responseStream = response.ResponseStream;
return responseStream;
}
catch (AmazonS3Exception amazonS3Exception) …Run Code Online (Sandbox Code Playgroud) 如何在Java中使用owlapi找到所有已定义的类?我知道不推荐使用的getClassesInSignature方法,但是文档(JavaDocs)缺少方面,应该使用什么代替?
我一直在这个问题上摸不着头脑.我正在尝试编写一个程序,将给定文本文件中每个单词的频率输出到.csv文件.我已经成功创建了查找每个单词频率的函数,并将其结果作为映射输出,但我的tocsv函数由于某种原因将结果写为Stream结果,我无法弄清楚原因,或者如何避免这种情况.这是我的代码:
defmodule WordFrequency do
def wordCount(readFile) do
readFile
|> words
|> count
|> tocsv
end
defp words(file) do
file
|> File.stream!
|> Stream.map(&String.trim_trailing(&1))
|> Stream.map(&String.split(&1,~r{[^A-Za-z0-9_]}))
|> Enum.to_list
|> List.flatten
end
defp count(words) when is_list(words) do
Enum.reduce(words, %{}, &update_count/2)
end
defp update_count(word, acc) do
Map.update acc, String.to_atom(word), 1, &(&1 + 1)
end
defp tocsv(map) do
file = File.open!("test.csv", [:write, :utf8])
map
|> IO.inspect
|> Enum.map(&CSV.encode(&1))
|> Enum.each(&IO.inspect(file, &1, []))
end
end
Run Code Online (Sandbox Code Playgroud)
count(它是一个测试文件)的结果是:
bitterness: 1, fan: 1, respiration: 1, radiator: 1, ceiling: …Run Code Online (Sandbox Code Playgroud) HashMap<String, Double> missions = new HashMap<>();
missions.put("name", 1.0);
missions.put("name1", 2.0);
missions.keySet().stream().forEach(el-> System.out.println(el));
Run Code Online (Sandbox Code Playgroud)
仅打印键
我有这个功能:
void aggiornadatabase(void) {
FILE* fp;
int c=0;
char str[30];
int m;
sprintf(str, "%s.csv", utenti[posizioneuser].id);
printf("%s\n", str);
fp = fopen(str, "w");
if (fp == NULL)
printf("Database error\n");
else
m = remove(str);
if (m == 0)
printf("Success\n");
else
printf("Unable to delete the File\n");
fclose(fp);
}
Run Code Online (Sandbox Code Playgroud)
执行此函数时,它会删除所选.csv文件中的所有内容,但它不会删除文件本身(实际上它会打印"无法删除文件").
为什么会这样?
我已经看到使用流作为comonad的默认示例,但我无法确定它们是如何无限的,但不是.
假设我们有数据构造函数(从这里)
data Stream a = a :> Stream a
Run Code Online (Sandbox Code Playgroud)
我们如何最终完成一个流?我们在最后写了一个未定义的吗?我知道这种语言是懒惰的,但某些地方必须削减,对吧?我错了吗?