Swift 读标准库源码笔记 -- Integers(基本数据类型篇),带你碾压面试官

52 阅读7分钟

总结

面试前要精心做好准备,简历上写的知识点和原理都需要准备好,项目上多想想难点和亮点,这是面试时能和别人不一样的地方。

还有就是表现出自己的谦虚好学,以及对于未来持续进阶的规划,企业招人更偏爱稳定的人。

万事开头难,但是程序员这一条路坚持几年后发展空间还是非常大的,一切重在坚持。

前端面试题汇总

JavaScript

前端资料汇总

开源分享:docs.qq.com/doc/DSmRnRG…

/// The AdditiveArithmetic protocol provides a suitable basis for additive

/// arithmetic on scalar values, such as integers and floating-point numbers,

/// or vectors. You can write generic methods that operate on any numeric type

/// in the standard library by using the AdditiveArithmetic protocol as a

/// generic constraint.

/// “加法算术”协议为标量值(如整数、浮点数或向量)的加法算术提供了一个合适的基础。通过使

/// 用“additivearith”协议作为泛型约束,可以编写对标准库中的任何数字类型进行操作的泛型方法。

///

/// The following code declares a method that calculates the total of any

/// sequence with AdditiveArithmetic elements.

///

/// extension Sequence where Element: AdditiveArithmetic {

/// func sum() -> Element {

/// return reduce(.zero, +)

/// }

/// }

///

/// The sum() method is now available on any sequence with values that

/// conform to AdditiveArithmetic, whether it is an array of Double or a

/// range of Int.

///

/// let arraySum = [1.1, 2.2, 3.3, 4.4, 5.5].sum()

/// // arraySum == 16.5

///

/// let rangeSum = (1..<10).sum()

/// // rangeSum == 45

///

/// Conforming to the AdditiveArithmetic Protocol

/// =============================================

///

/// To add AdditiveArithmetic protocol conformance to your own custom type,

/// implement the required operators, and provide a static zero property

/// using a type that can represent the magnitude of any value of your custom

/// type.

public protocol AdditiveArithmetic : Equatable {

/// The zero value.

///

/// Zero is the identity element for addition. For any value,

/// x + .zero == x and .zero + x == x.

static var zero: Self { get }

/// Adds two values and produces their sum.

///

/// The addition operator (+) calculates the sum of its two arguments. For

/// example:

///

/// 1 + 2 // 3

/// -10 + 15 // 5

/// -15 + -5 // -20

/// 21.5 + 3.25 // 24.75

///

/// You cannot use + with arguments of different types. To add values of

/// different types, convert one of the values to the other value's type.

///

/// let x: Int8 = 21

/// let y: Int = 1000000

/// Int(x) + y // 1000021

///

/// - Parameters:

/// - lhs: The first value to add.

/// - rhs: The second value to add.

static func +(lhs: Self, rhs: Self) -> Self

/// Adds two values and stores the result in the left-hand-side variable.

///

/// - Parameters:

/// - lhs: The first value to add.

/// - rhs: The second value to add.

static func +=(lhs: inout Self, rhs: Self)

/// Subtracts one value from another and produces their difference.

///

/// The subtraction operator (-) calculates the difference of its two

/// arguments. For example:

///

/// 8 - 3 // 5

/// -10 - 5 // -15

/// 100 - -5 // 105

/// 10.5 - 100.0 // -89.5

///

/// You cannot use - with arguments of different types. To subtract values

/// of different types, convert one of the values to the other value's type.

///

/// let x: UInt8 = 21

/// let y: UInt = 1000000

/// y - UInt(x) // 999979

///

/// - Parameters:

/// - lhs: A numeric value.

/// - rhs: The value to subtract from lhs.

static func -(lhs: Self, rhs: Self) -> Self

/// Subtracts the second value from the first and stores the difference in the

/// left-hand-side variable.

///

/// - Parameters:

/// - lhs: A numeric value.

/// - rhs: The value to subtract from lhs.

static func -=(lhs: inout Self, rhs: Self)

}

public extension AdditiveArithmetic where Self : ExpressibleByIntegerLiteral {

/// The zero value.

///

/// Zero is the identity element for addition. For any value,

/// x + .zero == x and .zero + x == x.

static var zero: Self {

return 0

}

}

上面的代码主要介绍了AdditiveArithmetic协议:

AdditiveArithmetic协议里的+,+=,-,-=四个方法,还有一个zero属性,下面是zero的默认实现。

协议用法如注释例子(重复一遍):

extension Sequence where Element: AdditiveArithmetic {

func sum() -> Element {

return reduce(.zero, +)

}

}

let arraySum = [1.1, 2.2, 3.3, 4.4, 5.5].sum()

// arraySum == 16.5

let rangeSum = (1..<10).sum()

// rangeSum == 45

第三段:数字协议

//===----------------------------------------------------------------------===//

//===--- 数字协议 ----------------------------------------------------------===//

//===----------------------------------------------------------------------===//

/// A type with values that support multiplication.

/// 遵守该协议的类型值会支持乘法性质运算

///

/// The Numeric protocol provides a suitable basis for arithmetic on

/// scalar values, such as integers and floating-point numbers. You can write

/// generic methods that operate on any numeric type in the standard library

/// by using the Numeric protocol as a generic constraint.

///

/// Numeric这个协议为类似整型,浮点型这种标量提供计算的基础,

/// 你可以写操作标准库中任何数字类型的泛型方法,使用“Numeric”协议作为通用约束。

///

/// The following example extends Sequence with a method that returns an

/// array with the sequence's values multiplied by two.

///

/// 下面的例子是扩展了Sequence ,添加了一个方法使序列的每一个值都乘以2

///

/// extension Sequence where Element: Numeric {

/// func doublingAll() -> [Element] {

/// return map { $0 * 2 }

/// }

/// }

///

/// With this extension, any sequence with elements that conform to Numeric

/// has the doublingAll() method. For example, you can double the elements of

/// an array of doubles or a range of integers:

///

/// let observations = [1.5, 2.0, 3.25, 4.875, 5.5]

/// let doubledObservations = observations.doublingAll()

/// // doubledObservations == [3.0, 4.0, 6.5, 9.75, 11.0]

///

/// let integers = 0..<8

/// let doubledIntegers = integers.doublingAll()

/// // doubledIntegers == [0, 2, 4, 6, 8, 10, 12, 14]

///

/// Conforming to the Numeric Protocol

/// ==================================

///

/// To add Numeric protocol conformance to your own custom type, implement

/// the required initializer and operators, and provide a magnitude property

/// using a type that can represent the magnitude of any value of your custom

/// type.

/// 自定义类型想去遵守该协议,需要实现初始化方法和操作符, 和提供一个magnitude属性。

///

public protocol Numeric : AdditiveArithmetic, ExpressibleByIntegerLiteral {

/// Creates a new instance from the given integer, if it can be represented

/// exactly.

/// 可以从给定的整型初始化,并成功转换。

///

/// If the value passed as source is not representable exactly, the result

/// is nil. In the following example, the constant x is successfully

/// created from a value of 100, while the attempt to initialize the

/// constant y from 1_000 fails because the Int8 type can represent

/// 127 at maximum:

///

/// 如果给定的数据不能准确的转换成功,那么结果返回nil.

/// 下面的例子,100转换成常量x被成功创建.当试图去从1_000创建y的时候失败了,因为Int8 类型只能在 127之内转换。

/// let x = Int8(exactly: 100)

/// // x == Optional(100)

/// let y = Int8(exactly: 1_000)

/// // y == nil

///

/// - Parameter source: A value to convert to this type.

init?(exactly source: T)

/// A type that can represent the absolute value of any possible value of the

/// conforming type.

/// 一个可以等同于准守协议类型值绝对值的类型。

associatedtype Magnitude : Comparable, Numeric

/// The magnitude of this value.

///

/// For any numeric value x, x.magnitude is the absolute value of x.

/// You can use the magnitude property in operations that are simpler to

/// implement in terms of unsigned values, such as printing the value of an

/// integer, which is just printing a '-' character in front of an absolute

/// value.

/// numeric类型的值xx.magnitudex的绝对值。

/// 你可以使用这个属性很容易的去使用无符号值,例如打印一个整数的值,它只是在绝对值前面打印一个“-”字符。

/// let x = -200

/// // x.magnitude == 200

///

/// The global abs(_:) function provides more familiar syntax when you need

/// to find an absolute value. In addition, because abs(_:) always returns

/// a value of the same type, even in a generic context, using the function

/// instead of the magnitude property is encouraged.

/// 当您需要查找绝对值时,全局' abs(:) '函数提供了更熟悉的语法。此外,因为' abs(:) '总是返回

/// 相同类型的值,即使在通用上下文中也是如此,因此建议使用函数而不是'magnitude'属性。

var magnitude: Magnitude { get }

/// Multiplies two values and produces their product.

///

/// The multiplication operator (*) calculates the product of its two

/// arguments. For example:

///

/// 2 * 3 // 6

/// 100 * 21 // 2100

/// -10 * 15 // -150

/// 3.5 * 2.25 // 7.875

///

/// You cannot use * with arguments of different types. To multiply values

/// of different types, convert one of the values to the other value's type.

///

/// let x: Int8 = 21

/// let y: Int = 1000000

/// Int(x) * y // 21000000

///

/// - Parameters:

/// - lhs: The first value to multiply.

/// - rhs: The second value to multiply.

static func *(lhs: Self, rhs: Self) -> Self

/// Multiplies two values and stores the result in the left-hand-side

/// variable.

///

/// - Parameters:

/// - lhs: The first value to multiply.

/// - rhs: The second value to multiply.

/// lhs 要使用inout,为了避免值拷贝,应在原来内存的值里修改。

static func *=(lhs: inout Self, rhs: Self)

}

第四段:有符号数字协议

/// A type that can represent both positive and negative values.

/// 一个可以同时表示正值和负值得类型

///

/// The SignedNumeric protocol extends the operations defined by the

/// Numeric protocol to include a value's additive inverse.

///

/// SignedNumeric协议扩展了Numeric的操作,包括了加性逆元素(-x 是 x 的加性逆元素)。

/// Conforming to the SignedNumeric Protocol

/// ========================================

///

/// Because the SignedNumeric protocol provides default implementations of

/// both of its required methods, you don't need to do anything beyond

/// declaring conformance to the protocol and ensuring that the values of your

/// type support negation. To customize your type's implementation, provide

/// your own mutating negate() method.

///

///因为' SignedNumeric '协议提供了它所需要的两个方法的默认实现,所以除了声明协议的一致性和确保类型 的值支持否定之外,您不需要做任何事情。要自定义类型的实现,请提供您自己的“negate()”方法。

///

/// When the additive inverse of a value is unrepresentable in a conforming

/// type, the operation should either trap or return an exceptional value. For

/// example, using the negation operator (prefix -) with Int.min results in

/// a runtime error.

///

/// 当准守这个协议的值不可以被表示出来的时候,应该被捕捉或者返回异常值。

/// 举个例子,在Int.min使用用负号(前缀-)运行时会报错误。(因为内存溢出)

/// let x = Int.min

/// let y = -x

/// // Overflow error

public protocol SignedNumeric : Numeric {

/// Returns the additive inverse of the specified value.

///

/// The negation operator (prefix -) returns the additive inverse of its

/// argument.

///

/// let x = 21

/// let y = -x

/// // y == -21

///

/// The resulting value must be representable in the same type as the

/// argument. In particular, negating a signed, fixed-width integer type's

/// minimum results in a value that cannot be represented.

///

/// let z = -Int8.min

/// // Overflow error

///

/// - Returns: The additive inverse of this value.

static prefix func - (_ operand: Self) -> Self

/// Replaces this value with its additive inverse.

///

/// The following example uses the negate() method to negate the value of

/// an integer x:

///

/// var x = 21

/// x.negate()

/// // x == -21

///

/// The resulting value must be representable within the value's type. In

/// particular, negating a signed, fixed-width integer type's minimum

/// results in a value that cannot be represented.

///

/// var y = Int8.min

/// y.negate()

/// // Overflow error

mutating func negate()

}

extension SignedNumeric {

/// Returns the additive inverse of the specified value.

///

/// The negation operator (prefix -) returns the additive inverse of its

/// argument.

///

/// let x = 21

/// let y = -x

/// // y == -21

///

/// The resulting value must be representable in the same type as the

/// argument. In particular, negating a signed, fixed-width integer type's

/// minimum results in a value that cannot be represented.

///

/// let z = -Int8.min

/// // Overflow error

///

文末

逆水行舟不进则退,所以大家要有危机意识。

同样是干到35岁,普通人写业务代码划水,榜样们深度学习拓宽视野晋升管理。

这也是为什么大家都说35岁是程序员的门槛,很多人迈不过去,其实各行各业都是这样都会有个坎,公司永远都缺的高级人才,只用这样才能在大风大浪过后,依然闪耀不被公司淘汰不被社会淘汰。

为了帮助大家更好温习重点知识、更高效的准备面试,特别整理了《前端工程师核心知识笔记》电子稿文件。

内容包括html,css,JavaScript,ES6,计算机网络,浏览器,工程化,模块化,Node.js,框架,数据结构,性能优化,项目等等。

269页《前端大厂面试宝典》

包含了腾讯、字节跳动、小米、阿里、滴滴、美团、58、拼多多、360、新浪、搜狐等一线互联网公司面试被问到的题目,涵盖了初中级前端技术点。

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

前端面试题汇总