functional-programming

Understanding stack and frame in javascript

ε祈祈猫儿з 提交于 2019-12-14 03:02:55
问题 I am brand new to js, and am having trouble understanding what happens in the background. So if I have a recursive js function, do all the recursive calls create a stack frame on the stack? and then get popped off once we return? Is the heap involved in this process? Next the idea of closures. With this I am very confused with where things go does the first function get put on the heap? Any explanation would be greatly appreciated. 回答1: So if I have a recursive js function, do all the

What would be a good example of an endofunctor that is not the identity functor?

大憨熊 提交于 2019-12-14 02:25:03
问题 In Professor Frisby Introduces Composable Functional JavaScript the identity functor was introduced: const Box = x => ({ map: f => Box(f(x)), fold: f => f(x) // for testing }) I spent the better part of the day understanding functors and why the above JavaScript code is actually the identity functor. So I thought I would alter it to get a "real" functor that is not the identity functor. I came up with this: const Endo = x => ({ map: f => Endo(f(x).split('')), fold: f => f(x).split('') // for

algorithm to merge two arrays into an array of all possible combinations

人盡茶涼 提交于 2019-12-14 02:14:46
问题 Example given in JavaScript: Suppose we have two arrays [0,0,0] and [1,1,1]. What's the algorithm to produce all possible ways these two arrays can be combine. Example: mergeEveryWayPossible([0,0,0],[1,1,1]) // [ [0,0,0],[1,0,0], [0,1,0], [0,0,1], [1,1,0], [0,1,1], [1,0,1], [1,1,1] ] merge the arrays into an array of all possible combinations. This is different than finding the cartesian product. I'm also not sure what this kind of combination is called. If the algorithm or technique has a

Type to capture either integer, float or a string value and then do pattern matching in Scala

不打扰是莪最后的温柔 提交于 2019-12-14 02:11:16
问题 In Ocaml it is possible to define something like this: type univ = I of int | F of float | S of string ;; In order to create objects with like this: let pol_list = [I 3; F 4.3; S "potato"; I 4];; And then do pattern matching to extract a certain property (Either value or length depending on the case) like this: let get_value val = match val with | I v -> v | F v -> (int_of_float v) | S s -> (String.length s) How would this be done in Scala? If not possible, is there another similar

How to change the type of a function reference?

偶尔善良 提交于 2019-12-14 01:45:42
问题 Java8 lets me use a method declaration as implementation of any of my interfaces, as long as it only contains one method. However once it is defined, the type cannot be changed. My code: import java.util.function.IntConsumer; public class A { interface MyConsumer { void doSomething(int i); } public static void main(String[] args) { IntConsumer i = A::consume; MyConsumer i2 = A::consume; IntConsumer i3 = (IntConsumer) i2; // ClassCastException MyConsumer i4 = (MyConsumer) i; //

Where exactly does the performance advantage of LazyEvaluation emerge from?

陌路散爱 提交于 2019-12-14 01:16:12
问题 In the past few days, I have researched LazyEvaluation, in performance aspect mostly, wondering from where the performance advantage of LazyEvalutaion emerges . It's been so unclear to me reading various articles but a very few including What are the advantages of Lazy Evaluation? This refers to the evaluation of a syntax tree . If you evaluate a syntax tree lazily (i.e. when the value it represents is needed), you must carry it through the preceeding steps of your computation in its entirety

for vs map in functional programming

廉价感情. 提交于 2019-12-14 00:24:37
问题 I am learning functional programming using scala. In general I notice that for loops are not much used in functional programs instead they use map. Questions What are the advantages of using map over for loop in terms of performance, readablity etc ? What is the intention of bringing in a map function when it can be achieved using loop ? Program 1: Using For loop val num = 1 to 1000 val another = 1000 to 2000 for ( i <- num ) { for ( j <- another) { println(i,j) } } Program 2 : Using map val

What are the benefits of currying?

你离开我真会死。 提交于 2019-12-14 00:23:08
问题 I don't think I quite understand currying, since I'm unable to see any massive benefit it could provide. Perhaps someone could enlighten me with an example demonstrating why it is so useful. Does it truly have benefits and applications, or is it just an over-appreciated concept? 回答1: (There is a slight difference between currying and partial application , although they're closely related; since they're often mixed together, I'll deal with both terms.) The place where I realized the benefits

Is there a fast, functional prime generator?

荒凉一梦 提交于 2019-12-14 00:22:23
问题 Suppose I've got a natural number n and I want a list (or whatever) of all primes up to n . The classic prime sieve algorithm runs in O(n log n) time and O(n) space -- it's fine for more imperative languages, but requires in-place modification to lists and random access, in a fundamental way. There's a functional version involving priority queues, which is pretty slick -- you can check it out here. This has better space complexity at about O(n / log(n)) (asymptotically better but debatable at

Spark: Using iterator lambda function in RDD map()

一曲冷凌霜 提交于 2019-12-13 19:19:00
问题 I have simple dataset on HDFS that I'm loading into Spark. It looks like this: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... basically, a matrix. I'm trying to implement something that requires grouping matrix rows, and so I'm trying to add a unique key for every row like so: (1, [1 1 1 1 1 ... ]) (2, [1 1 1 1 1 ... ]) (3, [1 1 1 1 1 ... ]) ... I tried something somewhat naive: set a global variable and write a lambda function to iterate over the global variable: # initialize global index