How to rewrite this function as a pure function?
I have this function
function sumUpToNum(num) {
let sum = 0;
for(let z=0;z<num;z++) {
sum+=z;
}
return sum;
}
How to rewrite as a pure function? What i have tried:
function sumUpToNum(num) {
let sum = 0;
while(num) {
sum+=num;
}
return sum;
}
See also questions close to this topic

React Native infinite scroll problems with few elements
I'm using React Native with Firebase and I'm trying to use an infinite scroll (load more) on my Listview, everything works fine if the first fetch can fill the list for the entire height of the screen because onEndReached is fired but when there are few elements and the listview is not filled at all on the screen, onEndReached is not fired and so it doesn't download and fill with next records. Anyone can help me handling this situation? Thanks in advance.

Convert a cURL request to Javascript for an API call using hashed Authentication
I am a beginner at Javascript and APIs and I am trying to make PUT request with my credentials hashed with MD5 using Javascript which brings me back an Auth Code so that I can use to stay connected with the server but I haven't been able to.
I have a cURL request that does exactly what I need but I don't know how to make the same request from Javascript, I am using jQuery Ajax to try and do it.
my cURL code that works fine, looks like this:
curl s X PUT H "ContentType: application/json" d '{"data": {"credentials": "MYHASHEDCREDENTIALS","account_name": "Myaccount","method": "md5"}}' k https://my.domain.com:8443/v1/user_auth
And here is the Javascript version that brings back the error:
400 (Bad Request)
$.ajax({ type: "PUT", dataType: "json", url: "https://my.domain.com:8443/v1/user_auth", headers: {"ContentType" : "application/json"}, data: {"credentials": "MYHASHEDCREDENTIALS","account_name": "Centrecom","method": "md5"}, success: function(data) { console.log(data); } });
I am open to suggestions on how to get this done in Javascript, either by translating the cURL or by using a library.

Hello. i try to click all element of div but it now works
I try clicking on all divs but event works for last divs. how to call function for each div ? i want which div has been clicked the javascript call function for this. enter code here
enter code here
<style> .es{ margintop: 30px; } .es:hover { cursor: pointer; } </style> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es"> </rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" x="100"></rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" x="200"></rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="100"></rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="100" x="100"></rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="100" x="200"></rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="200"></rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="200" x="100"></rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="200" x="200"></rect> </svg> var svg = document.getElementsByClassName("es"); for (var property in svg) { svg[property].addEventListener("click",function(){ alert("clicked"+svg[property]); }); };

Is it possible to compute with high accuracy if a series converges using Haskell?
I am trying to develop a custom math library in Haskell that encodes expressions for a project. I'm wondering whether it is possible to determine (with high accuracy) if a mathematical from n to infinity converges or diverges. I have a sigma sum function available in my library, which I initially thought I could use to determine if a series converges by finding the difference of the sigma sum from some n to say something like 1000 and the sum from the same n to something much higher 100000. However, while I thought this might work at first, I realized this wouldn't work as any constant times a convergent series is also convergent and will wind up increasing the difference in the sums I was using to determine if a series is convergent or not.
This is what I have:
isConverging :: String > a > Expr a > Bool isConverging x a expr = if abs((eval (Map.fromList [(x, a)]) $ sigma x a (1000) expr)  (eval (Map.fromList [(x, a)]) $ sigma x a (100000) expr)) <= 1.0e3 then True else False
As a result, I'm wondering if there is a better way to compute if a series likely converges. Any help would be much appreciated.

Typelevel Cats.Effect vs Typlevel eff. which and why?
I want do do some programming that needs effects ( who doesn't :). In particular something like scalaz Task to run some asynchronous data retrieval and ruturn a Future like effect that will handle the resuts on completion.
I notice that typelevel Cats now has the Effect Monad but also there is the typelevel eff Monad project. Both of which are for dealing functionally with effects. So now I have a choice of where to invest my time and I'm confused.
 Which should I use?
 Is there some underlying ethos or intent of each library that will guide my choice?
 What is each library for?
 What problem does it solve?
Or can I just use both eg wrap cats.Effect in an eff FutureEffect or TaskEffect. Is that even a good idea?
Thanks

Recursive Multiplication in Scheme (trouble with negatives)
I am trying to multiply in Scheme using recursion, and I am able to do so with positive numbers but not negatives (they get stuck in an infinite loop). I assume the problem comes in the 3rd and 4th lines, which I wrote in an attempt to be able to handle negatives. Any help would be appreciated :)
(define Multiply(lambda (x y) (if (eq? x 0) 0 (if (eq? y 0) 0 ;if either x or y are zero, return 0 (if (> 0 x) ( 0 (+ x (Multiply x ( y 1)))) (if (> 0 y) ( 0 (+ x (Multiply x ( y 1)))) (+ x (Multiply x ( y 1)))))))))

How map work on Options in Scala?
I have this two functions
def pattern(s: String): Option[Pattern] = try { Some(Pattern.compile(s)) } catch { case e: PatternSyntaxException => None }
and
def mkMatcher(pat: String): Option[String => Boolean] = pattern(pat) map (p => (s: String) => p.matcher(s).matches)
Map is the higherorder function that applies a given function to each element of a list.
Now I am not getting that how map is working here as per above statement.

Multiple inline pure function calls using JavaScript...?
I'm scratching my head about solving a problem within JS and pure functions. The one below is an example of impure function but useful to understand what it does.
function fn(some) { var ret = 'g', mid = 'o'; if (some) return ret + some; ret += mid; return function d(thing) { if (thing) return ret += thing; ret += mid; return d; } } // Some output examples fn('l') => 'gl' fn()('l') => 'gol' fn()()()()()()()()('l') => 'gooooooool'
What if I need to make it pure to avoid any side effect? In the following example, the issue of an impure function shows up.
var state = fn()(); state('l') => 'gool' state()()()('z') => 'goooooz' // ...and not 'gooloooz'
Thanks!