[Question]
  [
Given a number as input, print out its **collective sum**
What is a collective sum?
Consider the number `13214`, the input
Looping through each of the digits starting from the left, we will be able to obtain its collective sum.
`1` means look at the first digit, and add it to the sum, sum = 1
`3` means look at the "first 3 digits" and add it to the sum, sum = 1 + 132
`2` means look at the "first 2 digits" and add it to the sum, sum = 1 + 132 + 13
`1` means look at the first digit, and add it to the sum, sum = 1 + 132 + 13 + 1
`4` means look at the "first 4 digits" and add it to the sum, sum = 1 + 132 + 13 + 1 + 1321
Total sum = `1468` and this is your output
---
> 
> Special cases:
> 
> 
> 
If we encounter a `0`, then clearly we keep the sum the same
```
The number 1301 would have a sum = 1 + 130 + 1 = 132
```
If we encounter a number that is larger then the length of the input, we add it all
```
The number 251 would have a sum = 25 + 251 + 2 = 278
```
---
> 
> Test cases:
> 
> 
> 
```
collectiveSum(0) = 0
collectiveSum(2) = 2
collectiveSum(2315) = 23 + 231 + 2 + 2315 = 2571
```
---
Shortest amount of bytes wins. Happy golfing!
  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~4~~ 3 [bytes](https://github.com/Adriandmen/05AB1E/blob/master/docs/code-page.md)
-1 thanks to Kevin Cruijssen (use of `€` avoiding a `}`)
```
€£O
```
**[Try it online!](https://tio.run/##MzBNTDJM/f//UdOaQ4v9//83MjY0BQA "05AB1E – Try It Online")**
### How?
```
€£O - implicit input   e.g. 2315
€   - map with:
 £  -   head to             23, 231, 2, 2315
  O - sum                   2571
```
[Answer]
# [Python 2](https://docs.python.org/2/), 43 bytes
```
lambda n:sum(int('0'+n[:int(x)])for x in n)
```
[Try it online!](https://tio.run/##K6gsycjPM/qfZhvzPycxNyklUSHPqrg0VyMzr0RD3UBdOy/aCsSs0IzVTMsvUqhQyMxTyNP8X1AEFFVI0yhKLI/PzCsoLdHQ1PxvaGxgCAA "Python 2 – Try It Online")
[Answer]
# Python 2, 72 bytes
First submission! Thanks to @DestructibleLemon for the help!
```
import sys;d=sys.argv[1];s=0;for e in d:s+=int(d[:int(e)]);print str(s)
```
[Answer]
## Haskell, ~~43~~ 37 bytes
```
f l=sum[read$'0':take(read[d])l|d<-l]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P00hx7a4NDe6KDUxRUXdQN2qJDE7VQPEi06J1cypSbHRzYn9n5uYmadgq5CbWOAbr6BRUJSZV6KXpqkQzaWgoGRobGRooqQDYhpAKCMoZWxoqhT7HwA "Haskell – Try It Online")
The input number is taken as a string.
```
   [        |d<-l]      -- for every digit d in the input string
        read[d]         -- convert to a number
      take     l        -- and take that many digits from l
     0:                 -- prepend a 0 (to handle 0s)
   read                 -- convert to a number
sum                     -- sum all numbers
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), 36 bytes
```
->n{n.sum{|x|n[0,x.to_i].join.to_i}}
```
[Try it online!](https://tio.run/##KypNqvyfZvtf1y6vOk@vuDS3uqaiJi/aQKdCryQ/PjNWLys/Mw/MrK39X6CQFp2eWlKsl5yRWFQc@9/Q2MjQBAA "Ruby – Try It Online")
[Answer]
# JavaScript, ~~42~~ 40 bytes
Thanks @Shaggy for golfing off 2 bytes
```
f=
x=>[...x].map(y=>a+=+x.slice(0,y),a=0)|a
console.log(f("2315"))
console.log(f("0100"))
console.log(f("2"))
console.log(f("01025"))
```
Receives input as a string and returns an integer. As currently written, this code leaves leading zeroes on any integers.
[Answer]
# R, 57 bytes
saved one more thanks to @Vlo
```
sum(strtoi(substring(i<-scan(,""),1,el(strsplit(i,"")))))
```
was:
Saved 4 bytes thanks to @Robert Hacken's suggestion.
```
i=scan(,"");sum(strtoi(substring(i,1,el(strsplit(i,"")))))
```
A simple string approach.
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 106 bytes
```
j,k,c,t;f(char*S){for(t=j=0;S[j];j++)c=S[k=S[j]-48<(c=strlen(S))?S[j]-48:c],S[k]=0,t+=atoi(S),S[k]=c;S=t;}
```
[Try it online!](https://tio.run/##LY3NCsIwEIRfpQQKuzaF@gdiXKTPkGPNoaxGk2oqaW7FZ68tepxvvmG4vDNPk5edZJmUBX60caVxtH2ERJ4qpRtvlC8KZNJNR0ssd4cTMA0pPm8BNOL5T49s5CwZqmQqqE29m9sfYaUpqc/0al0AHJefrG4Mic12vRfqHV1IFoSFfMCMsvx6CULW0kKNOM@@ "C (gcc) – Try It Online")
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 10 bytes
```
ＩΣＩＥθ✂θ⁰Ｉι
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvRMM5sbhEI7g0F8LwTSzQKNRRCM7JTE4FMQx0FMDimZoQYP3/v5Gxoel/3bIcAA "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
   Ｅθ       Map over input string
        Ｉι  Cast current character to integer
     ✂θ⁰    Slice input string to that length
 ΣＩ         Cast slices to integer and take the sum
Ｉ           Cast result to string and implicitly print
```
[Answer]
# [Octave](https://www.gnu.org/software/octave/), 56 bytes
```
@(n)sum(str2num(['' 32+char(n.*(find(n)<=(n'-48))-32)]))
```
Anonymous function that takes a string as input argument and returns a number as output.
[**Try it online!**](https://tio.run/##LcpBDkAwEIXhvVPYzQzR6JTEgsQ9xELQsDASxfVL6Or/XvL28Rzu2dtGKeVbFHLXhu48WN52ALHhdFyGA0UlaFeZ3kvdoEBWVESZYeqJvEXQhnUBFH3M9S8uA/KwQ4wugfwD "Octave – Try It Online")
The shorter version
```
@(n)sum(str2num(['' char(n.*(find(n)<=(n'-48)))]))
```
works in Matlab, because `char(0)` is treated as a space.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~7~~ 5 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
-2 thanks to Dennis (head vectorises >.<)
4 if we may take lists of digits\*
```
Dḣ`ḌS
```
**[Try it online!](https://tio.run/##y0rNyan8/9/l4Y7FCQ939AT////fyNjQFAA "Jelly – Try It Online")**
\* `ḣ`ḌS`
### How?
```
Dḣ`ḌS - Link: integer, n   e.g. 2315
D     - to decimal list         [2,3,1,5]
  `   - repeat left as right    [2,3,1,5]
 ḣ    - head                    [2,3], [2,3,1], [2], [2,3,1,5]
   Ḍ  - from decimal lists      23, 231, 2, 2315
    S - sum                     2571
```
[Answer]
# [Perl 6](https://perl6.org), 27 bytes
```
{sum $_ X[&substr]^«.comb}
```
[Test it](https://tio.run/##RZDRaoMwGIXv8xQHJkWpFZNWWyqWPcQuBmMbrc2GTKuYWFZKn2hvsRdz/x9lvTlJvnP4k5NWd1U69EbjnEZFJkR9waxojhr5cDV9De8dzy8z0x@M7V7ffn@ioqkPt4Fij1Ybixy@AORSyRXyHeQq3YR4gMScoVOW8SgpSmssOcrgHo1Z2YZKRlut3SSVkMOM1PlA7HxayZ@QmpCb6HJqKROGKllLB/kdBFnHXSICKszdn6hJJtpqf8Lc1crER9NNDRc7/gQf2y998b3y1PY2CLE976te@57@bnVh9TFAgCvdWxrw992D/4kQ0WdprLgNfw "Perl 6 – Try It Online")
## Expanded:
```
{  # bare block lambda with implicit param ｢$_｣
  sum
    $_           # the input
      X[&substr] # crossed using &substr sub as if it was an infix operator
    ^«\          # upto ｢^｣ for each of the following ｢«｣ (creates Range objects)
    .comb        # the input split into digits (implicit method call on ｢$_｣
}
```
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~77~~ 75 bytes
Has to be compiled with the `-lm` switch or GCC doesn't recognize the math functions.
```
r,q,i;f(n){for(r=0,i=n;i;i/=10)q=log10(n)+1-i%10,r+=n/pow(10,q>0?q:0);n=r;}
```
[Try it online!](https://tio.run/##Nc3RCoMgFAbg6/kUEgRKRlrrZmJ7kd2EoQh1Sje2i@jV53JrV/8H/885urRaxxiYZ04aAnQ1cyBBceYUSCddpQSnXo2zFXyvC1G6XHAWCgXVMr/Ibt/xq79wKkEFucXn7AZsiYMHBopWdFrCbkOyfMCqw/lwg4xhYDi9oxJtCKXt1Dsg370l@60U9RGNaH8STS3Of3Jx1G3CFt/ajL29x3KcPg "C (gcc) – Try It Online")
[Answer]
# [dc](https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html), 55 bytes
```
[0*]sq?dsfZ1-se[lfddZrIle^/I%-d0>qIr^/+led1-se0<a]dsaxp
```
No strings nor arrays! Indeed, the required digits are acquired solely through mathematical manipulation.
[Try it online!](https://tio.run/##S0n@/z/aQCu2uNA@pTgtylC3ODU6Jy0lJarIMyc1Tt9TVTfFwK7QsyhOXzsnNQUkbWCTGJtSnFhR8P@/obGRoQkA "dc – Try It Online")
[Answer]
# [Husk](https://github.com/barbuz/Husk), 6 bytes
```
ṁd´M↑d
```
[Try it online!](https://tio.run/##yygtzv7//@HOxpRDW3wftU1M@f//v5GxoSkA "Husk – Try It Online")
### Explanation
```
ṁd´M↑d  -- example input: 1301
     d  -- decimal digits: [1,3,0,1]
  ´M    -- map over it using it as argument (example with 3):
    ↑   -- | take: [1,3,0]
        -- : [[1],[1,3,0],[],[1]]
ṁ       -- map and then sum the result (example on [1,3,0]):
 d      -- | as decimal: 130
        -- : 1 + 130 + 0 + 1 = 132
```
[Answer]
# [J](http://jsoftware.com/), 18 bytes
```
[:+/"."0".@{."0 1]
```
## Explanation
Takes a string as input
```
           {."0 1  - take
    "."0           - current char as int items
                 ] - from the argument
         ".        - and convert them to an integer  
[:+/               - add them up
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/o6209ZX0lAyU9ByqgZSCYex/TS4FrtTkjHyFNAV1Q2MjQxN1JK6BIYJnZIrEMUASR2IaG5qqc/0HAA "J – Try It Online")
[Answer]
# Japt, 5 bytes
Takes input as a string.
```
¬x@¯X
```
[Try it](https://ethproductions.github.io/japt/?v=1.4.5&code=rHhAr1g=&input=IjEzMjE0Ig==)
---
## Explanation
```
          :Implicit input of integer string U
¬         :Split to an array of characters/digits
  @       :Pass each X through a function
   ¯X     :Slice U from the first to the Xth character
 x        :Reduce by addition
```
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 6 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
ç╫&º±å
```
[Run and debug it online](https://staxlang.xyz/#c=%C3%A7%E2%95%AB%26%C2%BA%C2%B1%C3%A5&i=13214%0A1301%0A251%0A2%0A0%0A2315&a=1&m=2)
The corresponding ascii representation of the same program is this.
```
EZFy(e+
E        get array of digits
 Z       push 0 under array of digits
  F      for each digit, run the rest of the program
   y     input as a string
    (    get start of string for specified number of characters
     e   evaluate substring as integer
      +  add
```
[Answer]
# [Actually](https://github.com/Mego/Seriously), 10 bytes
```
╝ß⌠≈╛H≈⌡MΣ
```
[Try it online!](https://tio.run/##S0wuKU3Myan8///R1LmH5z/qWfCos@PR1NkeIKpnoe@5xf//KxkaGxmaKAEA "Actually – Try It Online")
# Explanation
```
╝          Push input to register 1
 ß         Push n-th input (0 by default)
        M  Map
  ⌠    ⌡   Function
   ≈       Cast current character of input to int
    ╛      Get value of register 1 (input)
     H     Push input[:current character]
      ≈    Cast to int
         Σ Sum
```
[Answer]
# JavaScript, 43 bytes
It's well after 3 in the morning, why am I still golfing?!
Takes input as a string.
```
s=>(g=x=>s[x]?+s.slice(0,s[x])+g(++x):0)(0)
```
[Try it online](https://tio.run/##bco9CoQwEEDh3mNYzRCViT@NED2IbCExBiUYcZYlt49ubcr38fb5N7O@tvNbHn4xcVWR1QBWBTXwFD6j4Irdpg1Q8W8UFoQI2BMCYdT@YO9M5byFFXLZ1LLNEbOXk0xw3aWUUmfKGtk9HG8)
[Answer]
# Perl 5, 26 bytes
Includes `+1` for `p`
```
perl -pE 's%.%/.{0,$&}/;$\+=$&%eg}{' <<< 2315; echo
```
[Answer]
# [K4](http://kx.com/download/), 22 bytes
**Solution:**
```
+/10/:'(x&#x)#\:x:10\:
```
**Examples:**
```
q)k)+/10/:'(x&#x)#\:x:10\:13214
1468
q)k)+/10/:'(x&#x)#\:x:10\:1301
132
q)k)+/10/:'(x&#x)#\:x:10\:251
278
q)k)+/10/:'(x&#x)#\:x:10\:2315
2571
```
**Explanation:**
Break into base-10, take min of each number and the length of list from this. Convert back and sum up.
```
+/10/:'(x&#x)#\:x:10\: / the solution
                  10\: / to base 10, 123 => 1 2 3
                x:     / save as x
             #\:       / take (#) each left
       (    )          / the left
          #x           / count (#) length of x
        x&             / min of x and the length
  10/:'                / from base 10 each
+/                     / sum up the results
```
[Answer]
# Java 8, 92 bytes
```
n->n.chars().map(c->(c-=48)>0?new Integer(n.substring(0,c<n.length()?c:n.length())):0).sum()
```
**Explanation:**
[Try it online.](https://tio.run/##hY8/b8JADMV3PoXFdJaaUwJUqvgTZgZYGKsO5jjCQeJEuQsIVfnsqRsidaqQ7MHPP/k9X@hGUVlZvhyvncnJe9iS4@8RgONg6xMZC7vfsRfAqH2oHWfAuBCxlZbygYIzsAOGFXQcpazNmWqvUBdUKROl0qvZB6bxmu0dNnI6s7Vi7ZuD7w@q@M0sWeeWs3BWuDbzvwFxHqOghcJu8XSsmkMujoPxrXRHKCT3kO7zi/CZef/wwRa6bIKuZBNyFlOjxvEY@wf@JyaviWny/hJKppNkNlDtqO1@AA)
```
n->                                  // Method with String parameter and int return-type
  n.chars()                          //  Loop over the characters
   .map(c->(c-=48)                   //   Convert character to digit
        >0?                          //    And if it's larger than 0
         new Integer(n.substring(0,  //     Take the first `x` digits, where `x` is:
          c<n.length()?              //      If the digit higher than the total a.o. digits
           c                         //       Take the first `c` digits
          :                          //      Else:
           n.length()))              //       Take all digits
         :                           //    Else:
          0)                         //     Take 0
   .sum()                            //   And sum everything
```
[Answer]
**REXX 118 bytes**
```
pull n                           
l=length(n)                      
a=0                              
j=1                              
do while (j<=l)                  
if substr(n,j,1)==0 then m=0;    
else m=substr(n,1,substr(n,j,1));
a=a+m                            
j=j+1                            
end                              
say a                                 
```
[Try it here](http://tpcg.io/Qm71Pi)  
Provide Input value in the STDIN tab. 
[Answer]
# [Python 3](https://docs.python.org/3/), 66 bytes
```
lambda n:sum([int(str(n)[:int(i)])if i!='0'else 0for i in str(n)])
```
[Try it online!](https://tio.run/##TcqxCoAgEIDhvae4Jr1Ns5agJ6kGo6SDukJt6OmNiMDt@@E/77gebJLrhrTZfZotcBuuXfbEUYboJWPfviYckRxQ2Qklli0soNzhgYAYvm/EdPp3dVKbSteIxd8qc5Xb6AYxPQ "Python 3 – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 6 bytes
```
DµḣµVS
```
[Try it online!](https://tio.run/##y0rNyan8/9/l0NaHOxYf2hoW/P//f0NjI0MTAA "Jelly – Try It Online")
Get the `D`igits of the input, then get the first [each digit] elements of the input (`ḣ`ead), then e`V`al each sum to make it a number again and `S`um. 
]