functional-programming

map vs list; why different behaviour?

map vs list; why different behaviour? Question: In the course of implementing the "Variable Elimination" algorithm for a Bayes’ Nets program, I encountered an unexpected bug that was the result of an iterative map transformation of a sequence of objects. For simplicity’s sake, I’ll use an analogous piece of code here: >>> nums = [1, …

Total answers: 3

Tensorflow "map operation" for tensor?

Tensorflow "map operation" for tensor? Question: I am adapting the cifar10 convolution example to my problem. I’d like to change the data input from a design that reads images one-at-a-time from a file to a design that operates on an already-in-memory set of images. The original inputs() function looks like this: read_input = cifar10_input.read_cifar10(filename_queue) reshaped_image …

Total answers: 3

Functional pipes in python like %>% from R's magrittr

Functional pipes in python like %>% from R's magrittr Question: In R (thanks to magrittr) you can now perform operations with a more functional piping syntax via %>%. This means that instead of coding this: > as.Date("2014-01-01") > as.character((sqrt(12)^2) You could also do this: > "2014-01-01" %>% as.Date > 12 %>% sqrt %>% .^2 %>% …

Total answers: 16

Finding a sum in nested list using a lambda function

Finding a sum in nested list using a lambda function Question: I have a data structure similar to this table = [ (“marley”, “5”), (“bob”, “99”), (“another name”, “3”) ] What I would like to do, to get the sum of the 2nd column (5 + 99 + 3) functionally like this: total = sum(table, …

Total answers: 6

Is there a Python equivalent for Scala's Option or Either?

Is there a Python equivalent for Scala's Option or Either? Question: I really enjoy using the Option and Either monads in Scala. Are there any equivalent for these things in Python? If there aren’t, then what is the pythonic way of handling errors or “absence of value” without throwing exceptions? Asked By: Melvic Ybanez || …

Total answers: 11

Python: Difference between filter(function, sequence) and map(function, sequence)

Python: Difference between filter(function, sequence) and map(function, sequence) Question: I’m reading through the Python documentation to really get in depth with the Python language and came across the filter and map functions. I have used filter before, but never map, although I have seen both in various Python questions here on SO. After reading about …

Total answers: 5

Does PHP have an equivalent to Python's all()

Does PHP have an equivalent to Python's all() Question: Python has a nice all() (doc) method that returns true if all elements in an iterable are true, which is equivalent to: def all(iterable): for element in iterable: if not element: return False return True Is there a similarly nice way to do this in PHP? …

Total answers: 3

Composing functions in python

Composing functions in python Question: I have an array of functions and I’m trying to produce one function which consists of the composition of the elements in my array. My approach is: def compose(list): if len(list) == 1: return lambda x:list[0](x) list.reverse() final=lambda x:x for f in list: final=lambda x:f(final(x)) return final This method doesn’t …

Total answers: 16

How does this lambda/yield/generator comprehension work?

How does this lambda/yield/generator comprehension work? Question: I was looking through my codebase today and found this: def optionsToArgs(options, separator=’=’): kvs = [ ( “%(option)s%(separator)s%(value)s” % {‘option’ : str(k), ‘separator’ : separator, ‘value’ : str(v)} ) for k, v in options.items() ] return list( reversed( list( (lambda l, t: (lambda f: (f((yield x)) for x …

Total answers: 2

How does functools partial do what it does?

How does functools partial do what it does? Question: I am not able to get my head on how the partial works in functools. I have the following code from here: >>> sum = lambda x, y : x + y >>> sum(1, 2) 3 >>> incr = lambda y : sum(1, y) >>> incr(2) …

Total answers: 8