【趣写代码】从《基督山伯爵》谈生活中的回调函数(Callback function)

目录

1 等待和希望

2 回调函数的概念

3 生活中的回调函数


1 等待和希望

读过《基督山伯爵》一书的朋友,一定记得下面这段话。

世界上既无所谓快乐或也无所谓痛苦;只有一种状况与另一种状况的比较,如此而已。只有体验过不幸的人才能体会最大的快乐。莫雷尔,我们必须体验过死的痛苦,才能体会到生的快乐。所以,我心爱的孩子们,享受生命的快乐吧!永远不要忘记,直至上帝揭露人的未来图景的那一天以前,人类的一切智慧就包含在这四个字里面:“等待”和“希望”。

 

以上图片的角色来自美国的一个儿童电视节目《芝麻街》,如果大家还对人工智能的一些模型有了解,一定会知道芝麻街的角色是很多经典人工智能模型的名字,例如BERT,ELMo,ERNIE等等。

把这段话转换成程序员术语,那就是提前写好回调函数,可以让我们的人生充满惊喜。总会等到被调用的时候,而其他的时候,我们还是要前行,毕竟很多时候只能用异步方式,毕竟很多时候我们遇到的只是time out(超时),或者service unavailable(服务不可用). 

2 回调函数的概念

 一起回顾一下回调函数的概念吧。回调函数,或简称回调(Callback 即call then back 被主函数调用运算后会返回主函数),是指通过函数参数传递到其它代码的,某一块可执行代码的引用。这一设计允许了底层代码调用在高层定义的子程序。

在JavaScript中,回调函数具体的定义为:如果函数A作为参数(函数引用)传递到另一个函数B中,并且这个函数B执行函数A。我们称函数A叫做回调函数。如果没有名称(函数表达式),就叫做匿名回调函数。注意,回调callback 不一定用于异步,一般同步(阻塞)的场景下也经常用到回调,比如要求执行某些操作后执行回调函数。

3 生活中的回调函数

来看看我写过的一段和生活有关的代码吧,Windows程序。这段代码写好了面对成长、爱情、家庭、成功、友谊和健康的回调函数。

// A completewindows program

// INCLUDES///
#define WIN32_LEAN_AND_MEAN  // just say no to MFC

#include<windows.h>   // include all the windows headers
#include <windowsx.h>  // include useful macros
#include <stdio.h>     
#include <math.h>

// DEFINES

// defines forwindows 
#define WINDOW_CLASS_NAME "WINCLASS1"

// GLOBALS

// FUNCTIONS//
LRESULT CALLBACK WindowProc(HWND hwnd, 
                           UINT msg, 
                           WPARAM wparam, 
                           LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT        ps;       // used in WM_PAINT
HDC               hdc;    // handle to a device context

// what is themessage 
switch(msg)
    {    
    case WM_CREATE: 
        {
        // do initialization stuff here

       // return success
        return(0);
        } break;

   case WM_PAINT: 
        {
        // simply validate the window
        hdc =BeginPaint(hwnd,&ps);     
        // you would do all your painting here
        EndPaint(hwnd,&ps);

       // return success
        return(0);
           } break;

   case WM_PARENTS_LEAVE_ME_ALONE: 
        {
        delete fear;
        // return success
        return(0);
           } break;

   case WM_GET_LOVE:

       {

       family = new (Family);
        power = new (Power);

       // return success

       return(0);

          } break;

    case WM_LOST_LOVE:

       {

       delete sadness;

       // return success

       return(0);

          } break;

    case WM_NEW_ACHIEVEMENTS:

       {

       startpoint = new (Startpoint);

       // return success

       return(0);

          } break;

   case WM_SOS_FROM_FRIENDS:

       {

       hand = new (Hand);

       // return success

       return(0);

          } break;
   
   case WM_BAD_HEALTH:

       {

       body = new (Body);

       // return success

       return(0);

          } break;  

   case WM_DESTROY: 
        {
        // kill the application, this sends aWM_QUIT message 
        PostQuitMessage(0);

       // return success
        return(0);
        } break;

   default:break;

   } // end switch

// process anymessages that we didn’t take care of 
return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc

// WINMAIN
int WINAPI WinMain(    HINSTANCE hinstance,
                   HINSTANCE hprevinstance,
                   LPSTR lpcmdline,
                   int ncmdshow)
{

WNDCLASSEXwinclass; // this will hold the class we create
HWND       hwnd;     // genericwindow handle
MSG           msg;        // generic message

// first fill inthe window class stucture
winclass.cbSize         =sizeof(WNDCLASSEX);
winclass.style            =CS_DBLCLKS | CS_OWNDC | 
                         CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc    = WindowProc;
winclass.cbClsExtra        = 0;
winclass.cbWndExtra        = 0;
winclass.hInstance        = hinstance;
winclass.hIcon            =LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor        = LoadCursor(NULL,IDC_ARROW);
winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName    = NULL;
winclass.lpszClassName    = WINDOW_CLASS_NAME;
winclass.hIconSm        = LoadIcon(NULL,IDI_APPLICATION);

// register thewindow class
if (!RegisterClassEx(&winclass))
    return(0);

// create thewindow
if (!(hwnd = CreateWindowEx(NULL, // extended style
                           WINDOW_CLASS_NAME,   // class
                           "Your BasicWindow", // title
                           WS_OVERLAPPEDWINDOW |WS_VISIBLE,
                           0,0,        // initial x,y
                           400,400,  //initial width, height
                          NULL,        // handle to parent 
                          NULL,        // handle to menu
                           hinstance,// instanceof this application
                          NULL)))    // extra creation parms
return(0);

// enter mainevent loop
while(GetMessage(&msg,NULL,0,0))
      { 
     // translate any accelerator keys
     TranslateMessage(&msg);

    // send the message to the window proc
     DispatchMessage(&msg);
     } // end while

// return toWindows like this
return(msg.wParam);

} // end WinMain

///

 

 如果你喜欢写软件程序,期待你看完文章后会心一笑。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值