~andrewrk/ziglang

2 2

ComptimeStringMap with tuples of strings

Andreas Reuleaux <rx@a-rx.info>
Details
Message ID
<87wn6nrvkz.fsf@laptop>
DKIM signature
missing
Download raw message
Hi,

I would like to use ComptimeStringMap with string-tuples, like so:


--8<---------------cut here---------------start------------->8---
const map = std.ComptimeStringMap(?????, .{
    .{ "foo", .{ "ba"
                    , "bana"
                    , "banana"
                    , "bananana"
                }},
    
    .{ "bar", .{ "toto"
                    , "titi"
                    , "tata"
                }},
    
    .{ "baz", .{ "hello"
                    , "world"
                    , "nice"
                    , "to"
                    , "meet"
                    , "you"
                    
                }}
    
});
--8<---------------cut here---------------end--------------->8---


I.e. with each string (key): "foo", "bar", "baz" - there is a tuple
of strings associated (as a value) 


and then iterate over these respectively:


--8<---------------cut here---------------start------------->8---
inline for (map.get("foo")) |i| {
    std.debug.print("foo word: {s}\n", .{i});
}        

inline for (map.get("bar")) |i| {
    std.debug.print("bar word: {s}\n", .{i});
}        


inline for (map.get("baz")) |i| {
    std.debug.print("baz word: {s}\n", .{i});
}        
--8<---------------cut here---------------end--------------->8---


I don't know if it's possible at all [?], and in particular,
what type to use for ????? - I have tried all kind of things for ?????,
like so:

--8<---------------cut here---------------start------------->8---
std.meta.Tuple([]type{[]const u8})
--8<---------------cut here---------------end--------------->8---

etc. - without success so far.


Alternatively maybe somehow like this: arrays of string:
(even though I would prefer the above tuple solution:
less clutter to write)


--8<---------------cut here---------------start------------->8---
const map2 = std.ComptimeStringMap([][]const u8, .{
    .{ "foo", [_][]const u8{ "ba"
                    , "bana"
                    , "banana"
                    , "banana"
                }},
    
    .{ "bar", [_][]const u8{ "toto"
                    , "titi"
                    , "tata"
                }},
    
    .{ "baz", [_][]const u8{ "hello"
                    , "world"
                    , "nice"
                    , "to"
                    , "meet"
                    , "you"
                    
                }}
    
});
--8<---------------cut here---------------end--------------->8---


and I haven't gotten this to work either yet:


--8<---------------cut here---------------start------------->8---
for (map2.get("foo")) |i| {
    std.debug.print("foo word {s}\n", .{i});
}        

for (map2.get("bar")) |i| {
    std.debug.print("foo word {s}\n", .{i});
}        
--8<---------------cut here---------------end--------------->8---


Thanks in advance
  -A
Ganesan Rajagopal <rganesan@gmail.com>
Details
Message ID
<CALeQYX1pvXdUuBhiog_e2B76N0LvPrdHQW3ErOWJxauvi3qnMg@mail.gmail.com>
In-Reply-To
<87wn6nrvkz.fsf@laptop> (view parent)
DKIM signature
missing
Download raw message
On Tue, Dec 20, 2022 at 1:53 AM Andreas Reuleaux <rx@a-rx.info> wrote:
> Alternatively maybe somehow like this: arrays of string:
> (even though I would prefer the above tuple solution:
> less clutter to write)

Your 2nd example (map2) worked for me with two changes.
1. The value type should be "[]const []const u8"
2. You need &s before the values array to coerce them into slices.

--8<---------------cut here---------------start------------->8---
const std = @import("std");

const map2 = std.ComptimeStringMap([]const []const u8, .{ //
    .{
        "foo", //
        &[_][]const u8{ "ba", "bana", "banana", "bananana" },
    },
    .{
        "bar", //
        &[_][]const u8{ "toto", "titi", "tata" },
    },
    .{
        "baz", //
        &[_][]const u8{ "hello", "world", "nice", "to", "meet", "you" },
    },
});

pub fn main() void {
    for (map2.get("foo").?) |v| {
        std.debug.print("{s}\n", .{v});
    }
    for (map2.get("bar").?) |v| {
        std.debug.print("{s}\n", .{v});
    }
    for (map2.get("baz").?) |v| {
        std.debug.print("{s}\n", .{v});
    }
}
--8<---------------cut here---------------end--------------->8---

Ganesan

-- 
Ganesan Rajagopal
Andreas Reuleaux <rx@a-rx.info>
Details
Message ID
<875ye6i8eg.fsf@laptop>
In-Reply-To
<CALeQYX1pvXdUuBhiog_e2B76N0LvPrdHQW3ErOWJxauvi3qnMg@mail.gmail.com> (view parent)
DKIM signature
missing
Download raw message
Hi,

Great! And many thanks!


(I would still like the tuple example working sometime,
but that is less urgent now, with the second example working)


-A


Ganesan Rajagopal <rganesan@gmail.com> writes:

> On Tue, Dec 20, 2022 at 1:53 AM Andreas Reuleaux <rx@a-rx.info> wrote:
>> Alternatively maybe somehow like this: arrays of string:
>> (even though I would prefer the above tuple solution:
>> less clutter to write)
>
> Your 2nd example (map2) worked for me with two changes.
> 1. The value type should be "[]const []const u8"
> 2. You need &s before the values array to coerce them into slices.
>
> const std = @import("std");
>
> const map2 = std.ComptimeStringMap([]const []const u8, .{ //
>     .{
>         "foo", //
>         &[_][]const u8{ "ba", "bana", "banana", "bananana" },
>     },
>     .{
>         "bar", //
>         &[_][]const u8{ "toto", "titi", "tata" },
>     },
>     .{
>         "baz", //
>         &[_][]const u8{ "hello", "world", "nice", "to", "meet", "you" },
>     },
> });
>
> pub fn main() void {
>     for (map2.get("foo").?) |v| {
>         std.debug.print("{s}\n", .{v});
>     }
>     for (map2.get("bar").?) |v| {
>         std.debug.print("{s}\n", .{v});
>     }
>     for (map2.get("baz").?) |v| {
>         std.debug.print("{s}\n", .{v});
>     }
> }
>
> Ganesan
Reply to thread Export thread (mbox)