Python 3.x支持(可选)函数注释:
def add_ints(x:int, y:int) -> int :
return x+y
Run Code Online (Sandbox Code Playgroud)
我有时会遇到如何表示给定"类型"可以表示的问题,而这次,我有一个返回生成器的函数:
def myfunc(x: [int]) -> "generator that returns ints":
# ^~~~~~~~~~~~~~~~~~~~~~~~~~
return (n for n in x if n%2 == 0)
Run Code Online (Sandbox Code Playgroud)
我该如何注释返回值?有什么参考我可以咨询吗?
从Doctest的自述文件中,可以使用带有QuickCheck的 doctest,如下所示:
-- |
-- prop> sort xs == (sort . sort) (xs :: [Int])
Run Code Online (Sandbox Code Playgroud)
我想用多行描述这个属性,可能就像
-- |
-- prop> sort xs ==
-- (sort . sort) (xs :: [Int])
Run Code Online (Sandbox Code Playgroud)
Doctest本身支持多行输入(同样来自自述文件)
-- |
-- >>> :{
-- let
-- x = 1
-- y = 2
-- in x + y + multiline
-- :}
-- 6
Run Code Online (Sandbox Code Playgroud)
我尝试了几种类似的语法,比如
-- |
-- prop> :{ sort xs ==
-- (sort . sort) (xs :: [Int])
-- }:
Run Code Online (Sandbox Code Playgroud)
没有任何成功.(在上面的示例中,错误消息是 parse …
因为ghci,我可以限制ghci可以使用的内存
$ ghci +RTS -M10m -RTS
Run Code Online (Sandbox Code Playgroud)
当我编译整个程序时,我可以
$ ghc -rtsopts a.hs
Run Code Online (Sandbox Code Playgroud)
然后
$ ./a +RTS -M10m
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做runghc a.hs?我尝试了几种方法,runghc a.hs +RTS -M10m但似乎没有一种方法可行.我可以限制内存的唯一选择是
$ export GHCRTS='-M10m'
$ runghc a.hs
Run Code Online (Sandbox Code Playgroud)
,但我希望这只是一次,所以我更喜欢通过传递参数来做到这一点runghc.
编辑:我正在使用以下策略检查选项是否正常(仅仅因为我不知道更好的方法):
-- a.hs
f x = f (f x)
main = print $ seq (f 0) 0
Run Code Online (Sandbox Code Playgroud)
打开两个终端,一个用于top命令,另一个用于执行代码.如果执行停止说"堆耗尽",我认为这-M[number]m是有效的.如果执行继续并使用大量内存,我会终止该过程并断定它没有成功.
我认为标题很好地描述了我的问题:我想安排git push.更具体地说,我想知道我怎么做
git在指定的时间推进git push在指定时间执行而不重新认证(最好是以其他方式尝试push进行身份验证的方式).我在linux上(Ubuntu 13.04),因此使用bash编写脚本会很好.
我是一名haskell初学者,最近我读到了关于haskell wiki的严格性分析.GHC用户指南内容如下:
严格性分析器可以确定函数中的参数和变量何时可以"严格"处理(即它们总是在某个时刻在函数中进行求值).
我还读到了一般情况下何时可以推断出严格性.然而,作为一个初学者,我并不总是知道如果GHC居然把我的一段代码,我打算要严格要求,为严格.
目前,我没有其他方法可以确定是否正在进行严格的分析,而不是用大数据提供程序.
有没有办法问ghc是否可以推断给定的代码是严格的?
我看过一些关于如何以列数和行数获取终端大小的帖子和答案。我能否获得终端大小,或等效地,终端中使用的字体大小(以像素为单位)?
(我写等价是因为终端宽度[px] =字体宽度[px]*列数。或者这就是我所说的终端宽度。)
我正在寻找一种适用于 linux 上的 python 2 的方法,但我很欣赏仅适用于 python 3 的答案。谢谢!
在尝试熟悉的伟大的想法,例如Foldable,Functor等我写的数据结构为2×2矩阵.它不是真正的用途,所以我认为这个天真的实现是一个良好的开端:
data Matrix2d a = M2 a a a a
Run Code Online (Sandbox Code Playgroud)
我希望这是一个Num实例
instance Num a => Num (Matrix2d a) where
(M2 a0 b0 c0 d0) + (M2 a1 b1 c1 d1) = M2 (a0+a1) (b0+b1) (c0+c1) (d0+d1)
-- ....
Run Code Online (Sandbox Code Playgroud)
这似乎不对.我不想+为这个明显的定义键入五次.当然还有更多抽象的空间.我更喜欢像
(+) = fzipWith (+) -- f does not mean anything here
Run Code Online (Sandbox Code Playgroud)
这实际上很容易实现:
class Zippable z where
fzipWith :: (a -> b -> c) -> z a -> z b -> z c
instance …Run Code Online (Sandbox Code Playgroud) 在Google App Engine中,我想在10分钟内每小时安排一项任务.我的意思是,每天00:10,01:10,02:10,... 22:10和23:10:每时每刻+十分钟.Crontab等价就好了
10 * * * * mytask
Run Code Online (Sandbox Code Playgroud)
我该怎么写cron.yaml?
cron:
- description: do something
url: /myscript
schedule: (?)
Run Code Online (Sandbox Code Playgroud)
PS Season的问候,大家好!
在javascript switch语句中,如果其中任何一个case满意,我想执行一些函数:
switch (myVar){
case 0:
do_something_0();
break;
case 1:
do_something_1();
break;
// I want to execute myFunc() if myVar === 1 or myVar === 2
}
Run Code Online (Sandbox Code Playgroud)
我提出了拥有辅助变量的想法haveMatched,就像这样.
var haveMatched=false;
switch (myVar){
case 0:
do_something_0();
haveMatched=true;
break;
case 1:
do_something_1();
haveMatched=true;
break;
}
if (haveMatched){
do_finally();
}
Run Code Online (Sandbox Code Playgroud)
我认为可能有更好的方法来实现这一点(例如,如果我不知道default:关键字,我会尝试类似的方式).我做得对,还是我错过了什么?
标题可能不合适,请继续阅读.
我最初想要的是以下内容:我正在编写2D矢量数据
data Vect a = Vect a a deriving (Show)
Run Code Online (Sandbox Code Playgroud)
并且想要编写一个norm :: Vect a -> Double适用于所有Vect aa Integral或其实例的函数Floating.
对于Double,我可以写道:
norm :: Vect Double -> Double
norm (Vect x y) = sqrt $ x^2 + y^2
Run Code Online (Sandbox Code Playgroud)
但我希望这个功能也适用Vect Int.我可以写另一个函数,如:
normFromInt :: (Integral a) => Vect a -> Double
normFromInt (Vect x y) = sqrt . fromIntegral $ x^2 + y^2
Run Code Online (Sandbox Code Playgroud)
有两个功能似乎相当尴尬.实现这一目标的好方法是什么?
我尝试使用特殊类:
class Vectorlike a where
norm :: a -> Double
instance …Run Code Online (Sandbox Code Playgroud) 我有一个清单: var a = [0,1,2,3];
然后我错误地使用了一对数字来访问元素,如下所示:
console.log(a[0,0]) // => 0
console.log(a[0,1]) // => 1
console.log(a[1,0]) // => 0
console.log(a[4,2,5,1,2]) // => 2
Run Code Online (Sandbox Code Playgroud)
我认为它会抛出一个错误,返回undefined,或使用它遇到的第一个数字,但相反,似乎使用了数字的最后一个元素.为什么?有什么参考我可以咨询吗?
对于那些可能想知道我为什么这样做的人a[0,0]:我将用python + numpy编写的脚本重写为javascript.该脚本处理2D数组和a[m,n]要访问的numpy箭头a[m][n].
haskell ×5
ghc ×2
javascript ×2
python ×2
annotations ×1
arrays ×1
cron ×1
doctest ×1
git ×1
git-push ×1
linux ×1
python-3.x ×1
quickcheck ×1
scheduler ×1
scheduling ×1
terminal ×1