I was looking into the elm-form package docs and found here this piece of code:
field "myfield" (int `andThen` minInt 10)
What do these quotes mean? Is it yet another way to apply a function?
It was a way to use any function as an infix, as in 3 + 5.
BUT: It's been removed in elm 0.18: migration notes.
Related
I'm removing builder pattern on multiple places. Following example would help me with the task, but mainly I'd like to learn how to use live templates more.
Preexisting code:
Something s = s.builder
.a(...)
.b(bbb)
.build();
and I'd like to remove it to:
Something s = new Something();
s.setA(...);
s.setB(bbb);
part of it can be trivially done using intellij regex, pattern: \.(.*)$ and replacement .set\u$1. Well it could be improved, but lets keep it simple.
I can create surround live template using variable:
regularExpression(SELECTION, "\\.(.*)", "\\u$1")
but \\u will be evaluated as u.
question 1: is it possible to get into here somehow \u functionality?
but I might get around is differently, so why not try to use live temlate variable:
regularExpression(SELECTION, "\\.(.)(.*)", concat(capitalize($1), "$2"))
but this does not seem to work either. .abc is replaced to bc
question 2: why? How would correct template look like? And probably, if it worked, this would behave incorrectly for multiline input. How to make it working and also for multiline inputs?
sorry for questions, I didn't find any harder examples of live templates than trivial replacements.
No, there is no \u functionality in the regularExpression() Live Template macro. It is just a way to call String.replaceAll(), which doesn't support \u.
You can create a Live Template like this:
set$VAR$
And set the following expression for the $VAR$ variable:
capitalize(regularExpression(SELECTION, "\\.(.*)", "$1"))
Thanks to #RedBassett for this Ressource (Kotlin problem solving): https://kotlinlang.org/docs/tutorials/koans.html
I'm aware this question exists here:
Creating a 4 digit Random Number using java with no repetition in digits
but I'm new to Kotlin and would like to explore the direct Kotlin features.
So as the title suggests, I'm trying to find a Kotlin specific way to nicely solve generate a 4 digit number (after that it's easy to make it adaptable for length x) without repeating digits.
This is my current working solution and would like to make it more Kotlin. Would be very grateful for some input.
fun createFourDigitNumber(): Int {
var fourDigitNumber = ""
val rangeList = {(0..9).random()}
while(fourDigitNumber.length < 4)
{
val num = rangeList().toString()
if (!fourDigitNumber.contains(num)) fourDigitNumber +=num
}
return fourDigitNumber.toInt()
}
So the range you define (0..9) is actually already a sequence of numbers. Instead of iterating and repeatedly generating a new random, you can just use a subset of that sequence. In fact, this is the accepted answer's solution to the question you linked. Here are some pointers if you want to implement it yourself to get the practice:
The first for loop in that solution is unnecessary in Kotlin because of the range. 0..9 does the same thing, you're on the right track there.
In Kotlin you can call .shuffled() directly on the range without needing to call Collections.shuffle() with an argument like they do.
You can avoid another loop if you create a string from the whole range and then return a substring.
If you want to look at my solution (with input from others in the comments), it is in a spoiler here:
fun getUniqueNumber(length: Int) = (0..9).shuffled().take(length).joinToString('')
(Note that this doesn't gracefully handle a length above 10, but that's up to you to figure out how to implement. It is up to you to use subList() and then toString(), or toString() and then substring(), the output should be the same.)
I have a DataFrame structured as parName|region|year, and access function as getData(parName,reg,year) ( I use access function because I implement my own query logic).
Would it be possible, based on unique(df[:parName]), to dynamically create a set of functions like par1(region,year) "pointing to" getData("par1",region,year) ?
If so, using which approach?
This is a bit the opposite of this question.. there it is explained how to dynamically call a function, while I wander if it possible to dynamically declare/define one..
EDIT:
I am using this approach in order to get the cleanest and most compact syntax possible in writing multi-dimensional equations.
I managed (thanks to the #Liso answer) to implement it as:
for par in unique(dropna(df[:parName]))
#eval ($(Symbol("$(par)_"))) = (r,d1,d2="",y=-1,op=sum) -> gd($par,r,d1,d2,y,op)
#eval ($(Symbol("$(par)!"))) = (v,r,d1,d2="",y=-1) -> sd(v,$par,r,d1,d2,y)
end
i.e., I am using the convention that par!() is a setData-type and par_() is a getData-type equation.
When I'll be able to complete the macro that transforms f(dim1,dim2) = value into f(value,dim1,dim2) I will be able to write my model using a LaTeX-like (and AMPL-like) syntax that is very clear:
#meq price!(tp in secProducts, r in fr2) = sum(price_(r,pp,"",y2)*a_(r,pp,tp,y2) for pp in priProducts) + margin_(r,tp,"",y2)
I am just beginner trying to understand Julia, so I am not sure if it is good idea or not!
See https://docs.julialang.org/en/stable/manual/metaprogramming/#Code-Generation-1 .
I was able to adapt that example to this :
julia> for i in 4:6
#eval ($(Symbol("func$i")))(a) = a^$i
end
julia> func4(2), func5(2), func6(2)
(16, 32, 64)
Maybe it could help you to play and learn :)
I am trying to evaluate the following expression:
7088.800/(((((((24.65995+24.43061+24.54517+24.65192)/4)-32.0)*5/9)+273.15)/288.15)^.5)
If you are asking yourself why I didn't use Sqrt() instead of ^0.5 it's because I'm doing some things with the string beforehand that require there be no letters.
I am using this simple code:
Expression.CacheEnabled = False
x = New Expression(xEquation)
y = New Expression(yEquation)
System.Diagnostics.Debug.Write(x.Error)
System.Diagnostics.Debug.Write(y.Error)
Return New PointF(x.Evaluate, y.Evaluate)
The answer I get is: 7088.800
The correct answer is:7336.46922305(according to google)
I am using .net 3.5 and ncalc 1.3.8
I suspect it doesn't like the amount of brackets there are but I can't find any mention of that being a problem anywhere...
Thanks!
I cannot get Ncalc or Ncalc-edge (v1.4.1) to use the exponentiation operator ^ and produce the correct result. E.g., "4 ^ 2" gives 6. It does not accept ** as an operator.
A little bit of investigation shows that it uses ^ as the Xor operator, in the style of C#. C# does not have an exponentiation operator, so you will have to devise a way of parsing your actual input string and using Sqrt.
There are currently were some requests on the Ncalc discussion forum regarding this, such as Override ^ operator (link now dead, and it's not even available on the Wayback Machine).
Given
String s = "a" + "b" + "c";
Is it possible to transform it to
StringBuilder s = new StringBuilder().append("a").append("b").append("c");
There is an intention for this, called Replace '+' with 'StringBuilder.append()'.
At least, it's offered to me - I'm using IDEA 12 EAP (Early Access Program).
You can get it here: IDEA 12 EAP
The intention is offered when the caret is on the concatenated sequence, not from the String variable declaration itself.
Of course, after the transformation IDEA will inform you that such an append() sequence is silly, and will offer to simplify to a regular concatenation without any downsides.