functional-programming

Kadane's Algorithm in Scala

坚强是说给别人听的谎言 提交于 2021-02-07 05:35:13
问题 Does anyone have a Scala implementation of Kadane's algorithm done in a functional style? Edit Note: The definition on the link has changed in a way that invalidated answers to this question -- which goes to show why questions (and answers) should be self-contained instead of relying on external links. Here's the original definition: In computer science, the maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional array of numbers (containing at least

Eliminate redundant template argument in C++

南楼画角 提交于 2021-02-07 03:46:47
问题 I'm trying to write a demo that implements the fmap in Haskell with continuation , and my code looks like this: #include <cstdio> #include <functional> template <typename X> using Callback = std::function<void(X)>; template <typename X, typename Y> using Fun = std::function<Y(X)>; template <typename X, typename Y> struct F_map; template <typename X> struct __F { virtual void operator()(Callback<X>&& callback) = 0; virtual __F<X>* self() { return this; } template <typename Y> auto map(Fun<X, Y

Scala: Iterate over CSV files in a functional way?

▼魔方 西西 提交于 2021-02-06 05:32:16
问题 I have CSV files with comments that give column names, where the columns change throughout the file: #c1,c2,c3 a,b,c d,e,f #c4,c5 g,h i,j I want to provide a way to iterate over (only) the data rows of the file as Maps of column names to values (all Strings). So the above would become: Map(c1 -> a, c2 -> b, c3 -> c) Map(c1 -> d, c2 -> e, c3 -> f) Map(c4 -> g, c5 -> h) Map(c4 -> i, c5 -> j) The files are very large, so reading everything into memory is not an option. Right now I have an

clojure recursion conj a list

梦想的初衷 提交于 2021-02-05 09:28:23
问题 ((fn foo [x] (when (> x 0) (conj (foo (dec x)) x))) 5) For this code, the result is [5 4 3 2 1] Why isn't is [1,2,3,4,5]? I see we do conf from result of recursive foo call with a value. For I thought it should be 1 2 3 4 5? Need help to understand this. Thanks. 回答1: From the documentation of conj : clojure.core/conj ([coll x] [coll x & xs]) conj[oin]. Returns a new collection with the xs 'added'. (conj nil item) returns (item). The 'addition' may happen at different 'places' depending on the

Function application for curried functions in JavaScript and ES6

╄→гoц情女王★ 提交于 2021-02-05 08:35:24
问题 I love that ECMAScript 6 allows you to write curried functions like this: var add = x => y => z => x + y + z; However, I hate that we need to parenthesize every argument of a curried function: add(2)(3)(5); I want to be able to apply curried functions to multiple arguments at once: add(2, 3, 5); What should I do? I don't care about performance. 回答1: Currying and the application of curried functions are controversial issues in Javascript. In simple terms, there are two opposing views, which I

How to find the maximum nesting depth of a S-expression in scheme?

拈花ヽ惹草 提交于 2021-02-05 07:33:20
问题 for example (nestFind '(a(b)((c))d e f)) => 3 (nestFind '()) => 0 (nestFind '(a b)) => 1 (nestFind '((a)) )=> 2 (nestFind '(a (((b c d))) (e) ((f)) g)) => 4 this is what i tried so far but its not working properly: (define (nestFind a) (cond ((null? a)0) ((atom? a)0) ((atom? (car a))(+ 1 (nestFind (cdr a)))) (else (+(nestFind (car a))(nestFind (cdr a)))))) 回答1: It's a bit simpler. Give this a try: (define (nestFind lst) (if (not (pair? lst)) 0 (max (add1 (nestFind (car lst))) (nestFind (cdr

How to apply a function between the elements of 2 lists in R?

最后都变了- 提交于 2021-02-05 06:42:53
问题 Imagine that you have these variables: > a <- list(matrix(1:25, 5, 5, byrow = TRUE), matrix(31:55, 5, 5, byrow = TRUE)) > b <- list(rep(1, 5), rep(2, 5)) > a [[1]] [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 6 7 8 9 10 [3,] 11 12 13 14 15 [4,] 16 17 18 19 20 [5,] 21 22 23 24 25 [[2]] [,1] [,2] [,3] [,4] [,5] [1,] 31 32 33 34 35 [2,] 36 37 38 39 40 [3,] 41 42 43 44 45 [4,] 46 47 48 49 50 [5,] 51 52 53 54 55 > b [[1]] [1] 1 1 1 1 1 [[2]] [1] 2 2 2 2 2 I want to end up with something like this:

How to apply a function between the elements of 2 lists in R?

蓝咒 提交于 2021-02-05 06:42:26
问题 Imagine that you have these variables: > a <- list(matrix(1:25, 5, 5, byrow = TRUE), matrix(31:55, 5, 5, byrow = TRUE)) > b <- list(rep(1, 5), rep(2, 5)) > a [[1]] [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 6 7 8 9 10 [3,] 11 12 13 14 15 [4,] 16 17 18 19 20 [5,] 21 22 23 24 25 [[2]] [,1] [,2] [,3] [,4] [,5] [1,] 31 32 33 34 35 [2,] 36 37 38 39 40 [3,] 41 42 43 44 45 [4,] 46 47 48 49 50 [5,] 51 52 53 54 55 > b [[1]] [1] 1 1 1 1 1 [[2]] [1] 2 2 2 2 2 I want to end up with something like this:

What is the exact meaning of the equal sign in Elixir?

Deadly 提交于 2021-02-05 06:18:46
问题 I don't get what exactly means the equal sign in Elixir. What is unclear is that it looks like a mix between assignment and a pattern matching operation. iex(1)> x=4 4 iex(2)> y=5 5 iex(3)> 3=y ** (MatchError) no match of right hand side value: 5 iex(3)> y=3 3 iex(4)> y=x 4 I understand that in Elixir, the equals operator means to match the left side of the = sign to the the right side. First two lines make sense to me. x and y are unbound variables, hence they could match anything. They are

Getting first element and returning after apply a function

末鹿安然 提交于 2021-02-04 16:18:25
问题 I am new in Java 8, I want to make a method that gets the first element that matched and returning after apply a function public void test() { List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API"); String str = features .stream() .filter(s -> "Lambdas".equals(s)) .findFirst() .ifPresent(this::toLowerCase); } private String toLowerCase (String str) { return str.toLowerCase(); } but I got an Incompatible types error. 回答1: Optional.ifPresent