There are a lot of buzz about Haskell, Lisp, Erlang and other languages few people code in. But while they play their role as banners, functional programming sneaks into our code in multi-paradigm languages.
I was going to continue this way and later introduce my library of a variety of functional tricks, but suddenly realized it’s not about FP, it’s about utility. And that’s what I will focus on below trying to show you real-life value of funcy.
Funcy started from a try to bunch up a couple of data manipulation utils. Therefore most of my examples will be about that. Some of them may seem trivial to you, but there are many gains these tiny tools can offer you both in terms of code brevity and expressiveness. Or they’ll just save your time, still good.
I’ll go through the typical tasks any python programmer face during her day.
Everyday data manipulation
1. Flatten list of lists. That’s how you usually do it:
1 2 3 4 5 6 7 8 9
They all work and all have their flaws: require imports, additional calls or restrict what you can pass into. But the main flaw is that they are all patterns not obvious calls. There should be a simple function to do such a simple and common thing and there is one in funcy:
cat joins list of lists, tuples, iterators and generally any iterables into single list. And it comes with extra shortcut if you want to
cat results of
map call. For example, this
will result in flat list of all lines of all the texts. There are lazy versions of both functions:
2. Merge some dicts. There are several clumsy ways in python:
1 2 3 4 5
I always wondered why one can’t just add them up, but that’s what we have. Anyway, this is also easy in funcy:
1 2 3 4
The best part here is these are omnivorous. They work with anything: sets, dicts, ordered dicts, lists, tuples, iterators, even strings, carefully preserving collection type.
3. Capturing something with regular expression. A usual way:
1 2 3 4
Much more straightforward with funcy:
Still not impressed? Then look here:
1 2 3 4 5 6 7 8 9 10 11
About imports and practicality
As you can see I import everything directly from funcy, not using any sub-packages. The reason it’s designed this way is practicality. That would be too annoying to remember where each one tiny thing comes from. There are enough libraries to clutter your file heads anyway.
This also enables you to write:
And start enjoying all functional python niceness right away. Ok, now when you know where all the stuff is kept I won’t repeat imports in every new example.
A bit more functional things
We’ve seen a pair of examples of higher order functions earlier, particularly
partial. One thing to note is that
re_finder itself is a partial application of
re_find meant to be used with
map and friends. Naturally, there is a similar utility to be used with
1 2 3
We can create a bunch of predicates and filter with them:
1 2 3
But what if we want to apply several of them at once or use some predicate logic. That’s easy:
1 2 3
Or you can use convenience function complementary to
I hope everyone have their functional appetite satisfied so we can switch to something less abstract.
Aside from sequence utilities, funcy provides lots of ones to work with collections.
And the two fundamental are
select, a versions of
filter preserving collection type:
1 2 3 4 5 6 7 8 9 10 11 12 13
This pair of functions is backed up with a set of ones to work with dicts:
1 2 3 4 5
The last example in this section will include several new functions at once:
silent – catches all exceptions in passed function, returning
compact – removes falsy values from collection;
walk_values – maps dict values with given function. Anyhow, this line constructs a dict of integer params from typical stringy request dict you get:
Back to data manipulation
Finally, the interesting part. I included some examples here just because they seem cool. Although, I did this earlier to be honest. Anyway, let’s split and group:
1 2 3 4 5
Partition and chunk:
1 2 3 4 5 6 7 8 9 10 11 12
And a couple more, just for fun:
1 2 3 4 5 6 7 8 9 10
More than just a library
Maybe some of you recognized some functions from Clojure or Underscore.js (Shakespear example was shamelessly ripped of the docs of the latter, for example). That should not surprise you, in many respects I drew inspiration from these two sources. Nonetheless I followed python spirit and stayed practical as far as I could.
And one more thought. We used to call programming languages languages, still rarely think of keywords and functions as words. We define our own words by writing new functions, but they are usually too specific to make it into our everyday use. Funcy utilities are designed the other way around, to be broadly used as a layer over python and it’s standard library.
So, how about extending your vocabulary?