我正在研究一个相对较大的Python应用程序,并且我希望保留几个资源,因为全局变量可以在几个不同的模块中访问.这些值类似于版本号,版本日期,全局配置以及一些资源的静态路径.我还包含了一个DEBUG
由命令行选项设置的标志,这样我就可以在调试模式下运行我的应用程序而无需完整的环境.
我正在导入的值我一直小心翼翼地确保在运行程序的过程中没有改变的值,并且我将它们记录为不应该触及的全局常量变量.我的代码看起来很像
# Main.py
import wx
from gui import Gui
DEBUG = False
GLOBAL_CONFIG = None
VERSION = '1.0'
ICON_PATH = 'some/path/to/the/app.ico'
def main():
global DEBUG, GLOBAL_CONFIG
# Simplified
import sys
DEBUG = '--debug' in sys.argv
GLOBAL_CONFIG = load_global_config()
# Other set-up for the application, e.g. setting up logging, configs, etc
app = wx.App()
gui = Gui()
app.MainLoop()
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
# gui.py
import wx
from __main__ import DEBUG, GLOBAL_CONFIG, ICON_PATH
import controller
class Gui(wx.Frame):
def …
Run Code Online (Sandbox Code Playgroud) 我最近一直在教自己关于免费套餐中的Free
monad ,但我遇到了一个问题.我想为不同的库提供不同的免费monad,基本上我想为不同的上下文构建DSL,但我也希望能够将它们组合在一起.举个例子:
{-# LANGUAGE DeriveFunctor #-}
module TestingFree where
import Control.Monad.Free
data BellsF x
= Ring x
| Chime x
deriving (Functor, Show)
type Bells = Free BellsF
data WhistlesF x
= PeaWhistle x
| SteamWhistle x
deriving (Functor, Show)
type Whistles = Free WhistlesF
ring :: Bells ()
ring = liftF $ Ring ()
chime :: Bells ()
chime = liftF $ Chime ()
peaWhistle :: Whistles ()
peaWhistle = liftF $ PeaWhistle ()
steamWhistle …
Run Code Online (Sandbox Code Playgroud) 我希望在Windows 7上的GHCi(7.8)中有一个lambda(λ)符号作为我的提示,所以我将我的.ghci
文件设置为
:set +m
:set prompt "?: "
:set prompt2 " | "
Run Code Online (Sandbox Code Playgroud)
我将我的控制台字体设置为Lucida控制台,因为它应该支持Unicode,但是当我加载GHCi时,它看起来像这样
如何让Windows正确识别λ符号?
假设我定义了这个函数:
f = ($ 5)
Run Code Online (Sandbox Code Playgroud)
然后我可以申请它:
> f (\x -> x ^ 2)
25
Run Code Online (Sandbox Code Playgroud)
它的类型是:
:t f
f :: (Integer -> b) -> b
Run Code Online (Sandbox Code Playgroud)
这是有道理的,它将一个函数作为参数,并返回应用于该函数的函数Integer
5
.
现在我定义这个函数:
g = flip f
Run Code Online (Sandbox Code Playgroud)
我希望这没有意义,因为它f
是单个参数的函数.
但是,检查其类型:
:t g
g :: b -> (Integer -> b -> c) -> c
Run Code Online (Sandbox Code Playgroud)
所以现在g
是2个参数的函数!
将它应用于某些值:
> g [2, 4, 6] (\x y -> x:y)
[5,2,4,6]
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?什么是flip ($ 5)
真正的意思?
我正在开发一个专门的数值数据处理库,我遇到了一个错误,我无法弄清楚如何修复.我认为首先展示一个例子然后解释我的问题会更容易.我也为这些陌生的名字道歉,我不得不为法律目的进行混淆.
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
data MyError = MyError String deriving (Eq, Show)
data MyList = MyList [Double] deriving (Eq, Show)
data NamedList = NamedList String MyList deriving (Eq, Show)
class MyNum a b ret where
myAdd :: a -> b -> Either MyError ret
myLessThan :: a -> b -> Either MyError Bool
instance MyNum MyList Double MyList where
myAdd (MyList xs) x = Right $ MyList $ map (+x) xs
myLessThan (MyList xs) x …
Run Code Online (Sandbox Code Playgroud) 美好的一天.我是Haskell的新手.关于声明和实例化一些自定义类,有一点我不清楚.
Integral
haskell 有一个标准类.根据hackage,Integral
声明强制方法quot :: a -> a -> a
.所以这意味着该类的每个实例都应该有这个方法实现,对吗?
我们可以使用Integral作为参数来声明一些函数,例如:
proba :: (Integral a) => a -> a -> a
proba x y = x `quot` y
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好
class Proba a where
proba :: a -> a -> a
Run Code Online (Sandbox Code Playgroud)
我可以实现这样的Int或Integer(或其他数据类型)实例:
instance Proba Integer where
proba x y = x `quot` y
instance Proba Int where
proba x y = x `quot` y
Run Code Online (Sandbox Code Playgroud)
但我不想.我希望每个Integral都有一个实例.但是当我尝试这样做时,我收到一个错误:
instance (Integral a) => Proba a where
proba x …
Run Code Online (Sandbox Code Playgroud) 探索类型类本质上是没有嵌套继承的C++抽象类的想法,我编写了类型类
class Interface i c where
i :: c -> i
instance Interface i i where i = id
infixl 1 #
(#) :: Interface i c => c -> (i -> r) -> r
c # f = f $ i c
Run Code Online (Sandbox Code Playgroud)
有了像这样的界面
data IDrawable' = IDrawable { draw :: IO () }
Run Code Online (Sandbox Code Playgroud)
我想要有类似的东西
type IDrawable c = Interface IDrawable' c
Run Code Online (Sandbox Code Playgroud)
所以我能做到
data Object = Object { objectDraw :: IO () }
data Person = Person { …
Run Code Online (Sandbox Code Playgroud) 我想在Dictionary中存储从泛型类派生的类的实例; 也就是说,Dictionary应该存储从这个泛型派生的任何类的实例.
像这样的东西:
class ParentClass {}
class ChildClass: ParentClass {}
class GenericClass<T: ParentClass> {
var foo:T?
}
typealias DerivedClass = GenericClass<ChildClass>
class TestGenerics {
var dict: Dictionary<String, GenericClass<**what goes here??**>> = [:]
func test() {
var derivedClassInstance = DerivedClass()
dict.updateValue(derivedClassInstance, forKey: "name")
}
}
Run Code Online (Sandbox Code Playgroud)
这在Java中相当简单:
public class TestGenericsOuter {
class ParentClass {}
class ChildClass extends ParentClass {}
class GenericClass<T extends ParentClass> {
T foo;
}
class DerivedClass extends GenericClass<ChildClass> {}
class TestGenerics {
Dictionary<String, GenericClass<? extends ParentClass>> dict; …
Run Code Online (Sandbox Code Playgroud) 我今天遇到了这个有趣的例子
class TestableEq(object):
def __init__(self):
self.eq_run = False
def __eq__(self, other):
self.eq_run = True
if isinstance(other, TestableEq):
other.eq_run = True
return self is other
Run Code Online (Sandbox Code Playgroud)
>>> eq = TestableEq()
>>> eq.eq_run
False
>>> eq == eq
True
>>> eq.eq_run
True
>>> eq = TestableEq()
>>> eq is eq
True
>>> eq.eq_run
False
>>> [eq] == [eq]
True
>>> eq.eq_run # Should be True, right?
False
>>> (eq,) == (eq,) # Maybe with tuples?
True
>>> eq.eq_run
False
>>> {'eq': eq} == …
Run Code Online (Sandbox Code Playgroud) 我有一个想要实现的简单结构Index
,但作为 Rust 的新手,我在借用检查器方面遇到了许多麻烦。我的结构非常简单,我想让它存储一个开始和步长值,然后当被 a 索引时usize
它应该返回start + idx * step
:
pub struct MyStruct {
pub start: f64,
pub step: f64,
}
Run Code Online (Sandbox Code Playgroud)
我的直觉是,我可以简单地获取签名Index
并插入我的类型:
impl Index<usize> for MyStruct {
type Output = f64;
fn index(&self, idx: usize) -> &f64 {
self.start + (idx as f64) * self.step
}
}
Run Code Online (Sandbox Code Playgroud)
这给出了错误mismatched types
说expected type &f64, found type f64
。作为还没有完全理解 Rust 的类型系统如何工作的人,我尝试简单地&
对表达式进行处理:
fn index(&self, idx: usize) -> &f64 {
&(self.start + (idx as …
Run Code Online (Sandbox Code Playgroud)