可能重复:
Currying减法
我开始了第一个不是来自教程的haskell项目,当然我偶然发现了最简单的东西.
我有以下代码:
moveUp y = modifyMVar_ y $ return . (+1)
moveDn y = modifyMVar_ y $ return . (-1)
Run Code Online (Sandbox Code Playgroud)
我花了一些时间来理解为什么我的代码不能编译:我曾经使用过(-1)这被认为是负面的.将减号括起来并没有帮助,因为它为它添加前缀并使其成为第一个参数.
简而言之,这点的免费版本是什么?
dec :: Num a => a -> a
dec x = x - 1
Run Code Online (Sandbox Code Playgroud) Go的时间包声称给出了纳秒精度. http://golang.org/src/pkg/time/time.go
我想知道它是如何实现的,如果我可以相信它.我的疑虑来自Python,它清楚地记录了它的困难和时间限制:
来自http://docs.python.org/2/library/time.html
各种实时函数的精度可能低于表示其值或参数的单位所建议的精度.例如,在大多数Unix系统上,时钟"滴答"仅为每秒50或100次.
另一方面,time()和sleep()的精度优于Unix等价:时间表示为浮点数,time()返回最准确的可用时间(使用Unix gettimeofday()),以及sleep()将接受非零分数的时间(Unix select()用于实现此功能,如果可用).
由于操作系统给python带来了如此艰难的时期,Go如何实现其纳秒精度?
我知道python集的元素没有排序.调用pop方法返回一个任意元素; 我很好.
我想知道的是,当集合具有相同的历史记录时,pop是否总是会返回相同的元素.当然,在python的一个版本中,我不介意python的不同版本/实现是否有自己的功能.特别是,我问的是python 2.7.在这种情况下,这不仅仅是api的实现问题.
我在游戏的程序性地牢生成器中使用了很多集合,我希望结果对于给定的种子是确定性的.
以下代码是不好的做法?
for i in some_values:
do_whatever(i)
do_more_things(i)
Run Code Online (Sandbox Code Playgroud)
不知何故,我觉得变量i应该保留在for循环内的块的范围内.但是python 2.7允许我在循环之后重用它.
python是否正式支持该功能,或者我是否滥用该语言?
我一直在寻找一段时间,但我找不到答案.类似的问题涉及一些棘手的案件与第三方图书馆和所有,但我的情况似乎是教科书简单.然而,我无法弄清楚这是如何工作的.
我正在使用Eclipse 3.5.2,Pydev 2.2.0,在Ubuntu 11.04机器上运行.
我的情况看起来很简单:我想编写一个包作为自己的项目,然后我想在另一个项目中使用这个包编写一个应用程序.
我应该如何设置pydev以便我可以将这两个项目"核心"(包)和"app"(使用核心包),并从"app"中执行:
import core
Run Code Online (Sandbox Code Playgroud)
?
通过"不起作用",我的意思是我总是得到一个"未解决的导入:核心".
想法?
我习惯打字random.randrange.我会做一个from random import Random从现在开始发现错误.
对于涉及程序生成的游戏(nope,而不是Minecraft克隆:p)我想保留几个不同的伪随机数生成器:
理由是我希望能够重现第一个,所以我不希望第二个干扰.
我以为random.Random是为此而做的.不过有些令我困惑的是:
import random
rnd = random.Random()
rnd.seed(0)
print [random.randrange(5) for i in range(10)]
rnd.seed(0)
print [random.randrange(5) for i in range(10)]
Run Code Online (Sandbox Code Playgroud)
产生两个不同的序列.当我这样做时,rnd = random事情按预期工作,但我确实需要几个发电机.
我错过了什么?
我正在目睹日志记录模块以一种有趣的方式运行.我错过了什么吗?
我正在做通常的事情,有两个处理程序:一个StreamHandler,用于仅记录INFO和更高的控制台,以及一个FileHandler,它也将处理所有的DEBUG信息.
它工作正常,直到我决定为exeptions提供不同的格式.我想在文件中有一个完整的堆栈跟踪,但只是控制台上的异常类型和值.由于处理程序具有setFormatter函数,并且因为编写logging.Formatter的子类似乎很容易,所以我认为它会起作用.
控制台处理程序和文件处理程序都有自己的格式化程序.代码中的print语句证明了这一点.但是,对logger.exception的调用将仅使用添加的第一个处理程序的formatException =>在文件中记录异常,并在控制台中记录该格式.更改logger.addHandler行的顺序,然后是遍布各处的文件处理程序的formatException.
import logging
import sys
class ConsoleFormatter(logging.Formatter):
def formatException(self, exc_info):
# Ugly but obvious way to see who's talking.
return "CONSOLE EXCEPTION %s: %s" % exc_info[:2]
def setup(path):
logger = logging.getLogger()
#
file_handler = logging.FileHandler(path, mode='w')
if __debug__:
file_handler.setLevel(logging.DEBUG)
else:
file_handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s %(levelname)-8s "
"%(name)-16s %(message)s "
"[%(filename)s@%(lineno)d in %(funcName)s]")
file_handler.setFormatter(formatter)
#
console_handler = logging.StreamHandler(sys.stderr)
console_handler.setLevel(logging.INFO)
console_formatter = ConsoleFormatter("%(levelname)-8s - %(message)s")
console_handler.setFormatter(console_formatter)
# >>> FUN HAPPENS HERE <<<
# Only the formatter of the …Run Code Online (Sandbox Code Playgroud) 我在ubuntu机器上使用python 2.7.
客户端尝试连接到服务器.我得到了一个EINPROGRESS,预计非阻塞套接字.
要检查连接是否成功,我会执行{connect}的手册页建议:
# EINPROGRESS The socket is nonblocking and the connection cannot be
# completed immediately. It is possible to select(2) or poll(2) for
# completion by selecting the socket for writing. After select(2)
# indicates writability, use getsockopt(2) to read the SO_ERROR option at
# level SOL_SOCKET to determine whether connect() completed successfully
# (SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the usual error
# codes listed here, explaining the reason for the failure)
Run Code Online (Sandbox Code Playgroud)
当服务器脱机时,这给了我一个ECONNREFUSED.到现在为止还挺好. …
当下面的代码无法编译时,我有点惊讶:
-- Code 1
import Complex
type Velocity = Complex Double
type Force = Complex Double
type FrictionCoeff = Double
frictionForce :: FrictionCoeff -> Velocity -> Force
frictionForce mu vel = mu * vel
Run Code Online (Sandbox Code Playgroud)
错误说
Couldn't match expected type `Complex Double'
with actual type `Double'
Expected type: Force
Actual type: FrictionCoeff
In the first argument of `(*)', namely `mu'
In the expression: mu * vel
Run Code Online (Sandbox Code Playgroud)
所以,简而言之
-- Code 2
let z = 1 :+ 2
z * 3 -- Goes …Run Code Online (Sandbox Code Playgroud) 我正在阅读一些Arrow教程,玩弄函数返回自己的新版本以试图维持某种状态.
新类型定义如下:
newtype Circuit a b = Circuit {runCircuit :: a -> (b, Circuit a b)}
Run Code Online (Sandbox Code Playgroud)
因为我希望能够编写电路,所以我将它作为Category的一个实例.在组成两个电路时,结果也必须是电路. (Circuit b c) . (Circuit a b)给了一个Circuit a c.
我写了这个:
import qualified Control.Category as Cat
instance Cat.Category Circuit where
(Circuit g) . (Circuit f) = Circuit $ \a -> let
(b, new_f) = f a
(c, new_g) = g b
new_circ = new_g . new_f
in (c, new_circ)
Run Code Online (Sandbox Code Playgroud)
但它失败了:
Main.hs:70:64:
Couldn't match expected type `b0 -> c0'
with …Run Code Online (Sandbox Code Playgroud)