Introduction to Basic Haskell Components (In Chinese)ChengHui Weng
In 2012, we had the first Chinese functional meetup about general functional programming techniques in Taipei. I gave this talk to introduce several classes in the famous Typeclassesopedia article.
My inspiration from reading *C++ Primer*, *Effective C++*, *More Effective C++*, *The C++ Standard Library* and some experience from coding.
Include:
* Debug
* C++ Syntax
* Habit && Optimization
* Trick
* Trap
* Reference
Since the previous LTS, JDK 17, was released in 2021, there are quite lots of features added into the Java specification. In this year, the latest JDK 21 LTS version is released and brings us many formal functions to improve our daily works. This session will focus on the new features, including String Templates, Sequenced Collections, Record Patterns, Pattern Matching for switch, Unnamed features and Virtual Threads.
Introduction to Basic Haskell Components (In Chinese)ChengHui Weng
In 2012, we had the first Chinese functional meetup about general functional programming techniques in Taipei. I gave this talk to introduce several classes in the famous Typeclassesopedia article.
My inspiration from reading *C++ Primer*, *Effective C++*, *More Effective C++*, *The C++ Standard Library* and some experience from coding.
Include:
* Debug
* C++ Syntax
* Habit && Optimization
* Trick
* Trap
* Reference
Since the previous LTS, JDK 17, was released in 2021, there are quite lots of features added into the Java specification. In this year, the latest JDK 21 LTS version is released and brings us many formal functions to improve our daily works. This session will focus on the new features, including String Templates, Sequenced Collections, Record Patterns, Pattern Matching for switch, Unnamed features and Virtual Threads.
24. //定义变量
var myVariable = 42 // 一旦赋值为 Int 类型,不可改变
myVariable = 5_000
// 使用中文、Unicode 字符当变量名
var 微博域名 = "http://weibo.com"
// 定义常量
let myConstant = 42
let explicitDouble: Double = 70 // 带类型定义常量
let label = "some string" // 定义字符串
let appleSummary = "I have (myVariable) apples."
/*
* 注释是可以嵌套的。。。
/* 这是注释 */
*/
25. // 数组
var shoppingList = ["catfish", "water", "tulips"]
shoppingList[1] = "bottle of water"
// 字典
var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechainc",
]
Occupations["Jayne"] = "Public Relations"
26. // Optional
var str1 // 不赋值,直接报错
var str: String?
println(str) // 输出 nil
if let str2 = str { // 注意,if 的条件必须是布尔表达式
println(str)
} else {
println("str is nil")
}
// Optional chaining
if let upper =
john.residence?.buildingIdentifier()?uppercaseString {
println("John’s uppercase building id is (upper)")
} else {
println("I can’t find john’s address")
}
27. // Tuples
let http404Error = (404, "Not Found")
let (statusCode, statusMessage) = http404Error
println("The status code is (statusCode)")
// prints "The status code is 404"
println("The status message is (statusMessage)")
// prints "The status message is Not Found"
let http200Status = (statusCode: 200, description: "OK")
println("The status code is (http200Status.statusCode)")
// prints "The status code is 200"
println("The status message is(http200Status.description)")
// prints "The status message is OK"
28. //范围操作符 .. 和 ...
// .. 相当于 <
for i in 1..3 {
println(i)
}
// 输出 1、2
// ...相当于 <=
for i in 1...3 {
println(i)
}
// 输出 1、2、3
注意:操作符前后,只能是整数,且操作符后面的数字必须 >=0
29. // switch 语句
let vegetable = "red pepper"
switch vegetable {
case "celery":
let vegetableComment = "Add some raisins and make ants
on a log."
case "cucumber", "watercress":
let vegetableComment = "That would make a good tea
sandwich."
case let x where x.hasSuffix("pepper"):
let vegetableComment = "Is it a spicy (x)?"
default:
let vegetableComment = "Everything tastes good in
soup."
}
30. // 函数和闭包
func greet(name: String, day: String) -> String {
return "Hello (name), today is (day)."
}
greet("Bob", "Tuesday")
// 可变参数
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
sumOf()
sumOf(42, 597, 12)
31. // 函数嵌套
func returnFifteen() -> Int {
var y = 10
func add() {
y += 5
}
add()
return y
}
returnFifteen()
// 闭包,没有 func、函数名
numbers.map({
(number: Int) -> Int in
let result = 3 * number
return result
})
32. // 类
class Shape {
var numberOfSides = 0
func simpleDescription() -> String {
return "A shape with (numberOfSides) sides."
}
}
var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
33. // 扩展(Extension)
extension Int: ExampleProtocol {
var simpleDescription: String {
return "The number (self)"
}
mutating func adjust() {
self += 42
}
}
7.simpleDescription
34. // 泛型(Generics)
func swapTwoInts(inout a: Int, inout b: Int) {
let temporaryA = a
a = b
b = temporaryA
}
func swapTwoStrings(inout a: String, inout b: String) {
let temporaryA = a
a = b
b = temporaryA
}
func swapTwoDoubles(inout a: Double, inout b: Double) {
let temporaryA = a
a = b
b = temporaryA
}
35. // 泛型(Generics)
func swapTwoValues<T>(inout a: T, inout b: T) {
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoValues(&someInt, &anotherInt)
// someInt is now 107, and anotherInt is now 3
var someString = "hello"
var anotherString = "world"
swapTwoValues(&someString, &anotherString)