scala

Scala regex pattern match of ip address

守給你的承諾、 提交于 2021-02-16 15:09:51
问题 I can't understand why this code returns false: val reg = """.*(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}).*""".r "ttt20.30.4.140ttt" match{ case reg(one, two, three, four) => if (host == one + "." + two + "." + three + "." + four) true else false case _ => false } and only if I change it to: val reg = """.*(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}).*""".r "20.30.4.140" match{ case reg(one, two, three, four) => if (host == one + "." + two + "." + three + "." + four) true else false case _ =>

Creating an object from a type parameter in Scala

╄→гoц情女王★ 提交于 2021-02-16 14:54:09
问题 I want to create a method that takes a type parameter, obviously with no parameters on its constructor, and returns a dummy object constructed with that constructor. Basically some kind of factory pattern. Is that even possible in Scala? Is that a good idea? Any better pattern if it is not? Is there a way to achieve this at compile-time only (i.e. without reflection)? Code example : trait Model class A extends Model class B extends Model def dummy[T <: Model] = new T // Fails compilation with

Scala - difference between for each loops

本秂侑毒 提交于 2021-02-16 06:12:46
问题 Is there any difference between the two following statements. They achieve the same end, correct? Do they compile to the same Java code? Is there any performance difference between them or is it just a matter of preference/readability? for (thing <- things) { doSome(thing) } things.foreach( thing => doSome(thing) ) 回答1: for comprehensions are defined as simple syntactic translations. That's extremely important, because that allows any object to work with for comprehensions, it just has to

How to get Kafka messages based on timestamp

爷,独闯天下 提交于 2021-02-15 07:42:15
问题 I am working on a application in which I am using kafka and tech is scala. My kafka consumer code is as follows: val props = new Properties() props.put("group.id", "test") props.put("bootstrap.servers", "localhost:9092") props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer") props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer") props.put("auto.offset.reset", "earliest") props.put("group.id", "consumer-group") val

When doing implicit resolution with type parameters, why does val placement matter?

家住魔仙堡 提交于 2021-02-15 05:33:51
问题 In one file, I have: trait JsonSchema[T] { val propertyType: String override def toString: String = propertyType } object JsonSchema { implicit def stringSchema: JsonSchema[String] = new JsonSchema[String] { override val propertyType: String = "string" } implicit def intSchema: JsonSchema[Int] = new JsonSchema[Int] { override val propertyType: String = "integer" } implicit def booleanSchema: JsonSchema[Boolean] = new JsonSchema[Boolean] { override val propertyType: String = "boolean" } } In

从普通Java程序员到阿里高级架构师,他用了6年!

我怕爱的太早我们不能终老 提交于 2021-02-14 09:35:52
6年间,一位架构师待过四大门户中的两户,已完成了工程师到架构师的蜕变。经手几款从零到一产品的开发和增涨,也亲身经历国內最大社交网络平台亿级数据流量和用户的架构设计及优化工作。在工作中思路清晰、尽职尽责,是同事们心目中出色 Problem Solver。 参加工作时间:8 年 服务公司:4 家(含四大门户中的两户) 近期岗位:Java 架构师 职场关键词:社交网络平台、高并发系统架构设计、技术团队管理、多款从零到一的产品城市! 问: 介绍一下下你自身 答: 我 2007 年本科大学毕业,前 2 年在一家传统式 it互联网 企业,近期 6 年在互联网企业,现任 Java 开发工程师、高级工程师、架构师等职位。工作内容上,经历过多款产品从零到一的诞生开发过程,也经手过国內用户、內容和数据流量最大的社交/社区产品的架构改造优化工作,有丰富的社交产品的研发经验,目前在一家创业公司担任技术合伙人。 问: 你擅长的技术各个领域是啥? 答: 擅长的开发语言是 Java、Golang、Scala,熟悉程度依次递减。专注于高性能、高并发系统架构设计和实现。 问: 平常如何向亲戚朋友解释你的工作是干什么的? 答: 通常不详细解释,即便解释了也是白费力气。所以她们会按照自身的了解来描述我的工作,例如维修电脑的,例如买手机的。 问: 你认为程序猿能否当一辈子吗?有木有想像过自个 45 岁时在做什么工作? 答

Scala Kleisli throws an error in IntelliJ

人盡茶涼 提交于 2021-02-13 12:27:55
问题 trying to implement Kleisli category for a made-up Partial type in Scala (reading Bartosz Milewski's "category theory for programmers", that's exersize for chapter 4) object Kleisli { type Partial[A, B] = A => Option[B] implicit class KleisliOps[A, B](f1: Partial[A, B]) { def >=>[C](f2: Partial[B, C]): Partial[A, C] = (x: A) => for { y <- f1(x) z <- f2(y) } yield z def identity(f: Partial[A, B]): Partial[A, B] = x => f(x) } val safeRecip: Partial[Double, Double] = { case 0d => None case x =>

Scala Kleisli throws an error in IntelliJ

為{幸葍}努か 提交于 2021-02-13 12:23:00
问题 trying to implement Kleisli category for a made-up Partial type in Scala (reading Bartosz Milewski's "category theory for programmers", that's exersize for chapter 4) object Kleisli { type Partial[A, B] = A => Option[B] implicit class KleisliOps[A, B](f1: Partial[A, B]) { def >=>[C](f2: Partial[B, C]): Partial[A, C] = (x: A) => for { y <- f1(x) z <- f2(y) } yield z def identity(f: Partial[A, B]): Partial[A, B] = x => f(x) } val safeRecip: Partial[Double, Double] = { case 0d => None case x =>

Scala Kleisli throws an error in IntelliJ

邮差的信 提交于 2021-02-13 12:22:54
问题 trying to implement Kleisli category for a made-up Partial type in Scala (reading Bartosz Milewski's "category theory for programmers", that's exersize for chapter 4) object Kleisli { type Partial[A, B] = A => Option[B] implicit class KleisliOps[A, B](f1: Partial[A, B]) { def >=>[C](f2: Partial[B, C]): Partial[A, C] = (x: A) => for { y <- f1(x) z <- f2(y) } yield z def identity(f: Partial[A, B]): Partial[A, B] = x => f(x) } val safeRecip: Partial[Double, Double] = { case 0d => None case x =>