functional-programming

Expression that returns mutated list

Expression that returns mutated list Question: I’m looking for a single expression, mutates an element and returns the modified list The following is a bit verbose # key=0; value=3; rng=[1,2] [(v if i != key else value) for i, v in enumerate(rng)] Edit: I’m looking for a way to inline the following function in a …

Total answers: 3

Separating elements from a list in Python depending on a condition

Separating elements from a list in Python depending on a condition Question: I have a list of elements and want to separate the elements of the list by a certain condition. A simple example is a list of numbers and i want to separate the odd from the even ones. For that could use the …

Total answers: 4

How can a function takes another function WITH arguments, as a parameter?

How can a function takes another function WITH arguments, as a parameter? Question: def Wrapper(func): def MyPrint(message): print(‘Hello ‘ + message) func(MyPrint) Wrapper(lambda my_print: my_print(‘world’)) I’m confused of the above code. I get that a function can take another function as parameter and use func() to invoke it, but how can Wrapper(lambda my_print: my_print(‘world’)) call …

Total answers: 2

function returning None (functional programming)

function returning None (functional programming) Question: I’m trying to make an functional function and I want it to return an array (ndarray). I don’t know why, but my code is returning None. Here’s my code: def upgrade_array(array:np.ndarray, max_value:int, value_int=1): a = array.copy() index = value-1 a[index,:] = value #display(a) if value==max_value: return np.array(a) else: upgrade_array(array=a, …

Total answers: 2

Changing nested element in matrix only using constants python

Changing nested element in matrix only using constants python Question: Hi I was working with a matrix in python call it a : a = [ [0,0,0], [0,0,0], [0,0,0] ] I would like to change the element on the second row in the first column (a[1][0]) to 1 yielding the following result : a = …

Total answers: 3

How to use MLFlow in a functional style / functional programming?

How to use MLFlow in a functional style / functional programming? Question: Is there a reliable way to use MLFlow in a functional style? As it is not possible to pass the run ID for example to the function which logs a parameter, I wonder whether it is possible to seperate code executed in my …

Total answers: 2

Making dictionary more functional

Making dictionary more functional Question: how can i change the code, to make it more functional (change the for loop for the same result only to make it shorter in 2 sentences)? def get_language_from_text(): """ Simply return an array with the predicted languages. """ resultSet = [] inputdata = request.data #print(inputdata) inputdataparsed = json.loads(request.data) array_of_sentences …

Total answers: 1

pandas assign across multiple columns functionally

pandas assign across multiple columns functionally Question: Is there a way, in pandas, to apply a function to some chosen columns, while strictly keeping a functional pipeline (no border effects,no assignation before the result, the result of the function only depends of its arguments, and I don’t want to drop the other columns). Ie, what …

Total answers: 1