Elixir's Range for arithmetic progression - haskell

Is it possible to express arithmetic progression in a list without listing them all?
In Haskell, you could do it with the range function.
[2,4..10] == [2,4,6,8,10]
Is there a similar way to do it with Elixir ?

Stream.iterate/2 does what you want:
Stream.iterate(2, &(&1+2))

You can use Erlang's lists:seq function, from Elixir:
:lists.seq(2,10,2)

As I can see, there is a Stream.seq() added a month ago:
Add Stream.seq() for generating potentially infinite streams of values
Add a range/step/seq function

Related

Does Julia have a way to solve for unknown variables

Is there a function in Julia that is similar to the solver function in Excel where I can provide and equation, and it will solve for the unknown variable? If not, does anybody know the math behind Excel's solver function?
I am not expecting anybody to solve the equation, but if it helps:
Price = (Earnings_1/(1+r)^1)+(Earnings_2/(1+r)^2)++(Earnings_3/(1+r)^3)+(Earnings_4/(1+r)^4)+(Earnings_5/(1+r)^5)+(((Earnings_5)(RiskFreeRate))/((1+r)^5)(1-RiskFreeRate))
The known variables are: Price, All Earnings, and RiskFreeRate. I am just trying to figure out how to solve for r.
Write this instead as an expression f(r) = 0 by subtracting Price over to the other side. Now it's a rootfinding problem. If you only have one variable you're solving for (looks to be the case), then Roots.jl is a good choice.
fzero(f, a::Real, b::Real)
will search for a solution between a and b for example, and the docs have more choices for algorithms when you don't know a range to start with and only give an initial condition for example.
In addition, KINSOL in Sundials.jl is good when you know you're starting close to a multidimensional root. For multidimensional and needing some robustness to the initial condition, I'd recommend using NLsolve.jl.
There's nothing out of the box no. Root finding is a science in itself.
Luckily for you, your function has an analytic first derivative with respect to r. That means that you can use Newton Raphson, which will be extremely stable for your function.
I'm sure you're aware your function behaves badly around r = -1.

How do I avoid repeating long formulas in Excel when working with comparisons?

I know that something like the following
=IF(ISERROR(LONG_FORMULA), 0, LONG_FORMULA)
can be replaced with
=IFERROR(LONG_FORMULA, 0)
However I am looking for an expression to avoid having to type REALLY_LONG_FORMULA twice in
=IF(REALLY_LONG_FORMULA < threshold, 0, REALLY_LONG_FORMULA)
How can I do this?
I was able to come up with the following:
=IFERROR(EXP(LN(REALLY_LONG_FORMULA – threshold)) + threshold, 0)
It works by utilizing the fact that the log of a negative number produces an error and that EXP and LN are inverses of each other.
The biggest benefit of this is that it avoids accidentally introducing errors into your spreadsheet when you change something in one copy of REALLY_LONG_FORMULA without remembering to apply the same change to the other copy of REALLY_LONG_FORMULA in your IF statement.
Greater than comparisons as in
=IF(REALLY_LONG_FORMULA>=threshold,0,REALLY_LONG_FORMULA)
can be replaced with
=IFERROR(threshold-EXP(LN(threshold-REALLY_LONG_FORMULA)),0)
Example below (provided by #Jeeped):
For strict inequality comparisons use SQRT(_)^2 as pointed out by #Tom Sharpe.
If you're comparing against a threshold amount, I would consider checking out ExcelJet's recent blog post about
Replacing Ugly IFs with MAX() or MIN().
Also, the MAX() and MIN() functions are much more intuitive than using lessor known functions like EXP() and LN().
Comparing Ln Exp with SQRT ^2:-
because SQRT(0) gives 0 but ln(0) gives #NUM!
So you can choose which one to use depending whether you want the equality or not.
These also work for negative numbers - in theory.

Does Gherkin have any negation?

I am used to something like NOT in specifying conditions - but perhaps it will only allow distinct positive conditions? e.g. If the user is not anonomous
There isn't a not operator in the gherkin syntax. As #orde has said in the comments, you just have to write a negative step definition that checks for what you want.
One thing that can make this a little nicer to use is knowing that you can write steps using the 'But' keyword instead of only using 'And'
Given I have set something up
When I do something
Then something positive should happen
And some other thing should happen
But some negative thing should not happen
Not a huge difference but sounds nicer.
As Dave McNulla has said you can write your steps to take 'is | is not' if you want to reuse them.

Can I perform mathematics on a selection in Sublime Text?

Short of writing my own plugin to do so, does there exist a mechanism with which I can add/subtract from multiple selected numbers?
I need to apply the same equation (in this case, subtract 5) to many values; and I'd rather not do it individually. Could this perhaps be a job for another program?
If you already use Emmet (which I highly suggest), use its Evaluate Math function. No need to install more plugins. Only issue is that it cuts the result at 2 decimal digits.
There's plugin called Sublime-Evaluate which allows you to evaluate bits of Python, and output it straight into your buffer. For example, [i for i in range(5)] evaluated turns itself into [0, 1, 2, 3, 4]. That can be used for arithmetic operations as well, just use those multiple selections and write +5 after each value, then select it all and evaluate. The plugin usually screws positions of your cursors, don't get confused by that.
Sublime Calculate evaluates selected text, and looks great for small calculations, especially paired with the replace option: https://github.com/colinta/SublimeCalculate
As stated before, Sublime Evaluate has python evaluation covered: https://github.com/jbrooksuk/Sublime-Evaluate
Saw this yesterday and thought I would take a shot at it. Basically does the same thing as the Sublime-Evaluate (to bad I didn't see that earlier, could have saved some time). Anyways, the benefit of mine is it allows you to predefine python snippets to run. It also does some variable substitution of the content selected by the cursor. It doesn't do any expansion so you will have to select the content to insert. I was thinking of creating a setting to modify the word separator for the plugin, but hadn't decided yet. If you have an opinion on it, let me know.
https://github.com/skuroda/EvalInsert

Generate a list of number with interval in Groovy

I'm learning Groovy. I want an array of numbers from 0 to n with interval 0.1.
double arr=[0,0.1,0.2....n]
I could write a java style for-loop, but is there a easier syntax to do this? I know Groovy has a lot of syntactic sugar.
I would go with 0.0..10.0.collect{it/10.0} but maybe there is clever way to do it by specifying increments.
there was an exact question here earlier
n=10;(0..10*n).collect{it/10}
there are no decimal step values for Groovy's ranges
currently

Resources