functional-programming

Improve this code by eliminating nested for cycles

蹲街弑〆低调 提交于 2019-12-23 18:20:42
问题 The R package corrplot contains, among the other stuff, this nifty function cor.mtest <- function(mat, conf.level = 0.95){ mat <- as.matrix(mat) n <- ncol(mat) p.mat <- lowCI.mat <- uppCI.mat <- matrix(NA, n, n) diag(p.mat) <- 0 diag(lowCI.mat) <- diag(uppCI.mat) <- 1 for(i in 1:(n-1)){ for(j in (i+1):n){ tmp <- cor.test(mat[,i], mat[,j], conf.level = conf.level) p.mat[i,j] <- p.mat[j,i] <- tmp$p.value lowCI.mat[i,j] <- lowCI.mat[j,i] <- tmp$conf.int[1] uppCI.mat[i,j] <- uppCI.mat[j,i] <- tmp

Take From a List While Increasing

梦想的初衷 提交于 2019-12-23 17:53:25
问题 I have a list of values that I would like to take from while the value is increasing. I assume it would always take the head of the list and then compare it to the next value. The function will continue to take as long as this continues to increase. Upon reaching an list element that is less than or equal the pervious value the list is returned. takeIncreasing :: (Ord a) => [a] -> [a] takeIncreasing [1,2,3,4,3,5,6,7,8] -- Should return [1,2,3,4] A fold could compare the last element of the

Error FS0037 sometimes, very confusing

时光总嘲笑我的痴心妄想 提交于 2019-12-23 16:35:52
问题 If I write the following F# code, the compiler issues an error. let a = 123 let a = 123 The error produced is: error FS0037: Duplicate definition of value 'a' If I write the same code in a function like this: let fctn = let a =123 let a =123 a it doesn't produce any error. I don't understand the difference. Can anyone please explain? Edit : first code I write in module level. 回答1: I agree this is confusing. The problem is that let behaves differently when it is used as a local variable

Is it possible to have a recursive anonymous function in MATLAB? [duplicate]

核能气质少年 提交于 2019-12-23 16:24:15
问题 This question already has an answer here : Recursive Anonymous Function Matlab (1 answer) Closed last year . I repeatedly want to apply a function, using a past output as the new input. For readability (I'm writing from a mathematics perspective, not a programmer's perspective), I would like to define it as a simple anonymous function instead of a full function block. So, instead of something like function f=myfun(x,n) if n>1 f=myfun(myfun(x,n-1),1); else f=expression(x); end end I would like

Sort string without any builtin methods

不问归期 提交于 2019-12-23 15:50:55
问题 I want to sort a string in javascript without using a built in method, just by using for's and comparisons like 'a' > 'b'; Something that doesn't work: function replaceAt(str, i, char) { return str.substr(0,i) + char + str.substr(i + 1) } function swap(str, i1, i2) { return replaceAt(replaceAt(str, i1, str[i2]),i2,str[i1]); } function sort(str) { var sorted = str; for (var i = 0; i < str.length; i++) { if (str[i] > str[i + 1]) { str = swap(str, i, i+1) } } return str; } Pseudo-code or books,

Is Ruby's returning of a different type after a filter unusual from a functional programming perspective?

丶灬走出姿态 提交于 2019-12-23 15:43:53
问题 In Ruby, there are some filter functions that produce a different type than what you started off with. For example, if you do {a: 2, b: 0}.find_all{|key, value| value.zero?} # Use Hash[new_array] to turn it into a hash you end up with an array of keys and values, not another hash. And if you do str = "happydays" all_indexes = [1, 2, 7, 8] str.each_char.reject.with_index{|char, index| all_indexes.include?(index)} # Use .join to turn it into a string you end up with an array of characters,

Chain call in clojure?

懵懂的女人 提交于 2019-12-23 15:15:41
问题 I'm trying to implement sieve of Eratosthenes in Clojure. One approach I would like to test is this: Get range (2 3 4 5 6 ... N) For 2 <= i <= N Pass my range through filter that removes multiplies of i For i+1 th iteration, use result of the previous filtering I know I could do it with loop/recur , but this is causing stack overflow errors (for some reason tail call optimization is not applied). How can I do it iteratively? I mean invoking N calls to the same routine, passing result of i th

Tail recursive functions for BinaryTree

删除回忆录丶 提交于 2019-12-23 14:44:33
问题 I am stuck with implementing tail recursive foreach, reduce, map and toList functions for a very simple implementation of binary tree. sealed trait Tree[+A] case object EmptyTree extends Tree[Nothing] case class Node[A](value: A, left: Tree[A], right: Tree[A]) extends Tree[A] object Tree { def apply[A]: Tree[A] = EmptyTree def apply[A](value: A): Tree[A] = Node(value, EmptyTree, EmptyTree) def apply[A](value: A, left: Tree[A], right: Tree[A]): Tree[A] = Node(value, left, right) def foreach[A]

How do I use Name as an applicative?

ぐ巨炮叔叔 提交于 2019-12-23 13:14:05
问题 scala> val a = Need(20) a: scalaz.Name[Int] = scalaz.Name$$anon$2@173f990 scala> val b = Need(3) b: scalaz.Name[Int] = scalaz.Name$$anon$2@35201f scala> for(a0 <- a; b0 <- b) yield a0 + b0 res90: scalaz.Name[Int] = scalaz.Name$$anon$2@16f7209 scala> (a |@| b) res91: scalaz.ApplicativeBuilder[scalaz.Name,Int,Int] = scalaz.ApplicativeBuilde r@11219ec scala> (a |@| b) { _ + _ } <console>:19: error: ambiguous implicit values: both method FunctorBindApply in class ApplyLow of type [Z[_]](implicit

F# How should I think about delimiting items in sequences?

江枫思渺然 提交于 2019-12-23 12:43:49
问题 Apologies for a rookie question. I'm trying to change my mental paradigm from procedural to functional. For instance, suppose I have a list of names that I want to print like this "John, Paul, George, and Ringo." But this code does not satisfy: let names = [ "John"; "Paul"; "George"; "Ringo" ] names |> Seq.iter (fun s -> printf "%s, " s) My procedural instinct is to seek a way to insinuate a predicate into that lambda so that it can branch between ", " or ", and " or ". " depending upon where