functional-programming

Split a list or ordered dates into weeks using linq [closed]

拥有回忆 提交于 2019-12-23 06:58:12
问题 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 last year . Say I have a list of 5 dates [Mar 2,Mar 6, Mar 7, Mar 26] all in the year 2018. The week start on Saturday and end Sunday. I want the following result [Mar 2] [Mar 6, Mar 7] [Mar 26] How can I do it with LINQ? Or in a functional way. 回答1: You can use the following on DateTime

How to use a callback function in python?

限于喜欢 提交于 2019-12-23 06:55:56
问题 I wonder how to correctly use python 2.7 callback functions. I have some callback functions from Cherrypy auth examples in my code. (These callbacks return a function that can evaluate to True or False, depending on the logged in user being in a group or not.) I wonder if a callback is executed or not if I write a piece of code like this: Given the definition from the library is: def member_of(groupname): def check(): if groupname == 'admin': if cherrypy.request.login == 'joe': return True if

How to assign functor to function pointer?

心不动则不痛 提交于 2019-12-23 05:41:58
问题 Generally, can I assign a function object to a function pointer? I want to do something like this: #include <iostream> class Foo { int num; public: Foo(int num_par) : num(num_par) {} void operator()(int multiplier) { std::cout << multiplier * num << std::endl; } }; int main() { typedef void(*bar)(int); Foo f(42); bar b = f; // MSVC error // ^ C2440: 'initializing' : cannot convert from 'Foo' to 'bar' b(2); // wanted to print 84 } If that's impossible, I’d like an alternative specifically for

How to assign functor to function pointer?

微笑、不失礼 提交于 2019-12-23 05:41:06
问题 Generally, can I assign a function object to a function pointer? I want to do something like this: #include <iostream> class Foo { int num; public: Foo(int num_par) : num(num_par) {} void operator()(int multiplier) { std::cout << multiplier * num << std::endl; } }; int main() { typedef void(*bar)(int); Foo f(42); bar b = f; // MSVC error // ^ C2440: 'initializing' : cannot convert from 'Foo' to 'bar' b(2); // wanted to print 84 } If that's impossible, I’d like an alternative specifically for

Combining two arrays of same size and returns sum

雨燕双飞 提交于 2019-12-23 05:06:05
问题 In Javascript (or any other programming language with a functional-like syntax), if I have two arrays of the same size, say a = [1,2,3,4] and b=[5,6,7,8] , what is the most efficient way to have the following result: c=[6,8,10,12] . For now, I do: a.map(function(x,i){ return x+b[i] }) but ideally I would like a solution that doesn't involve the use of indexes. 回答1: ES5 array methods are great, but old for loops are faster. var sum = Array(a.length); for(var i=0; i<a.length; ++i) sum[i] = a[i]

Why does reduceRight return NaN in Javascript?

泪湿孤枕 提交于 2019-12-23 04:35:28
问题 I'm using Firefox 3.5.7 and within Firebug I'm trying to test the array.reduceRight function, it works for simple arrays but when I try something like that I get a NaN . Why? >>> var details = [{score : 1}, {score: 2}, {score: 3}]; >>> details [Object score=1, Object score=2, Object score=3] >>> details.reduceRight(function(x, y) {return x.score + y.score;}, 0) NaN I also tried map and at least I can see the .score component of each element: >>> details.map(function(x) {console.log (x.score);

Can you use a functor/functional programming to group a list in Java 7 (and count its elements per group)?

不打扰是莪最后的温柔 提交于 2019-12-23 04:24:38
问题 Can you group List<TypeEnum> types = new ArrayList(Arrays.asList(TypeEnum.A, TypeEnum.B, TypeEnum.A)); into a Map<TypeEnum, Integer> countPerType; , using functors (e.g. Google's Guava, Apache's Commons Functor) pre Java 8? I'm trying to get my head around functional programming, but am unsure if that sort of thing would actually be possible (as I'm not just mapping a collections value, but trying to aggregate)? In imperative style, I would have done something like this: public Map<TypeEnum,

Lazy parameter binding in Python

社会主义新天地 提交于 2019-12-23 04:21:57
问题 I tried to design a workflow using composite pattern , the sample codes looks like this: class CommandInterface(object): def __init__(self, name, template=None, tool=None, param : dict={},input=None, output=None ): self.name = name self.input = input self.output = output self.param = param self.template = template def before_invoke(self): pass # check before invoke def invoke(self): pass def after_invoke(self): pass # check after invoke class ShellCommand(CommandInterface): def render(self):

The correct way to build a Binary Search Tree in OCaml

﹥>﹥吖頭↗ 提交于 2019-12-23 03:59:06
问题 Ok, I have written a binary search tree in OCaml. type 'a bstree = |Node of 'a * 'a bstree * 'a bstree |Leaf let rec insert x = function |Leaf -> Node (x, Leaf, Leaf) |Node (y, left, right) as node -> if x < y then Node (y, insert x left, right) else if x > y then Node (y, left, insert x right) else node The above code was said to be good in The right way to use a data structure in OCaml However, I found a problem. This insert will only work when building a bst from a list in one go, such as

Overriding func in Swift [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-23 03:06:58
问题 This question already has answers here : Compiler error: Method with Objective-C selector conflicts with previous declaration with the same Objective-C selector (6 answers) Closed 4 years ago . I am learning Swift and I am following a tutorial from Paul Hegarty on how to build a calculator using Polish Inverse Notation. The code looks like this: @IBAction func operate(sender: UIButton) { let operation = sender.currentTitle! if userIsEnteringData{ enter() } switch operation { case "×":