Computer science map and exclude - functional-programming

I have an array with some values:
[1, 2, 3, 4]
I'd like to make a new array that contains mapped version of the items in the array above, but only add them to the new array if they pass a truth test.
A combination of map and filter?
[1, 2, 3, 4].mapFilter(function(n) { if (n > 2) return n * 3 })
What is this called?

This is map composed with filter. It doesn't need a name.
map (*3) . filter (>2)

Related

Julia language: Inserting global list to a function, change the global list

I have an information on a global list variables. I want to use this information and make some manipulation with it. But I don’t want to change this global list. Right now any manipulation I make change this global list. I have already a long code code with this global list.
The following example represents my bug difficulty:
function Dont_Change_Global_List(L)
Manipulating_List = L
deleteat!(Manipulating_List,2)
return Manipulating_List
end
global s = [1,2,3,4,5]
println("function result: ",Dont_Change_Global_List(s))
println(“global list”, s)
I get:
function result: [1, 3, 4, 5]
global list[1, 3, 4, 5]
I need the following result:
function result: [1, 3, 4, 5]
global list[1, 2, 3, 4, 5]
Is there anything I can do without changing the global list s but still make a manipulations?
You can copy the list internally:
function dont_change_list(L)
Manipulating_List = copy(L)
deleteat!(Manipulating_List,2)
return Manipulating_List
end

Shallow copy for one dimensional list

While reading shallow copy. It says that copy.copy(x) create shallow copy. But I don't see shallow copy behavior in case of the single dimensional list.
Example:
>> new = [1,2,3,4,5,6]
>> original = copy.copy(new)
>> new
[1, 2, 3, 4, 5, 6]
>> original
[1, 2, 3, 4, 5, 6]
>> id(new)
65022912
>> id(original)
65022512
>> new[2]=13
>> new
[1, 2, 13, 4, 5, 6]
>> original
[1, 2, 3, 4, 5, 6]
So here I assume updating "new" list should update "original" list but it is not happening.
In the case of the multidimensional list, the shallow copy is working properly.
Example:
>> parent_list = [1, 2, [3,4], [5,6]]
>> child_list = copy.copy(parent_list)
>> parent_list[2][1] = "Python"
>> parent_list
[1, 2, [3, 'Python'], [5, 6]]
>> child_list
[1, 2, [3, 'Python'], [5, 6]]
Please guide me, why the shallow copy is not working in case of a one-dimensional list.
Thanks.
There are actually 3 "Levels" to what you want.
1) Create a new reference to the same list. This aliasing is a trivial operation and would look like "original = new" or something like that. original[1] =x will update new[1]; This is the equivalent of copying a pointer in a pointer based language.
original = new
original[1] = x
new[1] will now be updated to x
This should be obvious but for completeness, If you follow the above with:
new = other
original is NOT affected at all.
2) Create a "Copy" of the list. It will allocate a new area and copy elements of the list. This is a "Shallow" copy. The children copied will be references, but the list itself will contain copies. original[1] =x will NOT update new[1], but original[1].childValue =x will update new[1].childValue
original = shallow copy of new
original[1].value = x
new[1].value WILL change to x
original[1] = y
new[1] will NOT be affected
3) Create a deep copy of the list. This will allocate a new area and shallow copy the list, but then will recurse and copy each child referenced in the list. No updates to original will modify new, or vice-a-versa.
original = deep copy of new
original[1].value = x
new[1].value will NOT be affected
original[1] = y
new[1] will NOT be affected
A shallow copy of a list is not usually what you want because your list is left in a hybrid state with some members referencing other lists and some not which will lead to unpredictable behavior, but it may be necessary if your tree is fairly deep and/or you never modify the child nodes.
original[1].value = x is giving error :AttributeError: 'int' object has no attribute 'values'
After writing the below code:
new = [1,2,3,4,5,6]
original = new.copy()
original[1] = 10
print("new:",new , "\n " ,"original:", original)
the output I am getting is :
new: [1, 2, 3, 4, 5, 6]
original: [1, 10, 3, 4, 5, 6]
changes done in the original are not getting reflected for a new list where an index is the same or a shallow copy

Indexing an array with a tuple

Suppose I have a tuple of (1, 2, 3) and want to index a multidimensional array with it such as:
index = (1, 2, 3)
table[index] = 42 # behaves like table[1][2][3]
index has an unknown number of dimensions, so I can't do:
table[index[0]][index[1]][index[2]]
I know I could do something like this:
functools.reduce(lambda x, y: x[y], index, table)
but it's utterly ugly (and maybe also inefficient), so I wonder if there's a better, more Pythonic choice.
EDIT: Maybe a simple loop is best choice:
elem = table
for i in index:
elem = elem[i]
EDIT2: Actually, there's a problem with both solutions: I can't assign a value to the indexed array :-(, back to ugly:
elem = table
for i in index[:-1]:
elem = elem[i]
elem[index[-1]] = 42
The question is very interesting and also your suggested solution looks good (havn't checked it, but this kind of problem requires a recursive treatment and you just did it in one line).
However, the pythonic way I use in my programs is to use dictionaries of tuples. The syntax is array-like, the performance - of a dictionary, and there was no problem in it for me.
For example:
a = {(1, 2, 3): 'A', (3, 4, 5): 'B', (5, 6, 7, 8): 'C'}
print a[1, 2, 3]
print a[5, 6, 7, 8]
Will output:
A
B
And assigning to an index is super easy:
a[1, 4, 5] = 42. (But you might want to first check that (1, 4, 5) is within the dict, or else it will be created by the assignment)

dynamic values in kwargs

I have a layer which helps me populating records from the form to tables and viceversa, it does some input checking, etc.
Now several methods of this layer which are called several times in different parts of the webform take the same parameters, so I wanted to pack them at the begining of the codefile.
kwargs(): return
{"tabla":"nombre_tabla","id":[hf_id.Value]
,"container": Panel1,"MsgBox1":
MsgBox1}
then I call
IA.search(**kwargs)
but doing that way the values of the dictionary get fixed with the ones they had in the begining, and one of them is retrieved from a webcontrol so it needs to be dynamic. So I wrapped them in a function
def kwargs(): return
{"tabla":"nombre_tabla",
"id":[hf_id.Value] ,"container":
Panel1,"MsgBox1": MsgBox1}
and then I call
IA.search(*kwargs())
IA.save(*kwargs())
etc.
and that way the value of the dictionary which comes from the webform (hf_id) is dynamic and not fixed. But I was wondering if in this case there is another way, a pythonic way, to get the values of the dictionary kwargs to be dynamic and not fixed
Python objects are pointers (though they are not directly manipulatable by the user.)
So if you create a list like this:
>>> a = [1, 2, 3]
and then store it in a dictionary:
>>> b = { 'key': a, 'anotherkey': 'spam' }
you will find modifications to the value in the dictionary also modify the original list:
>>> b['key'].append(4)
>>> print b['key']
[1, 2, 3, 4]
>>> print a
[1, 2, 3, 4]
If you want a copy of an item, so that modifications will not change the original item, then use the copy module.
>>> from copy import copy
>>> a = [1, 2, 3]
>>> b['key'] = copy(a)
>>> print b['key']
[1, 2, 3]
>>> b['key'].append(4)
>>> print b['key']
[1, 2, 3, 4]
>>> print a
[1, 2, 3]

JavaFX: concatenating sequences

Is there a standard library function or built-in construct to concatenate two sequences in JavaFX?
Here a Sequences.concatenate() function is mentioned, but it is nowhere to be seen in the official API.
Of course one could iterate over each sequence, inserting the values into a new sequence e.g:
function concatenate(seqA: Object[], seqB: Object[]) : Object[] {
for(b in seqB) insert b into seqA;
seqA;
}
..but surely something as basic as concatenation is already defined for us somewhere..
It is very simple, since there cannot be sequence in sequence (it all gets flattened), you can do it like this:
var a = [1, 2];
var b = [3, 4];
// just insert one into another
insert b into a;
// a == [1, 2, 3, 4];
// or create a new seq
a = [b, a];
// a == [3, 4, 1, 2];
Hope that helps.

Resources