当使用require时,我需要在左括号之前使用引号但是当我使用ns时:require我不必使用引号.这是为什么?
(ns foo)
(ns user)
(require '[foo :as f]) ;; quote
(ns bar (:require [foo :refer :all])) ;; no quote
Run Code Online (Sandbox Code Playgroud)
我知道引用不评估括号前面的表达式,但不完全确定为什么在括号前需要引用,因为它们不是表达式所以不应该评估任何内容.
在下面的代码片段中,传递 x 和 y 值会将点放在 (y,x) 坐标中,而绘图是在 (x,y) 中完成的。设置绘图缓冲区以便在同一坐标系中放置像素和绘图的正确方法是什么?
from PIL import Image, ImageDraw
def visual_test(x, y):
grid = np.zeros((100, 100, 3), dtype=np.uint8)
grid[:] = [0, 0, 0]
grid[x, y] = [255, 0, 0]
img = Image.fromarray(grid, 'RGB')
draw = ImageDraw.Draw(img)
draw.line((x, y, x, y-5), fill=(255,255,255), width=1)
img.show()
Run Code Online (Sandbox Code Playgroud) 这是我从下面的简单重现案例中得到的输出:
2015/06/22 21:09:50 ok: false
2015/06/22 21:09:50 stub: *main.Stub <nil>
Run Code Online (Sandbox Code Playgroud)
显然,存根已正确标记为指向存根类型的指针,但转换失败。我正在尝试更新数组的内容。
package main
import "log"
const BUFFER_SIZE = 8
type Value struct {
value int
}
func (v Value) Value() int { return v.value }
func (v *Value) SetValue(value int) { v.value = value }
type Stub struct {
Value
testString string
}
type StubFactory struct{}
type FactoryInterface interface {
NewInstance(size int) []interface{}
}
func (factory StubFactory) NewInstance(size int) []interface{} {
stubs := make([]interface{}, size)
for i, _ := range …Run Code Online (Sandbox Code Playgroud) typealias CBType = () -> Void
class A {
let b = B()
func test() {
let token = b.register { CBType in
self.b.waitFor([token]) // ERROR: Variable used within its own initial value
}
b.dispatch()
}
}
class B {
private var _callbacks = [String:CBType]()
func register(callback: CBType) -> String {
let id = "1234"
_callbacks[id] = callback
return id
}
func dispatch() {
for (_, cb) in self._callbacks {
cb()
}
}
func waitFor(tokens: [String]) {
}
}
A().test() …Run Code Online (Sandbox Code Playgroud) 方案是使用公共字段传递类似的结构,并将它们设置为作为参数传递的值:
package main
type A struct {
Status int
}
type B struct {
id string
Status int
}
// It's okay to pass by value because content is only used inside this
func foo(v interface{}, status int) {
switch t := v.(type) {
case A, B:
t.Status = status // ERROR :-(
}
}
func main() {
a := A{}
foo(a, 0)
b := B{}
b.id = "x"
foo(b, 1)
}
Run Code Online (Sandbox Code Playgroud)
令我沮丧的是,我收到了这个错误:
? test go run test.go
# command-line-arguments …Run Code Online (Sandbox Code Playgroud) 约束此扩展的正确方法是什么?我的试验没有成功.
例如:
extension CollectionType where Generator.Element: [T.Key:Self.Value], T: DictionaryLiteralConvertible, T.Key: StringLiteralConvertible, T.Value: Encodable {
Run Code Online (Sandbox Code Playgroud) Go的主要主题专家之一Dave Cheney写道:"在使用复合文字初始化变量时,Go要求复合文字的每一行以逗号结尾,即使是声明的最后一行.这是分号规则的结果."
但是,当我尝试将这个漂亮的规则应用于JSON文本时,解析器似乎不同意这种理念.在下面的代码中,删除逗号是有效的.有没有解决这个问题,所以当我在差异中添加元素时,我可以看到一行更改?
package main
import (
"fmt"
"encoding/json"
)
type jsonobject struct {
Objects []ObjectType `json:"objects"`
}
type ObjectType struct {
Name string `json:"name"`
}
func main() {
bytes := []byte(`{ "objects":
[
{"name": "foo"}, // REMOVE THE COMMA TO MAKE THE CODE WORK!
]}`)
jsontype := &jsonobject{}
json.Unmarshal(bytes, &jsontype)
fmt.Printf("Results: %v\n", jsontype.Objects[0].Name) // panic: runtime error: index out of range
}
Run Code Online (Sandbox Code Playgroud) 令人惊讶的是,下面的代码打印出来,SAME而初始化程序Z()每次都应该调用构造函数。如何使用具有不同实例的这种方法初始化数组Z?
import Foundation
class Z {
var i: Int = 0
}
var z: [Z] = [Z](repeating: Z(), count: 10)
if z[0] === z[1] {
print("SAME")
} else {
print("NOT SAME")
}
Run Code Online (Sandbox Code Playgroud) Swift 4 中最近的变化提供了使用 Data 对象初始化字节数组的简单方法。结果使您[UInt8]可以将整个数据存储在其中。
let array = [UInt8](data)
Run Code Online (Sandbox Code Playgroud)
我找不到仅使用偏移量和长度部分加载相同数据对象的解决方案。是否可以不切片整个数组或者我应该切换到 InputStream?
在公共日志格式的维基百科条目中,strftime格式如下:
[10/Oct/2000:13:55:36 -0700]是收到请求的日期,时间和时区,默认情况下为strftime格式%d /%b /%Y:%H:%M: %S%z.
当我尝试使用time.Format函数时:
package main
import (
"fmt"
"time"
)
func main() {
t, _ := time.Parse(time.UnixDate, "Tue Oct 10 13:55:36 PDT 2000")
fmt.Println(time.Time(t).Format("01/Feb/2006:15:04:05 -0700"))
}
Run Code Online (Sandbox Code Playgroud)
我得到了输出[10/Feb/2000:13:55:36 +0000],而我期待[10/Oct/2000:13:55:36 -0700](每个维基百科).我的格式有什么问题?
我检查那天是星期二,那个时间的时区是-7h(PDT).