functional-programming

How to sum up the individual fields of the object list and return the results as a single object

时光毁灭记忆、已成空白 提交于 2019-12-19 18:31:55
问题 I have a method which is calculating nutrients for a list of object which we are receiving from API request call. The method looks like: public Nutrients nutrientsCalculator(DailyMeals dailyMeals) { String foodNamesForRequest = prepareFoodNamesForRequest(dailyMeals); HttpEntity<NutrientsBodyForRequest> requestBody = prepareRequestForAPICall(foodNamesForRequest); ResponseEntity<List<FoodNutritional>> response = //create request here if (nonNull(response.getBody())) { double totalFat = response

Break or shortcircuit a fold in Scala

南楼画角 提交于 2019-12-19 16:58:40
问题 I have written a simple depth-first search in Scala with a recursive function like that: search(labyrinth, path, goal) where labyrinth is a specification of the problem (as graph or whatever), path is a list that holds the path taken so far and goal is a specification of the goal state. The function returns a path to the goal as a List and Nil if no path can be found. The function expands, e.g. finds all suitable next nodes (candidates) and then has to recursively call itself. I do this by

Break or shortcircuit a fold in Scala

人盡茶涼 提交于 2019-12-19 16:58:10
问题 I have written a simple depth-first search in Scala with a recursive function like that: search(labyrinth, path, goal) where labyrinth is a specification of the problem (as graph or whatever), path is a list that holds the path taken so far and goal is a specification of the goal state. The function returns a path to the goal as a List and Nil if no path can be found. The function expands, e.g. finds all suitable next nodes (candidates) and then has to recursively call itself. I do this by

Is there a way to use print with the formats of printf in Haskell?

旧城冷巷雨未停 提交于 2019-12-19 16:52:58
问题 On my quest to acquire further experience in Haskell, I started working with print and printf. I wanted to try to print an array (well, several, but it's just a start) and I wanted to use the format "%+.4f" , meaning I would get: +2.1234 or -1.2345 I noticed however that it's pretty hard to print an array using printf, so I tried switching to print. It seems easier to print a list this way, but I'm not sure how I can print the elements of the list using the same format I used for printf. My

When would you use reduce() instead of sum()?

泄露秘密 提交于 2019-12-19 16:38:21
问题 I began learning functional programming recently, and came up with this example when attempting to calculate my quiz average for a class. The example I came up with is: scores = [90, 91, 92, 94, 95, 96, 97, 99, 100] def add(num1, num2): '''returns the sum of the parameters''' return num1 + num2 import operator timeit reduce(add, scores) / len(scores) #--> 1000000 loops, best of 3: 799 ns per loop timeit sum(scores) / len(scores) #--> 1000000 loops, best of 3: 207 ns per loop timeit reduce

How to cast a character to int in Clojure?

僤鯓⒐⒋嵵緔 提交于 2019-12-19 15:26:12
问题 How to cast a character to int in Clojure? I am trying to write a rot 13 in clojure, so I need to have something to cast my char to int. I found something called (int), so I put: (int a) Get: CompilerException java.lang.RuntimeException: Unable to resolve symbol: a in this context, compiling:(NO_SOURCE_PATH:13:1) Then I put: (int 'a) Get: ClassCastException clojure.lang.Symbol cannot be cast to `java.lang.Character clojure.lang.RT.intCast (RT.java:1087) Then: (rot13 ''a') Get:

How to cast a character to int in Clojure?

微笑、不失礼 提交于 2019-12-19 15:26:07
问题 How to cast a character to int in Clojure? I am trying to write a rot 13 in clojure, so I need to have something to cast my char to int. I found something called (int), so I put: (int a) Get: CompilerException java.lang.RuntimeException: Unable to resolve symbol: a in this context, compiling:(NO_SOURCE_PATH:13:1) Then I put: (int 'a) Get: ClassCastException clojure.lang.Symbol cannot be cast to `java.lang.Character clojure.lang.RT.intCast (RT.java:1087) Then: (rot13 ''a') Get:

Java 8 fill array with supplier

耗尽温柔 提交于 2019-12-19 12:24:08
问题 Is there a way to fill an array using java 8 Supplier ? I would like to write: Supplier<Object> supplier = () -> new Object(); Object[] array = new Object[size]; Arrays.fill(array, supplier); Note: I know i could write my own method. 回答1: In case you want to create new array filled with results generated by Supplier you can use Object[] array = Stream.generate(supplier) .limit(arraySize) .toArray(); // will generate new *Object[]* array For different types than Object[] you can use toArray

Java 8 fill array with supplier

安稳与你 提交于 2019-12-19 12:24:05
问题 Is there a way to fill an array using java 8 Supplier ? I would like to write: Supplier<Object> supplier = () -> new Object(); Object[] array = new Object[size]; Arrays.fill(array, supplier); Note: I know i could write my own method. 回答1: In case you want to create new array filled with results generated by Supplier you can use Object[] array = Stream.generate(supplier) .limit(arraySize) .toArray(); // will generate new *Object[]* array For different types than Object[] you can use toArray

What is a function composition algorithm that will work for multiple arguments, such as h(x,y) . f(x) . g(x) = h(f(x),g(x))?

安稳与你 提交于 2019-12-19 10:48:29
问题 For example, suppose we had the functions double(x) = 2 * x , square(x) = x ^ 2 and sum(x,y) = x + y . What is a function compose such as compose(compose(sum, square), double) = x^2 + 2*x ? Notice that I'm asking a function that can be used for functions of any arity. For example, you could compose f(x,y,z) with g(x) , h(x) , i(x) into f(g(x), h(x), i(x)) . 回答1: This is a common Haskell idiom, applicative functors: composed = f <$> g1 <*> g2 <*> ... <*> gn (A nicer introduction can be found