functional-programming

Limit throughput with RxJava

↘锁芯ラ 提交于 2019-12-22 05:49:45
问题 The case I'm into right now is quite hard to explain so I will write a simpler version just to explain the issue. I have an Observable.from() which emits a sequence of files defined by an ArrayList of files. All of these files should be uploaded to a server. For that I have an function that does the job and returns an Observable . Observable<Response> uploadFile(File file); When I run this code it gets crazy, the Observable.from() emits all of the files and they are uploaded all at ones, or

How are point-free functions actually “functions”?

有些话、适合烂在心里 提交于 2019-12-22 05:39:07
问题 Conal here argues that nullary-constructed types are not functions. However, point-free functions are described as such for example on Wikipedia, when they take no explicit arguments in their definitions, and it seemingly is rather a property of currying. How exactly are they functions? Specifically: how are f = map and f = id . map different in this context? As in, f = map is simply just a binding to a value that happens to be a function where f simply "returns" map (similar to how f = 2

How to call impure functions from pure ones?

﹥>﹥吖頭↗ 提交于 2019-12-22 05:33:12
问题 I have just finished reading a "Learn You a Haskell for Great Good!" book so my question can be very naive. What I don't understand is how to call "impure" IO functions from the pure code. Here is a working example written in C#. In our business logic we plan some actions based on weather. We do it in usual C# manner. interface IWeatherForecast { WeatherData GetWeather(Location location, DateTime timestamp); } // concrete implementation reading weather from DB class DbWeather :

Function Factory in R

怎甘沉沦 提交于 2019-12-22 05:26:15
问题 I try to come up with a function factory by returning a dictionary of specialized functions, more or less like functional programming style. I try to do this in the following code. require(hash) names = c("aa","bb","cc"); funs = hash() for (i in seq(length(names))) { n = names[i] funs[[n]] = function(x) { print(paste(n,":",x, sep="")) } } Obviously, I have 3 functions in the array; however, they all behave the same as the last function in the iteration. > funs[["aa"]](1) [1] "cc:1" > funs[[

How safe would it be to use functional-java to add closures to a Java production project?

假装没事ソ 提交于 2019-12-22 05:13:15
问题 I would love to use closures in Java. I have read that they may or may not make it into Java 7. But an open-source project called functional-java has implemented functional features including closures. How safe would it be to use such a library in an enterprise production app? Is there a better way to add closures to Java currently? 回答1: Closures will definitely not make it into Java 7, due to a lack of consensus around a single implementation. See here. The Functional Java library is

Java 8 functional constructor from templated object

╄→尐↘猪︶ㄣ 提交于 2019-12-22 05:10:42
问题 I am using Eclipse Luna Service Release 2 (4.4.2), Java 8 u51. I'm trying to create a method which will create instances of a passed object based on another method parameter. The prototype is simplified to public <T> T test(Object param, T instance) { Constructor<?> constructor = instance.getClass().getConstructors()[0]; // I actually choose a proper constructor // eclipse reports "Unhandled exception type InvocationTargetException" Function<Object, Object> createFun = constructor:

What are the real advantages of immutable collections?

你离开我真会死。 提交于 2019-12-22 05:09:21
问题 Scala provides immutable collections, such as Set , List , Map . I understand that the immutability has advantages in concurrent programs. However what are exactly the advantages of the immutability in regular data processing? What if I enumerate subsets , permutations and combinations for example? Does the immutable collections have any advantage here? 回答1: What are exactly the advantages of the immutability in regular data processing? Generally speaking, immutable objects are easier/simpler

Using 'map' with different sized collections in clojure

落花浮王杯 提交于 2019-12-22 05:01:01
问题 I'd like to understand the idiomatic way with which to operate over collections of different sizes in clojure. Is there a way I can tell the function 'map' to pad the rest of a collection with some default value? As an example, suppose I have 3 vectors: (def x [1 2 3 4]) (def y [1 2 3 4 5]) (def z [1 2 3 4 5 6 7]) (map + x y z) ; yields (3 6 9 12) In this case, how can I pad x and y with zeroes and have this yield: (3 6 9 12 10 6 7) 回答1: map doesn't do it itself, but you can use a combination

Is there a way to prevent union types in TypeScript?

China☆狼群 提交于 2019-12-22 04:48:40
问题 I'm new to conditional types, so I tried the most obvious static way, no success: type NoUnion<Key> = Key extends 'a' ? 'a' : Key extends 'b' ? 'b' : never; type B = NoUnion<'a'|'b'>; The B type is still a union. Would somebody please school me? Here's a playground. 回答1: I am unsure what the usecase for this is, but we can force the NoUnion to never if the passed type is a union type. As other mentioned conditional types distribute over a union, this is called distributive conditional types

Enumeration Combinations of K elements using Java 8

拥有回忆 提交于 2019-12-22 04:48:24
问题 Given an instance of List<E> , using Java 8 features, how is it possible to build a List<List<E>> enumerating all possible combinations of k elements of the original List? 回答1: This is an algorithm that I wrote for solving some Euler Project problems : public static <E> Stream<List<E>> combinations(List<E> l, int size) { if (size == 0) { return Stream.of(Collections.emptyList()); } else { return IntStream.range(0, l.size()).boxed(). <List<E>> flatMap(i -> combinations(l.subList(i+1, l.size())