functional-programming

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 =>

Scala Kleisli throws an error in IntelliJ

我的未来我决定 提交于 2021-02-13 12:21:02
问题 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 =>

Is this statement true that Java 8 provides functional style but is not functional programming?

坚强是说给别人听的谎言 提交于 2021-02-11 15:00:20
问题 Is this statement true that Java 8 provides functional style but is not functional programming as the syntax which it uses is also an object? Calculator calc = (i, j) -> i/j; If yes, then why we get articles everywhere Functional Programming with Java 8? 回答1: Here is a (non-exhaustive) list of abstract FP concepts: Focus on immutability Referential transparency for functions Limitations on side effects (follows from 1 & 2) Expression-based, no statements. Statements are not first-class.

Generate a custom pattern number sequence in one go

五迷三道 提交于 2021-02-11 13:59:45
问题 I'd like to generate the following number sequence in one go using a functional initialization construct: Array(0, 0, 0, 0, 3, 3, 6, 6, 9, 9, ..., n*3, n*3) One way is to do: Array.fill[Int](2)(0) ++ Array.tabulate(4)(_*3) but I'd need to double each value of the second part of the construct i.e. to get 0, 0 then 3, 3 etc. How can I duplicate the values of the second construct? I also couldn't figure out a mathematical function that would generate such sequence. 回答1: Consider tail-recursive

Generate a custom pattern number sequence in one go

这一生的挚爱 提交于 2021-02-11 13:58:59
问题 I'd like to generate the following number sequence in one go using a functional initialization construct: Array(0, 0, 0, 0, 3, 3, 6, 6, 9, 9, ..., n*3, n*3) One way is to do: Array.fill[Int](2)(0) ++ Array.tabulate(4)(_*3) but I'd need to double each value of the second part of the construct i.e. to get 0, 0 then 3, 3 etc. How can I duplicate the values of the second construct? I also couldn't figure out a mathematical function that would generate such sequence. 回答1: Consider tail-recursive

Conversion of Looping to Recursive Solution

时光怂恿深爱的人放手 提交于 2021-02-11 05:12:37
问题 I have written a method pythagoreanTriplets in scala using nested loops. As a newbie in scala, I am struggling with how can we do the same thing using recursion and use Lazy Evaluation for the returning list(List of tuples). Any help will be highly appreciated. P.S: The following method is working perfectly fine. // This method returns the list of all pythagorean triples whose components are // at most a given limit. Formula a^2 + b^2 = c^2 def pythagoreanTriplets(limit: Int): List[(Int, Int,

Conversion of Looping to Recursive Solution

做~自己de王妃 提交于 2021-02-11 05:07:41
问题 I have written a method pythagoreanTriplets in scala using nested loops. As a newbie in scala, I am struggling with how can we do the same thing using recursion and use Lazy Evaluation for the returning list(List of tuples). Any help will be highly appreciated. P.S: The following method is working perfectly fine. // This method returns the list of all pythagorean triples whose components are // at most a given limit. Formula a^2 + b^2 = c^2 def pythagoreanTriplets(limit: Int): List[(Int, Int,

Scheme: Iterative process to reconstruct a list in original order?

别等时光非礼了梦想. 提交于 2021-02-11 04:55:20
问题 My question is: how to write a procedure that utilises tailcall, and that constructs a list not in the reverse order. To show what I mean, here is an example of a very simple procedure that is iterative, and that creates a copy of a list: (define (copy-list ls) (define (iter cp-ls rest-ls) (if (null? rest-ls) cp-ls (iter (cons (car rest-ls) cp-ls) (cdr rest-ls)))) (iter '() ls)) The problem is that, due to the iterative order in which the elements are cons ed together, the returned list ends

Scheme: Iterative process to reconstruct a list in original order?

不问归期 提交于 2021-02-11 04:54:20
问题 My question is: how to write a procedure that utilises tailcall, and that constructs a list not in the reverse order. To show what I mean, here is an example of a very simple procedure that is iterative, and that creates a copy of a list: (define (copy-list ls) (define (iter cp-ls rest-ls) (if (null? rest-ls) cp-ls (iter (cons (car rest-ls) cp-ls) (cdr rest-ls)))) (iter '() ls)) The problem is that, due to the iterative order in which the elements are cons ed together, the returned list ends