functional-programming

Implicit conversion from A to Some(a)

烂漫一生 提交于 2020-01-15 07:20:20
问题 out of curiosity, I was wondering if it was possible to do something like : def myMethod( a: Option[A] = None, b: Option[B] = None, ... z: Option[Z] = None ): Something = { ... } What I want is not to have to call it that way: myMethod( b = Some(newB), m = Some(newM) ) but instead being able to just do myMethod(b = newB, m = newM) without having to always convert A to Some(a) . Is it possible ? 回答1: Possible, yes: object Test { implicit def anythingToOption[A](a: A): Option[A] = Option(a) def

Functional Programming in Swit to distribute array elements to correct “buckets”

心已入冬 提交于 2020-01-15 03:21:19
问题 I'm new to functional programming. My problem is that I have a main array and a fixed number of "destination" arrays. I would like to distribute the elements from the main array into the correct resulting array based on a certain value of each element. I'm guessing that one approach would be to have one map function that goes through the main array elements, determines the correct "destination array" value (based on some logic) and then adds the elements to that array. However, I'm not sure

How to filter list elements in Haskell based on previous value in the list?

柔情痞子 提交于 2020-01-15 02:55:31
问题 I'm working on creating a function in Haskell that filters the numbers of a list on a condition based on the previous element in the list. Example the previous number is a multiple of 2 myFunction [1, 2, 5, 6, 3] # expected output: [5,3] I know how to apply filter but so far I have seen that the filters take only one argument at a time. I tried with scanl1 , foldl1 , and map but I'm new to Haskell and I have not been able to do so; any clue? 回答1: Edit It should be: myFunction [] = []

Generalised Curry - Javascript

烈酒焚心 提交于 2020-01-14 19:05:27
问题 While reading an article on implementation of a generalised curry in Javascript, I stumbled upon this piece of code. function curry(fn) { return (...xs) => { if (xs.length === 0) { throw Error('EMPTY INVOCATION'); } if (xs.length >= fn.length) { return fn(...xs); } return curry(fn.bind(null, ...xs)); }; } I am unable to grok the part of the explanation which states We create a copy of fn that has the first k arguments bound (partially applied) and pass it to curry as the next fn, with its

Referential transparency in functional programming

风流意气都作罢 提交于 2020-01-14 17:50:13
问题 I am new to JS and was learning functional programming and came across the term "referential transparency". Also, I found this statement "Referential transparency says it's safe to replace a pure function with its value". Does it mean that the use of RT makes it easy for JIT compiler to replace function with its return value as long as function gets hot? Is that true? 回答1: Here's an example: This is a pure function: it will always return the same output for the same input const even = x => x

How can this function be written using foldr?

放肆的年华 提交于 2020-01-14 14:07:41
问题 I have this simple function which returns a list of pairs with the adjacents elements of a list. adjacents :: [a] -> [(a,a)] adjacents (x:y:xs) = [(x,y)] ++ adjacents (y:xs) adjacents (x:xs) = [] I'm having problems trying to write adjacents using foldr . I've done some research but nothing seems to give me a hint. How can it be done? 回答1: Tricky folds like this one can often be solved by having the fold build up a function rather than try to build the result directly. adjacents :: [a] -> [(a

List[Try[T]] to Try[List[T]] in Scala

不打扰是莪最后的温柔 提交于 2020-01-14 13:59:07
问题 I would like to know how to convert a List[Try[T]] to Try[List[T]] in Scala? I have tried using an accumulator and folding right but it doesn't seem ideal. 回答1: Using cats it's as easy as: import cats._ import cats.data._ import cats.implicits._ import scala.util.{Try, Success, Failure} val tries: List[Try[Int]] = List(Success(1), Success(2), Success(3)) tries.sequence More information in the Traverse docs. 回答2: Try(list.map(_.get)) This will return only the first failure, so you need

Average of large number of Dice Rolls in Haskell

懵懂的女人 提交于 2020-01-14 10:15:54
问题 In an attempt to learn Haskell better, I'm trying to write a program that displays the average value of the sum of 2 die, rolled X number of times. This is fairly simple in C, Java, Python... but I'm stuck in Haskell. Here's a naive attempt: import System.Random main = do g <- getStdGen let trials = 10000000 let rolls = take trials (randomRs (2, 12) g :: [Int]) let average = div (sum rolls) trials print average For low number of trials, the program works. But when I run this code with ten

Is it a rule that unapply will always return an Option?

爱⌒轻易说出口 提交于 2020-01-14 08:40:49
问题 I tried to create an unapply method to use in pattern matching, and I tried to make it return something different than Option , however, Eclipse shows that as an error. Is it a rule that unapply must return an Option[T] ? EDIT: here's the code I'm trying to use. I switched the code from the previous section so that unapply returns a Boolean import java.util.regex._ object NumberMatcher { def apply(x:String):Boolean = { val pat = Pattern.compile("\\d+") val matcher = pat.matcher(x) return

Is it a rule that unapply will always return an Option?

末鹿安然 提交于 2020-01-14 08:40:28
问题 I tried to create an unapply method to use in pattern matching, and I tried to make it return something different than Option , however, Eclipse shows that as an error. Is it a rule that unapply must return an Option[T] ? EDIT: here's the code I'm trying to use. I switched the code from the previous section so that unapply returns a Boolean import java.util.regex._ object NumberMatcher { def apply(x:String):Boolean = { val pat = Pattern.compile("\\d+") val matcher = pat.matcher(x) return