functional-programming

Is there a way to check whether function output is assigned to a variable in Python?

て烟熏妆下的殇ゞ 提交于 2019-12-31 04:20:13
问题 In Python, I'd like to write a function that would pretty-print its results to the console if called by itself (mostly for use interactively or for debugging). For the purpose of this question, let's say it checks the status of something. If I call just check_status() I would like to see something like: Pretty printer status check 0.02v NOTE: This is so totally not written for giant robots ================================= System operational: ... ok Time to ion canon charge is 9m 21s Booster

Find path to object in object nested array

冷暖自知 提交于 2019-12-31 03:59:05
问题 I have an object, of which parameters contain and array of object. I receive 1 object id and I need to find its position in that whole mess. With procedural programming I got it working with: const opportunitiesById = { 1: [ { id: 1, name: 'offer 1' }, { id: 2, name: 'offer 1' } ], 2: [ { id: 3, name: 'offer 1' }, { id: 4, name: 'offer 1' } ], 3: [ { id: 5, name: 'offer 1' }, { id: 6, name: 'offer 1' } ] }; const findObjectIdByOfferId = (offerId) => { let opportunityId; let offerPosition;

resolving map function issue in python 3 vs python 2

徘徊边缘 提交于 2019-12-31 03:40:47
问题 I'm interested in functional programming with python and am working through Mary Rose Cook's blog post A practical introduction to functional programming . Apparently, it was written in python 2 as this: name_lengths = map(len, ["Mary", "Isla", "Sam"]) print name_lengths # => [4, 4, 3] in Python 3 yields this: <map object at 0x100b87a20> I have two questions: Why is this is so? Other than converting the map object to a list and then use numpy, are there any other solutions? 回答1: As documented

Map a function by key path in nested dict including slices, wildcards and ragged hierarchies

浪子不回头ぞ 提交于 2019-12-31 02:08:11
问题 This question is an extension based on here and here. What is a good approach to mapping a function to a specified key path in nested dicts, including these path specification: List of keys at a given path position Key slices (assuming sorting) Wildcards (ie all keys at a path position) Handling ragged hierarchies by ignoring keys that don't appear at a given level If it is makes is simpler, can assume that only dicts are nested, no lists of dicts, since the former can be obtained with dict

Functionally processing a database cursor in Scala

拜拜、爱过 提交于 2019-12-31 01:45:16
问题 When I need to read millions of database rows from a PostgreSQL database using the JDBC driver, I always use a cursor, otherwise I will get an OutOfMemoryError. Here is the pattern (pseudocode) that I use: begin transaction execute("declare cursor...") while (true) { boolean processedSomeRows = false resultSet = executeQuery("fetch forward...") while (resultSet.next()) { processedSomeRows = true ... } if (!processedSomeRows) break } close cursor commit This is a more "functional" equivalent

Greaters function define

情到浓时终转凉″ 提交于 2019-12-30 23:21:48
问题 I would like to define a greaters function, which selects from a list items that are larger than the one before it. For instance: greaters [1,3,2,4,3,4,5] == [3,4,4,5] greaters [5,10,6,11,7,12] == [10,11,12] The definition I came up with is this : greaters :: Ord a => [a] -> [a] Things I tried so far: greaters (x:xs) = group [ d | d <- xs, x < xs ] Any tips? 回答1: I would start from here: greaters :: Ord a => [a] -> [a] greaters [] = [] greaters (x:xs) = greatersImpl x xs where greatersImpl

how to pipe functions, when a promise in the promise in the middle checks authorization?

我的未来我决定 提交于 2019-12-30 11:26:08
问题 i'm trying to compose some functions together: compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args))); checkAuthorization returns a promise that check if a user is authorized. buildParams receives someRequestData , and pipes the result to searchItem . checkAuthorization() .then(() => { compose( searchItem, buildParams )(someRequestData) }, (e) => { handleError(e) }) I think it's OK, but I wish to have a more elegant look for readability, something like: compose( searchItem,

Haskell int list to String

天涯浪子 提交于 2019-12-30 10:26:41
问题 I would like to know if there is a simple way to turn [5,2,10] into "52a" . Where its not just to this case, I want to associate any number >9 with the corresponding letter. Thanks in advance. 回答1: You want to do something to each element of a list in order to get a new list. In other words, you want to apply a function (that you will have to define yourself) to each element. This is what the map function from the Prelude is for. To convert between integers and individual characters, you

flatMap or bind in Python 3?

廉价感情. 提交于 2019-12-30 09:21:54
问题 Python provides list comprehensions that provide map/filter type functionality. Can I do a flatMap aka bind operation with this? I've seen solutions with itertools or other add-on libraries. Can I do this with core Python? # this [[x,10*x] for x in [1,2,3]] # will result in unflattened [[1, 10], [2, 20], [3, 30]] 回答1: [y for x in [1, 2, 3] for y in [x, 10*x]] Just add another for to the list comprehension. 来源: https://stackoverflow.com/questions/21418764/flatmap-or-bind-in-python-3

How to implement Future as Applicative in Scala?

只愿长相守 提交于 2019-12-30 09:05:31
问题 Suppose I need to run two concurrent computations, wait for both of them, and then combine their results. More specifically, I need to run f1: X1 => Y1 and f2: X2 => Y2 concurrently and then call f: (Y1, Y2) => Y to finally get a value of Y . I can create future computations fut1: X1 => Future[Y1] and fut2: X2 => Future[Y2] and then compose them to get fut: (X1, X2) => Future[Y] using monadic composition. The problem is that monadic composition implies sequential wait . In our case it implies