Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6470189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:04:50+00:00 2026-05-25T06:04:50+00:00

I found myself in the position of needing to increment a value which was

  • 0

I found myself in the position of needing to increment a value which was deeply nested in a series of erlang records. My first attempts at doing this with list comprehensions were dismal failures. Originally, the list contained a number of records where the target value would be absent because the record that contained it would, at some level, be undefined.

I dealt with that easily enough by using lists:partition to filter out only those entries that actually needed incrementing, but I was still unable to come up with a list comprehension that would do such a simple operation.

The code sample below probably doesn’t compile – it is simply to demonstrate what I was trying to accomplish. I put the “case (blah) of undefined” sections to illustrate my original problem:

-record(l3, {key, value}).
-record(l2, {foo, bar, a_thing_of_type_l3}).
-record(l1, {foo, bar, a_thing_of_type_l2}).

increment_values_recursive([], Acc
increment_values_recursive([L1 | L1s], Acc) ->
    case L1#l1.a_thing_of_type_l2 of
        undefined -> NewRecord = L1;
        L2        ->
            case L2#l2.a_thing_of_type_l3 of
                undefined    -> NewRecord = L2;
                {Key, Value} ->
                    NewRecord = L1#l1{l2 = L2#l2{l3 = {Key, Value + 1}}}
            end
    end,

    increment_values_recursive(L1s, [NewRecord | Acc]).

increment_values(L1s) ->
    lists:reverse(increment_values_recursive(L1s, [])).

........

NewList = increment_values(OldList).

That was what I started with, but I’d be happy to see a list comprehension that would process this when the list didn’t have to check for undefined members. Something like this, really:

increment_values_recursive([], Acc
increment_values_recursive([L1 | L1s], Acc) ->
    %I'm VERY SURE that this doesn't actually compile:
    #l1{l2 = #l2{l3 = #l3{_Key, Value} = L3} = L2} = L1, 
    %same here:
    NewRecord = L1#l1{l2=L2#l2{l3=L3#l3{value = Value+1}}},  
    increment_values_recursive(L1s, [NewRecord | Acc]).

increment_values(L1s) ->
    lists:reverse(increment_values_recursive(L1s, [])).

AKA:

typedef struct { int key, value; } l3;
typedef struct { int foo, bar; l3 m_l3 } l2;
typedef struct { int foo, bar; l2 m_l2 } l1;

for (int i=0; i<NUM_IN_LIST; i++)
{
    objs[i].m_l2.m_l3.value++;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:04:50+00:00Added an answer on May 25, 2026 at 6:04 am

    It is not as hard as it seems. @Peer Stritzinger gave a good answer, but here is my take, with a clean list comprehension:

    -record(l3, {key, value}).
    -record(l2, {foo=foo, bar=bar, al3}).
    -record(l1, {foo=foo, bar=bar, al2}).
    
    increment(#l1{al2 = Al2}=L1) -> L1#l1{al2 = increment(Al2)};
    increment(#l2{al3 = Al3}=L2) -> L2#l2{al3 = increment(Al3)};
    increment(#l3{value = V}=L3) -> L3#l3{value = V + 1}.
    
    test() ->
      List =
        [ #l1{al2=#l2{al3=#l3{key=0, value = 100}}}
        , #l1{al2=#l2{al3=#l3{key=1, value = 200}}}
        , #l1{al2=#l2{al3=#l3{key=2, value = 300}}}
        , #l1{al2=#l2{al3=#l3{key=3, value = 400}}}],
      [increment(L) || L <- List].
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found myself in the position of needing to update one or two
I have found myself doing this in my code to 'cache' the work done
So I've found myself needing to do a little bit of flash... I'm playing
I have recently found myself in the position of explaining an (In-House) application I
I've found myself in the odd position of being a TA for a software
I found myself having to remove the first line of a string quite often
I found myself needing to define some styles and templates for a couple ToggleButton
I've found myself doing too much error handling with try\catch statements and getting my
This is the first question I have found myself not being able to get
If I were to make a single-window Java program, I have found myself doing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.