higher order function ^ first class function

See also http://stackoverflow.com/questions/10141124/any-difference-between-first-class-function-and-high-order-function.

Higher Order Function — A “boss” function that takes in smaller worker functions. Usually these workers are “applied” on a sequence.

Simplest Example: Python filter()/reduce()/map()

First Class Function — Each function is treated like a regular object and passed in/out of HOF. Often implemented as lambda (or closure). Functor too [[Essential C++]] P127.

Why FirstClass? I think objects are the only first class thing in traditional languages.

Simplest Example: Python lambda
——————
Python has other HOF features. I guess decorator might be.

STL functors qualify as FCF. STL algorithms taking those functors qualify as HOF. For example,
for_each()
transform()
generate()
find_if()
copy_if()

C# Linq has many HOF functions like ForEach(), Aggregate() and many aggregate functions. Many other C# function take (unicast) delegate arguments.
C# lambda (and anonymous delegates) is the typical FCF.

Advertisements

python q(import)directive complexity imt java/c++/c#

I would say it “does more work” not just “more complicated”…

Name conflicts, name resolution … are the main purpose of (import/using/include) in java/c++/c#. (Side question — Can c++ header files execute arbitrary statements? I think so since it’s just pasted in… Minor question)

In contrast, python modules are executed line by line the first time they are imported. P300 [[programming python]]. I think this can include arbitrary statements. This is the major departure from “Tradition”.

I guess “from … import …” is more traditional and won’t execute arbitrary code??

WPF binding system queries VM upon PropertyChanged event

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx pointed out …

The INotifyPropertyChanged interface contains an event pseudo-field called PropertyChanged.

Whenever a property on a ViewModel object (or a Model object) has a new value, it can (should?) raise the PropertyChanged event to notify the WPF binding system. Upon receiving that notification, the binding system queries the property, and the bound property on some UI element receives the new value. I believe this is how new data hits the screen.

I believe the callbacks run on the UI thread, just like swing.

In order for WPF to know which property on the ViewModel object has changed, the PropertyChangedEventArgs class exposes a PropertyName property of type String. You must be careful to pass the correct property name into that event argument; otherwise, WPF will end up querying the wrong property for a new value.

For PropertyChanged to work, i think we have to use xaml binding with a prop name. In contrast, the alternative — defining ItemSource in codebehind probably doesn’t work. 

sample code showing boost scoped_lock# !! c++14

#include <iostream>
#include <boost/thread.hpp>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
boost::posix_time::milliseconds sleepTime(1);
 
template<typename T>
class MyQueue {
 public:
 void enqueue(const T& x) {
  cout << "\t\t\t > enqueuing ... " << x << "\n";
  boost::mutex::scoped_lock    myScopedLock(this->mutex_);
  cout << "\t\t\t >> just got lock ... " << x << "\n";
  list_.push_back(x);
  // A scoped_lock is destroyed (and thus unlocked) when it goes out of scope
 }
 T dequeue() {
  boost::mutex::scoped_lock lock(this->mutex_);
  if (list_.empty()) {
   throw 0; // unlock
  }
  T tmp = list_.front();
  list_.pop_front();
  cout << "< dequeu " << tmp << "\n";
  return (tmp);
 }
 private:
 std::list<T> list_;
 boost::mutex mutex_;
};
MyQueue<std::string> queueOfStrings;
int reps = 5;
void sendSomething() {
 for (int i = 0; i < reps; ++i) {
  stringstream st;
  st << i;
  queueOfStrings.enqueue("item_" + st.str());
  boost::this_thread::sleep(sleepTime);
 }
}
void recvSomething() {
 for (int i = 0; i < reps*3; ++i) {
  try {
   queueOfStrings.dequeue();
  } catch (int ex) {
   cout << "<- - (    ) after releasing lock, catching " << ex <<"\n";
   boost::this_thread::sleep(sleepTime*2);
  }
 }
}
int main() {
 boost::thread thr1(sendSomething);
 boost::thread thr2(recvSomething);
 thr1.join();
 thr2.join();
}

double-ptr usage #5 – pointer allocated on heap

Whenever a pointer Object (32-bit object[1]) is allocated on heap, there’s usually (always?) a double pointer somewhere.

new int*; //returns an address, i.e. the address of our pointer Object. If you save this address in a var3, then var3 must be a double-ptr.

int ** var3 = new int*; //compiles fine, but you should not access **var3

However, I feel we seldom allocate a pointer on heap directly. More common patterns of allocating pointer on heap are
– if an Account class has a pointer field, then an Account on heap would have this pointer field allocated on heap.
– a pointer array allocated on heap

[1] assuming a 32-bit machine.

CompletionService.java – initial phrasebook

producer/consumer – upgraded.

buffer – as explained elsewhere in my blog, there’s buffer in any async design. In the ExecutorCompletionService scenario, the buffer is the “completion queue”. In the classic producer/consumer scenario, buffer is the item queue.

items = “tasks” – In P/C setup, Thread 1 could be producing “items” and Thread 2 could be taking up the items off the buffer and using them. In the important special case of task items, the consumer thread
(possibly worker thread) would pick up the task from queue and execute them. CompletionService is all about task items.

tasks executed by..? – in P/C with task queue, tasks are executed by consumer. In CompletionService, tasks are executed by the mysterious “Service”, not consumer. See CompletionService javadoc.

3-party – 1 more than P/C. Beside the P and C threads, the task executor could run on another thread.

func ptr as template non-type param

This is the #1 eye-opener in [[essential c++]]

http://bigblog.tanbin.com/2012/03/non-dummy-type-template-parameters.html describes the background of Non-Dummy-Type parameters in a class template. Things like the maxRows in template class matrix_double. [[Essential c++]] succinctly describes that usage but also illustrates on P185 how you can put in a func ptr Type (not a dummy type) in place of the “int maxRows”. I find it a very powerful technique. (I guess java and c# take other routes since they don’t support NDT template parameters.) This is how I guess it works. 

First understand a real usage scenario and focus on a concrete class instantiated from such a template. At runtime such a class can have 888 class-instances, but they all share a single special “static field”, which is the function address[1] supplied to concretize the template.
If you later concretize the template with a 2nd function address, you get a 2nd concrete class. You can create 877 instances of this 2nd class. 
For the simple NDT, you supply an integer constant like 55 when you concretize the template matrix_double. Similarly, you supply a function address as a constant when concritizing our numeric_sequence template. More generally, any constant expression can be used for a NDT.
How useful is this technique? It allows you to concretize a template using a specific function address — a specific “behavior”. It could beat a static field in some cases. For example, You can concretize a given template
* once with type Account + AccountBehavior
* once with type Student + StudentBehavior
* once with type int + intBehavior
[1] the address of a real function, not a func ptr Type.

2 focuses when learning a new tech skill: IV^GTD

When we learn a new tech skill, we need very, very specific goals and guidelines to help us choose what NOT to dig into too early.

Focus #1: IV topics, a well-defined focus. Basically textbook knowledge. Go deep enough but not too deep on a particular subtopic. Examples — op-new, pure virtual, const ref param of copier, recursive locks, OOM calling new,

Focus #2: GTD i.e. dev productivity — as measured by managers, bench-marked against other developers. Incidentally, perceived productivity sometimes depends so heavily on local system knowledge that a new hire always lags behind no matter what. I feel productivity and GTD has low dependency on #1, as most of the textbook knowledge is irrelevant.

Focus #2b: tools — see post on 2 categories of tools.

Focus #2c? tips on StackOverflow. No such books. No way to

Focus #?: best practices — or Acceptable practices and well-trodden paths

Focus #?: learn all the syntax details to read any source code on books, interviews and online

Focus #: high-level design decisions, for technical leadership roles. Myriads of decisions are made every hour during development, some at high some at low levels. Apparently, a lot of these decisions involve theoretical understanding.