functional-programming

How to call Python functions dynamically

白昼怎懂夜的黑 提交于 2019-12-17 06:29:08
问题 I have this code: fields = ['name','email'] def clean_name(): pass def clean_email(): pass How can I call clean_name() and clean_email() dynamically? For example: for field in fields: clean_{field}() I used the curly brackets because it's how I used to do it in PHP but obviously doesn't work. How to do this with Python? 回答1: If don't want to use globals, vars and don't want make a separate module and/or class to encapsulate functions you want to call dynamically, you can call them as the

Large-scale design in Haskell? [closed]

£可爱£侵袭症+ 提交于 2019-12-17 04:36:13
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. What is a good way to design/structure large functional programs, especially in Haskell? I've been

Best explanation for languages without null

喜欢而已 提交于 2019-12-17 04:09:40
问题 Every so often when programmers are complaining about null errors/exceptions someone asks what we do without null. I have some basic idea of the coolness of option types, but I don't have the knowledge or languages skill to best express it. What is a great explanation of the following written in a way approachable to the average programmer that we could point that person towards? The undesirability of having references/pointers be nullable by default How option types work including strategies

Best explanation for languages without null

有些话、适合烂在心里 提交于 2019-12-17 04:09:18
问题 Every so often when programmers are complaining about null errors/exceptions someone asks what we do without null. I have some basic idea of the coolness of option types, but I don't have the knowledge or languages skill to best express it. What is a great explanation of the following written in a way approachable to the average programmer that we could point that person towards? The undesirability of having references/pointers be nullable by default How option types work including strategies

What is the point of the class Option[T]?

拥有回忆 提交于 2019-12-17 02:58:29
问题 I am not able to understand the point of Option[T] class in Scala. I mean, I am not able to see any advanages of None over null . For example, consider the code: object Main{ class Person(name: String, var age: int){ def display = println(name+" "+age) } def getPerson1: Person = { // returns a Person instance or null } def getPerson2: Option[Person] = { // returns either Some[Person] or None } def main(argv: Array[String]): Unit = { val p = getPerson1 if (p!=null) p.display getPerson2 match{

Generating permutations lazily

試著忘記壹切 提交于 2019-12-17 02:54:53
问题 I'm looking for an algorithm to generate permutations of a set in such a way that I could make a lazy list of them in Clojure. i.e. I'd like to iterate over a list of permutations where each permutation is not calculated until I request it, and all of the permutations don't have to be stored in memory at once. Alternatively I'm looking for an algorithm where given a certain set, it will return the "next" permutation of that set, in such a way that repeatedly calling the function on its own

Is there a Haskell idiom for updating a nested data structure?

风流意气都作罢 提交于 2019-12-17 02:38:10
问题 Let's say I have the following data model, for keeping track of the stats of baseball players, teams, and coaches: data BBTeam = BBTeam { teamname :: String, manager :: Coach, players :: [BBPlayer] } deriving (Show) data Coach = Coach { coachname :: String, favcussword :: String, diet :: Diet } deriving (Show) data Diet = Diet { dietname :: String, steaks :: Integer, eggs :: Integer } deriving (Show) data BBPlayer = BBPlayer { playername :: String, hits :: Integer, era :: Double } deriving

Bind Vs Lambda?

走远了吗. 提交于 2019-12-17 02:34:35
问题 I have a question about which style is preferred: std::bind Vs lambda in C++0x. I know that they serve -somehow- different purposes but lets take an example of intersecting functionality. Using lambda : uniform_int<> distribution(1, 6); mt19937 engine; // lambda style auto dice = [&]() { return distribution(engine); }; Using bind : uniform_int<> distribution(1, 6); mt19937 engine; // bind style auto dice = bind(distribution, engine); Which one should we prefer? why? assuming more complex

Set operations (union, intersection) on Swift array?

拥有回忆 提交于 2019-12-17 02:12:17
问题 Are there any standard library calls I can use to either perform set operations on two arrays, or implement such logic myself (ideally as functionally and also efficiently as possible)? 回答1: Yes, Swift has the Set class. let array1 = ["a", "b", "c"] let array2 = ["a", "b", "d"] let set1:Set<String> = Set(array1) let set2:Set<String> = Set(array2) Swift 3.0+ can do operations on sets as: firstSet.union(secondSet)// Union of two sets firstSet.intersection(secondSet)// Intersection of two sets

Set operations (union, intersection) on Swift array?

瘦欲@ 提交于 2019-12-17 02:12:13
问题 Are there any standard library calls I can use to either perform set operations on two arrays, or implement such logic myself (ideally as functionally and also efficiently as possible)? 回答1: Yes, Swift has the Set class. let array1 = ["a", "b", "c"] let array2 = ["a", "b", "d"] let set1:Set<String> = Set(array1) let set2:Set<String> = Set(array2) Swift 3.0+ can do operations on sets as: firstSet.union(secondSet)// Union of two sets firstSet.intersection(secondSet)// Intersection of two sets