higher-order-functions
Creating a new function as return in python function?
You should be able to do this by defining a new function inline: def fourier_series(f, N): def F(x): … return F You are not limited to the arguments you pass in to fourier_series: def f(a): def F(b): return b + 5 return F >>> fun = f(10) >>> fun(3) 8
What does “.()” mean in Kotlin?
A function that takes in nothing and returns nothing in Kotlin looks like: var function : () -> Unit The difference is that the function in your code takes in nothing, returns nothing, but is invoked on an object. For example, class Builder (val multiplier: Int) { fun invokeStuff(action: (Builder.() -> Unit)) { this.action() } … 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
Select/map each item of a Powershell array to a new array
An array in Powershell is declared with @() syntax. % is shorthand for foreach-object. Let’s declare an array with all the file names and loop through it with foreach. join-path combines a path and a child path into a single path. $files = @(“file1.txt”, “file2.txt”) $pFiles = $files | % {join-path “c:\temp” $_ } $pFiles … Read more
Difference between using a HOC vs. Component Wrapping
Higher-Order Components (HOC) and Container Components are different. They have different use cases and solve similar, but different problems. HOC are like mixins. They are used to compose functionality that the decorated component is aware of. This is opposed to Container Components that wrap children and allow the children to be dumb (or not aware … Read more
What is `lambda` in Python code? How does it work with `key` arguments to `sorted`, `sum` etc.?
A lambda is an anonymous function: >>> f = lambda: ‘foo’ >>> print f() foo It is often used in functions such as sorted() that take a callable as a parameter (often the key keyword parameter). You could provide an existing function instead of a lambda there too, as long as it is a callable … Read more
Transposing multidimensional arrays in PHP
function transpose($array) { array_unshift($array, null); return call_user_func_array(‘array_map’, $array); } Or if you’re using PHP 5.6 or later: function transpose($array) { return array_map(null, …$array); }
Does PHP have an equivalent to Python’s list comprehension syntax?
Maybe something like this? $out=array_map(function($x) {return $x*$x;}, range(0, 9)) This will work in PHP 5.3+, in an older version you’d have to define the callback for array_map separately function sq($x) {return $x*$x;} $out=array_map(‘sq’, range(0, 9));
What are paramorphisms?
Yes, that’s para. Compare with catamorphism, or foldr: para :: (a -> [a] -> b -> b) -> b -> [a] -> b foldr :: (a -> b -> b) -> b -> [a] -> b para c n (x : xs) = c x xs (para c n xs) foldr c n (x : … Read more