我有一个返回上下文管理器的迭代器。
我想要一个 pythonicwith语句,它模拟多个嵌套语句的行为with,每个嵌套语句对应迭代器返回的每个上下文管理器。
有人可能会说,我想要(已弃用的)函数的泛化contextlib.nested。
我安装了 Agda 并开始玩。我试图定义一个相等类型并提出以下解决方案:
data _?_ {A : Set} : A -> A -> Set where
refl : {x : A} -> x ? x
Run Code Online (Sandbox Code Playgroud)
然而,在 Ulf Norell 和 James Chapman 的教程中,他们是这样定义的:
data _==_ {A : Set}(x : A) : A -> Set where
refl : x == x
Run Code Online (Sandbox Code Playgroud)
与我的版本有什么区别(除了选择不同的角色)?推荐哪一款?
我有一个script.py在服务器上运行的Python CGI脚本.它生成一个CSV文件作为输出.我希望那些不了解文件扩展名的计算机缺乏经验的人能够将文件保存到他们的硬盘驱动器并通过双击来使用它.
问题:如果他们现在在浏览器的保存对话框中单击"确定",则保存文件的名称是script.py,而不是script.csv.
如何在CGI中设置某种"默认文件名"?也许有些HTTP头诡计?我无权访问服务器配置.
我尝试写一个变体show来处理字符串与其他实例不同Show,不包括"并直接返回字符串.但我不知道怎么做.模式匹配?警卫?我找不到任何关于在任何文件中.
这是我试过的,不编译的:
show_ :: Show a => a -> String
show_ (x :: String) = x
show_ x = show x
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
{-# LANGUAGE TypeFamilies #-}
data Twothings a b = Twothings a b
type family Leftthing a where
Leftthing (Twothings a b) = Leftthing a
Leftthing a = a
leftthing :: a -> Leftthing a
leftthing (Twothings a b) = leftthing a
leftthing b = b
Run Code Online (Sandbox Code Playgroud)
它没有编译,出现以下错误:
Couldn't match expected type ‘a’
with actual type ‘Twothings a0 t0’
‘a’ is a rigid type variable bound by
the type signature for:
leftthing :: forall a. a -> Leftthing a
Run Code Online (Sandbox Code Playgroud)
它抱怨这条线leftthing …
假设我定义了一个类A,我不希望任何人写出该类的不等式而不会离开.
class A():
def __ne__(self, other):
return NotImplemented
print(A() != A())
Run Code Online (Sandbox Code Playgroud)
但这打印出来True并没有提出一个TypeError虽然我故意"关闭"了!=运营商?
在长长的例子中提前道歉,我无法想出一个较短的例子.
让我们定义一个类型类Box,除了包含另一种类型之外什么都不做Content.
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
class Box t where
type Content t
data IntBox = IntBox
data StringBox = StringBox
Run Code Online (Sandbox Code Playgroud)
我们来写一些例子:
instance Box IntBox where
type Content IntBox = Int
instance Box StringBox where
type Content StringBox = String
data JointBox a b = JointBox a b
instance (Box a, Box b) => Box (JointBox a b) where
type Content (JointBox a b) = Either (Content a) (Content b)
Run Code Online (Sandbox Code Playgroud)
到目前为止,这些都是编译和工作.输入GADT.我想要一个由一个盒子及其内容构成的代数数据类型.构造函数完全决定了框的类型.
data …Run Code Online (Sandbox Code Playgroud) 众所周知,如何ContT基于 Koen Claessen 的功能性明珠基于 制作纯并发 monad :
data Action m where
Atom :: m (Action m) -> Action m
Fork :: [Action m] -> Action m
Stop :: Action m
fork :: Applicative m => [ContT (Action m) m a] -> ContT (Action m) m ()
fork processes = ContT $ \next -> Fork <$> sequenceA (next () : [ process $ const $ pure $ Const | ContT process <- processes ])
Run Code Online (Sandbox Code Playgroud)
我将如何实现像IORefs 或MVar …
请考虑以下代码:
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ScopedTypeVariables #-}
class Foo a where
type Bar a
class Foo a => Foo2 a where
bar :: Bar a
Run Code Online (Sandbox Code Playgroud)
它在GHC 8.2中给出以下错误消息:
error:
• Couldn't match expected type ‘Bar a’ with actual type ‘Bar a0’
NB: ‘Bar’ is a type function, and may not be injective
The type variable ‘a0’ is ambiguous
• In the ambiguity check for ‘bar’
To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
When checking the class …Run Code Online (Sandbox Code Playgroud) 假设我有一个表"节点",我存储一棵树.每个节点都有一个主键id和一个parent_id列.当然,我想访问每个节点实例的父属性,即关系.有人可能会尝试:
import sqlalchemy, sqlalchemy.orm, sqlalchemy.ext.declarative
engine = sqlalchemy.create_engine('sqlite:///PATHTOMYDATABASE', echo=True)
Base = sqlalchemy.ext.declarative.declarative_base()
class Node(Base):
__tablename__ = "nodes"
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
parent_id = sqlalchemy.Column(sqlalchemy.Integer, sqlalchemy.ForeignKey("nodes.id"))
parent = sqlalchemy.orm.relationship("Node", backref=sqlalchemy.orm.backref("childs"))
Base.metadata.create_all(engine)
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,我收到一个错误:
sqlalchemy.exc.InvalidRequestError: Table 'nodes' is already defined for this MetaData instance. Specify 'useexisting=True' to redefine options and columns on an existing Table object.
我不明白我可以在什么时候设置这个选项'useexisting=True'.这是正确的方法吗?
编辑:事实上,错误来自原始脚本的另一部分.如果用临时数据库替换数据库路径:memory:,它可以正常工作.感谢TokenMacGuy.
因此,上面的例子可以被视为一个工作示例.
我知道tail函数返回列表的最后n-1个元素(其中n是列表的长度),所以我定义了自己的"cotail"函数来返回前n-1个元素:
cotail = (reverse . tail . reverse)
Run Code Online (Sandbox Code Playgroud)
这是最好的方法,还是有内置函数或更聪明的方法来实现这一目标?
haskell ×6
python ×3
types ×2
agda ×1
cgi ×1
concurrency ×1
equality ×1
filenames ×1
foreign-keys ×1
gadt ×1
http ×1
iterator ×1
list ×1
monads ×1
nested ×1
python-3.x ×1
sqlalchemy ×1
tail ×1