scala

scala way to generate prime factors of a number

僤鯓⒐⒋嵵緔 提交于 2021-02-19 04:37:12
问题 What's the scala way to generate the factors of an integer? Here's my take 1: def factorize(x: Int): List[Int] = { def foo(x: Int, a: Int): List[Int] = { if (a > Math.pow(x, 0.5)) return List(x) x % a match { case 0 => a :: foo(x / a, a) case _ => foo(x, a + 1) } } foo(x, 2) } factorize(360) //List(2, 2, 2, 3, 3, 5) Take 2 based on @SpiderPig and @seth-tisue's comments def factorize(x: Int): List[Int] = { def foo(x: Int, a: Int): List[Int] = { (a*a < x, x % a) match { case (true, 0) => a ::

Scala read continuous http stream

天大地大妈咪最大 提交于 2021-02-19 04:27:05
问题 How can I connect to and read a continuous (chunked) http stream in scala? For example, if I have this simple service written in python/bottle: from gevent import monkey; monkey.patch_all() import gevent from bottle import route, run @route('/stream') def stream(): while True: yield 'blah\n' gevent.sleep(1) run(host='0.0.0.0', port=8100, server='gevent') I'm planning to use akka-stream to process the data, I just need a way to retrieve it. 回答1: This should work. Basically, you do a single

Spark Get only columns that have one or more null values

混江龙づ霸主 提交于 2021-02-19 04:25:47
问题 From a dataframe I want to get names of columns which contain at least one null value inside. Considering the dataframe below: val dataset = sparkSession.createDataFrame(Seq( (7, null, 18, 1.0), (8, "CA", null, 0.0), (9, "NZ", 15, 0.0) )).toDF("id", "country", "hour", "clicked") I want to get column names 'Country' and 'Hour'. id country hour clicked 7 null 18 1 8 "CA" null 0 9 "NZ" 15 0 回答1: this is one solution, but it's a bit awkward, I hope there is an easier way: val cols = dataset

How do I start a server before running a test suite in SBT?

孤人 提交于 2021-02-19 04:23:07
问题 Problem is that if I do test in Test <<= (taskA, taskB) { (A, B) => A doFinally B or test in Test := (taskB dependsOn taskA).value and taskA is forked , then sbt execution doesn't continue to taskB and get's stuck indefinitely. It's caused by doFinally/dependsOn , because they probably makes it a single-threaded sequential execution. But I can't find any other way to order those 2 tasks, to make them run sequentially. So far I've gotten this far : lazy val startServer = taskKey[Unit]("Start

Spark: Replace Null value in a Nested column

跟風遠走 提交于 2021-02-19 03:53:08
问题 I would like to replace all the n/a values in the below dataframe to unknown . It can be either scalar or complex nested column . If it's a StructField column I can loop through the columns and replace n\a using WithColumn . But I would like this to be done in a generic way inspite of the type of the column as I dont want to specify the column names explicitly as there are 100's in my case? case class Bar(x: Int, y: String, z: String) case class Foo(id: Int, name: String, status: String, bar:

How to use spark with large decimal numbers?

不打扰是莪最后的温柔 提交于 2021-02-19 03:51:46
问题 My database has numeric value, which is up to 256-bit unsigned integer. However, spark's decimalType has a limit of Decimal(38,18). When I try to do calculations on the column, exceptions are thrown. java.lang.IllegalArgumentException: requirement failed: Decimal precision 39 exceeds max precision 38". Is there any third-party library or workarounds that solve this issue? Or Spark is designed for numbers smaller than Decimal(38,18)? 来源: https://stackoverflow.com/questions/53074721/how-to-use

In scala, how to make type class working for Aux pattern? - Part 2

随声附和 提交于 2021-02-19 03:46:28
问题 This is a follow up question of: In scala, how to make type class working for Aux pattern? Considering the following example: trait Base { type Out def v: Out } object Base { type Aux[T] = Base { type Out = T } type Lt[T] = Base { type Out <: T } class ForH() extends Base { final type Out = HNil override def v: Out = HNil } object ForH extends ForH } trait TypeClasses { class TypeClass[B] def summon[B](b: B)(implicit ev: TypeClass[B]): TypeClass[B] = ev } object T1 extends TypeClasses {

Optimizing a Free Monad

牧云@^-^@ 提交于 2021-02-19 03:44:10
问题 If I have a value a: Free[Op, A] , is it possible to "flatten" the structure of a so that two Op s that are bound together by the free monad may be collapsed into one? Context: I'd like to perform this as an optimization step before interpretation because a semantic of Op is that its operations are idempotent. So if two appear "in a row", the second can be eliminated at no cost to the semantics of the program. 回答1: As far as I understand there is no way for this kind introspection of Free

How to map postgresql custom enum column with Slick2.0.1?

橙三吉。 提交于 2021-02-19 02:57:05
问题 I just can't figure it out. What I am using right now is: abstract class DBEnumString extends Enumeration { implicit val enumMapper = MappedJdbcType.base[Value, String]( _.toString(), s => this.withName(s) ) } And then: object SomeEnum extends DBEnumString { type T = Value val A1 = Value("A1") val A2 = Value("A2") } The problem is, during insert/update JDBC driver for PostgreSQL complains about parameter type being "character varying" when column type is "some_enum", which is reasonable as I

Access to WrappedArray elements

百般思念 提交于 2021-02-19 02:37:39
问题 I have a spark dataframe and here is the schema: |-- eid: long (nullable = true) |-- age: long (nullable = true) |-- sex: long (nullable = true) |-- father: array (nullable = true) | |-- element: array (containsNull = true) | | |-- element: long (containsNull = true) and a sample of rows:. df.select(df['father']).show() +--------------------+ | father| +--------------------+ |[WrappedArray(-17...| |[WrappedArray(-11...| |[WrappedArray(13,...| +--------------------+ and the type is DataFrame