euccas.github.io

why a developer writes

C++ Functors

| Comments

A functor is a powerful C++ entity that everyone who wants to master C++ needs to know. A functor, which is short for “function object”, is a C++ class that acts like a function. Functors can be called using the familiar function call syntax, and can yield values and accept parameters just like regular functions.

To create a functor, we create a class (or a struct) that overloads the function operator(). Note here the function is called operator(), and it’s not the operator function, i.e. (). We then create an instance of this class (or struct) to use the created functor.

Create and use functors

Let’s look at two examples of creating and using a functor. In the first example, a functor is created with a class, and in the second example we use a struct to create the functor.

Get Start on Machine Learning

| Comments

So here comes 2017, a year when you hear people talk about the words Machine Learning, Reinforcement Learning, and Artificial Intelligence everywhere.

Last year when Mark Zuckberg was working on building Jarvis, I didn’t spend much time on AI or Machine Learning. But I know the efforts I made last year get me ready to start on it right now.

Tonight I just talked to a former colleague who is working at Nvidia, and he gave me a few helpful suggestions about getting started on machine learning according to his own experience. Here are some of his advices:

Python Multiprocessing

| Comments

Python’s GIL (Global Interpreter Lock) was designed to be a thread-safe mechanism, and it effectively prevents conflicts between multiple threads. GIL makes it easy to implemente multi-threading with Python. However, it also prevents Python multi-threading from utilizing the multiple cores of a computer to achieve improved execution speed. This is why using the threading module in Python won’t help your program run faster through parallelism.

The good thing is Python provides a multiprocessing module since Python 2.6. With the multiprocessing module we can spawn subprocesses and effectively avoid some of the limitations that GIL brings, on both Unix and Windows platforms.

In this post I’ll briefly introduce multiprocess module and show how it can be used for parallel programming.

Levels of Design

| Comments

Recently I’m taking a course “Algorithm Toolbox” on Coursera. This course provides me a good chance to review and enhance my knowledge in the fundamental algorithms, which usually would help on achieving better system design. This morning I came across one slide of this course and thought it could be very useful. Sharing it here.

It’s important to keep the levels of (algorithm) design in mind when solving a problem.

Python Generators

| Comments

Generators is a powerful weapon of Python. Generators help you write concise code, give you lazy evaluation, and improve the efficience for calculating large sets of results. Personally I think it’s a good habit to use generators in Python whenever you can, if you really want your code to be Pythonic.

How to create a generator

There are mainly two ways to create a generator: using the yield keyword in the function, or using the () as a generator expression.

  • The yield keyword makes the function yields control back to the calling function on every iteration
  • The () expression returns a generator object