functional-programming

Is there any difference between flatten and flatMap(identity)?

血红的双手。 提交于 2019-12-18 04:17:11
问题 scala> List(List(1), List(2), List(3), List(4)) res18: List[List[Int]] = List(List(1), List(2), List(3), List(4)) scala> res18.flatten res19: List[Int] = List(1, 2, 3, 4) scala> res18.flatMap(identity) res20: List[Int] = List(1, 2, 3, 4) Is there any difference between these two functions? When is it appropriate to use one over the other? Are there any tradeoffs? 回答1: You can view flatMap(identity) as map(identity).flatten . (Of course it is not implemented that way, since it would take two

Functional way to create an array of numbers

强颜欢笑 提交于 2019-12-18 04:15:13
问题 How could I write the following code more functionally using ES6, without any 3rd party libraries? // sample pager array // * output up to 11 pages // * the current page in the middle, if page > 5 // * don't include pager < 1 or pager > lastPage // * Expected output using example: // [9,10,11,12,13,14,15,16,17,18,19] const page = 14 // by example const lastPage = 40 // by example const pagerPages = page => { let newArray = [] for (let i = page - 5; i <= page + 5; i++) { i >= 1 && i <=

Convert a String list to an Int list

微笑、不失礼 提交于 2019-12-18 03:56:44
问题 I've got a list of strings, is it possible to convert it to an list of ints? E.g.: ["1","2"] -> [1,2] 回答1: Well f :: [String] -> [Int] f = map read No? 回答2: This fails: map read ["1","2"] [*Exception: Prelude.read: no parse The way to do it is : map (read::String->Int) ["1","2"] [1,2] :: [Int] Outside of GHCI, in a .hs file it would be: let intList = map (read::String->Int) ["1","2"] 回答3: The general answer to such questions is to split the task up into parts: [String] -> [Int] looks like a

Feed elements of a tuple to a function as arguments in Haskell?

醉酒当歌 提交于 2019-12-18 03:06:09
问题 In my Haskell program, I want to use printf to format a list of tuples. I can map printf over a list to print out the values one at a time like this: mapM_ (printf "Value: %d\n") [1,2,3,4] Value: 1 Value: 2 Value: 3 Value: 4 I want to be able to do something like this: mapM_ (printf "Values: %d %d\n") [(1,100),(2,350),(3,600),(4,200)] Values: 1 100 Values: 2 350 Values: 3 600 Values: 4 200 But this passes a tuple to printf, not two separate values. How can I turn the tuple into two arguments

Scala tail-recursive Stream processor function defined in trait holds reference to stream-head

删除回忆录丶 提交于 2019-12-18 02:43:33
问题 In the following situation trait T { @tailrec def consume[A](as: Stream[A]): Unit = { if (as.isEmpty) () else consume(as.tail) } } object O extends T calling O.consume(Range(1, N).toStream) with N big enough, the program will run out of memory, or at least will consume O(N) instead of the needed O(1). 回答1: The tail-recursive method is generated for the trait. The method entry in the extender of the trait (here O ) forwards the call to the method of the trait, but while doing so, it keeps a

Scala tail-recursive Stream processor function defined in trait holds reference to stream-head

戏子无情 提交于 2019-12-18 02:43:18
问题 In the following situation trait T { @tailrec def consume[A](as: Stream[A]): Unit = { if (as.isEmpty) () else consume(as.tail) } } object O extends T calling O.consume(Range(1, N).toStream) with N big enough, the program will run out of memory, or at least will consume O(N) instead of the needed O(1). 回答1: The tail-recursive method is generated for the trait. The method entry in the extender of the trait (here O ) forwards the call to the method of the trait, but while doing so, it keeps a

Is the PartialFunction design inefficient?

柔情痞子 提交于 2019-12-18 02:41:25
问题 This is something I've wondered about for a while. I see this pattern a lot: if (pf.isDefinedAt(in)) pf(in) By breaking this up into two separate calls, all of the patterns that were evaluated in #isDefinedAt are then also evaluated in #apply. For example: object Ex1 { def unapply(in: Int) : Option[String] = { println("Ex1") if (in == 1) Some("1") else None } } object Ex2 { def unapply(in: Int) : Option[String] = { println("Ex2") if (in == 2) Some("2") else None } } val pf : PartialFunction

Have I implemented Y-combinator using C# dynamic, and if I haven't, what is it?

徘徊边缘 提交于 2019-12-18 02:28:26
问题 My brain seems to be in masochistic mode, so after being drowned in this, this and this, it wanted to mess around with some DIY in C#. I came up with the following, which I don't think is the Y-combinator, but it does seem to manage to make a non-recursive function recursive, without referring to itself: Func<Func<dynamic, dynamic>, Func<dynamic, dynamic>> Y = x => x(x); So given these: Func<dynamic, Func<dynamic, dynamic>> fact = self => n => n == 0 ? 1 : n * self(self)(n - 1); Func<dynamic,

Is there idiomatic C# equivalent to C's comma operator?

只谈情不闲聊 提交于 2019-12-17 23:33:09
问题 I'm using some functional stuff in C# and keep getting stuck on the fact that List.Add doesn't return the updated list. In general, I'd like to call a function on an object and then return the updated object. For example it would be great if C# had a comma operator: ((accum, data) => accum.Add(data), accum) I could write my own "comma operator" like this: static T comma(Action a, Func<T> result) { a(); return result(); } It looks like it would work but the call site would ugly. My first

Functional data structures in C++

蹲街弑〆低调 提交于 2019-12-17 23:32:23
问题 Does anyone know of a C++ data structure library providing functional (a.k.a. immutable, or "persistent" in the FP sense) equivalents of the familiar STL structures? By "functional" I mean that the objects themselves are immutable, while modifications to those objects return new objects sharing the same internals as the parent object where appropriate. Ideally, such a library would resemble STL, and would work well with Boost.Phoenix (caveat- I haven't actually used Phoenix, but as far as I