本人是服务端编程人员.
这个周末学了下 kotlin,从使用层面上确实比较爽,但是写代码的时候有一种不知道如何下手的感觉.花样太多了,貌似没有一些最佳实践.求哪位大牛出一本 effect kotlin 吧.
另外关于集合的 map, forEach 等高阶函数看了下源码有坑啊.
var liss = listOf<Int>(1, 2, 3)
// kotlin 中可以用的写法,每一步都是一个循环处理
liss.map {x -> x * x }.forEach(::println)
// Java8 的 Stream, 构建好处理链路,最后一个循环中全部处理
liss.stream().map { x -> x * x }.forEach(::println)
像这样的区别,没有一些最佳实践来指导吗?对于大容量处理必须 Stream 更加高效啊.
1
nl101531 OP 附上 IDEA 中 AMS 插件显示的 Bytecode
```java // access flags 0x19 public final static main([Ljava/lang/String;)V @Lkotlin/jvm/JvmStatic;() @Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0 L0 ALOAD 0 LDC "args" INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull (Ljava/lang/Object;Ljava/lang/String;)V L1 LINENUMBER 22 L1 ICONST_3 ANEWARRAY java/lang/Integer DUP ICONST_0 ICONST_1 INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer; AASTORE DUP ICONST_1 ICONST_2 INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer; AASTORE DUP ICONST_2 ICONST_3 INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer; AASTORE INVOKESTATIC kotlin/collections/CollectionsKt.listOf ([Ljava/lang/Object;)Ljava/util/List; ASTORE 1 L2 LINENUMBER 23 L2 ALOAD 1 CHECKCAST java/lang/Iterable ASTORE 2 L3 LINENUMBER 28 L3 ALOAD 2 ASTORE 3 NEW java/util/ArrayList DUP ALOAD 2 BIPUSH 10 INVOKESTATIC kotlin/collections/CollectionsKt.collectionSizeOrDefault (Ljava/lang/Iterable;I)I INVOKESPECIAL java/util/ArrayList.<init> (I)V CHECKCAST java/util/Collection ASTORE 4 L4 LINENUMBER 29 L4 ALOAD 3 INVOKEINTERFACE java/lang/Iterable.iterator ()Ljava/util/Iterator; ASTORE 5 L5 FRAME FULL [[Ljava/lang/String; java/util/List java/lang/Iterable java/lang/Iterable java/util/Collection java/util/Iterator] [] ALOAD 5 INVOKEINTERFACE java/util/Iterator.hasNext ()Z IFEQ L6 ALOAD 5 INVOKEINTERFACE java/util/Iterator.next ()Ljava/lang/Object; ASTORE 6 L7 LINENUMBER 30 L7 ALOAD 4 ALOAD 6 CHECKCAST java/lang/Number INVOKEVIRTUAL java/lang/Number.intValue ()I ISTORE 7 ASTORE 11 L8 LINENUMBER 23 L8 ILOAD 7 ILOAD 7 IMUL L9 INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer; ASTORE 12 ALOAD 11 ALOAD 12 INVOKEINTERFACE java/util/Collection.add (Ljava/lang/Object;)Z POP L10 LINENUMBER 29 L10 GOTO L5 L6 LINENUMBER 31 L6 FRAME SAME ALOAD 4 L11 CHECKCAST java/util/List L12 CHECKCAST java/lang/Iterable ASTORE 2 L13 LINENUMBER 23 L13 NOP L14 LINENUMBER 32 L14 ALOAD 2 INVOKEINTERFACE java/lang/Iterable.iterator ()Ljava/util/Iterator; ASTORE 3 L15 FRAME FULL [[Ljava/lang/String; java/util/List java/lang/Iterable java/util/Iterator java/lang/Object] [] ALOAD 3 INVOKEINTERFACE java/util/Iterator.hasNext ()Z IFEQ L16 ALOAD 3 INVOKEINTERFACE java/util/Iterator.next ()Ljava/lang/Object; ASTORE 4 L17 ALOAD 4 CHECKCAST java/lang/Number INVOKEVIRTUAL java/lang/Number.intValue ()I ISTORE 5 L18 LINENUMBER 23 L18 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; ILOAD 5 INVOKEVIRTUAL java/io/PrintStream.println (I)V L19 NOP L20 GOTO L15 L16 LINENUMBER 33 L16 FRAME SAME NOP L21 LINENUMBER 25 L21 ALOAD 1 INVOKEINTERFACE java/util/List.stream ()Ljava/util/stream/Stream; GETSTATIC com/itoolshub/task/fetch/FetchSite$main$3.INSTANCE : Lcom/itoolshub/task/fetch/FetchSite$main$3; CHECKCAST java/util/function/Function INVOKEINTERFACE java/util/stream/Stream.map (Ljava/util/function/Function;)Ljava/util/stream/Stream; GETSTATIC com/itoolshub/task/fetch/FetchSite$main$4.INSTANCE : Lcom/itoolshub/task/fetch/FetchSite$main$4; CHECKCAST kotlin/jvm/functions/Function1 DUP IFNULL L22 ASTORE 2 NEW com/itoolshub/task/fetch/FetchSiteKt$sam$Consumer$89e5f0a8 DUP ALOAD 2 INVOKESPECIAL com/itoolshub/task/fetch/FetchSiteKt$sam$Consumer$89e5f0a8.<init> (Lkotlin/jvm/functions/Function1;)V L22 FRAME FULL [[Ljava/lang/String; java/util/List java/lang/Object java/util/Iterator java/lang/Object] [java/util/stream/Stream java/lang/Object] CHECKCAST java/util/function/Consumer INVOKEINTERFACE java/util/stream/Stream.forEach (Ljava/util/function/Consumer;)V L23 LINENUMBER 26 L23 RETURN L24 LOCALVARIABLE x I L8 L9 7 LOCALVARIABLE $i$a$1$map I L8 L9 8 LOCALVARIABLE item$iv$iv Ljava/lang/Object; L7 L10 6 LOCALVARIABLE $receiver$iv$iv Ljava/lang/Iterable; L4 L11 3 LOCALVARIABLE destination$iv$iv Ljava/util/Collection; L4 L11 4 LOCALVARIABLE $i$f$mapTo I L4 L11 9 LOCALVARIABLE $receiver$iv Ljava/lang/Iterable; L3 L12 2 LOCALVARIABLE $i$f$map I L3 L12 10 LOCALVARIABLE p1 I L18 L19 5 LOCALVARIABLE $i$a$2$unknown I L18 L19 6 LOCALVARIABLE element$iv Ljava/lang/Object; L17 L20 4 LOCALVARIABLE $receiver$iv Ljava/lang/Iterable; L14 L21 2 LOCALVARIABLE $i$f$forEach I L14 L21 7 LOCALVARIABLE liss Ljava/util/List; L2 L24 1 LOCALVARIABLE args [Ljava/lang/String; L0 L24 0 MAXSTACK = 4 MAXLOCALS = 13 ``` |
2
sorra 2018-01-28 13:42:26 +08:00 1
放心,没坑
seq 方法生成 lazy view,类似 Java 8 Stream |
3
mx1700 2018-01-28 13:45:42 +08:00 via Android 1
不想循环多次,你需要
list.asSequence() |
6
mx1700 2018-01-28 19:51:20 +08:00 via Android
翻翻官方 API 文档?可能比较枯燥
|
7
zhuangzhuang1988 2018-01-29 10:02:59 +08:00
学习啊 linq 就知道了。
|