Categories

Back 2 Code

Higher-order Functions

Introduction Martin Odersky1 in his Functional Programming in Scala course illustrates higher-order functions2 with a simple example in Scala. Scala is a modern functional programming language where functions are first-class citizen so they can be used, like any other value, as a parameter and returned as a result. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Simple sum function using a recursion to perform an operation on integers between a and b def sum(f: Int => Int, a: Int, b: Int): Int = if (a > b) 0 else f(a) + sum(f, a + 1, b) // Using anonymous function -- addict to syntactic sugar -- to define // The sum of integers between a and b def sumInts(a: Int, b: Int) = sum(x => x, a, b) // The sum of the cubes of integers between, a and b def sumCubes(a: Int, b: Int) = sum(x => x * x * x, a, b) > sumInts(1, 10) Int = 55 > sumCubes(1,10) Int = 3025 I love the Scala lean syntax–and its syntactic sugars–, but Python and R–my old friends–are also, among other paradigms, functional languages.

Shared Stories in Practice

In my previous article on DevOps , I talked about an important tool in the process of enabling cooperative learning: Shared stories. But in practice what does it means? Here is a very simple proposal that can be applied almost anywhere and that do not require any tool–except a ticketing tool. During team meeting each member of the team take some time to talk about a mistake, a problem or a failure he has made or he has to solve.

Functional Sequences

This feature blew my mind. R permits to define functional sequences. You can use %>% to not only produce values but also to produce functions (or functional sequences)! It’s really all the same, except sometimes the function is applied instantly and produces a result, and sometimes it is not, in which case the function itself is returned.1 Here is an example. 1 2 3 4 5 6 7 suppressPackageStartupMessages(library(tidyverse)) suppressPackageStartupMessages(library(babynames)) # Defining the functional sequence prepare <- .

DevOps the new view

While reading Effective DevOps1 I found a very interesting point. In an environment where humans are blamed and punished for errors, a culture of fear can build up walls that prevent clear communication and transparency. It is absolutely true, organization tends to segregate the work between distinct very specialized teams hiding behind walls built with SLA (Service Level Agreement) and RAM (Responsibility assignment matrix) . These are useful tools and the management is more confortable with clear contracts between teams, but at the opposite they kill collaboration.

Working days

I’ve worked a lot with Excel spreadsheets on financial data. Among other things, I had to use working days. Excel provides a function (WORKDAY) to compute working days but holidays have to be provided manually and in France we have a lot of public holidays :-)—and they are not all anchored. The python Pandas package comes from the financial analysis domain and in consequence, it provides a lot of features to manipulate time series data.

Pandas apply and map

The way to apply a function to pandas data structures is not always obvious–several methods exist (apply, applymap, map) and their scope is different. First there is two main structures (fortunately I’m not talking about Panel here): Series: one-dimensional labeled array DataFrame: 2-dimensional labeled data structure The apply / map methods can work on different ways. Element-wise The function is called (mapped) for each individual element (value)–so it takes the element (each distinct value) as parameter.