Package: alakazam
Functional programming sugar for Python.InstallingA simplepipcommand should do the trick. (In some cases, you may
need to usesudo)pip install alakazamThe Alakazam library does not, as of now, have any additional
dependencies and is designed for Python 2 and 3.UsingTo use the stream functionality of Alakazam, import thealakazampackage. It is recommended that you alias the package as something
likezzfor easier typing.To use the Alakazam lambda syntax, import the placeholders fromalakazamexplicitly.import alakazam as zz
from alakazam import _1, _2, _3, _4, _5This library aims to make functional-style, and specifically
stream-oriented programming in Python prettier, more pleasant, and
easier on the eyes. Python has been capable of many functional
programming tasks, but it has always been a little bit awkward to use
those features for anything nontrivial. For instance, suppose we had
some listarr, and we wanted to square every element of the list
and then keep only the even squares. This is how we might approach
this problem using Python’s built-in functional tools.list(filter(lambda x: x % 2 == 0, map(lambda x: x ** 2, arr)))There’s a lot of cruft here, with having to explicitly declare that
we’re using lambdas every time we need to make a
function. Additionally, we have to read the code almost backward to
understand what it’s doing. While this backward sequencing of
operations is fine in a language like Haskell (where pointfree
notation hides the messy bracketing), in Python it would make much
more sense if we could read our code in the normal order. This is
where Alakazam comes in.zz.of(arr).map(_1 ** 2).filter(_1 % 2 == 0).list()Now the code reads left-to-right, and the lambdas are not nearly as
bulky. Thezz.ZZ(which is an alias for the classalakazam.Alakazam) is the entry point to any stream-based
operations you might want to perform with Alakazam. Theofmethod
wraps any iterable in an Alakazam instance. Then themapandfilterdo the same thing as their global function
equivalents. Finally, thelistmethod converts the Alakazam
iterable into an ordinary Python list. The important thing is that,
now, a cursory left-to-right reading of the code yields “arr -> map ->
filter -> list”, which is the sequence of operations we’re actually
performing.The lambda syntax is also significantly shortened. In cases where your
anonymous function merely uses operators, element access, and
attribute access, you can shorten it by using the placeholder
lambdas. The placeholder constants_1, _2, _3, _4, _5, based
loosely on the C++ Boost placeholders with the same names, are each
defined as callable objects which return their nth argument. If you
need more than five arguments, you can usezz.arg(n)directly, which
is how the placeholders are implemented in the first place. These
placeholders can be used with (almost) any built-in Python operator,
and they can also be subscripted and have arbitrary attributes
accessed on them. All of these operations will be translated into an
anonymous function that performs the corresponding operation on its
arguments. So_1 ** 2is a function that squares its first (and
only) argument,_1 + _2is a function that adds two arguments
together, and_1.name == "Alakazam"is a function which checks
whether its argument’s name attribute is equal to “Alakazam”.Reference MaterialTheAlakazam Wikilists the functions available to userLicensePlease seeLICENSE.txtfor licensing information.AuthorAlakazam was written by Silvio Mayolo.Release Notes (0.7.0)applyreducer added, as per Issue #2.indicesandindex, for returning indices of all matches in
an iterable.join,each, andfirstreducers added.splitfunction, combining the behaviors oftakeanddrop, added.foldrshould perform significantly better when used on iterables
which implement__reversed__.Several minor bugfixes involving utility functions.Release Notes (0.6.0)intersperseandintertwinetransformers added.stringreducer added.assignas a synonym forset.All classes are now new-style in Python 2 as well as Python 3.Release Notes (0.5.0)accumulatecan take an initial value now.accumulate,filterfalse, andzip_longestnow work
correctly on Python 2.sumnow works on any type that has__add__, including
strings.composeworks correctly when the argument list is empty now.Several functions that used to raise Python errors now raiseAlakazamError.Several bugfixes having to do with lazy evaluation in Python 2.Release Notes (0.4.0)Trace functionstrace,traceid, andtracestack, for
debugging help.New convenience syntax for invokingbind.mapcan take multiple arguments now (Issue #1).withobjecttransformer method.zipupproducer method.absorbandconsumereduction methods.swapconvenience function for tuples.Terminology change: “Generator” to “Producer” to avoid confusion
with the Python “generator” concept.Release Notes (0.3.0)New convenience functionid.Boolean functionsnot,and_,or_, andxor.minandmaxmethods onAlakazamobjects.Newflattenanditeratemethods.lengthandnullreduction methods.Noneis now permitted as an argument to some functions where its
behavior would have caused issues before.Release Notes (0.2.0)Alakazam now uses Python 3 semantics for division (from __future__ import division) for consistency.New functionssetindex,getindex, anddelindex, for
subscripted access and manipulation.New utility functionraise_.Assignment lambdas withset, binder lambdas withbind, and
deletion lambdas withdelete.Errors are reported throughAlakazamErrornow.Changedzip_longestandcross_productargument order to
better match theitertoolsequivalents.Static methods on theAlakazamclass can now be called globally.Functionof_dictprovided to load dictionaries into Alakazam as
lists of key-value 2-tuples.