在Swift中,扩展是一种在事后定义类成员的方法.或者,你可以说,它是(来自一个全新的)组合函数的奇特方式:
extension Double {
var mm: Double { return self * 1_000.0 }
func mm1() -> Double { return self * 1_000.0 }
}
func mm(a: Double) -> Double {
return a * 1_000.0
}
print("One meter is \(1.mm) milimeters")
print("One meter is \(1.mm1()) milimeters")
print("One meter is \(mm(1)) milimeters")
Run Code Online (Sandbox Code Playgroud)
我从来没有见过这样的东西.在任何其他语言中都有这样的东西吗?
我试图理解像 RAII 这样的 C++ 和像 ARC 这样的 Obj-C 或 Swift 之间的区别。
假设我有这个功能:
foo() {
bar = new obj();
} // lifetime of bar object ends
Run Code Online (Sandbox Code Playgroud)
如果 obj 是 RAII 语言中的对象,bar则会调用 s 析构函数。
如果 obj 是 ARC 语言中的对象,则bars 保留计数将减一,可能会释放它。
因此,在这一点上,这两种方法都具有另一种方法所缺乏的一些功能:
ARC 知道bar程序的其他部分是否被指向(这在我的示例中基本上是不可能的,但请耐心等待),并且可以利用该信息来发挥它的优势。
RAII 可以做的不仅仅是释放对象,因为您可以根据需要定义析构函数。
然而,C++ 也有智能指针,Swift 和 Obj-C 有释放函数。那么这些语言的功能基本不一样,Swift 基本上可以称为 RAII 语言,C++ 可以称为 ARC 语言(或者说can be used 可以与 ARC idiom 一起使用)?
我正在尝试理解一些Haskell源代码,我有时会遇到这种结构:
x <*> y <$> z
Run Code Online (Sandbox Code Playgroud)
例如
(+) <*> (+1) <$> a
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释这个结构吗?我得到它转换为fmap a (+ a + 1),但我无法建立连接
我有以下结构:
y = [
fromList([("c", 1 ::Int)]),
fromList([("c", 5)]),
fromList([("d", 20)])
]
Run Code Online (Sandbox Code Playgroud)
我可以用它来更新每个“c”:
y & mapped . at "c" . mapped %~ (+ 1)
-- [fromList [("c",2)], fromList [("c",6)], fromList [("d",20)]]
Run Code Online (Sandbox Code Playgroud)
所以第三个条目基本上被忽略了。但我想要的是操作失败。
仅更新,如果所有映射都包含键“c”。
所以我想要:
y & mysteryOp
-- [fromList [("c",1)], fromList [("c",5)], fromList [("d",20)]]
-- fail because third entry does not contain "c" as key
Run Code Online (Sandbox Code Playgroud)
我想我知道在这里使用哪些功能:
over
-- I want to map the content of the list
mapped
-- map over the structure and transform to [(Maybe Int)]
traverse
-- …Run Code Online (Sandbox Code Playgroud) 无论我做什么,我似乎都没有为shell脚本运行语法折叠。
所以我有一个叫abc.shcall 的文件
:let g:sh_fold_enabled=7
:let g:is_bash=1
:set foldmethod=syntax
Run Code Online (Sandbox Code Playgroud)
但是它仍然找不到任何褶皱。这是为什么?
我见过几个程序员在将文件提供给 less 之前使用 ./lessfilter 过滤器来预处理文件。\n但是我的 less 似乎无法识别 ~ 中的 .lessfilter 文件。\n我做错了什么?我的 less 手册页没有提到它。
\n\n$ less \xe2\x80\x94version\nless 481 (POSIX regular expressions)\nCopyright (C) 1984-2015 Mark Nudelman\nRun Code Online (Sandbox Code Playgroud)\n 我有这个功能:
min (max 10 20) (max 30 40)
Run Code Online (Sandbox Code Playgroud)
我可以改写为:
min (max 10 20) $ max 30 40
Run Code Online (Sandbox Code Playgroud)
但是还有办法解决这些最后的括号吗?
不像这不够好,但我不能放下思想,必须有一些方法来做到这一点......
我尝试在ghci提示符中使用终端颜色.
所以,当我打开ghci并尝试:
Prelude> :set prompt '\[\033[1haskell > \033[0m\]'
'\[\033[1\]haskell> \[\033[0m\] '
Run Code Online (Sandbox Code Playgroud)
我知道这些代码是由bash echo和-e标志解释的.但是我怎么能在ghci中做到这一点?
在Haskell中,您有产品类型,并且您有元组.
如果您不想将专用类型与值关联,则可以使用元组,如果您愿意,可以使用产品类型.
但是我觉得产品类型的符号有冗余
data Foo = Foo (String, Int, Char)
data Bar = Bar String Int Char
Run Code Online (Sandbox Code Playgroud)
为什么有这两种符号?有没有你更喜欢另一个的情况?
我猜你在使用元组时不能使用记录符号,但这只是一个方便的问题.另一件事可能是元组中的顺序概念,而不是产品类型,但我认为这只是由于函数fst和命名的命名snd.
我正在使用该bool :: a -> a -> Bool -> a功能。我想使用中缀版本,因为我认为它更具可读性,但我注意到:
(-1) `bool` 1 True
Run Code Online (Sandbox Code Playgroud)
是一个错误
(-1) `bool` 1 $ True
Run Code Online (Sandbox Code Playgroud)
作品。甚至
(-1) `bool` 1 (True)
Run Code Online (Sandbox Code Playgroud)
不起作用,直到现在我认为这是一个平等的选择(即使用$与从该位置到最后用括号括起来)
这怎么能有所作为呢?在第一个版本中只有一个操作。
haskell ×6
bash ×2
applicative ×1
c++ ×1
dollar-sign ×1
folding ×1
functor ×1
ghci ×1
haskell-lens ×1
methods ×1
objective-c ×1
prompt ×1
raii ×1
shell ×1
swift ×1
syntax ×1
tuples ×1
vim ×1
zsh ×1