我试图用scala 2.8解决Project Euler 7号问题
我实施的第一个解决方案需要大约8秒钟
def problem_7:Int = {
var num = 17;
var primes = new ArrayBuffer[Int]();
primes += 2
primes += 3
primes += 5
primes += 7
primes += 11
primes += 13
while (primes.size < 10001){
if (isPrime(num, primes)) primes += num
if (isPrime(num+2, primes)) primes += num+2
num += 6
}
return primes.last;
}
def isPrime(num:Int, primes:ArrayBuffer[Int]):Boolean = {
// if n == 2 return false;
// if n == 3 return false;
var r …Run Code Online (Sandbox Code Playgroud) 与我最近关于处理大型数据块的问题密切相关,我已经达到了这样的程度,即我需要采用一个大的不可变数据块,使其对某些操作变得可变,然后在完成后再次使其变为不可变.
由于我想保留至少纯度的外观,可变数据将是原始不可变数据的可变副本.作为参考,我正在查看Real World Haskell 中的Bloom Filter示例,但发现我实际上无法使我的代码在runST中运行.
我的数据结构,首先是纯数据,然后是不纯的:
import Data.Vector.Unboxed (Vector)
import Data.Vector.Unboxed.Mutable (MVector)
data PixelMap = Bitmap Int Int (Vector Bool)
data MPixelMap s = MBitmap Int Int (MVector s Bool)
Run Code Online (Sandbox Code Playgroud)
然后我只创建一个基本的newBitmapM函数:
newBitmapM :: (Int, Int) -> ST s (MPixelMap s)
newBitmapM (width, height) = MBitmap width height `liftM` MV.replicate (width * height) False
Run Code Online (Sandbox Code Playgroud)
这加载到GHCI就好了,但后来我尝试运行它:
> runST $ newBitmapM (15, 15)
<interactive>:78:9:
Couldn't match type `a' with `PixelMapMutable s'
`a' is a rigid type variable bound …Run Code Online (Sandbox Code Playgroud) 当调用.map()大长度的地图时,会更快
有没有办法让"可变" Fraction?
我试过这个,但看起来像Fraction中的分子/分母是不可变的.
>>> from fractions import Fraction
>>> x = Fraction(0,1)
>>> numerators = [1,2,3,4,5]
>>> denominators = [9,8,7,6,5]
>>> for n,d in zip(numerators, denominators):
... x.numerator+= n
... x.denominator+= d
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AttributeError: can't set attribute
Run Code Online (Sandbox Code Playgroud)
我一直在这样做,因为我的分子/分母来自另一个返回分数生成器的函数.
>>> inputs = [Fraction(1,9), Fraction(2,8), Fraction(3,7), Fraction(4,6)]
>>> numerators, denominators = zip(*[(f.numerator, f.denominator) for f in inputs])
>>> x = Fraction(sum(numerators), sum(denominators))
>>> x
Fraction(7, 23)
Run Code Online (Sandbox Code Playgroud)
(注意,我没有添加1/9 + 2/8 …
我目前正在JavaScript的学习过程中,真的对不可变值感到困惑。据我了解,当创建一个值(stings / numbers / booleans)时,就永远无法更改它。
现在我的问题是,我分配值的变量,该变量的值是可变的。我可以为该变量分配新值。那么为什么不变性很重要?
另外,当我为变量分配新值时,以前的值会怎样?它会保留在内存中并阻塞一些空间吗?它是否失去了该变量的指针?究竟会发生什么?
请帮助我了解JavaScript中“可变”和“不可变”概念的实际用法。提前致谢。
struct A
{
// clang 3.8 error : cannot combine with previous 'static' declaration specifier
static mutable int n;
};
Run Code Online (Sandbox Code Playgroud)
我认为static mutable int n;在某些情况下有明确的语义.为什么在C++中不允许这样做?
更新:
另一个例子显示clear semantics:
class SharedValue
{
public:
void Set(int n)
{
std::lock_guard lock(_mtx);
_n = n;
}
int Get() const
{
std::lock_guard lock(_mtx);
//
// _mtx should be mutable,
// because this is in const member function
//
return _n;
}
private:
static mutable std::mutex _mtx;
int _n;
};
Run Code Online (Sandbox Code Playgroud) 我是F#的新手,我正在编写一个程序,需要找到某个列表中给定长度的每个子列表.我不知道如何解决这个问题所以我读了这个问题并决定将答案移植到F#.这就是我所拥有的:
let rec getSubLists (len : int) (list : List<int>) : List<List<int>> =
let result = new List<List<int>>()
let current = new List<int>()
let rec findSubLists (len : int) (superSet : List<int>) (current : List<int>) (soln : List<List<int>>) (idx : int) : unit =
if current.Length = len then soln.Insert(len - 1, current)
elif idx = superSet.Length then
let x = superSet.[idx]
current.Insert(len, x)
findSubLists len superSet current soln (idx + 1)
current.RemoveAt(x)
findSubLists len superSet …Run Code Online (Sandbox Code Playgroud) 我对 Kotlin 相当陌生,目前我经常处理 JSON。
我从服务器收到一个 JSON 字符串,我将其解析JSONArray如下
var dataArray = JSONArray(String(resultVar!!))
Run Code Online (Sandbox Code Playgroud)
但据我所知,JSONArray它并没有真正给我足够的能力来改变它的数据JSONArray,如果我没记错的话,它迫使我创建一个新的。所以我想我应该使用MutableList<JSONObject>,这似乎已经足够了,但是我找不到将 JSONArray 或 String 解析到其中的方法。
我如何以简单的方式做到这一点?我是否必须遍历 JSONArray 逐个添加每个 JSONObject?
作为一个附带问题,我应该坚持使用 JSONArray 吗?有没有办法操作里面的数据?
我想限制向量不变。在下面的代码中,当我为每个循环使用引用并增加每个值时,向量中也反映了相同的内容。但我想避免。
#include <vector>
#include <iostream>
int main()
{
std::vector<int> port = {8, 0, 8, 0};
for (auto &digit: port){
digit++;
std::cout << digit << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud) 我在使用函数时出错,因为借用一个可变的,如果还有一个不可变的借用,就像多次借用一个可变的一样是不允许的。
pub fn _function(list: &mut Vec<char>) {
for (n, c) in list.iter().enumerate() {
if *c == ' ' {
list.remove(n);
}
}
}
Run Code Online (Sandbox Code Playgroud)
pub fn _function(list: &mut Vec<char>) {
for (n, c) in list.iter().enumerate() {
if *c == ' ' {
list.remove(n);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的唯一解决方案是克隆列表。
pub fn _function(list: &mut Vec<char>) {
for (n, c) in list.clone().iter().enumerate() {
if *c == ' ' {
list.remove(n);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有其他解决方案可以在不克隆列表和使用更多内存的情况下使用这两个函数。