Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #91262

Re: Let's do that functional

From Bonita Montero <Bonita.Montero@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Let's do that functional
Date 2023-08-16 10:25 +0200
Organization A noiseless patient Spider
Message-ID <ubi15o$37vc3$1@dont-email.me> (permalink)
References <ubf8mm$2npob$1@dont-email.me> <tCXCM.132385$cc2c.67713@fx37.iad>

Show all headers | View raw


Am 16.08.2023 um 05:44 schrieb Pavel:
> Bonita Montero wrote:
>> I wanted to check the typed of different Win32 HANDLEs. Although this
>> is documented I wanted to do that myself. I did that with the functional
>> programming style I love so much
> (Yours is not the functional style but hybrid at best
>   (because
>    (functional programmers don't use
>     (or
>      (iterative loops)
>      (mutable variables
>       (truly functional programmers don't use mutable variables either)
>       (hence functional programmers don't use cout
>        (and not only because of the above))))))
>   (-Pavel))

Pure functional languages like SQL don't have any control flow. I like
the mix of imperative programming with OOP and functional programming,
but not pure functional but _with_ state like with C++11. I discovered
functional programming in C++ much later than C++ can (2011).

> 
>> - having code in an initializer_list<>.
>>
>> #include <WinSock2.h>
>> #include <Windows.h>
>> #include <iostream>
>> #include <functional>
>> #include <system_error>
>>
>> using namespace std;
>>
>> int main( int argc, char **argv )
>> {
>>      WSADATA wsaData;
>>      (void)WSAStartup( MAKEWORD(2, 2), &wsaData );
>>      using fn_ret_t = pair<char const *, HANDLE>;
>>      using fn_t = function<fn_ret_t ()>;
>>      static fn_t const functions[] =
>>      {
>>          [&]() -> fn_ret_t
>>          {
>>              if( argc < 1 || !argv[0] )
>>                  return { nullptr, NULL };
>>              return { "file: ", CreateFileA( argv[0], GENERIC_READ, 
>> FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ) };
>>          },
>>          []() -> fn_ret_t { return { "stdin: ", GetStdHandle( 
>> STD_INPUT_HANDLE ) }; },
>>          []() -> fn_ret_t { return { "TCP-socket: ", (HANDLE)socket( 
>> AF_INET, SOCK_STREAM, IPPROTO_TCP ) }; },
>>          []() -> fn_ret_t { return { "UDP-socket: ", (HANDLE)socket( 
>> AF_INET, SOCK_DGRAM, IPPROTO_UDP ) }; },
>>          []() -> fn_ret_t { return { "ICMP-socket: ", (HANDLE)socket( 
>> AF_INET, SOCK_RAW, IPPROTO_ICMP ) }; }
>>      };
>>      using map_t = pair<DWORD, char const*>;
>>      static map_t const mappings[] =
>>      {
>>          { FILE_TYPE_CHAR, "FILE_TYPE_CHAR" },
>>          { FILE_TYPE_DISK, "FILE_TYPE_DISK" },
>>          { FILE_TYPE_PIPE, "FILE_TYPE_PIPE" },
>>          { FILE_TYPE_REMOTE, "FILE_TYPE_REMOTE" },
>>          { FILE_TYPE_UNKNOWN, "FILE_TYPE_UNKNOWN" }
>>      };
>>      for( fn_t const &fn : functions )
>>          if( fn_ret_t fr = fn(); fr.first && fr.second )
>>          {
>>              DWORD dwFileType = GetFileType( fr.second );
>>              map_t const *type = find_if( mappings, end( mappings ), 
>> [&]( map_t const &mapEntry ) { return mapEntry.first == dwFileType; } );
>>              if( type == end( mappings ) )
>>                  continue;
>>              cout << fr.first << type->second << endl;
>>          }
>> }
> 

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Let's do that functional Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-15 09:15 +0200
  Re: Let's do that functional "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-08-15 16:02 +0200
    Re: Let's do that functional Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-15 17:16 +0200
  Re: Let's do that functional Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-08-15 23:44 -0400
    Re: Let's do that functional Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-16 10:25 +0200
      Re: Let's do that functional Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-08-16 21:56 -0400
  Re: Let's do that functional Andrey Tarasevich <andreytarasevich@hotmail.com> - 2023-08-18 07:37 -0700
    Re: Let's do that functional Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-18 17:43 +0200

csiph-web