Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Let's do that functional

37 views
Skip to first unread message

Bonita Montero

unread,
Aug 15, 2023, 3:15:54 AM8/15/23
to
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 - 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;
}
}

Alf P. Steinbach

unread,
Aug 15, 2023, 10:02:38 AM8/15/23
to
I don't see any purpose of the lambda expressions. You can use
expressions directly for the HANDLEs. With a conditional expression for
the first.

- Alf

Bonita Montero

unread,
Aug 15, 2023, 11:16:22 AM8/15/23
to
But you'd have multiple calls of the lambdas. That's just a pattern
for me which I use often to make code mor easily extendible.

Pavel

unread,
Aug 15, 2023, 11:44:43 PM8/15/23
to
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))

Bonita Montero

unread,
Aug 16, 2023, 4:25:46 AM8/16/23
to
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).

Pavel

unread,
Aug 16, 2023, 9:56:54 PM8/16/23
to
Bonita Montero wrote:
> 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.
SQL IS NOT PURE FUNCTIONAL AS IT HAS TEMPORARY TABLES FOR VARIABLES QED

Andrey Tarasevich

unread,
Aug 18, 2023, 10:37:32 AM8/18/23
to
On 08/15/23 12:15 AM, 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 - having code in an initializer_list<>.
>

Huh? There's no "code in an initializer_list<>" in the code you posted.

--
Best regards,
Andrey

Bonita Montero

unread,
Aug 18, 2023, 11:44:01 AM8/18/23
to
Am 18.08.2023 um 16:37 schrieb Andrey Tarasevich:

> Huh? There's no "code in an initializer_list<>" in the code you posted.

Right, it's just a C-style array.

0 new messages