Loop through the elements of a set in elm - elm

Can I loop through elements of a Set in Elm? Or at least convert the Set to a List?
There exists Set.fromList, but I don't find any List.fromSet or something similar.
I know there is no for loop in Elm, but I want to go through the elements recursively like I do in case of list:
interate lst =
case lst of
[] -> False
x::xs -> ...
but for a Set.

I would use Set.toList for this.

Related

Using Numpy.where() with a function on each element

I have a rather complicated function, say:
def func(elem):
// blah blah blah
return True
// blah blah blah
return False
I wish to use the numpy.where() function along the lines of
arr2 = np.where(func(arr1), arr1, 0)
But when I try this syntax and debug, I see that in func, that the entire array is passed rather than individual elements. The typical use cases I see in the documentation/examples only rely on simple comparators like arr < 5, but I need something quite a bit fancier that I don't want to try and write in a single line.
If this is possible, or if there is some vectorized substitute (emphasis on efficiency), any insights appreciated.
I figured out how to do it by using np.vectorize, followed by a list comprehension, not np.where. Maybe from this, one can find out a way to use numpy rather than of a list comprehension.
func_vec = np.vectorize(func)
[arr1 if cond else 0 for cond in func_vec(arr1)]
Anyways, by using func_vec(arr1) you get the True/False values per element.
Note: If you want a new array like arr1, replacing by 0 the elements that return False in your function, then this should work:
arr2 = np.where(func_vec(arr1), arr1, 0)
Edit:
Indeed, np.vectorize is not optimized to performance (by bad), is essentially a for loop under the hood. So I would recommend trying to write your function in a vectorized way rather than trying to vectorize it thereafter.
For example, try converting a function like this:
def func(elem):
if elem > 5:
return True
else:
return False
to something like this:
def func(elem):
return elem > 5
so that you can easily apply func(arr1) without error.
If you really have a function that returns just True or False, I'm pretty sure you can do it, regardless of its complexity. Anyways we're here to help you out!
It seems like you try to get the elements of arr1 you want using func function, but judging from the definition func works for a single element. You need a True/False array of the same shape as arr1 to do so.
If I get it right, a potential solution would be to modify func to operate on the whole array and not only on one element and return the True/False array of shape arr1.shape you need for np.where, since you want to do it in a single line that way.

Incrementing uniqueID over each List.map iteration

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

Use findAll and then Sort in vb.NET listOf

I'm trying to get specific elements of a listOf structure with the function findAll plus a lambda function, and then sort that result and keep that sorting of those elements saved in the list. The structure of the listOf has an id and an age, so what I want is get all the items with id=0 and then sort the ages of that result, keeping that modification in the variable.
This is what I've tried, but it doesn't work
list.FindAll(Function(p1) p1.id = 0).Sort(Function(p1, p2) p1.age > p2.age)
Your line of code doesn't work, because FindAll returns a new list with the items found, and you don't assign that new list to the list variable. Also, you can't chain the call to Sort because this function changes the underlying list and doesn't return anything. To use FindAll and Sort together you will need two statements:
list = list.FindAll(Function(p1) p1.id = 0)
list.Sort(Function(p1, p2) p1.age.CompareTo(p2.age))
It would be easier to use Linq to do this, as you can always chain expressions with Linq, and the sorting is easier to do. But you need to assign the result to the list again:
list = list.Where(Function(p1) p1.id = 0) _
.OrderBy(Function(p1) p1.Age).ToList()

Search arraylist of objects

I have an arraylist of objects, I would like to know the index within the arraylist of the object that contains a certain value, is there a built-in way to do the search?
I know I could simply iterate through the arraylist to find the correct value e.g. :
ReportToFind="6"
For i = 0 To ReportObjList.Count - 1
If ReportObjList.Item(i).ReportCode = ReportToFind Then
ReportName.Text = ReportObjList.Item(i).ReportName ' found it - show name
Exit For
End If
Next
Is the only other solution be to replace this code a binary search?
.Net 1.1
You need to use better data structures in the case that searching through a list is a problem. You can use a binary search for your arraylist in the case that your list is sorted with respect to the value to be searched. In other cases you would be better of using smarter data structures such as a binary tree or a map.
I don't know if .Net 1.1 has it, but you could try the .IndexOf method on your array list.
It looks like you need to index your reportObjectList by reportCode in addition to the item index.
You can do this either in a second parallel list with the reportCode as the index and the itemIndex as the value.

How do I get the index of an object in a For Each...Next loop?

I'm using the following syntax to loop through a list collection:
For Each PropertyActor As JCPropertyActor In MyProperty.PropertyActors
i = IndexOf(PropertyActor)
Next
How do I get the index of the current object within the loop? I'm using IndexOf(PropertyActor) but this seems inefficient as it searches the collection when I already have the object available!
An index doesn't have any meaning to an IEnumerable, which is what the foreach construct uses. That's important because foreach may not enumerate in index order, if your particular collection type implements IEnumerable in an odd way. If you have an object that can be accessed by index and you care about the index during an iteration, then you're better off just using a traditional for loop:
for (int i=0;i<MyProperty.PropertyActors.Length;i++)
{
//...
}
AFAIK since this pulls the object out of the collection, you would have to go back to the collection to find it.
If you need the index, rather than using a for each loop, I would just use a for loop that went through the indices so you know what you have.
It might be easiest to just keep a separate counter:
i = 0
For Each PropertyActor As JCPropertyActor In MyProperty.PropertyActors
...
i = i + 1
Next
As an aside, Python has a convenient way of doing this:
for i, x in enumerate(a):
print "object at index ", i, " is ", x
just initialize an integer variable before entering the loop and iterate it...
Dim i as Integer
For Each PropertyActor As JCPropertyActor In MyProperty.PropertyActors
i++
Next
Add an index variable that you increase yourself for each iteration?
You could use the "FindIndex" method.
MyProperty.PropertyActors.FindIndex(Function(propActor As JCPropertyActor) propActor = JCPropertyActor)
But inside of a for each loop that seems like alot of extra overhead, and seems like the same resulting problem as the "IndexOf" method. I suggest using old fashioned index iteration. This way you have your index and your item.
Dim PropertyActor As JCPropertyActor
For i As Integer = 0 To MyProperty.PropertyActors.Count - 1
PropertyActor = MyProperty.PropertyActors.Item(i)
Next