惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
J
Java Code Geeks
雷峰网
雷峰网
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
S
Securelist
The Hacker News
The Hacker News
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
AI
AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
博客园 - 司徒正美
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
U
Unit 42
V
V2EX
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
Recorded Future
Recorded Future
P
Privacy & Cybersecurity Law Blog
美团技术团队
小众软件
小众软件
F
Fortinet All Blogs

博客园 - ZhangShengjie

苹果Universal Link 配置 pod 错误 iignoring ffi-1.15.4 because its extensions are not built. Try: gem pristine ffi --version 1.15.4 Xcode 模拟器 运行ipa iOS订阅详解 Flutter界面跳转 Flutter 使用inspector 调试UI Android Studio 调试flutter项目 Flutter 新建的Project Type类型对比 Mac上好用的数据库 Flutter常见库使用 iOS项目中加入flutter pod init 报错 /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/user_interface/error_report.rb:34:in `force_encoding': can't modify frozen String (FrozenError) HASH与对称加密详解 Mac 安装 Flutter flutter iOS 使用BasicMessageChannel 通信 详解RSA加密原理 insert_dylib 编译没有product class-dump使用报错 Cannot find offset for address 0x88000000010af973 in stringAtAddress: 终端使用ipatool下载Appstore的Ipa文件到电脑
swift高阶函数
ZhangShengjie · 2024-05-14 · via 博客园 - ZhangShengjie

1. map

map 函数用于将一个集合(数组、字典等)中的每个元素都按照一定的规则进行转换,并返回一个新的集合,其中包含转换后的元素。

let numbers = [1, 2, 3, 4, 5]
let doubledNumbers = numbers.map { $0 * 2 }
print(doubledNumbers) // 输出: [2, 4, 6, 8, 10]

在这个例子中,map 函数将数组 numbers 中的每个元素乘以 2,然后返回一个新的数组 doubledNumbers

2. filter

filter 函数用于根据指定的条件筛选集合中的元素,并返回一个只包含符合条件的元素的新集合

let numbers = [1, 2, 3, 4, 5]
let evenNumbers = numbers.filter { $0 % 2 == 0 }
print(evenNumbers) // 输出: [2, 4]

在这个例子中,filter 函数从数组 numbers 中筛选出所有偶数,并返回一个新的数组 evenNumbers

3. reduce

reduce 函数用于将一个集合中的所有元素通过某种规则进行组合,最终得到一个结果。

let numbers = [1, 2, 3, 4, 5]
let sum = numbers.reduce(0) { $0 + $1 }
print(sum) // 输出: 15

在这个例子中,reduce 函数将数组 numbers 中的所有元素相加,初始值为 0

4. contains

contains 函数用于检查集合中是否包含某个特定的元素,并返回一个布尔值表示结果

let numbers = [1, 2, 3, 4, 5]
let containsThree = numbers.contains(3)
print(containsThree) // 输出: true

在这个例子中,contains 函数检查数组 numbers 是否包含元素 3,最终返回 true