Incrementing uniqueID over each List.map iteration - elm

I'm trying to initialise a set of elements with a unique identifier. The elements are part of the initial model.
What I have in mind is a function
initElement: Int -> InputElement -> Output
initElement id element = ...
that adds the given id to the given element. Now I'm iterating over these elements in another function like this:
uid = 0
elementsList = ...
newList = List.map (initElement uid++) elementsList
and I would want this integer uid increased with every iteration over an element by List.map, so that every element gets a unique number assigned. The ++ obviously doesn't work though.
Is this possible, or am I thinking too object-oriented? I'm pretty new to functional programming.

One option is to use List.indexedMap
http://package.elm-lang.org/packages/elm-lang/core/3.0.0/List#indexedMap
It gives you a nice incrementing index as a parameter to your function as first parameter. Which you can then use to modify the incoming uid approriattely

Related

How to expand this dynamic column with only 1 value

This Id column is dynamic type, but only holds 1 value (f3019...). I want to get rid of the array so it only has the field value.
When I try mv-expand Id it doesn't do anything
Also the current query is like:
Id = Target.Properties[0].value
When I try
Id = Target.Properties[0].value[0]
Id returns a blank
The dynamic types can hold arrays and dictionaries, but also scalar types.
The fact that Target.Properties[0].value does not behave like an array indicates that it is not an array, but a string.
The representation of it as an array in the GUI relates to the serving lair and not the way it is actually stored.
Use tostring(parse_json(tostring(Target.Properties[0].value))[0]).
Every element within a dynamic field is also of dynamic type.
When running on a dynamic element, parse_json() returns the element As Is.
If we want the element to get parsed, we first need to convert it to string, using tostring().
parse_json() which is used to parse the string, returns an array (which is a dynamic element).
The first (and only) element of the array is also of a dynamic type.
We use an additional tostring() to convert it to string.
Demo
print value = dynamic('["Hello"]')
| extend value[0] // Null because it's not really an array
| extend result = tostring(parse_json(tostring(value))[0])
value
value_0
result
["Hello"]
Hello
Fiddle
Misleading representation in Azure Monitor:

Unable to increment Counter using linq

Hello AllSo i am trying to edit and insert rows into a datatable using linq but i am unable to increment the counter using Linq
Basically what i am trying to do is first edit or the data using Select method and assigning it to variable in my query using let and then using normal Add method to add all the rows into the dt SO my first question is can i directly add all the rows without needing the counter ?
The reason i need counter is what i am trying to do is take one element per iteration using its index for example x(1).....x(n) so for the index i need counter so for counter i created a variable outside the flow and incremented it inside our linq query but it didnt work out.....
The code i have tried
(From roww In DT.AsEnumerable() Let x=DT.AsEnumerable().Select(Function(r) CStr(r("Column1")).Substring(0,3) ).ToArray Select
DT.Clone.Rows.Add(roww.Item("Some Column"),roww.Item("Column1"),x)).CopyToDataTable
(From roww In DT.AsEnumerable() Let x=DT.AsEnumerable().Select(Function(r) CStr(r("Column1")).Substring(0,3) ).ToArray Select
DT.Clone.Rows.Add(roww.Item("Some Column"),roww.Item("Column1"),x(y+1))).CopyToDataTable
Another thing what i was looking to use Expression.Increment Method but i havent used it dont know what exactly it is and whether it can be converted to int
ANy inputs ??
Hello all i did the above question using this query
(From roww In DT.AsEnumerable() Select
DT.Clone.Rows.Add(roww.Item("Some other data"),roww.Item("Column1"),CStr(roww.Item("Column1")).Substring(0,4))).CopyToDataTable
But i still want to know how can i increment counter if any one knows how to do it please let me know.......Even if its in C#'s format ill learn the logic or syntax
Thank you
While your answer is the correct solution to your query, if you had some other type of array not dependent on your original data source, you may need to index into the array in parallel with your data source. While a For..Next would be the best way to handle this, you can emulate the same effect with LINQ using Enumerable.Range and Zip.
The Let statement is translated into a Select that carries the Let variable value along with each row. The Enumerable.Range provides a source of increasing integers you can use to index into an array.
Dim addedDT = (DT.AsEnumerable() _
.Select(Function(roww) New With { Key .x = DT.AsEnumerable.Select(Function(r) CStr(r("Column1")).Substring(0,3)).ToArray, roww }) _
.Zip(Enumerable.Range(0, DT.Rows.Count), Function(xroww, y) DT.Clone.Rows.Add(xroww.roww.Item("Some Column"),xroww.roww.Item("Column1"),xroww.x(y+1))
).CopyToDataTable
Looking at the Let translation makes it more apparent that the calculation of x occurs once per data source row and is very inefficient. Pulling the constant value out makes for a better version:
Dim x = DT.AsEnumerable.Select(Function(r) CStr(r("Column1")).Substring(0,3)).ToArray
Dim addedDT = (DT.AsEnumerable() _
.Zip(Enumerable.Range(0, DT.Rows.Count), Function(roww, y) DT.Clone.Rows.Add(roww.Item("Some Column"),roww.Item("Column1"),x(y+1))
).CopyToDataTable

How to structure message and update functions with a variable number of UI elements in Elm?

Greetings StackOverflow!
Suppose I have an Elm app with a variable number of text input fields. I'd like to reflect the state of these input fields in the model.
Model and view are easy enough: View just has a Array String field in it somewhere.
The view is then computed simply by calling List.map (HTML input ...) on that list of strings.
However, I'm a bit lost how to do the update function and message type.
The message could be somethign like this:
type Msg = InputFieldUpdated Int String
Here, the Int refers to the position in the Array that the string to be updated has. However, if I do it this way, I can create messages that refer to non-existant array positions simply by setting the Int to something that is out of range.
For a fixed number of input elements, one can solve this problem very elegantly by simply using a union type with a different value for each input, but what about my situation? In the domain of "making impossible states impossible", is there some trick for that that I'm missing?
However, if I do it this way, I can create messages that refer to non-existant array positions simply by setting the Int to something that is out of range.
According to the documentation of Array.set, the array stays untouched if the index is out of range.
Please, have a look at this ellie-app example. It mostly models the problem, that you've described. Array of elements is rendered and the elements can be added dynamically:
view : Model -> Html Msg
view model =
div []
[ div [] (toList (Array.indexedMap viewElement model.elements))
, button [ onClick Add ] [ text "Add" ]
]
In order to update a particular element, you need to get it by index. Since the type of the result from Array.get is Maybe a, type system will force you to process all the cases (when the element exists and doesn't):
Increment index ->
case get index model.elements of
Just element ->
{ model | elements = set index (element + 1) model.elements }
Nothing ->
model
The example is just for demonstration purposes, if you don't need the current element, then Array.set function can be safely used.

Jmeter -- how to combine variable name & counter()

I've got the following problem:
I do gather that ProductId variable is assigned with the first value and then only that value is used when I refer to ${productId}.
I was trying to apply ${__counter()} to reference name in RegexExtractor,
but then BeanShellAssertion fails to set the property.
How should I properly work this around?
You can use __V() function in order to combine 2 variables.
I.e. if you have 2 variables like:
productId
counter
And would like to evaluate i.e. ${productId_1}, ${productId_2}, etc.
It should be as simple as:
${__V(productId${counter})}
Same approach applies to __counter() function:
${__V(productId_${__counter(,)})}
Demo:
See How to Use JMeter Functions posts series for comprehensive information on above and other functions
ProdGroup
please store the product Id property as prod_id_1, prod_id_2,...prod_id_n or according to you convenience
How to to that ?
in post processor "Parameters" section use ${__counter(FALSE,)} and in the script part try getting that String counter = arg[0] and convert that to integer and store it to a script variable by default arg[0] value is String
int c= arg[0] as Integer //this is groovystyle check in your way to convert as int
now props.put("prod_id_"+c,"extractedfrom_response")
BID:
In you Bid group add a user defined variable section add the variable
"prod_id" and value is empty
Define a counter start with the same counter 1 and give counter reference name [if your counter value in prod group was 0 then here also it must start with zero]
script sampler convert all the props to the variables
Enumeration e = props.propertyNames();
while (e.hasMoreElements()) {
String propertyName = e.nextElement().toString();
if (propertyName.startsWith("prod_id_")) {
vars.put(propertyName, props.getProperty(propertyName));
}
}
With this you have converted properties to variables named prod_id_1 ...to ... prod_id_n
In http sampler user reference as ${__V(prod_id_${counterreference in step 2})} will do your job
For each thread the counter will increment by default
user ${__threadNum} or ${counter reference name} in sampler labels for debugging.
Hope this helps. Please let us know if still problem is there.

How Do I step through an IList one field at a time?

I'm using the Dynamic LINQ library code sample to dynamically return some data. The number of columns I'm returning is variable depending on user input. I'm enumerating the IQueryable into an IList.
What I need to do is step through the Ilist one field at a time. I can get one row at a time by iterating through the IList's rows but I can't for the life of me pull one field out of the collection.
For example, here I'm returning two columns (hard coded for testing but in prod it will be variable depending on what fields the user chooses):
Dim Dynq = dc.dt _
.Where("RUN_ID = """ & runNumber & """ and Upper_Pressure > 95") _
.OrderBy("Upper_Pressure") _
.Select(" new (Run_ID,Process)")
Dim something = DirectCast(Activator.CreateInstance(GetType(List(Of )).MakeGenericType(Dynq.ElementType), Dynq), IList)
Now I can pull a field out of the Ilist if I know the column name with something like:
something.Run_ID.ToString
but I wont know the columns I'm using until runtime and dynamically inserting it in a variable that's set at runtime doesn't work.
So in Summary I have a Ilist that looks something like this
1 | Auto
2 | Auto
3 | Manual
4 | Manual
and I'd like a way to return
1
and then return
Auto
and then
2
etc...
I would greatly appreciate the help of those more learned than I in this.
Your question is a little confusing, but I think you want to flatten your return set to return specific columns as elements in order by column then row...
One method, you could use is the SelectMany operator.
For example (sorry in C# as my brain is turning one-tracked!):
// Find flattened list of some explicitly specified property values...
var flattened = something.SelectMany(e => new [] { e.Run_ID.ToString(), e.Process.ToString() });
Not sure if that's what your after, but it could be a step in the right direction.