首先,请查看以下代码段:
package main
import (
"fmt"
)
func main() {
var para1 struct {
email, addr string
}
para1.email = "test@test.com"
if para1 != nil {
fmt.Println(para1)
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译这段代码时,我遇到了编译器错误:
./struct_func.go:15: cannot convert nil to type struct { email string; addr string }
Run Code Online (Sandbox Code Playgroud)
如何判断我的struct变量是否为零?或者我必须验证属性
if para1.email != nil {
fmt.Println(para1)
}
Run Code Online (Sandbox Code Playgroud) 我在第30页阅读了Twelve Go Best Practices和遭遇以及有趣的例子.
func sendMsg(msg, addr string) error {
conn, err := net.Dial("tcp", addr)
if err != nil {
return err
}
defer conn.Close()
_, err = fmt.Fprint(conn, msg)
return err
}
func broadcastMsg(msg string, addrs []string) error {
errc := make(chan error)
for _, addr := range addrs {
go func(addr string) {
errc <- sendMsg(msg, addr)
fmt.Println("done")
}(addr)
}
for _ = range addrs {
if err := <-errc; err != nil {
return err …Run Code Online (Sandbox Code Playgroud) 我有以下elixir代码片段:
defmodule Rectangle do
def area(a, b) do
a * b
end
def area(a) do
a * a
end
end
Run Code Online (Sandbox Code Playgroud)
然后我将文件加载到iex会话中,如下所示:
iex(1)> import_file "rectangle.exs"
{:module, Rectangle,
<<70, 79, 82, 49, 0, 0, 5, 84, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 204, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>,
{:area, 1}}
Run Code Online (Sandbox Code Playgroud)
它像我预期的那样工作得很好
iex(2)> Rectangle.area(9)
81 …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习 angular2dart 并按照anguar2dart 网站的教程进行操作。
考虑以下代码:
import 'package:angular2/core.dart';
class Hero {
final int id;
String name;
Hero(this.id, this.name);
}
@Component(
selector: 'my-app',
template: '''
<h1>{{title}}</h1>
<h2>{{hero.name}} details!</h2>
<div><label>id: </label>{{hero.id}}</div>
<div>
<label>name: </label>
<input [(ngModel)]="hero.name" placeholder="name">
</div>'''
)
class AppComponent {
String title = 'Tour of Heroes';
Hero hero = new Hero(1, 'Windstorm');
}
Run Code Online (Sandbox Code Playgroud)
当我编译它时,它向我显示了错误消息:
Build error:
Transform TemplateCompiler on Sample|lib/app_component.ng_meta.json threw error: Template parse errors:
Can't bind to 'ngModel' since it isn't a known native property or …Run Code Online (Sandbox Code Playgroud) 我有以下匿名功能:
*Exercises> g = \(Sum n) -> Sum (n - 1)
Run Code Online (Sandbox Code Playgroud)
我用它像:
*Exercises> g (Sum 56)
Sum {getSum = 55}
*Exercises> g 56
Sum {getSum = 55}
Run Code Online (Sandbox Code Playgroud)
第二个例子,编译器如何转换56为Sum 56?
在前奏中,我看到这Sum是一个实例Num,但不清楚转换.
我正在玩一下zipWith并遇到以下情况:
Prelude Control.Applicative> :t zipWith id
zipWith id :: [b -> c] -> [b] -> [c]
Run Code Online (Sandbox Code Playgroud)
为什么编译器期望下一个参数是一个函数列表?
我试图分析,但无法断定,为什么下一个参数必须是函数列表.
当我id转到时,签名是如何申请的zipWith?
我有一个真正的问题,派生类型,根本不了解,如何派生类型.
我定义了一个f具有以下实现的函数:
f x y z = x * y * z
Run Code Online (Sandbox Code Playgroud)
函数的类型签名f是:
f :: Num c => c -> c -> c -> c
Run Code Online (Sandbox Code Playgroud)
很简单吧?
现在,适用f于map和id功能,寻找的类型签名.
首先,让我们申请map f:
a -> b "The signature of the first argument of map
~ ~
c -> c -> c -> c "The signature of the f function
Run Code Online (Sandbox Code Playgroud)
你可以看到上面,我如何分离和获得类型相等,即a ~ c和b ~ c -> c -> c. …
我读了一篇关于部分统一的文章,作者提到了左偏和右偏的数据类型.
有人可以向我解释一下,什么是左偏和右偏的数据类型?
它与Function1有什么关系?
是Either左侧偏置或右侧偏置?
我只是尝试编译以下代码:
import cats.Monoid
import cats.instances.boolean._ // for Monoid
import cats.instances.int._ // for Monoid
import cats.instances.list._ // for Monoid
import cats.instances.string._ // for Monoid
import cats.syntax.apply._ // for imapN
import cats.syntax.semigroup._
case class Cat2(name: String, yearOfBirth: Int, favoriteFoods: List[String])
object FancyFunctor {
val tupleToCat: (String, Int, List[String]) => Cat2 =
Cat2.apply _
val catToTuple: Cat2 => (String, Int, List[String]) =
cat => (cat.name, cat.yearOfBirth, cat.favoriteFoods)
implicit val catMonoid: Monoid[Cat2] = (
Monoid[String],
Monoid[Int],
Monoid[List[String]]
).imapN(tupleToCat)(catToTuple)
def main(args: Array[String]): Unit = { …Run Code Online (Sandbox Code Playgroud) 我想知道,Haskell中的自然转变是什么。自然转换用以下签名描述:
F[a] ~> G[a]
Run Code Online (Sandbox Code Playgroud)
例如,我可以转换:
Maybe a ~> List a
Run Code Online (Sandbox Code Playgroud)
对?
那怎么IO做是不可能的呢?
自然转型的目的是什么?