[Question]
  [
# Inspired by, and in memory of, my dear friend and colleague,
[![Dan Baronet](https://i.stack.imgur.com/eSEJq.jpg)](http://www.dyalog.com/blog/2016/11/dan-baronet/)
# [Dan Baronet](http://www.dyalog.com/blog/2016/11/dan-baronet/), 1956 – 2016. [R.I.P.](http://danielbaronet.rip/)
He found [the shortest possible APL solution](https://codegolf.stackexchange.com/a/98764/43319) to this task:
### Task
Given a Boolean list, count the number of trailing truth values.
### Example cases
`{}` → `0`
`{0}` → `0`
`{1}` → `1`
`{0, 1, 1, 0, 0}` → `0`
`{1, 1, 1, 0, 1}` → `1`
`{1, 1, 0, 1, 1}` → `2`
`{0, 0, 1, 1, 1}` → `3`
`{1, 1, 1, 1, 1, 1}` → `6`
  
[Answer]
# Dyalog APL, ~~6~~ 2 bytes
```
⊥⍨
```
Test it on [TryAPL](http://tryapl.org/?a=%28%u22A5%u2368%29%A8%28%u236C%280%29%281%29%280%201%201%200%200%29%281%201%201%200%201%29%281%201%200%201%201%29%280%200%201%201%201%29%281%201%201%201%201%201%29%29&run).
### How it works
**⊥** (uptack, dyadic: decode) performs base conversion. If the left operand is a vector, it performs *mixed* base conversion, which is perfect for this task.
For a base vector **b = bn, ⋯, b0** and a digit vector **a = an, ⋯, a0**, **b ⊥ a** converts **a** to the mixed base **b**, i.e., it computes **b0⋯bn-1an + ⋯ + b0b1a2 + b0a1 + a0**.
Now, **⍨** (tilde dieresis, commute) modifies the operator to the left as follows. In a monadic context, it calls the operator with equal left and right arguments.
For example, **⊥⍨ a** is defined as **a ⊥ a**, which computes **a0⋯an + ⋯ + a0a1a2 + a0a1 + a0**, the sum of all cumulative products from the right to the left.
For **k** trailing ones, the **k** rightmost products are **1** and all others are **0**, so their sum is equal to **k**.
[Answer]
## JavaScript (ES6), 21 bytes
```
f=l=>l.pop()?f(l)+1:0
```
### Test cases
```
f=l=>l.pop()?f(l)+1:0
console.log(f([])); // → 0
console.log(f([0])); // → 0
console.log(f([1])); // → 1
console.log(f([0, 1, 1, 0, 0])); // → 0
console.log(f([1, 1, 1, 0, 1])); // → 1
console.log(f([1, 1, 0, 1, 1])); // → 2
console.log(f([0, 0, 1, 1, 1])); // → 3
console.log(f([1, 1, 1, 1, 1, 1])); // → 6
```
[Answer]
# [Brachylog](http://github.com/JCumin/Brachylog), ~~7~~ ~~6~~ 5 bytes
```
@]#=+
```
[Try it online!](http://brachylog.tryitonline.net/#code=QF0jPSs&input=WzE6MTowOjE6MV0&args=Wg)
### Explanation
```
@]        A suffix of the Input...
  #=      ...whose elements are all equal
    +     Sum its elements
```
Since `@] - Suffix` starts from the biggest suffix all the way up to the smallest one, it will find the longest run first.
[Answer]
# [Jelly](http://github.com/DennisMitchell/jelly), 4 bytes
```
ŒrṪP
```
[Try it online!](http://jelly.tryitonline.net/#code=xZJy4bmqUA&input=&args=WzEsIDEsIDAsIDEsIDFd) or [Verify all test cases.](http://jelly.tryitonline.net/#code=xZJy4bmqUArDh-KCrA&input=&args=W1tdLCBbMF0sIFsxXSwgWzAsIDEsIDEsIDAsIDBdLCBbMSwgMSwgMSwgMCwgMV0sIFsxLCAxLCAwLCAxLCAxXSwgWzAsIDAsIDEsIDEsIDFdLCBbMSwgMSwgMSwgMSwgMSwgMV1d)
For the case where the list is empty, there are some curious observations. First, run-length encoding the empty list `[]` returns another empty list `[]`. Then retreiving the last element from that using tail `Ṫ` returns `0` instead of a pair `[value, count]` which are the regular elements of a run-length encoded array. Then product `P` returns `0` when called on `0` which is the expected result.
## Explanation
```
ŒrṪP  Main link. Input: list M
Œr    Run-length encode
  Ṫ   Tail, get the last value
   P  Product, multiply the values together
```
[Answer]
# Haskell, ~~26~~ 25 bytes
```
a%b|b=1+a|0<3=0
foldl(%)0
```
Usage:
```
Prelude> foldl(%)0 [True,False,True,True]
2
```
Pointfree version (26 bytes):
```
length.fst.span id.reverse
```
---
Using an integer list instead of a bool list (21 bytes, thanks to Christian Sievers):
```
a%b=b*(a+1)
foldl(%)0
```
Usage:
```
Prelude> foldl(%)0 [1,0,1,1]
2
```
Pointfree version (25 bytes)
```
sum.fst.span(==1).reverse
```
[Answer]
## CJam (8 bytes)
```
{W%0+0#}
```
[Online test suite](http://cjam.aditsu.net/#code=qN%2F%7B'%E2%86%92%2F0%3D'%2C-~~%5D%0A%0A%7BW%250%2B0%23%7D%0A%0A~p%7D%2F&input=%7B%7D%20%E2%86%92%200%0A%7B0%7D%20%E2%86%92%200%0A%7B1%7D%20%E2%86%92%201%0A%7B0%2C%201%2C%201%2C%200%2C%200%7D%20%E2%86%92%200%0A%7B1%2C%201%2C%201%2C%200%2C%201%7D%20%E2%86%92%201%0A%7B1%2C%201%2C%200%2C%201%2C%201%7D%20%E2%86%92%202%0A%7B0%2C%200%2C%201%2C%201%2C%201%7D%20%E2%86%92%203%0A%7B1%2C%201%2C%201%2C%201%2C%201%2C%201%7D%20%E2%86%92%206)
### Dissection
```
{    e# begin a block
  W%  e# reverse the array
  0+  e# append 0 so there's something to find
  0#  e# find index of first 0, which is number of nonzeros before it
}
```
[Answer]
# [05AB1E](http://github.com/Adriandmen/05AB1E), ~~12~~ ~~10~~ ~~6~~ 5 bytes
Saved 1 byte thanks to *carusocomputing*.
```
Î0¡¤g
```
[Try it online!](http://05ab1e.tryitonline.net/#code=w44wwqHCpGc&input=MTEwMTEx)
**Explanation**
```
Î      # push 0 and input
 0¡    # split on 0
   ¤   # take last item in list
    g  # get length
```
[Answer]
## Python, 31 bytes
```
lambda a:(a[::-1]+[0]).index(0)
```
[Answer]
## [Retina](http://github.com/mbuettner/retina), ~~7~~ 5 bytes
```
r`1\G
```
[Try it online!](http://retina.tryitonline.net/#code=JShHYApyYDFcRw&input=CjAKMQowMTEwMAoxMTEwMQoxMTAxMQowMDExMQoxMTExMTE) (The first line enables a linefeed-separated test suite.)
Defining the input format for Retina isn't entirely unambiguous. Since Retina has no concept of any type except strings (and also no value that can be used for our usual definition of truthy and falsy), I usually use `0` and `1` (or something positive in general) to correspond to truthy and falsy, as they represent zero or some matches, respectively.
With single-character representations, we also don't need a separator for the list (which in a way, is more the more natural list representation for a language that only has strings). [Adám confirmed](https://codegolf.stackexchange.com/questions/98730/count-trailing-truths?noredirect=1#comment239932_98773) that this is an acceptable input format.
As for the regex itself, it matches from `r`ight to left and `\G` anchors each match to the previous one. Hence, this counts how many `1`s we can match from the end of the string.
[Answer]
# [MATL](http://github.com/lmendo/MATL), 4 bytes
```
PYps
```
[Try it online!](http://matl.tryitonline.net/#code=UFlwcw&input=WzEsIDEsIDAsIDEsIDFd)
```
P       % Flip
 Yp     % Cumulative product
   s    % Sum
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 4 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
ṣ0ṪL
```
**[TryItOnline!](http://jelly.tryitonline.net/#code=4bmjMOG5qkw&input=&args=WzEsMSwwLDEsMV0)**, or [all tests](http://jelly.tryitonline.net/#code=4bmjMOG5qkwKw4figqw&input=&args=W1tdLFswXSxbMV0sWzAsMSwxLDAsMF0sWzEsMSwxLDAsMV0sWzEsMSwwLDEsMV0sWzAsMCwxLDEsMV0sWzEsMSwxLDEsMSwxXV0)
### How?
```
ṣ0ṪL - Main link: booleanList
ṣ0   - split on occurrences of 0 ([] -> [[]]; [0] -> [[],[]]; [1] -> [[1]]; ...)
  Ṫ  - tail (rightmost entry)
   L - length
```
[Answer]
# Mathematica, ~~25~~ 24 bytes
```
Fold[If[#2,#+1,0]&,0,#]&
```
[Answer]
# J, ~~9~~ 3 bytes
```
#.~
```
This is reflexive mixed base conversion. [Because this is the same as mixed base conversion.](https://codegolf.stackexchange.com/a/98764/31957) [Again.](https://codegolf.stackexchange.com/a/79653/31957)
## Test cases
```
   v =: #.~
   ]t =: '';0;1;0 1 1 0 0;1 1 1 0 1;1 1 0 1 1;0 0 1 1 1;1 1 1 1 1 1
++-+-+---------+---------+---------+---------+-----------+
||0|1|0 1 1 0 0|1 1 1 0 1|1 1 0 1 1|0 0 1 1 1|1 1 1 1 1 1|
++-+-+---------+---------+---------+---------+-----------+
   v&.> t
+-+-+-+-+-+-+-+-+
|0|0|1|0|1|2|3|6|
+-+-+-+-+-+-+-+-+
   (,. v&.>) t
+-----------+-+
|           |0|
+-----------+-+
|0          |0|
+-----------+-+
|1          |1|
+-----------+-+
|0 1 1 0 0  |0|
+-----------+-+
|1 1 1 0 1  |1|
+-----------+-+
|1 1 0 1 1  |2|
+-----------+-+
|0 0 1 1 1  |3|
+-----------+-+
|1 1 1 1 1 1|6|
+-----------+-+
```
[Answer]
## Pyth, 6 bytes
```
x_+0Q0
```
[Try it here!](http://pyth.herokuapp.com/?code=x_%2B0Q0&input=%5B0%2C1%2C1%2C0%2C1%2C1%5D&debug=0)
Appends a 0, reverses and finds the index of the first 0
[Answer]
# C90 (gcc), 46 bytes
```
r;main(c,v)int**v;{while(0<--c&*v[c])r++;c=r;}
```
Input is via command-line arguments (one integer per argument), output via [exit code](http://meta.codegolf.stackexchange.com/a/5330/12012).
[Try it online!](https://tio.run/nexus/bash#@5@anJGvoJunoF5knZuYmaeRrFOmmZlXoqVVZl1dnpGZk6phYKOrm6ymVRadHKtZpK1tnWxbZF2rrmCnkFxSopfMlZ6crKBbXJJim2xpoKCbDxKFyujpA2lrBbANKvYQroIBuoAhhgoFQyA0wKISKmGITQKsC9MoiGGG2I1Clfr/HwA "Bash – TIO Nexus")
### How it works
**r** is a global variable. Its type defaults to *int* and, being global, it value defaults to **0**.
The function argument **c** defaults to *int* as well. It will hold the integer **n + 1** for arrays of **n** Booleans; the first argument of **main** is always the path of the executable.
The function argument **v** is declared as `int**`. The actual type of **v** will be `char**`, but since we'll only examine the least significant bit of each argument to tell the characters **0** (code point **48**) and **1** (code point **49**) apart, this won't matter on little-endian machines.
The while loop decrements **c** and compares it to **0**. Once **c** reaches **0**, we'll break out of the loop. This is needed only if the array contains no **0**'s.
As long as `0<--c` returns **1**, we takes the **c**th command-line argument (`v[c]`) and extract its first character with by dereferencing the pointer (`*`). We take the bitwise AND of the Boolean `0<--c` and the code point of the character (and three garbage bytes that follow it), so the condition will return **0** once a **0** is encountered, breaking out of the loop.
In the remaining case, while the command-line arguments are **1**, `r++` increments **r** by **1**, thus counting the number of trailing **1**'s.
Finally, `c=r` stores the computed value of **r** in **c**. With default settings, the compiler optimize and remove the assignment; it actually generates the `movl %eax, -4(%rbp)` instruction. Since `ret` returns the value of the EAX register, this generates the desired output.
Note that this code does *not* work with C99, which returns **0** from *main* if the end of *main* is reached.
[Answer]
# k, 6 bytes
```
+/&\|:
```
This function composition translates to `sum mins reverse` in `q`, the language's more readable sibling, where mins is a rolling minimum.
[Answer]
# R, 40 39 25 bytes
Completely reworked solution thanks to @Dason
```
sum(cumprod(rev(scan())))
```
Read input from stdin, reverse the vector and if the first element of is `!=0` then output the the first length of the run-length encoding (`rle`), else `0`.
[Answer]
## Haskell, 24 bytes
```
foldl(\a b->sum[a+1|b])0
```
Iterates over the list, adding one for each element, resetting to `0` after it hits a `False`.
**16 bytes** with 0/1 input:
```
foldl((*).(+1))0
```
If the list were guaranteed non-empty, we could get 14 bytes:
```
sum.scanr1(*)1
```
This computes the cumulative product from the back, then sums them. The cumulative product remains 1 until a 0 is hit, and then becomes 0. So, the 1's correspond to trailing 1's.
[Answer]
## Pyke, ~~10~~ 6 bytes
```
_0+0R@
```
[Try it here!](http://pyke.catbus.co.uk/?code=_0%2B0R%40&input=%5B1%2C++1%2C+1%2C+1%2C+1%5D)
[Answer]
# C#6, ~~103~~ 72 bytes
```
using System.Linq;
int a(bool[] l)=>l.Reverse().TakeWhile(x=>x).Count();
```
Using non-generic list beats generic list by 1 byte lol
-31 bytes thanks to Scott
[Answer]
# [Japt](https://github.com/ETHproductions/japt) `-hx`, 3 bytes
```
i ô
```
[Try it online!](https://ethproductions.github.io/japt/?v=1.4.5&code=aSD0&input=LWh4ClswLCAwLCAxLCAwLCAwLCAxLCAxLCAxXQ==)
Japt's flag combinations rock.
### How it works
```
Ui ô
Ui  Insert `undefined` at index 0
ô   Split at falsy items
-h  Take last element
-x  Sum
```
If we didn't have to handle the special case `[]`, we could get away with 1 byte `ô`, winning over APL.
[Answer]
# Python, 37 bytes
```
f=lambda l:len(l)and-~f(l[:-1])*l[-1]
```
[Answer]
# [DASH](https://github.com/molarmanful/DASH), 16 bytes
```
@len mstr"1+$"#0
```
It's not the shortest possible DASH solution, but the shortest possible DASH solution is bugging out on me. I'm posting this novel approach in its place.
Usage:
```
(@len mstr"1+$"#0)"100111"
```
# Explanation
```
@(                 #. Lambda
  len (            #. Get the length of the array after...
    mstr "1+$" #0  #. ... matching the argument with regex /1+$/
  )                #. * mstr returns an empty array for no matches
)
```
[Answer]
# Scala, 25 bytes
```
l=>l.reverse:+0 indexOf 0
```
Ungolfed:
```
l=>(l.reverse :+ 0).indexOf(0)
```
Reverses the list, appends a 0 and find the first index of 0, which is the number of elements before the first 0
[Answer]
## Batch, 57 bytes
```
@set n=0
@for %%n in (%*)do @set/an=n*%%n+%%n
@echo %n%
```
Takes input as command-line parameters. Works by multiplying the accumulator by the current value before adding it on, so that any zeros in the command line reset the count. Note that `%%n` is not the same as the `n` or `%n%` variable.
[Answer]
## GolfSharp, 14 bytes
```
n=>n.V().O(F);
```
[Answer]
# Java 7, 62 bytes
```
int c(boolean[]a){int r=0;for(boolean b:a)r=b?r+1:0;return r;}
```
**Ungolfed & test code:**
[Try it here.](https://ideone.com/CX1cOb)
```
class M{
  static int c(boolean[] a){
    int r = 0;
    for (boolean b : a){
      r = b ? r+1 : 0;
    }
    return r;
  }
  public static void main(String[] a){
    System.out.print(c(new boolean[]{}) + ", ");
    System.out.print(c(new boolean[]{ false }) + ", ");
    System.out.print(c(new boolean[]{ true }) + ", ");
    System.out.print(c(new boolean[]{ false, true, true, false, false }) + ", ");
    System.out.print(c(new boolean[]{ true, true, true, false, true }) + ", ");
    System.out.print(c(new boolean[]{ true, true, false, true, true }) + ", ");
    System.out.print(c(new boolean[]{ false, false, true, true, true }) + ", ");
    System.out.print(c(new boolean[]{ true, true, true, true, true, true }));
  }
}
```
**Output:**
```
0, 0, 1, 0, 1, 2, 3, 6
```
[Answer]
# Perl 5.10, 22 bytes
21 bytes + 1 byte for `-a` flag.
Since the regex-based expression was done... :p
The input values for the array must be separated by a space.
```
$n++while pop@F;say$n
```
[Try it online!](https://ideone.com/SaIsrK)
[Answer]
## Perl, 22 bytes
21 bytes of code + 1 byte for `-p` flag.
```
s/.(?=.*0)//g;$_=y;1;
```
To run it :
```
perl -pE 's/.(?=.*0)//g;$_=y;1;' <<< "0 1 1 0 1 1 1"
```
(Actually, the format of the input doesn't matter a lot : `0110111`, `0 1 1 0 1 1 1`, `[0,1,1,0,1,1,1]` etc. would all work)
---
**18 bytes version** from [@Dom Hastings](https://codegolf.stackexchange.com/users/9365/dom-hastings) but it requires to supply the input as a string of 0 and 1, which isn't allowed :
```
perl -pE '/1*$/;$_=length$&' <<< '0110111'
```
[Answer]
# PHP, 50 bytes
```
<?=strlen(preg_filter('/.*[^1]/','',join($argv)));
```
Weirdly my first try with a regex turned out shorter than my try with arrays...
  
Use like:
```
php tt.php 1 1 0 1 1
```
]