我有很多像这样的代码:
data Post =
Post
{ postOwner :: Integer
, postText :: ByteString
, postDate :: ByteString
}
sqlToPost :: [SqlValue] -> Post
sqlToPost [owner, text, date] = Post (fromSql owner) (fromSql text) (fromSql date)
Run Code Online (Sandbox Code Playgroud)
(这里使用的图书馆是HDBC).将来,会有很多类似的数据Post和功能sqlToVal.我可以减少这个样板代码sqlToVal吗?
Haskell为块提供了基于缩进的样式.我知道两种风格,但我无法确定哪种风格更好.(请原谅我一个非常愚蠢的示例函数)
第1名 - 漂亮:
funcA :: Integer -> IO ()
funcA n = if n == 0
then putStrLn "zero"
else do putStr "--> "
print n
Run Code Online (Sandbox Code Playgroud)
这种风格看起来很棒,但它非常脆弱:让我们重构这段代码并重命名funcA- 我们需要重新启动then,else以及所有表达式do.同名重命名n.这很烦人.非常.
编辑正如FUZxxl所提到的,线条变得越来越长,但大多数线条 - 开头的空格.
另一种风格是重构友好,但不是那么漂亮:
funcA :: Integer -> IO ()
funcA n = if n == 0
then putStrLn "zero"
else do
putStr "zero"
print n
Run Code Online (Sandbox Code Playgroud)
你更喜欢什么样的风格?为什么?可能你有另一个或者你有一些链接到代码风格的一些伟大的开发人员解释?
我用CherryPy写了一个小小的webapp.但我有一个问题 - 我无法获取我的POST数据,但GET没问题.使用CherryPy内置服务器在Opera 10上查看本地主机(Win 7).
这是一些代码:
class Expose:
def __init__(self, fn):
self.fn = fn
@cherrypy.expose()
def index(self, login=None):
print 'LOGIN: ' + str(login)
return self.fn(login=login)
import auth
root.process_form = Expose(auth.process_form)
Run Code Online (Sandbox Code Playgroud)
这是我的URL切换.如果表单使用POST,并且GET的值正确,则LOGIN打印无.这是我的表格(模板):
<!DOCTYPE html>
<body>
<p>Create new user</p>
<form action="/process_form" method="post">
<input type="text" name="login" value="login" />
<input type="text" name="email" value="me@company.com" />
<input type="text" name="password" value="123" />
<input type="submit" />
</form>
</body>
Run Code Online (Sandbox Code Playgroud)
我无法猜出出了什么问题.我可以检查什么?
定义Haskell函数的经典方法是
f1 :: String -> Int
f1 ('-' : cs) -> f1 cs + 1
f1 _ = 0
Run Code Online (Sandbox Code Playgroud)
我有点不满意在每一行写函数名.现在我通常用以下方式编写,使用模式保护扩展并认为它更易读和修改友好:
f2 :: String -> Int
f2 s
| '-' : cs <- s = f2 cs + 1
| otherwise = 0
Run Code Online (Sandbox Code Playgroud)
你认为第二个例子更具可读性,可修改性和优雅性吗?生成代码怎么样?(还没有时间看到脱落的输出,对不起!).什么是利弊?我唯一看到的是扩展用法.
按照关于搜索参数的文档https://developers.google.com/drive/search-parameters,我可以将参数与and.mimeType-s可以测试=.我可以在一个查询中提取多个mime类型文件吗?没有or运营商在doc或者,如果我可以使用contains了mimeType.
考虑以下Clojure代码:
(defn space? [c] (Character/isWhitespace c))
Run Code Online (Sandbox Code Playgroud)
没关系.但显然,这只是无意义风格的另一个名称和重构:
(def space? Character/isWhitespace)
Run Code Online (Sandbox Code Playgroud)
但我得到编译错误:
Caused by: java.lang.RuntimeException: Unable to find static field: isWhitespace
in class java.lang.Character
Run Code Online (Sandbox Code Playgroud)
为什么找不到它?它与Clojure功能完美配合:
(def wrap-space? space?) ; Compiles OK!
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?为什么def用Java静态函数不起作用?
让我从我想解决的任务开始,可能是我走错了路.我将Snap框架用于玩具项目,主要是它在Snapmonad 下的功能.我需要在它上面添加我的状态.我用monad变压器:
type SnapApp a = StateT AppState Snap a
Run Code Online (Sandbox Code Playgroud)
例如,这在模块中定义Base.由于我在其他模块中需要它,我必须导出它:
module Base
( ..
, SnapApp
) where
Run Code Online (Sandbox Code Playgroud)
这很好,但我希望该模块不会导出SnapApp状态monad,因为我有一些复杂的处理来为状态设置一些属性.例如,会话.我有当它变以写入文件,因此它不应当只是get比put修改的会话,特殊功能应该被调用.所以,我隐藏了使用newtype而不是数据导出construstor:
newtype SnapApp a = SnapApp (StateT AppState Snap a)
Run Code Online (Sandbox Code Playgroud)
我创建了我的类的实例,其中包含用于修改会话等的函数.但问题出现了:我丢失了Monad类和其他类的实例SnapApp.我坚持执行>>=:
instance Monad SnapApp where
return = SnapApp . return
mx >>= fm = -- HOW?
Run Code Online (Sandbox Code Playgroud)
谢谢!
此Go代码始终在模板中引用字符串:http://play.golang.org/p/8k4s8dv2PE - 您可以看到结果.我怎么生成var currentUser = null?请注意,它还会删除代码中的所有注释!它是如何调整的?这个问题是我的Go:模板中引用的字符串的延续.
这个问题类似于说,在clojure中,如何将'和'应用到列表中?,但解决方案不适用于我的问题.every?function只返回一个布尔值.
or 是一个宏,所以这段代码无效:
(apply or [nil 10 20]) ; I want to get 10 here, as if 'or was a function.
Run Code Online (Sandbox Code Playgroud)
使用not-every?我将得到布尔值,但我想保留语义or- "true"元素应该是表达式的值.什么是惯用的方法呢?
我的想法到目前为止:
(first (drop-while not [nil 10 20])) ; = 10
Run Code Online (Sandbox Code Playgroud) 让我们制作频率图:
(reduce #(update-in %1 [%2] (fnil inc 0)) {} ["a" "b" "a" "c" "c" "a"])
Run Code Online (Sandbox Code Playgroud)
我担心的是lambda中的表达#(...)- 这是规范的方式吗?我能做得更好/更短吗?
编辑:我发现的另一种方式:
(reduce #(assoc %1 %2 (inc %1 %2 0)) {} ["a" "b" "a" "c" "c" "a"])
Run Code Online (Sandbox Code Playgroud)
看似非常相似,有什么利弊?性能?