### BIN2DEC in Power Query: Implementations of List.Accumulate. Part 1

After @datachant have posted on using ** List.Accumulate** in Power Query, I was intrigued of practical implementations of this function.

As you can see from his post, ** List.Accumulate** takes a

*as an input parameter, and then, starting from initial*

**list***value, invokes the same*

**seed****on each element of list, consequently.**

*accumulator function*You just need to remember than ** List.Accumulate** can use two parameters in accumulator function,

**and**

*state***:**

*current**state*(i.e. current result of previous calculations) of accumulator and a

*current*element from given list. Final result will be a value that we got on the last iteration of accumulator function. Please note that

**value actually used as the first state value in accumulator.**

*seed*The power of ** List.Accumulate** is in iterations: we will execute accumulator function as many times as the number of elements in list. So, if we have an algorithm that needed repeating calculations on list members or as much times as list members count,

**is the answer.**

*List.Accumulate*I found a small algorithm that shows a practical use and core concepts of ** List.Accumulate**: conversion from binary number to decimal. In this algorithm bits of the binary number are used one by one, starting with the most significant (leftmost) bit. Beginning with the value 0, you need to repeatedly double the prior value and add the next bit to produce the next value.

So, if we would like to convert binary **1101 _{2}** to decimal number

**13**, we need to split binary number to the list of bits:

_{10}{1,1,0,1}

Then we take seed value of 0 and perform next operations: double the prior value and add the next bit, repeat while we have bits:

**0 _{(seed, or first state)} *2 + 1_{(current bit)} = 1**

**1**

** 2 + 1*_{(state) }*= 3*_{(current bit)}**3**

** 2 + 0*_{(state) }*= 6*_{(current bit)}**6**

** 2 + 1*_{(state) }*= 13*_{(current bit)}In this algorithm we can see all parameters for ** List.Accumulate**: given

*list*of bits from binary code, initial

*seed*value of 0,

*state*of accumulation transferred from the one step to the next step, and

*current*element of a list (current bit).

To implement this algorithm in Power Query ** List.Accumulate** we just need to get a list from given input, for example it is a text string: “101010”

1 2 3 4 5 |
BitList = Text.ToList("101010") // here we’ll get a list of text symbols, // so at the next step we need to implement type transformation // on list elements to be able to do calculations Bin2Dec = List.Accumulate(BitList, 0, (state, current)=> state*2 + Number.From(current)) |

The result could be nothing than 42, it’s quite universal 🙂

I hope that this example will help you to find more practical implementations of ** List.Accumulate**. And if you remember that initial list could be used just as iterations counter, and a seed value could be any… to be continued.