Zip with default value instead of dropping values?

There is some structure to this problem, and here it comes. I’ll be using this stuff: import Control.Applicative import Data.Traversable import Data.List First up, lists-with-padding are a useful concept, so let’s have a type for them. data Padme m = (:-) {padded :: [m], padder :: m} deriving (Show, Eq) Next, I remember that the … Read more

How does using a function (callback) as an argument to another function work in Python?

Yes, this is possible. myfunc can call the passed-in function like: def myfunc(anotherfunc, extraArgs): anotherfunc(*extraArgs) Here is a fully worked example: >>> def x(a, b): … print(‘a:’, a, ‘b:’, b) … >>> def y(z, t): … z(*t) … >>> y(x, (‘hello’, ‘manuel’)) a: hello b: manuel

Lifting a higher order function in Haskell

This can’t be done generically over all MonadIO instances because of the IO type in a negative position. There are some libraries on hackage that do this for specific instances (monad-control, monad-peel), but there’s been some debate over whether they are semantically sound, especially with regards to how they handle exceptions and similar weird IOy … Read more

How to repeat a function n times

That should do it: def repeated(f, n): def rfun(p): return reduce(lambda x, _: f(x), xrange(n), p) return rfun def square(x): print “square(%d)” % x return x * x print repeated(square, 5)(3) output: square(3) square(9) square(81) square(6561) square(43046721) 1853020188851841 or lambda-less? def repeated(f, n): def rfun(p): acc = p for _ in xrange(n): acc = f(acc) … Read more

Higher-order functions in Javascript

Boolean is a function. It’s the function you’re calling indirectly through noisy. A bit confusing, I know, because it looks like the name of a type. But in JavaScript, those initially-capped things (Boolean, Number, String, and so on) are functions. When you call Boolean (without using new), it tries to convert the argument you gave … Read more