Why can I not pass a function call (rather than a function reference or an anonymous function) to setTimeout()?

This is because of the order of execution. If you pass a function call to setTimeout, the function will be executed immediately, i.e. the function is put on javascript’s execution stack immediately. If you pass a function name, i.e. a reference to a function, the function is only put in the javascript thread’s execution stack … Read more

Object oriented programming in Haskell

You just don’t want to do that, don’t even start. OO sure has its merits, but “classic examples” like your C++ one are almost always contrived structures designed to hammer the paradigm into undergraduate students’ brains so they won’t start complaining about how stupid the languages are they’re supposed to use†. The idea seems basically … Read more

Any Functional Programming method of traversing a nested dictionary?

Use reduce(): reduce(dict.__getitem__, l, d) or better still, using operator.getitem(): from operator import getitem reduce(getitem, l, d) Demo: >>> d = {“a”: {“b”: {“c”: 4}}} >>> l = [“a”, “b”, “c”] >>> from operator import getitem >>> reduce(getitem, l, d) 4 Python 3 moved the reduce() function out of the built-ins and into functools.reduce().

Convert Swift Array to Dictionary with indexes [duplicate]

try like this: reduce(enumerate(a), [String:UIView]()) { (var dict, enumeration) in dict[“\(enumeration.index)”] = enumeration.element return dict } Xcode 8 • Swift 2.3 extension Array where Element: AnyObject { var indexedDictionary: [String:Element] { var result: [String:Element] = [:] for (index, element) in enumerate() { result[String(index)] = element } return result } } Xcode 8 • Swift 3.0 … Read more

Filtering a list of JavaBeans with Google Guava

Do it the old-fashioned way, without Guava. (Speaking as a Guava developer.) List<Person> filtered = Lists.newArrayList(); for(Person p : allPersons) { if(acceptedNames.contains(p.getName())) { filtered.add(p); } } You can do this with Guava, but Java isn’t Python, and trying to make it into Python is just going to perpetuate awkward and unreadable code. Guava’s functional utilities … Read more

Is it possible to decorate include(…) in django urls with login_required?

It is doable, and in fact I just found two snippets for this. Solution #1 The first snippet by cotton substitutes RegexURLPattern and RegexURLResolver with custom implementations that inject given decorator during resolve call. from django.core.urlresolvers import RegexURLPattern, RegexURLResolver from django.conf.urls.defaults import patterns, url, include from django.contrib import admin from myproject.myapp.decorators import superuser_required class DecoratedURLPattern(RegexURLPattern): … Read more