functional-programming

Functional way to find a pair of integers, which sum to X, in a sorted array

独自空忆成欢 提交于 2019-12-24 01:17:54
问题 This is a follow-up to my previous question. Suppose I want to find a pair of integers, which sum to a given number x , in a given sorted array. The well-known "one pass" solution looks like that: def pair(a: Array[Int], target: Int): Option[(Int, Int)] = { var l = 0 var r = a.length - 1 var result: Option[(Int, Int)] = None while (l < r && result.isEmpty) { (a(l), a(r)) match { case (x, y) if x + y == target => result = Some(x, y) case (x, y) if x + y < target => l = l + 1 case (x, y) if x +

Python functional approach: remove key from dict using filter

我是研究僧i 提交于 2019-12-24 00:58:48
问题 A follow up on the recent question Remove keys from object not in a list in python? That question turns out to be a duplicate of a previous one. All answers there, and among them the most voted, use list comprehension. I'm thinking on a functional approach. How can this be done using filter ? We have: testdict={'a':'vala', 'b':'valb', 'c':'valc','d':'vald'} keep=['a','c'] and I want filter(isKept,testdict) to give {'a':'vala','c':'valc'} I tried naively defining isKept as a function of either

How to export Scala Transformation to Java

 ̄綄美尐妖づ 提交于 2019-12-24 00:56:47
问题 I'm trying to implement a Scala trait by Java, the trait has a generic container type within another container, which couldn't be solved automatically by java import, the Scala code is below: import cats.data.{EitherNel, Kleisli, NonEmptyList} import cats.implicits._ package Export_To_Java { package object types { type Valid[A] = EitherNel[String, A] type ValidOperation[A, B] = Kleisli[Valid, A, B] type Amount = BigDecimal } trait InterestService[Account] { import types._ type

Looking for a way to refactor D3.js-style method chaining pattern

百般思念 提交于 2019-12-24 00:45:55
问题 While learning D3.js, I have come across the blog post explaining the main design-pattern behind it's reusable units of code. I have reproduced the relevant bit of code below. The way the pattern is presented below is exactly the way it is used in D3 codebase and plug-ins (example). My one issue with this code is that it has so much copy-paste for the properties. JavaScript being a functional language, I thought I'd be able to re-factor the boilerplate code out, but I can't think of a way to

ZIO : How to compute only once?

百般思念 提交于 2019-12-24 00:44:01
问题 I am using ZIO: https://github.com/zio/zio in my build.sbt : "dev.zio" %% "zio" % "1.0.0-RC9" No matter what I tried, my results are always being computed each time I need them: val t = Task { println(s"Compute") 12 } val r = unsafeRun(for { tt1 <- t tt2 <- t } yield { tt1 + tt2 }) println(r) For this example, the log look like : Compute Compute 24 I tried with Promise : val p = for { p <- Promise.make[Nothing, Int] _ <- p.succeed { println(s"Compute - P") 48 } r <- p.await } yield { r } val

CQS Design Principle Problem: Implementing a Queue

自作多情 提交于 2019-12-24 00:34:44
问题 I'm creating this question based upon a small discussion I had in the comments to the answer to this question: design a method returning a value or changing some data but not both @Kata pointed out that the pattern that the OP was interested in is called Command–query separation and argued that this is a good model to structure your code by. From wikipedia: Command–query separation (CQS) is a principle of imperative computer programming. It was devised by Bertrand Meyer as part of his

How to define the type profile for this function?

匆匆过客 提交于 2019-12-24 00:01:35
问题 I have to define the type profile of this function: twice f x = f (f x); The result should be the following, but I don't really get why. ('a -> 'a) -> 'a -> 'a 回答1: (a -> a) -> a -> a is the right answer. Let's split it to pieces to find out why. your function takes two arguments, f and x , so the signature will have three parts - say, a -> c -> d first of these arguments is an unary function - that makes a = (a -> b) (remember that a can be any type, as long as it appears only once in the

How do I call an anonymous function inside Enum.map

本小妞迷上赌 提交于 2019-12-23 23:52:05
问题 I am learning Elixir and I am working on Project Euler to try to strengthen my skills in Elixir. Right now I have this code fib = fn a,b,0 -> a a,b,n -> fib.(b, a+b, n-1) end IO.puts Enum.sum(Enum.filter(Enum.map(1..50, fn n -> fib.(0,1,n) end), even and fn(x) -> x < 4000000 end)) But when I run this code I get: undefined function fib/0 (elixir) src/elixir_fn.erl:33: anonymous fn/3 in :elixir_fn.expand/3 (stdlib) lists.erl:1238: :lists.map/2 (stdlib) lists.erl:1238: :lists.map/2 (elixir) src

Are there ways to call two functions (one just after another) in purely functional language? (in non-io mode)

女生的网名这么多〃 提交于 2019-12-23 20:35:48
问题 I'm trying to understand order of execution in purely functional language. I know that in purely functional languages, there is no necessary execution order. So my question is: Suppose there are two functions. I would like to know all ways in which I can call one function after another (except nested call of one function from another) (and except io-mode). I would like to see examples in Haskell or pseudo-code. 回答1: There is no way to do what you describe, if the functions are totally

Boost Phoenix (or Boost Lambda) - taking a pointer lazily

青春壹個敷衍的年華 提交于 2019-12-23 19:43:03
问题 Is there a way of taking a pointer of a lazy phoenix value / ref ? If so how ? 回答1: Phoenix placeholders overload operator&() , therefore a simple &_1 should do the trick (see Phoenix docs here). 来源: https://stackoverflow.com/questions/5211894/boost-phoenix-or-boost-lambda-taking-a-pointer-lazily