当前位置: 首页 > news >正文

kotlin 语法糖

  1. Use of “when” Expression Instead of “switch”

    fun getDayOfWeek(day: Int): String {return when (day) {1 -> "Monday"2 -> "Tuesday"3 -> "Wednesday"4 -> "Thursday"5 -> "Friday"6 -> "Saturday"7 -> "Sunday"else -> "Invalid day"}
    }
    
  2. Lambda Expressions and Higher-Order Functions

    val numbers = listOf(1, 2, 3, 4, 5)
    val doubled = numbers.map { it * 2 }  // Lambda expression
    
  3. Destructuring Declarations

    data class Person(val name: String, val age: Int)
    val person = Person("Alice", 30)
    val (name, age) = person  // Destructuring declaration
    
  4. Elvis Operator

    val nullableString: String? = null
    val nonNullableString: String = nullableString ?: "Default Value"  // Elvis operator
    
  5. String Templates

    val greeting = "Hello, $name!"
    
  6. Smart Casts

    fun getStringLength(obj: Any): Int? {if (obj is String) {return obj.length  // Smart cast}return null
    }
    
  7. Single-Expression Functions

    fun square(x: Int) = x * x
    
  8. Default Parameter Values

    fun greet(name: String = "Guest") {println("Hello, $name!")
    }
    
  9. Extension Functions

    fun String.isPalindrome(): Boolean {return this == this.reversed()
    }val word = "madam"
    val isPalindrome = word.isPalindrome()
    
  10. Operator Overloading

    data class Point(val x: Int, val y: Int) {operator fun plus(other: Point) = Point(x + other.x, y + other.y)
    }val point1 = Point(2, 3)
    val point2 = Point(4, 5)
    val point3 = point1 + point2
    
  11. Printing All Results

    fun main() {println(getDayOfWeek(3))  // Output: Wednesdayprintln(doubled)  // Output: [2, 4, 6, 8, 10]println("Name: $name, Age: $age")  // Output: Name: Alice, Age: 30println(nonNullableString)  // Output: Default Valueprintln(greeting)  // Output: Hello, Alice!println(getStringLength("Kotlin"))  // Output: 6println(square(4))  // Output: 16greet()  // Output: Hello, Guest!println(isPalindrome)  // Output: trueprintln(point3)  // Output: Point(x=6, y=8)
    }main()
    

let

The let function is used to invoke a lambda function on the object it is called on. It returns the result of the lambda expression.

val result = "Hello".let {println(it)it.length
}
println(result) // Output: 5

also

The also function is similar to let, but it returns the original object instead of the result of the lambda expression. It’s often used for side effects, such as logging or validation.

val result = "Hello".also {println(it)
}.toUpperCase()
println(result) // Output: HELLO

run

The run function is used to execute a block of code and return its result. It can be called on an object and within a scope.

val result = "Hello".run {println(this)this.length
}
println(result) // Output: 5

with

The with function is a non-extension function that can be used to call multiple methods on the same object without repeating the object’s name.

val result = with("Hello") {println(this)this.length
}
println(result) // Output: 5

apply

The apply function is used for configuring an object. It runs a block of code on the object and returns the object itself.

val result = StringBuilder().apply {append("Hello")append(" World")
}
println(result.toString()) // Output: Hello World

takeIf

The takeIf function returns the object if it satisfies the given predicate; otherwise, it returns null.

val result = "Hello".takeIf {it.length > 3
}
println(result) // Output: Hello

takeUnless

The takeUnless function returns the object if it does not satisfy the given predicate; otherwise, it returns null.

val result = "Hello".takeUnless {it.length > 5
}
println(result) // Output: Hello

相关文章:

kotlin 语法糖

Use of “when” Expression Instead of “switch” fun getDayOfWeek(day: Int): String {return when (day) {1 -> "Monday"2 -> "Tuesday"3 -> "Wednesday"4 -> "Thursday"5 -> "Friday"6 -> "Sa…...

.NET MAUI Sqlite数据库操作(一)

一、安装 NuGet 包 安装 sqlite-net-pcl 安装 SQLitePCLRawEx.bundle_green 二、配置数据库(数据库文件名和路径) namespace TodoSQLite; public static class Constants {public const string DatabaseFilename "TodoSQLite.db3";//数据库…...

SQL 窗口函数

1.窗口函数之排序函数 RANK, DENSE_RANK, ROW_NUMBER RANK函数 计算排序时,如果存在相同位次的记录,则会跳过之后的位次 有 3 条记录排在第 1 位时: 1 位、1 位、1 位、4 位…DENSE_RANK函数 同样是计算排序,即使存在相同位次的记录,也不会跳过之后的位次 有 3 条记录排在…...

staruml怎么合并多个Project工程文件

如图现在有两个staruml文件 现在我想要把project2合并到project1里面 步骤如下: 1、首先打开project2 2、如图选择导出Fragment 3、选中自己想导出的模块(可以不止一个) 4、将其保存在桌面 5、打开project1 6、选择导入 7、选中刚刚…...

设计模式——外观模式

外观模式(Facade) 为系统中的一组接口提供一个一致的界面&#xff0c;此模式定义了一个高层接口&#xff0c;这个接口使得这一子系统更加容易使用。 #include <iostream>using namespace std;// 四个系统子类 class SubSystemOne { public:void MethodOne(){cout <&l…...

开源-Docker部署Cook菜谱工具

开源-Docker部署Cook菜谱工具 文章目录 开源-Docker部署Cook菜谱工具介绍资源列表基础环境一、安装Docker二、配置加速器三、查看Docker版本四、拉取cook镜像五、部署cook菜谱工具5.1、创建cook容器5.2、查看容器运行状态5.3、查看cook容器日志 六、访问cook菜谱服务6.1、访问c…...

使用PHP对接企业微信审批接口的问题与解决办法(二)

在现代企业中&#xff0c;审批流程是非常重要的一环&#xff0c;它涉及到企业内部各种业务流程的规范和高效运转。而随着企业微信的流行&#xff0c;许多企业希望将审批流程整合到企业微信中&#xff0c;以实现更便捷的审批操作。本文将介绍如何使用PHP对接企业微信审批接口&am…...

RK3288 android7.1 实现ota升级时清除用户数据

一&#xff0c;OTA简介(整包&#xff0c;差分包) OTA全称为Over-The-Air technology(空中下载技术)&#xff0c;通过移动通信的接口实现对软件进行远程管理。 1. 用途&#xff1a; OTA两种类型最大的区别莫过于他们的”出发点“&#xff08;我们对两种不同升级包的创建&…...

okHttp的https请求忽略ssl证书认证

使用okhttp请求第三方https接口返回异常 sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target意思就是非安全的调用&#…...

在Java中使用Spring Boot设置全局的BusinessException

在线工具站 推荐一个程序员在线工具站&#xff1a;程序员常用工具&#xff08;http://cxytools.com&#xff09;&#xff0c;有时间戳、JSON格式化、文本对比、HASH生成、UUID生成等常用工具&#xff0c;效率加倍嘎嘎好用。 程序员资料站 推荐一个程序员编程资料站&#xff1a;…...

Java 异常处理 -- Java 语言的异常、异常链与断言

大家好,我是栗筝i,这篇文章是我的 “栗筝i 的 Java 技术栈” 专栏的第 009 篇文章,在 “栗筝i 的 Java 技术栈” 这个专栏中我会持续为大家更新 Java 技术相关全套技术栈内容。专栏的主要目标是已经有一定 Java 开发经验,并希望进一步完善自己对整个 Java 技术体系来充实自…...

Spring Cloud Nacos 详解:服务注册与发现及配置管理平台

Spring Cloud Nacos 详解&#xff1a;服务注册与发现及配置管理平台 Spring Cloud Nacos 是 Spring Cloud 生态系统中的一个子项目&#xff0c;提供了服务注册与发现、配置管理等功能&#xff0c;基于 Alibaba 开源的 Nacos 项目。Nacos 是一个易于使用的动态服务发现、配置管…...

java多线程临界区介绍

在Java多线程编程中&#xff0c;"临界区"是指一段必须互斥执行的代码区域。当多个线程访问共享资源时&#xff0c;为了防止数据不一致或逻辑错误&#xff0c;需要确保同一时刻只有一个线程可以进入临界区。Java提供了多种机制来实现这一点&#xff0c;例如synchroniz…...

基于JSP的超市管理系统

你好呀&#xff0c;我是计算机学长猫哥&#xff01;如果有相关需求&#xff0c;文末可以找到我的联系方式。 开发语言&#xff1a;Java 数据库&#xff1a;MySQL 技术&#xff1a;JSP MyBatis 工具&#xff1a;IDEA/Eclipse、Navicat、Maven 系统展示 员工管理界面图 管…...

一文讲清:生产报工系统的功能、报价以及如何选择

最近这几年&#xff0c;企业越来越注重生产的速度和成本&#xff0c;尤其是“性价比”&#xff0c;生产报工系统已经变成了制造业里不可或缺的一部分。不过&#xff0c;市场上生产报工系统的选择太多&#xff0c;价格也都不一样&#xff0c;这就给很多企业出了个难题&#xff1…...

blender bpy将顶点颜色转换为UV纹理vertex color to texture

一、关于环境 安装blender的bpy&#xff0c;不需要额外再安装blender软件。在python控制台中直接输入pip install bpy即可。 二、关于代码 本文所给出代码仅为参考&#xff0c;禁止转载和引用&#xff0c;仅供个人学习。 本文所给出的例子是https://download.csdn.net/downl…...

Flink Sql:四种Join方式详解(基于flink1.15官方文档)

JOINs flink sql主要有四种连接方式&#xff0c;分别是Regular Joins、Interval Joins、Temporal Joins、lookup join 1、Regular Joins&#xff08;常规连接 &#xff09; 这种连接方式和hive sql中的join是一样的&#xff0c;包括inner join&#xff0c;left join&#xff…...

(delphi11最新学习资料) Object Pascal 学习笔记---第14章泛型第3节(泛型约束)

14.3 泛型约束 ​ 正如我们所看到的&#xff0c;您在泛型类的方法中可以做的事情非常少。您可以传递它&#xff08;即分配它&#xff09;并执行上面我介绍的泛型类型函数允许的有限操作。 ​ 为了能够执行泛型类的实际操作&#xff0c;通常需要对其进行约束。例如&#xff0c…...

C语言详解(预编译)

Hi~&#xff01;这里是奋斗的小羊&#xff0c;很荣幸您能阅读我的文章&#xff0c;诚请评论指点&#xff0c;欢迎欢迎 ~~ &#x1f4a5;&#x1f4a5;个人主页&#xff1a;奋斗的小羊 &#x1f4a5;&#x1f4a5;所属专栏&#xff1a;C语言 &#x1f680;本系列文章为个人学习…...

解决el-table表格拖拽后,只改变了数据,表头没变的问题

先看看是不是你想要解决的问题 拖拽后表头不变的bug修复 这个问题一般是使用v-for对column的数据进行循环的时候&#xff0c;key值绑定的是个index导致的&#xff0c;请看我上篇文章&#xff1a;eleplus对el-table表格进行拖拽(使用sortablejs进行列拖拽和行拖拽)&#xff1a;-…...

浏览器访问 AWS ECS 上部署的 Docker 容器(监听 80 端口)

✅ 一、ECS 服务配置 Dockerfile 确保监听 80 端口 EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]或 EXPOSE 80 CMD ["python3", "-m", "http.server", "80"]任务定义&#xff08;Task Definition&…...

CVPR 2025 MIMO: 支持视觉指代和像素grounding 的医学视觉语言模型

CVPR 2025 | MIMO&#xff1a;支持视觉指代和像素对齐的医学视觉语言模型 论文信息 标题&#xff1a;MIMO: A medical vision language model with visual referring multimodal input and pixel grounding multimodal output作者&#xff1a;Yanyuan Chen, Dexuan Xu, Yu Hu…...

椭圆曲线密码学(ECC)

一、ECC算法概述 椭圆曲线密码学&#xff08;Elliptic Curve Cryptography&#xff09;是基于椭圆曲线数学理论的公钥密码系统&#xff0c;由Neal Koblitz和Victor Miller在1985年独立提出。相比RSA&#xff0c;ECC在相同安全强度下密钥更短&#xff08;256位ECC ≈ 3072位RSA…...

【WiFi帧结构】

文章目录 帧结构MAC头部管理帧 帧结构 Wi-Fi的帧分为三部分组成&#xff1a;MAC头部frame bodyFCS&#xff0c;其中MAC是固定格式的&#xff0c;frame body是可变长度。 MAC头部有frame control&#xff0c;duration&#xff0c;address1&#xff0c;address2&#xff0c;addre…...

SciencePlots——绘制论文中的图片

文章目录 安装一、风格二、1 资源 安装 # 安装最新版 pip install githttps://github.com/garrettj403/SciencePlots.git# 安装稳定版 pip install SciencePlots一、风格 简单好用的深度学习论文绘图专用工具包–Science Plot 二、 1 资源 论文绘图神器来了&#xff1a;一行…...

【SpringBoot】100、SpringBoot中使用自定义注解+AOP实现参数自动解密

在实际项目中,用户注册、登录、修改密码等操作,都涉及到参数传输安全问题。所以我们需要在前端对账户、密码等敏感信息加密传输,在后端接收到数据后能自动解密。 1、引入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId...

sqlserver 根据指定字符 解析拼接字符串

DECLARE LotNo NVARCHAR(50)A,B,C DECLARE xml XML ( SELECT <x> REPLACE(LotNo, ,, </x><x>) </x> ) DECLARE ErrorCode NVARCHAR(50) -- 提取 XML 中的值 SELECT value x.value(., VARCHAR(MAX))…...

三体问题详解

从物理学角度&#xff0c;三体问题之所以不稳定&#xff0c;是因为三个天体在万有引力作用下相互作用&#xff0c;形成一个非线性耦合系统。我们可以从牛顿经典力学出发&#xff0c;列出具体的运动方程&#xff0c;并说明为何这个系统本质上是混沌的&#xff0c;无法得到一般解…...

AI编程--插件对比分析:CodeRider、GitHub Copilot及其他

AI编程插件对比分析&#xff1a;CodeRider、GitHub Copilot及其他 随着人工智能技术的快速发展&#xff0c;AI编程插件已成为提升开发者生产力的重要工具。CodeRider和GitHub Copilot作为市场上的领先者&#xff0c;分别以其独特的特性和生态系统吸引了大量开发者。本文将从功…...

【HTTP三个基础问题】

面试官您好&#xff01;HTTP是超文本传输协议&#xff0c;是互联网上客户端和服务器之间传输超文本数据&#xff08;比如文字、图片、音频、视频等&#xff09;的核心协议&#xff0c;当前互联网应用最广泛的版本是HTTP1.1&#xff0c;它基于经典的C/S模型&#xff0c;也就是客…...