optional

random.randint()与np.random.randint()的区别

点点圈 提交于 2021-02-10 17:52:52
一.比较两个函数 先来看看random.randint() import random for n in range(5 ): for i in range(10 ): print (random.randint(1,5),end= ' ' ) print () # 运行结果 1 5 5 3 3 1 3 1 5 2 4 4 4 4 4 4 3 1 5 2 3 2 3 1 1 5 5 1 4 3 3 4 4 2 5 5 3 4 4 4 3 5 4 5 4 5 4 5 2 4 Process finished with exit code 0 再来看看numpy.random.randint()方法: import numpy as np for n in range(5 ): for i in range(10 ): print (np.random.randint(1, 5), end= ' ' ) print () # 运行结果 2 4 1 1 1 1 2 2 2 4 3 4 3 2 3 4 3 2 2 4 2 2 1 2 1 1 3 3 3 4 4 1 4 2 4 1 3 4 3 2 2 3 3 2 3 4 4 3 4 4 Process finished with exit code 0 看出有什么不同了吗? random.randint()方法里面的取值区间是前闭后闭区间

Conditional extract of value from Java Optional

戏子无情 提交于 2021-02-10 14:40:23
问题 I am trying to find the most concise (and meaningful) way of using Java Optional , to read the first value off a Optional<String> and return the String if exists, or return "NOT_FOUND". Here is the code I am working with: public static String getValue(Optional<String> input) { return input.ifPresent(val -> val.get()).orElse("NOT_FOUND") } The methods of Optional apparently have very specific purposes but the API has left me confused. Update (4/13/2018): The code in my question is incorrect,

Linux内存管理基本概念

好久不见. 提交于 2021-02-10 02:43:40
1. 前言 内存(memory)在Linux系统中是一种牵涉面极广的资源,上至应用程序、下至kernel和driver,无不为之魂牵梦绕。加上它天然的稀缺性,导致内存管理(Memory Management,简称MM)是linux kernel中非常重要又非常复杂的一个子系统。 重要性就不多说了,Kernel自有分寸。关于复杂性(鉴于Linux kernel优秀的抽象能力),应该不会被普通人(Linux系统的使用者、应用工程师、驱动工程师、轻量级的内核工程师)感知到才对。事实确实如此,Kernel屏蔽掉了大多数的实现细节,尽量以简单、易用的方式向其它模块提供memory服务。 不过呢,这个世界上没有完美的存在,kernel的内存管理也是如此,由于两方面的原因:一、众口难调,内存管理有关的需求实在太复杂了;二、CPU、Device和Memory之间纠结的三角恋(参考下面图片),导致它也(不得不)提供了很多啰里啰唆的、不易理解的功能(困扰了很多从入门级到资深级的linux软件工程师)。 图片1 CPU, Device and Memory 基于上面的原因,本站 内存管理子系统 发布了很多分析文章,以帮助大家理解内存管理有关的概念。不过到目前为止,还缺少一篇索引类的文章,从整体出发,理解Kernel内存管理所需要面对的软硬件局面、所要解决的问题,以及各个内存管理子模块的功能和意义

Java 10 ifPresentOrElse that return boolean

我与影子孤独终老i 提交于 2021-02-07 19:54:09
问题 I am a little confused on "how to do this properly": // return true: if present and number of lines != 0 boolean isValid(Optional<File> optFile) { return optFile.ifPresentOrElse(f -> return !isZeroLine(f), return false); } private boolean isZeroLine(File f) { return MyFileUtils.getNbLinesByFile(f) == 0; } I know the syntax is not correct and not compiling, but it's just for you to get the idea. How can I turn this into 'clean code'? i.e. avoid doing: if (optFile.isPresent()) {//} else {//}

Java 10 ifPresentOrElse that return boolean

a 夏天 提交于 2021-02-07 19:53:12
问题 I am a little confused on "how to do this properly": // return true: if present and number of lines != 0 boolean isValid(Optional<File> optFile) { return optFile.ifPresentOrElse(f -> return !isZeroLine(f), return false); } private boolean isZeroLine(File f) { return MyFileUtils.getNbLinesByFile(f) == 0; } I know the syntax is not correct and not compiling, but it's just for you to get the idea. How can I turn this into 'clean code'? i.e. avoid doing: if (optFile.isPresent()) {//} else {//}

java 8 optional to replace return null

廉价感情. 提交于 2021-02-07 07:36:34
问题 I am refactoring the code to Java 8 and I want to replace null checks with Optional. public Employee findEmployeeById(String id) { List<Employee> empList = .. //some db query return (empList.isEmpty() ? null : empList.get(0)); } Optional.ofNullable(empList.get(0)) won't work as when it will throw IndexOutofBoundException Or should I ideally replace null with Optional.empty() ? 回答1: As @Jesper already mentioned in the comments, you have to check whether the list is empty and then return an

Kafka Connect HDFS

Deadly 提交于 2021-02-07 06:50:47
概述 Kafka 的数据如何传输到HDFS?如果仔细思考,会发现这个问题并不简单。 不妨先想一下这两个问题? 1)为什么要将Kafka的数据传输到HDFS上? 2)为什么不直接写HDFS而要通过Kafka? HDFS一直以来是为离线数据的存储和计算设计的,因此对实时事件数据的写入并不友好,而Kafka生来就是为实时数据设计的,但是数据在Kafka上无法使用离线计算框架来作批量离线分析。 那么,Kafka为什么就不能支持批量离线分析呢?想象我们将Kafka的数据按天拆分topic,并建足够多的分区,然后通过Spark-Streaming,Flink,又或者是KSql等来处理单个topic中的所有数据--这就相当于处理某一天的所有数据。这种计算的性能从原理上来说是不比Spark或者Hive离线计算差的。 而且更好的是,这样我们就不用将kafka中的数据翻来覆去的导到hdfs,而是直接在kafka上作计算。 后面我们将对此展开更多的讨论,这里先回归正题,在常见的大数据系统架构(lambda)中,通常会将kafka中的数据导入到HDFS来作离线的数据分析。在Kafka的官方wiki中提到了这样的一些方式来对接Hadoop生态。 https://cwiki.apache.org/confluence/display/KAFKA/Ecosystem 其中最常用的是Flume

Optional Chaining Operator in Typescript

馋奶兔 提交于 2021-02-07 04:42:45
问题 In javascript, Optional Chaining Operator is supported by the babel plugin. But I can't find how to do this in Typescript. Any idea? 回答1: At time of writing, TypeScript does not support the optional chaining operator. See discussion on the TypeScript issue tracker: https://github.com/Microsoft/TypeScript/issues/16 As a warning, the semantics of this operator are still very much in flux, which is why TypeScript hasn't added it yet. Code written today against the Babel plugin may change

java.util.Objects vs Optional which is preferable?

和自甴很熟 提交于 2021-02-06 09:55:07
问题 The java.util.Objects class was extended with a number of new methods Objects#requireNonNullElse respectively Objects#requireNonNullElseGet() in Java-9 . Both will return the first argument if it is non-null and otherwise returns the non-null second argument or the non-null value of supplier.get() jshell> String nullStr = null; nullStr ==> null jshell> Objects.requireNonNullElse(nullStr,"lorem ipsum"); $13 ==> "lorem ipsum" jshell> Objects.requireNonNullElseGet(nullStr,() -> "lorem ipsum");

how to unwrap swift optionals within a struct

大兔子大兔子 提交于 2021-02-05 09:24:27
问题 I'm learning swift and found an example that contains an optional property within a struct. When I try to set a value to the optional I find that it is nil. struct Price{ var USD: Double = 0.0 var CAD: Double = 0.0 } struct Item{ var name: String = "not defined" var price: Price? } var purchase:Item = Item() purchase.name = "lampshade" purchase.price?.USD = 19.2 print("purchase name is \(purchase.name), purchase price is \(purchase.price?.USD)") yields purchase name is lampshade, purchase