[Question]
  [
Inspired by [this SO question](https://stackoverflow.com/q/34041614)
As input you will be given a non-empty list of integers, where the first value is guaranteed to be non-zero. To construct the output, walk from the start of the list, outputting each non-zero value along the way. When you encounter a zero, instead repeat the value you most recently added to the output.
You may write a program or function, and have input/output take any convenient format which does not encode extra information, as long as is still an ordered sequence of integers. If outputting from a program, you may print a trailing newline. Except for this trailing newline, your output should be an acceptable input for your submission.
The shortest code in bytes wins.
## Test Cases
```
[1, 0, 2, 0, 7, 7, 7, 0, 5, 0, 0, 0, 9] -> [1, 1, 2, 2, 7, 7, 7, 7, 5, 5, 5, 5, 9]
[1, 0, 0, 0, 0, 0] -> [1, 1, 1, 1, 1, 1]
[-1, 0, 5, 0, 0, -7] -> [-1, -1, 5, 5, 5, -7]
[23, 0, 0, -42, 0, 0, 0] -> [23, 23, 23, -42, -42, -42, -42]
[1, 2, 3, 4] -> [1, 2, 3, 4]
[-1234] -> [-1234]
```
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 3 bytes
```
o@\
```
[Try it online!](http://jelly.tryitonline.net/#code=b0Bc&input=&args=WzIzLCAwLCAwLCAtNDIsIDAsIDAsIDBd)
### How it works
```
o      Take the logical OR of its arguments.
 @     Reverse the argument order of the link to the left.
  \    Do a cumulative reduce, using the link to the left.
```
[Answer]
# Pyth, 6 bytes
```
mJ|dJQ
```
[Demonstration](https://pyth.herokuapp.com/?code=mJ%7CdJQ&input=%5B1%2C+0%2C+2%2C+0%2C+7%2C+7%2C+7%2C+0%2C+5%2C+0%2C+0%2C+0%2C+9%5D&debug=0)
`m ... Q` means this maps a function over the input. The function being mapped is `J|dJ`. That means `J = d or J` in Python, since `J` is implicity assigned to the following value on first use. Unlike Python, assignment expressions return the value assigned in Pyth, so the map returns each successive value of `J`, as desired.
[Answer]
## Ruby, 25 bytes
```
->a{a.map{|x|x==0?a:a=x}}
```
This is actually really evil.
Specifically, the snippet `x==0 ? a : (a=x)`.
If I had used any other variable name for `a` (the previous nonzero value)‚Äîlet's say `y`‚ÄîI would have to declare it outside the `map` (because `y=x` would only have a scope of inside that single `map` iteration). That would use four chars more (`y=0;`).
But if I use the variable name `a`... yep, you guessed it. I'm actually reassigning to *the argument that we got as input* (the original array).
`map` doesn't care because it only cares about the *original* value of the thing its being called on, so this actually works.
[Answer]
## Haskell, 21 bytes
```
a%0=a
a%b=b
scanl1(%)
```
The (anonymous) function we make is in the last line. The first two lines define a helper function.
```
scanl1(%) [1,0,2,0,7,7,7,0,5,0,0,0,9]
[1,1,2,2,7,7,7,7,5,5,5,5,9]
```
The binary function `%` outputs the second argument, unless it's `0`, in which case it outputs the first argument instead. `scanl1` iterates this function over the input list, outputting the result at each step.
[Answer]
## J, 8 bytes
```
{:@-.&0\
```
This is a unary function, invoked as follows.
```
   f =: {:@-.&0\
   f 2 0 0 4 0 _1 0
2 2 2 4 4 _1 _1
```
## Explanation
```
{:@-.&0\
       \  Map over non-empty prefixes:
   -.      remove all occurrences
     &0    of the number 0 and
{:@        take the last element.
```
[Answer]
# Sed, 8
```
/^0$/g
h
```
* `/^0$/` matches a zero on a line - if so `g` copies the hold space to the pattern space
* `h` copies the pattern space to the hold space
Integers are newline separated. e.g:
```
$ printf -- "-1\n0\n5\n0\n0\n7\n" | sed -f zerocover.sed
-1
-1
5
5
5
7
$ 
```
[Answer]
# Javascript ES6, 19 bytes
```
s=>s.map(i=>p=i||p)
```
Straightforward solution, loop through input, assign `p` to current element `i` or to `p` if `i` is `0` and output it.
Example run (assigning anonymous function to `f`):
```
>> f([1, 0, 2, 0, 7, 7, 7, 0, 5, 0, 0, 0, 9])
<< Array [1, 1, 2, 2, 7, 7, 7, 7, 5, 5, 5, 5, 9]
```
[Answer]
## Dyalog APL, ~~12~~ ~~10~~ 9 bytes
```
(‚äÉ0~‚ç®,‚ç®)\
```
Inspired by @Zgarb's J answer.
```
(‚äÉ0~‚ç®,‚ç®)\      Monadic function:
        \      Cumulative reduce by
(‚äÉ0~‚ç®,‚ç®)       the dyadic function:
     ,‚ç®           Arguments concatenated in reverse order
  0~‚ç®             With zeroes removed
 ‚äÉ                Take the first element
```
Try it [here](http://tryapl.org/?a=%28%7B%u2283%u233D%u2375%7E0%7D%A8%2C%5C%29%20%AF1%200%205%200%200%20%AF7&run).
[Answer]
## [Retina](https://github.com/mbuettner/retina), 15 bytes
```
+`(\S+) 0
$1 $1
```
[Try it online.](http://retina.tryitonline.net/#code=K2AoXFMrKSAwCiQxICQx&input=MSAwIDIgMCA3IDcgNyAwIDUgMCAwIDAgOSAxIDAgMCAwIDAgMCAtMSAwIDUgMCAwIC03IDIzIDAgMCAtNDIgMCAwIDAgMSAyIDMgNCAtMTIzNA)
Repeatedly replaces a number followed by a zero with twice that number until the string stops changing.
[Answer]
## Java, 78
```
int[]f(int[]a){for(int i=-1,b=i;++i<a.length;a[i]=b=a[i]==0?b:a[i]);return a;}
```
Here we just keep track of the last non-zero and shove it in where appropriate. Seems like the obvious way to do it.
[Answer]
## Python 2, 29 bytes
```
while 1:x=input()or x;print x
```
Takes input as numbers given one per line, and outputs in the same format. Terminates with error after finishing.
Using the short-circuiting nature of `or`, the variable `x` is updated to the input, unless that input is 0 (which is Falsey), in which case it remains its current value. Then, `x` is printed. Note that since the first list value is nonzero, `x` is not evaluated in the right hand side before it is assigned.
[Answer]
## Pyth, 8 bytes
```
t.u|YNQ0
```
Uses `.u` (cumulative reduce) by `|` (Python's `or`), with base case 0.
[Answer]
# [Prolog (SWI)](http://www.swi-prolog.org), 54 bytes
```
[X,0|T]+[X,X|Y]:-[X|T]+[X|Y].
[X|T]+[X|Y]:-T+Y.
[]+[].
```
[Try it online!](https://tio.run/##KyjKz8lP1y0uz/z/PzpCx6AmJFYbSEfURMZa6UZHQLhAjh4XEsdKN0Q7EigC5MbqAfUZ6hgAoREQGwKhrlmsdoQeAA "Prolog (SWI) ‚Äì Try It Online")
## Explanation
I'm really happy with this answer.
First we say that the empty list is the solution of the empty list:
```
[]+[].
```
Then we say that `[X,X|Y]` is the solution of `[X,0|T]`, if by removing the second entry of each of the remaining solutions.
```
[X,0|T]+[X,X|Y]:-[X|T]+[X|Y].
```
Lastly we say that any thing left over is valid if they start with the same value and the rest of the the two lists match each other.
If that explanation isn't working for you here is the code translated into Haskell:
```
g(a:0:x)=a:g(a:x)
g(a:x)=a:g x
g x=x
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P10j0crAqkLTNtEKxKzQ5IJQIL5CBRcQ21b8z03MzLMtKMrMK1FJjzbUMdIxAEPj2P8A "Haskell ‚Äì Try It Online")
[Answer]
# Mathematica 38 bytes
Pattern matching repeatedly replaces `...a,0,...` with `...a,a...`
```
#//.{b___,a_/;a!=0,0,e___}:>{b,a,a,e}&
```
[Answer]
# Matlab, 41 ~~46~~ bytes
This is inspired in my [original answer](https://stackoverflow.com/a/34043697/2586922), with the following differences:
1. Use logical indexing instead of `nonzeros`.
2. Double logical negation instead of comparing with `0`.
3. The transpose can be removed, as the output format is flexible
4. Removing an intermediate variable.
Thanks to [Tom Carpenter](https://codegolf.stackexchange.com/questions/65770/cover-up-zeroes-in-a-list/65797#comment159390_65797) for item 4, and for his suggestion to use a program instead of a function; together these allowed a reduction of 5 bytes.
```
x=input('');u=x(~~x);disp(u(cumsum(~~x)))
```
Example:
```
>> x=input('');u=x(~~x);disp(u(cumsum(~~x)))
[4 0 3 2 0 5 6 0]
     4     4     3     2     2     5     6     6
```
[Answer]
# [Gol><>](https://github.com/Sp3000/Golfish/wiki), 8 bytes
```
IE;:Z~:N
```
Input and output are newline separated numbers.
Explanation:
```
I         push next integer to stack
 E;       halt if EOF
   :Z~    remove top stack element if 0
      :N  print top stack element while also keeping it on the stack
          wrap around code implicitly
```
[Try it online here.](https://golfish.herokuapp.com/#)
[Answer]
# Japt, ~~8~~ 7 bytes
```
N¬£U=X¬™U
```
Pretty simple. Takes input separated by commas. [Try it online!](http://ethproductions.github.io/japt?v=master&code=TqNVPViqVQ==&input=MSwwLDIsMCw3LDcsNywwLDUsMCwwLDAsOQ==)
### Ungolfed and explanation
```
N¬£    U=X¬™ U
NmXYZ{U=X||U
        // Implicit: N = input, U = first item
NmXYZ{  // Map each item X to:
U=Z||U  //  Set U to (X || U) and return.
        //  If X is non-zero, this sets U to X.
        //  Otherwise, this leaves U as the last non-zero we've encountered.
        // Implicit: output last expression
```
**4-byte** version:
```
N√•!¬™
```
Explanation:
```
N√•!¬™
N√•!||
N√•XY{Y||X}
        // Implicit: N = input, U = first item
N√•XY{   // Cumulatively reduce N; take each item Y and prev value X,
Y||X}   //  and return Y if it is non-zero; return X otherwise.
        // Implicit: output last expression
```
[Try it online!](http://ethproductions.github.io/japt?v=master&code=TuUhqg==&input=MSwwLDIsMCw3LDcsNywwLDUsMCwwLDAsOQ==)
[Answer]
# GolfScript, 10 bytes
```
~{1$or}*]`
```
This program takes input from stdin, in the form of a GolfScript array literal (e.g. `[1 0 2 0]`), and writes its output to stdout in the same format (e.g. `[1 1 2 2]`).
[Try it online.](http://golfscript.apphb.com/?c=IyBDYW5uZWQgaW5wdXQ6CjsiWzEgMiAwIDMgMCAwIDQgNSAtMSAtMiAwIC0xMjM0IDBdIgoKIyBQcm9ncmFtOgp%2BezEkb3J9Kl1g)
A function (taking and returning a GolfScript array) would be three bytes longer, due to the need to wrap it in a block and assign it to a symbol:
```
{[{1$or}*]}:f
```
Of course, if only the function *body* (i.e. `[{1$or}*]`) is counted, then I can actually save one byte compared to the stand-alone program.
[Answer]
## [Minkolang 0.14](https://github.com/elendiastarman/Minkolang), ~~12~~ 10 bytes
```
$I?.nd?xdN
```
[Try it here.](http://play.starmaninnovations.com/minkolang/?code=%24I%3F%2End%3FxdN&input=-1%2C%200%2C%205%2C%200%2C%200%2C%20-7) Input can be given as in the question, but **without brackets**.
### Explanation
```
$I      Push the length of the input on the stack.
  ?.    If this is 0, stop. Otherwise, continue.
nd        Take number from input and duplicate it.
  ?x      If this number is 0, dump the top of stack.
    dN    Duplicate the top of stack and output as number
```
Minkolang is toroidal, so this loops around to the beginning and keeps going until it hits the `.` and stops.
[Answer]
# ùîºùïäùïÑùïöùïü, 7 chars / 12 bytes
```
√Ø‚ìúa=$‚ãéa
```
`[Try it here (Firefox only).](http://molarmanful.github.io/ESMin/interpreter.html?eval=true&input=%5B1%2C%200%2C%202%2C%200%2C%207%2C%207%2C%207%2C%200%2C%205%2C%200%2C%200%2C%200%2C%209%5D&code=%C3%AF%E2%93%9Ca%3D%24%E2%8B%8Ea)`
## Explanation
```
        // implicit: √Ø = input array
√Ø‚ìú     // map over input
  a=    // set a to:
    $   // (if element is truthy (not 0)) element itself
     ‚ãéa // else whatever a was set to before
        // implicit output
```
[Answer]
# [O](https://github.com/phase/o), 31 bytes
```
[[I',T%T/]{n#}d]{n.{:V}{;V}?}d]
```
This takes an input separated by `,` and outputs the same list in `[]`.
```
7,0,3,0,0,2,-50,0,0 => [7,7,3,3,3,2,-50,-50,-50]
```
Explanation:
```
[                             ] Put result into array
 [I',T%T/]{n#}d]                Format input into array of numbers
                {n.{:V}{;V}?}d  Fill in zeros (see below for how this works)
```
  
# 17 bytes
```
I~]{n.{:V}{;V}?}d
```
Takes input as a list of numbers separated by spaces using postfix notation and can only handle single digit hexadecimal numbers. Negatives are postfixed with `_`.
```
5 4 0 0 1 0 0 => 5 4 4 4 1 1 1
A 3 0 0 1 B 0 => 10 3 3 3 1 11 11
67* 0 0 78* 0 => 42 42 42 56 56
67*_ 4 3_ 0 0 => -42 4 -3 -3 -3
```
Explanation:
```
I~]               Puts input into integer array
   {           }d For each number in the input
    n.{;V}{:V}?   If the number is 0, push V
                  If not, set V to the number
```
[Answer]
## R, 36 bytes
```
function(x)x[cummax(seq(a=x)*(!!x))]
```
Let's see how this works using `x=`
```
c(1, 0, 2, 0, 7, 7, 7, 0, 5, 0, 0, 0, 9)
```
as an example. Here, `!!x` will be the logical (True/False) vector:
```
c(T, F, T, F, T, T, T, F, T, F, F, F, T)
```
Also, `seq(a=x)` gives a vector of indices as long as `x`:
```
c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
```
We multiply both, giving:
```
c(1, 0, 3, 0, 5, 6, 7, 0, 9, 0, 0, 0, 13)
```
We take the cumulative maximum:
```
c(1, 1, 3, 3, 5, 6, 7, 7, 9, 9, 9, 9, 13)
```
Finally, we use that last vector as the indices to extract from `x`:
```
c(1, 1, 2, 2, 7, 7, 7, 7, 5, 5, 5, 5, 9)
```
[Answer]
# R, ~~39~~ ~~37~~ 33 bytes
```
function(x)zoo::na.locf(x*(x|NA))
```
This is an unnamed function that accepts a vector and returns a vector. It requires the `zoo` package to be installed. Note that it doesn't require `zoo` to be attached to the namespace since we're referencing it directly.
The name for this operation in the world of statistics is LOCF imputation, where LOCF stands for Last Observation Carried Forward. To accomplish this in R, we can use `na.locf` from the `zoo` package, which replaces `NA` values with the last known non-`NA` value. We just have to replace the zeros in the input with `NA`s first.
To do that, we use `x|NA`, which will be `TRUE` when `x != 0` and `NA` otherwise. If we multiply this by `x`, the `TRUE` elements are replaced by the corresponding elements of `x` and the `NA`s stay `NA`, thereby replacing all zeros. This is then passed to `zoo::na.locf` which gives us exactly what we want.
Saved 4 bytes thanks to flodel!
[Answer]
# Rust, 100 bytes
```
fn f(i:&[i64])->Vec<i64>{let(mut o,mut l)=(i.to_vec(),0);
for x in&mut o{if *x==0{*x=l}else{l=*x}};o}
```
Stumbled across this challenge, thought I'd try it in my favorite language. Tried using `[T]::windows_mut()` at first, before finding out that [it doesn't exist](https://github.com/rust-lang/rust/issues/23783). And it might've actually been longer than this. Anyway, it turns out that golfed Rust is very ugly and very not-competitive (especially with all those goshdarned esoterics!)1
The newline isn't included in the bytecount; it's only there so you don't have to scroll sideways. It doesn't change the meaning of the code.
Ungolfed:
```
fn cover_zeroes(input: &[i64]) -> Vec<i64> {
    let mut output = input.to_vec();
    let mut last_nonzero = 0;
    for item in &mut output {
        if *item == 0 {
            *item = last_nonzero;
        }
        else {
            last_nonzero = *item;
        }
    }
    output
}
```
[1] At least it's not as bad as Java.
[Answer]
# [Husk](https://github.com/barbuz/Husk), 2 bytes
```
G|
```
[Try it online!](https://tio.run/##yygtzv5/aFtueW5w6qOmxv/uNf///4@ONtQx0DECYnMwNNAxBWIQtIzVActBIZCnawiX1jUH8o2MIWwTI7gSQ6BRxjomYMVGxiaxsQA "Husk ‚Äì Try It Online")
scan from left with or.
[Answer]
# [Milky Way 1.2.1](https://github.com/zachgates7/Milky-Way), 33 bytes
```
:y;=<:&{~<?{0b_^;:3‚â§_;}1-}^<Œ©!
```
This assumes that the list of integers is solely on the stack.
---
### Explanation
```
:    : :           :              # duplicate the TOS
 y                                # push the length of the TOS
  ;               ;    ;          # swap the TOS and STOS
   =                              # dump a list to the stack
    < <    <                 <    # rotate the stack leftward
        &{~                }      # while loop
            ?{  _     _ }         # if-else statements
              0     3    1        # push an integer
               b                  # == on the TOS and STOS
                 ^          ^     # pop the TOS without output
                     ‚â§            # rotate the top N stack elements leftward
                          -       # subtract the TOS from the STOS
                              Œ©   # push a list made of the top N stack elements
                               !  # output the TOS
```
[Answer]
# Julia, 33 bytes
```
g(x,a=0)=[(i!=0&&(a=i);a)for i=x]
```
This is a function `g` that accepts an array and returns an array. We start a temporary variable `a` at 0. For each element `i` of the input, if `i` isn't 0 then we assign `a` to `i`. If `i` is 0, `a` doesn't change at that iteration. We use `a` as the value in that position in the output array.
[Answer]
## [Perl 6](http://perl6.org), 21 bytes
```
*.map: {$_=($^a||$_)}
```
usage: 
```
# store the Whatever lambda as a subroutine
# just so that we don't have to repeat it
my &code = *.map: {$_=($^a||$_)}
say code [1, 0, 2, 0, 7, 7, 7, 0, 5, 0, 0, 0, 9];
# (1 1 2 2 7 7 7 7 5 5 5 5 9)
say [-1, 0, 5, 0, 0, -7].&code;
# (-1 -1 5 5 5 -7)
say ([1, 0, 0, 0, 0, 0],[-1, 0, 5, 0, 0, -7]).map: &code;
# ((1 1 1 1 1 1) (-1 -1 5 5 5 -7))
```
[Answer]
# CJam, 11 bytes
```
q~{1$e|}*]p
```
[Try it online.](http://cjam.aditsu.net/#code=q~%7B1%24e%7C%7D*%5Dp&input=%5B23%200%200%20-42%200%200%200%5D)
### How it works
```
q~             Read and evaluate all input.
  {    }*      Fold; for each element but the first:
   1$e|          Copy the previous element and take their logical OR.
         ]p   Wrap all results in an array and print it.
```
[Answer]
## Powershell, 32 bytes
```
param($x)$x|%{($t=($_,$t)[!$_])}
```
`$x|%{...}` does the script block for each element in `$x`. `($_,$t)` is an array of current element and `$t`, and `[!$_]` means that we use `!$_` to index into the array. The index will be `0` (false) for non-zero elements and `1` (true) when current element is zero, so `$t` will be either current element or `$t`. The parentheses surround the assignment expression so its value is emitted. Without parantheses it would be just a "quiet" assignment to `$t`.
]