functional-programming

Convert String array to Map using Java 8 Lambda expressions

我怕爱的太早我们不能终老 提交于 2019-12-28 05:56:35
问题 Is there a better functional way of converting an array of Strings in the form of "key:value" to a Map using the Java 8 lambda syntax? Arrays.asList("a:1.0", "b:2.0", "c:3.0") .stream() .map(elem -> elem.split(":") .collect(Collectors.toMap(keyMapper?, valueMapper?)); The solution I have right now does not seem really functional: Map<String, Double> kvs = new HashMap<>(); Arrays.asList("a:1.0", "b:2.0", "c:3.0") .stream() .map(elem -> elem.split(":")) .forEach(elem -> kvs.put(elem[0], Double

How do you compute the difference between successive elements of a list of unknown size, functionally?

你。 提交于 2019-12-28 05:49:05
问题 In a programming language that is purely functional (like Haskell) or where you are only using it in a functional way (eg clojure); suppose you have a list/seq/enumerable (of unknown size) of integers and you want to produce a new list/seq/enumerable that contains the differences between successive items, how would you do it? What I did previously in C# was to fold over the list and keep a state object as the aggregating value which recorded the 'previous' item so that you could do a diff on

A grasp of immutable datastructures

柔情痞子 提交于 2019-12-28 05:21:28
问题 I am learning scala and as a good student I try to obey all rules I found. One rule is: IMMUTABILITY!!! So I have tried to code everything with immutable data structures and vals, and sometimes this is really hard. But today I thought to myself: the only important thing is that the object/class should have no mutable state. I am not forced to code all methods in an immutable style, because these methods don't affect each other. My Question: Am I correct or are there any problems/disadvantages

python list comprehensions; compressing a list of lists?

故事扮演 提交于 2019-12-28 04:58:08
问题 guys. I'm trying to find the most elegant solution to a problem and wondered if python has anything built-in for what I'm trying to do. What I'm doing is this. I have a list, A , and I have a function f which takes an item and returns a list. I can use a list comprehension to convert everything in A like so; [f(a) for a in A] But this return a list of lists; [a1,a2,a3] => [[b11,b12],[b21,b22],[b31,b32]] What I really want is to get the flattened list; [b11,b12,b21,b22,b31,b32] Now, other

What is polymorphism in Javascript?

混江龙づ霸主 提交于 2019-12-28 04:39:06
问题 I have read some possible article I could found on the internet on polymorphism . But I think I could not quite grasp the meaning of it and its importance. Most of the articles don't say why it is important and how I can achieve polymorphic behavior in OOP (of course in JavaScript). I can not provide any code example because I haven't got the idea how to implement it, so my questions are below: What is it? Why we need it ? How it works? How can I achieve this polymorphic behavior in

Idiomatic Way to declare C++ Immutable Classes

独自空忆成欢 提交于 2019-12-28 03:45:18
问题 So I have some pretty extensive functional code where the main data type is immutable structs/classes. The way I have been declaring immutability is "practically immutable" by making member variables and any methods const. struct RockSolid { const float x; const float y; float MakeHarderConcrete() const { return x + y; } } Is this actually the way "we should do it" in C++? Or is there a better way? 回答1: The way you proposed is perfectly fine, except if in your code you need to make assignment

Functional Reactive Programming in Scala [closed]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-28 03:17:07
问题 Closed. This question is off-topic. It is not currently accepting answers. Closed last year . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. Are there any libraries written for Scala enabling Functional Reactive Programming? 回答1: See also Odersky et al.'s paper "Deprecating the Observer Pattern". It explains the library Scala.React, which was developed for the paper

Why no generics in Go?

喜你入骨 提交于 2019-12-28 02:35:46
问题 Disclaimer: I've only played with Go for one day now, so there's a good chance I've missed a lot. Does anybody know why there is no real support for generics/templates/whatsInAName in Go? So there is a generic map , but that's supplied by the compiler, while a Go programmer can't write her own implementation. With all the talk about making Go as orthogonal as possible, why can I USE a generic type but not CREATE a new one? Especially when it comes to functional programming, there are lambdas,

Why doesn't outer work the way I think it should (in R)?

有些话、适合烂在心里 提交于 2019-12-28 02:06:43
问题 Prompted by @hadley's article on functionals referenced in an answer today, I decided to revisit a persistent puzzle about how the outer function works (or doesn't). Why does this fail: outer(0:5, 0:6, sum) # while outer(0:5, 0:6, "+") succeeds This shows how I think outer should handle a function like sum : Outer <- function(x,y,fun) { mat <- matrix(NA, length(x), length(y)) for (i in seq_along(x)) { for (j in seq_along(y)) {mat[i,j] <- fun(x[i],y[j])} } mat} > Outer(0:5, 0:6, `+`) [,1] [,2]

How do you represent a graph in Haskell?

烂漫一生 提交于 2019-12-27 12:42:29
问题 It's easy enough to represent a tree or list in haskell using algebraic data types. But how would you go about typographically representing a graph? It seems that you need to have pointers. I'm guessing you could have something like type Nodetag = String type Neighbours = [Nodetag] data Node a = Node a Nodetag Neighbours And that would be workable. However it feels a bit decoupled; The links between different nodes in the structure don't really "feel" as solid as the links between the current