在阅读了一些非常基本的haskell后,我知道如何使用bind "链接"monadic动作,如:
echo = getLine >>= putStrLn
Run Code Online (Sandbox Code Playgroud)
(>>=) 运算符在这种方式下非常方便,但是如果我想链接带有多个参数的monadic动作(或仿函数)呢?
鉴于(>>=) :: m a -> (a -> m b) -> m b它似乎(>>=)只能提供一个论点.
例如,writeFile接受两个参数(a FilePath和内容).假设我有一个返回a的monadic动作FilePath,以及另一个返回String写入的动作.如何在writeFile不使用do-notation的情况下将它们组合在一起,但是以一般方式?
是否有任何类型的功能: m a -> m b -> (a -> b -> m c) -> m c可以做到这一点?
我想知道如何以快速和优雅的方式解决这个问题:
我们定义"丑陋"的每个数字n可以用以下形式写出:2 ^ x*3 ^ y*5 ^ z ;,其中x,y和z是自然数.找到第1500个丑陋的数字.
例如,第一个"丑陋"的数字是:
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
Run Code Online (Sandbox Code Playgroud)
我试图用暴力解决这个问题,这样:
import itertools as it
def is_ugly(n):
'''Return `True` if *n* is an ugly number.'''
if n == 1:
return True
while not n % 2:
n //= 2
while not n % 3:
n //= 3
while not n % 5:
n //= 5
return n …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用sphinx并成功生成html文件来记录python包.我正在记录的包由一组文件组成*.py,大多数包含一个类,其中几个文件是定义了函数的真正模块.我不需要暴露每个类都在一个模块中的事实,所以我from在__init__.py文件中添加了合适的语句,例如
from base import Base
Run Code Online (Sandbox Code Playgroud)
这样用户就可以使用该import pkg命令,然后不必指定包含该类的模块:
import pkg
class MyBase(pkg.Base): # instead of pkg.base.Base ...
...
Run Code Online (Sandbox Code Playgroud)
问题在于,斯芬克斯坚持将课程记录为pkg.base.Base.我试图设置add_module_names = False在conf.py.然而,这导致狮身人面像显示出简单的类Base而不是pkg.Base.此外,这也破坏了作为模块的几个*.py文件的文档.
我如何sphinx将课程作为一个班级pkg.Base?如何add_module_names为每个*.py文件选择性地设置指令?
在阅读Stream界面源代码时,我发现了这个方法签名:
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
Run Code Online (Sandbox Code Playgroud)
我想知道为什么输入类型mapper是? super T输出类型? extends R,为什么不? extends用于两者?
来自Java,我正在努力解决继承,抽象类,静态方法和Python中的OO编程的类似概念.
我有一个表达式树类的实现,由(简化)给出
# Generic node class
class Node(ABC):
@abstractmethod
def to_expr(self):
pass
@staticmethod
def bracket_complex(child):
s = child.to_expr()
return s if isinstance(child, Leaf) or isinstance(child, UnaryOpNode) else "(" + s + ")"
# Leaf class - used for values and variables
class Leaf(Node):
def __init__(self, val):
self.val = val
def to_expr(self):
return str(self.val)
# Unary operator node
class UnaryOpNode(Node):
def __init__(self, op, child):
self.op = op
self.child = child
def to_expr(self):
return str(self.op) + super().bracket_complex(self.child)
# Binary operator node
class …Run Code Online (Sandbox Code Playgroud) 在C++库数组中,在哪些情况下,拥有.begin()和.end()成员函数是有用的?
在cplusplus.com上,示例用法是遍历数组:
for ( auto it = myarray.begin(); it != myarray.end(); ++it )
Run Code Online (Sandbox Code Playgroud)
但
for (int i = 0; i < myarray.size(); i++)
Run Code Online (Sandbox Code Playgroud)
可以用于此.
我试图用正误差线和图中的最大值绘制4个平均值.
means = [26.82,26.4,61.17,61.55] # Mean Data
stds = [4.59,4.39,4.37,4.38] # Standard deviation Data
peakval = ['26.82','26.4','61.17','61.55'] # String array of means
ind = np.arange(len(means))
width = 0.35
colours = ['red','blue','green','yellow']
pyplot.figure()
pyplot.title('Average Age')
for i in range(len(means)):
pyplot.bar(ind[i],means[i],width,color=colours[i],align='center',yerr=stds[i],ecolor='k')
pyplot.ylabel('Age (years)')
pyplot.xticks(ind,('Young Male','Young Female','Elderly Male','Elderly Female'))
def autolabel(bars,peakval):
for ii,bar in enumerate(bars):
height = bars[ii]
pyplot.text(ind[ii], height-5, '%s'% (peakval[ii]), ha='center', va='bottom')
autolabel(means,peakval)
Run Code Online (Sandbox Code Playgroud)
但是我无法找出如何只绘制正误差条.所以我最终得到了这样一个图:

任何建议将不胜感激.
我正在使用ghci此代码部分
newtype Gold = Gold Int
deriving (Eq, Ord, Show, Num)
Run Code Online (Sandbox Code Playgroud)
显示错误为
Can't make a derived instance of 'Num Gold':
'Num' is not a derivable class
Try GeneralizedNewTypeDeriving for GHC's newtype-deriving extension in the newtype declaration for 'Gold'
Run Code Online (Sandbox Code Playgroud)
请建议解决方案.
我在python中有以下代码,至少对我来说会产生奇怪的结果:
import numpy as np
import timeit
a = np.random.rand(3,2)
print timeit.timeit('a[2,1] + 1', 'from __main__ import a', number=1000000)
print timeit.timeit('a.item((2,1)) + 1', 'from __main__ import a', number=1000000)
Run Code Online (Sandbox Code Playgroud)
这给出了结果:
0.533630132675
0.103801012039
Run Code Online (Sandbox Code Playgroud)
如果我只尝试访问numpy元素似乎没关系,但是当增加这个元素时,时间变得奇怪......为什么时间上有这么大的差异?
我有一种情况需要强制执行,并为用户提供多个select函数之一的选项,作为参数传递给另一个函数:
我真的想要实现以下内容:
from enum import Enum
#Trivial Function 1
def functionA():
pass
#Trivial Function 2
def functionB():
pass
#This is not allowed (as far as i can tell the values should be integers)
#But pseudocode for what I am after
class AvailableFunctions(Enum):
OptionA = functionA
OptionB = functionB
Run Code Online (Sandbox Code Playgroud)
所以可以执行以下操作:
def myUserFunction(theFunction = AvailableFunctions.OptionA):
#Type Check
assert isinstance(theFunction,AvailableFunctions)
#Execute the actual function held as value in the enum or equivalent
return theFunction.value()
Run Code Online (Sandbox Code Playgroud)