<commit_before>#last(predicate, resultSelector, defaultValue)

### TL;DR:
Emits the last value based on given argument(s).  Useful for specific scenarios where only the last value, or last value to pass a provided predicate expression is needed.

### last : Observable
The **last** operator only purpose is to emit the last value received from the source.  Once that is emitted, the observable completes.  This value can be the last value from the source, or the last value from the source to pass the conditional testing described by the predicate function.  How the operator does this is by updating the `lastValue` variable until there is no value to update it with.

---

### Arguments

###predicate : function
The `predicate` function is an optional argument that returns a boolean based on the conditional testing you wish to perform.  In addition, the predicate function would update a variable named `lastValue` to match the value that passed the test.  This allows you to add specifications to the single emitted value. If the supplied predicate returns `false`, the value will be ignored and the next value will be tested. If returned `true`, the value will be assigned to `lastValue`. The observable will continues to emit values until there are no more to emit, at which point `lastValue` and `index` would be emitted and the observable would completes.

###resultSelector : function
When a value is emitted you can perform a specified projection through the use of the optional `resultSelector` function.  This function provides you with the value and the index of the value that was emitted.  This index is the placement order of that value, or in other words, the number of items to be emitted before the current value (starting with `0`).  This may be useful in situations where you wish to know how many values have failed before one passed the `predicate` function.

###defaultValue : any
If the observable completes with no value being emitted, due to the `predicate` function or otherwise, an optional `default` value can be supplied to be emitted instead.  Without this value, an `EmptyError` will be thrown.

Overall, the `last` operator is simple, though can be lengthy.  When a predicate function is supplied, the source would emit a value, this value would go through a conditional test, if the result is true, the value would be assigned to `lastValue`. This would continues until a complete is emitted by the source which signals for `lastValue` and the index to be emitted before everything completes.  This is lengthy because `lastValue` has to be reassigned everytime the observable emits a value that pass the conditional test.

:bulb: The counterpart to last is [**first**](first.md).


### Examples

##### Example 1: Last value in sequence

( [jsBin](http://jsbin.com/pevaqeloki/1/edit?js,console) | [jsFiddle](https://jsfiddle.net/btroncone/b05r434a/) )

```js
const source = Rx.Observable.from([1,2,3,4,5]);
//no arguments, emit last value
const example = source.last();
//output: "Last value: 5"
const subscribe = example.subscribe(val => console.log(`Last value: ${val}`));
```

##### Example 2: Last value to pass predicate

( [jsBin](http://jsbin.com/yagexuwari/1/edit?js,console) | [jsFiddle](https://jsfiddle.net/btroncone/pkx2btsh/) )

```js
const source = Rx.Observable.from([1,2,3,4,5]);
//emit last even number
const exampleTwo = source.last(num => num % 2 === 0);
//output: "Last to pass test: 4"
const subscribeTwo = exampleTwo.subscribe(val => console.log(`Last to pass test: ${val}`));
```


### Additional Resources
* [last](http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-last) :newspaper: - Official docs
* [Filtering operator: takeLast, last](https://egghead.io/lessons/rxjs-filtering-operators-takelast-last?course=rxjs-beyond-the-basics-operators-in-depth) :video_camera: :dollar: - <NAME>

---
> :file_folder: Source Code:  [https://github.com/ReactiveX/rxjs/blob/master/src/operator/last.ts](https://github.com/ReactiveX/rxjs/blob/master/src/operator/last.ts)<commit_msg>last description update<commit_after>#last(predicate, resultSelector, defaultValue)

### TL;DR:
Emits the last value emitted from source on completion, based on given argument(s).  Useful for scenarios where only the last value, or last value to pass a provided predicate expression is needed.

### [last : Observable](#last-value-in-sequence)
The **last** operator, by default, emits the last value received from the source upon completion.

---

### Arguments (optional)

###[predicate : function](#last-value-to-pass-predicate)
The `predicate` function is an optional argument that returns a boolean based on the conditional testing you wish to perform.  In addition, the predicate function would update a variable named `lastValue` to match the value that passed the test.  This allows you to add specifications to the single emitted value. If the supplied predicate returns `false`, the value will be ignored and the next value will be tested. If returned `true`, the value will be assigned to `lastValue`. Upon completion, `lastValue` and `index` would be emitted and the observable would completes.

###[resultSelector : function](#last-with-result-selector)
When a value is emitted you can perform a specified projection through the use of the optional `resultSelector` function.  This function is provided the value and index of the emitted item.  This index is the placement order of that value, or in other words, the number of items to be emitted before the current value (starting with `0`).

###[defaultValue : any](#last-with-default-value)
If the observable completes with no value being emitted, due to the `predicate` function or otherwise, an optional `default` value can be supplied to be emitted instead.  Without this value, an `EmptyError` will be thrown.

Overall, the `last` operator is simple. By default, when the source observable completes the last value emitted will be emitted. If a predicate expression is supplied, the last value to pass this predicate will be emitted on completion. If no values pass the given predicate, and a default value is supplied, that is emitted on completion. A projection function can also be supplied, which will receieve the value and it's index, emitting the result of that function when the source observable completes.

:bulb: The counterpart to last is [**first**](first.md)!


### Examples

##### Example 1: Last value in sequence

( [jsBin](http://jsbin.com/pevaqeloki/1/edit?js,console) | [jsFiddle](https://jsfiddle.net/btroncone/b05r434a/) )

```js
const source = Rx.Observable.from([1,2,3,4,5]);
//no arguments, emit last value
const example = source.last();
//output: "Last value: 5"
const subscribe = example.subscribe(val => console.log(`Last value: ${val}`));
```

##### Example 2: Last value to pass predicate

( [jsBin](http://jsbin.com/yagexuwari/1/edit?js,console) | [jsFiddle](https://jsfiddle.net/btroncone/pkx2btsh/) )

```js
const source = Rx.Observable.from([1,2,3,4,5]);
//emit last even number
const exampleTwo = source.last(num => num % 2 === 0);
//output: "Last to pass test: 4"
const subscribeTwo = exampleTwo.subscribe(val => console.log(`Last to pass test: ${val}`));
```

##### Example 3: Last with result selector

( [jsBin](http://jsbin.com/hobinukisu/1/edit?js,console) | [jsFiddle](https://jsfiddle.net/btroncone/76247162/) )

```js
const source = Rx.Observable.from([1,2,3,4,5]);
//supply an option projection function for the second parameter
const exampleTwo = source.last(v => v > 4, v => `The highest emitted number was ${v}`);
//output: 'The highest emitted number was 5'
const subscribeTwo = exampleTwo.subscribe(val => console.log(val));
```

##### Example 4: Last with default value

( [jsBin](http://jsbin.com/fudubebabi/1/edit?js,console) | [jsFiddle](https://jsfiddle.net/btroncone/L7fbx3vp/) )

```js
const source = Rx.Observable.from([1,2,3,4,5]);
//no values will pass given predicate, emit default
const exampleTwo = source.last(v => v > 5, v => v, 'Nothing!');
//output: 'Nothing!'
const subscribeTwo = exampleTwo.subscribe(val => console.log(val));
```


### Additional Resources
* [last](http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-last) :newspaper: - Official docs
* [Filtering operator: takeLast, last](https://egghead.io/lessons/rxjs-filtering-operators-takelast-last?course=rxjs-beyond-the-basics-operators-in-depth) :video_camera: :dollar: - <NAME>

---
> :file_folder: Source Code:  [https://github.com/ReactiveX/rxjs/blob/master/src/operator/last.ts](https://github.com/ReactiveX/rxjs/blob/master/src/operator/last.ts)
{'fraction_non_alphanumeric': 0.11342592592592593, 'fraction_numerical': 0.009143518518518518, 'mean_word_length': 4.878231292517007, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2822730', 'n_tokens_mistral': 2558, 'n_tokens_neox': 2385, 'n_words': 1011}